Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions specs/04-auth-and-accounts.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ OAuth2 authorization code + PKCE in a popup (the single-window exception of
([01 §2.4](01-architecture.md)).
- **Accounts**: Google's default validation requires a verified email and a name. A first
Google sign-in for an email that already has a Wonky account signs in to **that** account
(the Google account is linked to it and the auth user the IDP just created is deleted);
otherwise a new `UserProfile` is created with Google's full name. Later sign-ins use the
(the Google account is linked to it and the auth user the IDP just created is deleted;
the IDP still treats the account as new and creates its Serverpod auth profile, so the
config passes `LinkedAccountUserProfiles`, which returns the linked account's existing
auth profile instead of inserting a second one); otherwise a new `UserProfile` is created with Google's full name. Later sign-ins use the
linked account.
- **Client** (`auth/google_web_flow.dart`, `google_popup_web.dart`): Wonky runs the popup
itself instead of `GoogleWebSignInService`/`flutter_web_auth_2`. The apps are
Expand Down
64 changes: 62 additions & 2 deletions wonky_server/lib/src/platform/users/google_idp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ import 'dart:io';

import 'package:serverpod/serverpod.dart';
import 'package:serverpod_auth_idp_server/core.dart'
show AuthUser, AuthUserModel;
show
AuthUser,
AuthUserModel,
AuthUsers,
TokenManager,
UserImageSource,
UserProfileData,
UserProfileModel,
UserProfiles;
import 'package:serverpod_auth_idp_server/providers/google.dart';

import '../common/models.dart' show UserProfile;
Expand Down Expand Up @@ -66,13 +74,65 @@ String? googleWebClientId() => googleClientSecret()?.clientId;
GoogleIdpConfig? wonkyGoogleIdpConfig() {
final secret = googleClientSecret();
if (secret == null) return null;
return GoogleIdpConfig(
return _WonkyGoogleIdpConfig(
clientSecret: secret,
getExtraGoogleInfoCallback: rememberGoogleDetails,
onAfterGoogleAccountCreated: afterGoogleAccountCreated,
);
}

/// [GoogleIdpConfig] whose IDP keeps the existing auth profile of an account
/// that [afterGoogleAccountCreated] linked the Google account to.
class _WonkyGoogleIdpConfig extends GoogleIdpConfig {
_WonkyGoogleIdpConfig({
required super.clientSecret,
super.getExtraGoogleInfoCallback,
super.onAfterGoogleAccountCreated,
});

@override
GoogleIdp build({
required TokenManager tokenManager,
required AuthUsers authUsers,
required UserProfiles userProfiles,
}) => super.build(
tokenManager: tokenManager,
authUsers: authUsers,
userProfiles: LinkedAccountUserProfiles(config: userProfiles.config),
);
}

/// [UserProfiles] that returns the existing auth profile instead of creating
/// a second one. The Google IDP creates the profile of every new Google
/// account, also when [afterGoogleAccountCreated] linked it to an existing
/// account, which already has one (unique per auth user).
class LinkedAccountUserProfiles extends UserProfiles {
const LinkedAccountUserProfiles({super.config});

@override
Future<UserProfileModel> createUserProfile(
Session session,
UuidValue authUserId,
UserProfileData userProfile, {
Transaction? transaction,
UserImageSource? imageSource,
}) async {
final existing = await maybeFindUserProfileByUserId(
session,
authUserId,
transaction: transaction,
);
if (existing != null) return existing;
return super.createUserProfile(
session,
authUserId,
userProfile,
transaction: transaction,
imageSource: imageSource,
);
}
}

/// Google's account details of the request, between the two callbacks.
final _details = Expando<GoogleAccountDetails>('googleAccountDetails');

Expand Down
56 changes: 56 additions & 0 deletions wonky_server/test/integration/platform/google_idp_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,62 @@ void main() {
},
);

test(
'when the Google login is linked to an existing account then the IDP '
'keeps its auth profile instead of creating a second one',
() async {
final anna = await TestUsers.create(sessionBuilder, name: 'Anna');
final session = sessionBuilder.build();
// The auth profile the email sign-up created.
await auth.UserProfile.db.insertRow(
session,
auth.UserProfile(authUserId: anna.authUserId, email: anna.email),
);
final (authUser, account) = await newGoogleAccount(anna.email);
await afterGoogleAccountCreated(
session,
authUser,
account,
transaction: null,
);

// What `GoogleIdp.login` does next for a new Google account.
await const LinkedAccountUserProfiles().createUserProfile(
session,
account.authUserId,
auth.UserProfileData(fullName: 'Anna Google', email: anna.email),
);

final profiles = await auth.UserProfile.db.find(
session,
where: (t) => t.authUserId.equals(anna.authUserId),
);
expect(profiles, hasLength(1));
expect(profiles.single.fullName, isNull);
},
);

test(
'when the Google account is new then the IDP creates its auth profile',
() async {
final session = sessionBuilder.build();
final email = 'new.${clock.now().microsecondsSinceEpoch}@example.test';
final (authUser, _) = await newGoogleAccount(email);

await const LinkedAccountUserProfiles().createUserProfile(
session,
authUser.id,
auth.UserProfileData(fullName: 'Grace Hopper', email: email),
);

final profile = await auth.UserProfile.db.findFirstRow(
session,
where: (t) => t.authUserId.equals(authUser.id),
);
expect(profile!.fullName, 'Grace Hopper');
},
);

test(
'when the email is new then a profile is created with the Google name',
() async {
Expand Down
Loading