diff --git a/specs/04-auth-and-accounts.md b/specs/04-auth-and-accounts.md index 14050c6b..d7681e12 100644 --- a/specs/04-auth-and-accounts.md +++ b/specs/04-auth-and-accounts.md @@ -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 diff --git a/wonky_server/lib/src/platform/users/google_idp.dart b/wonky_server/lib/src/platform/users/google_idp.dart index bbfc1aab..912f39f7 100644 --- a/wonky_server/lib/src/platform/users/google_idp.dart +++ b/wonky_server/lib/src/platform/users/google_idp.dart @@ -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; @@ -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 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'); diff --git a/wonky_server/test/integration/platform/google_idp_test.dart b/wonky_server/test/integration/platform/google_idp_test.dart index 76980033..ba419e68 100644 --- a/wonky_server/test/integration/platform/google_idp_test.dart +++ b/wonky_server/test/integration/platform/google_idp_test.dart @@ -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 {