Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(auth, ios): Apple Sign In on a secondary app doesnt sign in the correct Firebase Auth instance #10018

Merged
merged 1 commit into from Dec 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}];
}
}
}
Expand All @@ -622,21 +627,18 @@ - (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
NSLog(@"signInWithProvider is not supported on the "
@"MacOS platform.");
result.success(nil);
#else
FIRAuth *auth = [self getFIRAuthFromArguments:arguments];
self.authProvider = [FIROAuthProvider providerWithProviderID:arguments[@"signInProvider"]];
NSArray *scopes = arguments[kArgumentProviderScope];
if (scopes != nil) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down