From 48d4413049d6059c45b0a6cef431b13623cd37a9 Mon Sep 17 00:00:00 2001 From: Guillaume Bernos Date: Tue, 29 Nov 2022 15:55:59 +0100 Subject: [PATCH] fix(auth, ios): Apple Sign In on a secondary app doesnt sign in the correct Firebase Auth instance --- .../ios/Classes/FLTFirebaseAuthPlugin.m | 93 +++++++++---------- 1 file changed, 45 insertions(+), 48 deletions(-) diff --git a/packages/firebase_auth/firebase_auth/ios/Classes/FLTFirebaseAuthPlugin.m b/packages/firebase_auth/firebase_auth/ios/Classes/FLTFirebaseAuthPlugin.m index 19a1adb7b4e7..fe9a2be5462e 100644 --- a/packages/firebase_auth/firebase_auth/ios/Classes/FLTFirebaseAuthPlugin.m +++ b/packages/firebase_auth/firebase_auth/ios/Classes/FLTFirebaseAuthPlugin.m @@ -69,6 +69,7 @@ @interface FLTFirebaseAuthPlugin () @property(strong, nonatomic) FIROAuthProvider *authProvider; // Used to keep the user who wants to link with Apple Sign In @property(strong, nonatomic) FIRUser *linkWithAppleUser; +@property(strong, nonatomic) FIRAuth *signInWithAppleAuth; @property BOOL isReauthenticatingWithApple; @property(strong, nonatomic) NSString *currentNonce; @property(strong, nonatomic) FLTFirebaseMethodCallResult *appleResult; @@ -601,15 +602,19 @@ - (void)authorizationController:(ASAuthorizationController *)controller } else if (self.linkWithAppleUser != nil) { [self.linkWithAppleUser linkWithCredential:credential completion:^(FIRAuthDataResult *authResult, NSError *error) { + self.linkWithAppleUser = nil; handleSignInWithApple(self, authResult, error); }]; } else { - [FIRAuth.auth signInWithCredential:credential - completion:^(FIRAuthDataResult *_Nullable authResult, - NSError *_Nullable error) { - handleSignInWithApple(self, authResult, error); - }]; + FIRAuth *signInAuth = + self.signInWithAppleAuth != nil ? self.signInWithAppleAuth : FIRAuth.auth; + [signInAuth signInWithCredential:credential + completion:^(FIRAuthDataResult *_Nullable authResult, + NSError *_Nullable error) { + self.signInWithAppleAuth = nil; + handleSignInWithApple(self, authResult, error); + }]; } } } @@ -622,13 +627,11 @@ - (void)authorizationController:(ASAuthorizationController *)controller - (void)signInWithProvider:(id)arguments withMethodCallResult:(FLTFirebaseMethodCallResult *)result { + FIRAuth *auth = [self getFIRAuthFromArguments:arguments]; + if ([arguments[@"signInProvider"] isEqualToString:kSignInMethodApple]) { - if (@available(iOS 13.0, macOS 10.15, *)) { - launchAppleSignInRequest(self, arguments, result); - } else { - NSLog(@"Sign in with Apple was introduced in iOS 13, update your Podfile with platform :ios, " - @"'13.0'"); - } + self.signInWithAppleAuth = auth; + launchAppleSignInRequest(self, arguments, result); return; } #if TARGET_OS_OSX @@ -636,7 +639,6 @@ - (void)signInWithProvider:(id)arguments @"MacOS platform."); result.success(nil); #else - FIRAuth *auth = [self getFIRAuthFromArguments:arguments]; self.authProvider = [FIROAuthProvider providerWithProviderID:arguments[@"signInProvider"]]; NSArray *scopes = arguments[kArgumentProviderScope]; if (scopes != nil) { @@ -905,29 +907,34 @@ - (void)userGetIdToken:(id)arguments withMethodCallResult:(FLTFirebaseMethodCall static void launchAppleSignInRequest(FLTFirebaseAuthPlugin *object, id arguments, FLTFirebaseMethodCallResult *result) { - NSString *nonce = [object randomNonce:32]; - object.currentNonce = nonce; - object.appleResult = result; - object.appleArguments = arguments; - - ASAuthorizationAppleIDProvider *appleIDProvider = [[ASAuthorizationAppleIDProvider alloc] init]; - - ASAuthorizationAppleIDRequest *request = [appleIDProvider createRequest]; - NSMutableArray *requestedScopes = [NSMutableArray arrayWithCapacity:2]; - if ([arguments[kArgumentProviderScope] containsObject:@"name"]) { - [requestedScopes addObject:ASAuthorizationScopeFullName]; - } - if ([arguments[kArgumentProviderScope] containsObject:@"email"]) { - [requestedScopes addObject:ASAuthorizationScopeEmail]; + if (@available(iOS 13.0, macOS 10.15, *)) { + NSString *nonce = [object randomNonce:32]; + object.currentNonce = nonce; + object.appleResult = result; + object.appleArguments = arguments; + + ASAuthorizationAppleIDProvider *appleIDProvider = [[ASAuthorizationAppleIDProvider alloc] init]; + + ASAuthorizationAppleIDRequest *request = [appleIDProvider createRequest]; + NSMutableArray *requestedScopes = [NSMutableArray arrayWithCapacity:2]; + if ([arguments[kArgumentProviderScope] containsObject:@"name"]) { + [requestedScopes addObject:ASAuthorizationScopeFullName]; + } + if ([arguments[kArgumentProviderScope] containsObject:@"email"]) { + [requestedScopes addObject:ASAuthorizationScopeEmail]; + } + request.requestedScopes = [requestedScopes copy]; + request.nonce = [object stringBySha256HashingString:nonce]; + + ASAuthorizationController *authorizationController = + [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[ request ]]; + authorizationController.delegate = object; + authorizationController.presentationContextProvider = object; + [authorizationController performRequests]; + } else { + NSLog(@"Sign in with Apple was introduced in iOS 13, update your Podfile with platform :ios, " + @"'13.0'"); } - request.requestedScopes = [requestedScopes copy]; - request.nonce = [object stringBySha256HashingString:nonce]; - - ASAuthorizationController *authorizationController = - [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[ request ]]; - authorizationController.delegate = object; - authorizationController.presentationContextProvider = object; - [authorizationController performRequests]; } static void handleAppleAuthResult(FLTFirebaseAuthPlugin *object, id arguments, FIRAuth *auth, @@ -977,13 +984,8 @@ - (void)userLinkWithProvider:(id)arguments } if ([arguments[@"signInProvider"] isEqualToString:kSignInMethodApple]) { - if (@available(iOS 13.0, macOS 10.15, *)) { - self.linkWithAppleUser = currentUser; - launchAppleSignInRequest(self, arguments, result); - } else { - NSLog(@"Sign in with Apple was introduced in iOS 13, update your Podfile with platform :ios, " - @"'13.0'"); - } + self.linkWithAppleUser = currentUser; + launchAppleSignInRequest(self, arguments, result); return; } #if TARGET_OS_OSX @@ -1021,13 +1023,8 @@ - (void)reauthenticateWithProvider:(id)arguments } if ([arguments[@"signInProvider"] isEqualToString:kSignInMethodApple]) { - if (@available(iOS 13.0, macOS 10.15, *)) { - self.isReauthenticatingWithApple = YES; - launchAppleSignInRequest(self, arguments, result); - } else { - NSLog(@"Sign in with Apple was introduced in iOS 13, update your Podfile with platform :ios, " - @"'13.0'"); - } + self.isReauthenticatingWithApple = YES; + launchAppleSignInRequest(self, arguments, result); return; } #if TARGET_OS_OSX