Skip to content

Commit

Permalink
fix(auth, apple): bug with cached AuthCredential, hash key was prod…
Browse files Browse the repository at this point in the history
…ucing different value (#12957)
  • Loading branch information
russellwheatley committed Jun 13, 2024
1 parent a100829 commit ef0077e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,14 @@ + (FlutterError *)convertToFlutterError:(NSError *)error {
additionalData[kArgumentEmail] = [error userInfo][FIRAuthErrorUserInfoEmailKey];
}
// We want to store the credential if present for future sign in if the exception contains a
// credential
[FLTFirebaseAuthPlugin storeAuthCredentialIfPresent:error];
// credential, we pass a token back to Flutter to allow retreival of the credential.
NSNumber *token = [FLTFirebaseAuthPlugin storeAuthCredentialIfPresent:error];

// additionalData.authCredential
if ([error userInfo][FIRAuthErrorUserInfoUpdatedCredentialKey] != nil) {
FIRAuthCredential *authCredential = [error userInfo][FIRAuthErrorUserInfoUpdatedCredentialKey];
additionalData[@"authCredential"] = [PigeonParser getPigeonAuthCredential:authCredential];
additionalData[@"authCredential"] = [PigeonParser getPigeonAuthCredential:authCredential
token:token];
}

// Manual message overrides to ensure messages/codes matches other platforms.
Expand Down Expand Up @@ -641,14 +642,16 @@ static void handleAppleAuthResult(FLTFirebaseAuthPlugin *object, AuthPigeonFireb

#pragma mark - Utilities

+ (void)storeAuthCredentialIfPresent:(NSError *)error {
+ (NSNumber *_Nullable)storeAuthCredentialIfPresent:(NSError *)error {
if ([error userInfo][FIRAuthErrorUserInfoUpdatedCredentialKey] != nil) {
FIRAuthCredential *authCredential = [error userInfo][FIRAuthErrorUserInfoUpdatedCredentialKey];
// We temporarily store the non-serializable credential so the
// Dart API can consume these at a later time.
NSNumber *authCredentialHash = @([authCredential hash]);
credentialsMap[authCredentialHash] = authCredential;
return authCredentialHash;
}
return nil;
}

- (FIRAuth *_Nullable)getFIRAuthFromAppNameFromPigeon:(AuthPigeonFirebaseApp *)pigeonApp {
Expand Down
10 changes: 7 additions & 3 deletions packages/firebase_auth/firebase_auth/ios/Classes/PigeonParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ @implementation PigeonParser
makeWithUser:[self getPigeonDetails:authResult.user]
additionalUserInfo:[self getPigeonAdditionalUserInfo:authResult.additionalUserInfo
authorizationCode:authorizationCode]
credential:[self getPigeonAuthCredential:authResult.credential]];
credential:[self getPigeonAuthCredential:authResult.credential token:nil]];
}

+ (PigeonUserCredential *)getPigeonUserCredentialFromFIRUser:(nonnull FIRUser *)user {
Expand Down Expand Up @@ -88,7 +88,8 @@ + (PigeonTotpSecret *)getPigeonTotpSecret:(FIRTOTPSecret *)secret {
secretKey:secret.sharedSecretKey];
}

+ (PigeonAuthCredential *)getPigeonAuthCredential:(FIRAuthCredential *)authCredential {
+ (PigeonAuthCredential *)getPigeonAuthCredential:(FIRAuthCredential *)authCredential
token:(NSNumber *_Nullable)token {
if (authCredential == nil) {
return nil;
}
Expand All @@ -103,9 +104,12 @@ + (PigeonAuthCredential *)getPigeonAuthCredential:(FIRAuthCredential *)authCrede
}
}

NSUInteger nativeId =
token != nil ? [token unsignedLongValue] : (NSUInteger)[authCredential hash];

return [PigeonAuthCredential makeWithProviderId:authCredential.provider
signInMethod:authCredential.provider
nativeId:@([authCredential hash])
nativeId:nativeId
accessToken:accessToken ?: nil];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
+ (PigeonIdTokenResult *_Nonnull)parseIdTokenResult:(nonnull FIRAuthTokenResult *)tokenResult;
+ (PigeonTotpSecret *_Nonnull)getPigeonTotpSecret:(nonnull FIRTOTPSecret *)secret;
+ (PigeonAuthCredential *_Nullable)getPigeonAuthCredential:
(FIRAuthCredential *_Nullable)authCredential;
(FIRAuthCredential *_Nullable)authCredentialToken
token:(NSNumber *_Nullable)token;
@end

0 comments on commit ef0077e

Please sign in to comment.