Skip to content

Commit

Permalink
Fix pr issues
Browse files Browse the repository at this point in the history
  • Loading branch information
renkelvin committed Oct 13, 2023
1 parent ada050e commit b38882c
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 114 deletions.
5 changes: 0 additions & 5 deletions FirebaseAuth/Sources/Auth/FIRAuth.m
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,6 @@
*/
static NSString *const kRevertSecondFactorAdditionRequestType = @"REVERT_SECOND_FACTOR_ADDITION";

/** @var kMissingRecaptchaTokenErrorPrefix
@brief The prefix of the error message of missing recaptcha token during authenticating.
*/
static NSString *const kMissingRecaptchaTokenErrorPrefix = @"MISSING_RECAPTCHA_TOKEN";

/** @var kMissingPasswordReason
@brief The reason why the @c FIRAuthErrorCodeWeakPassword error is thrown.
@remarks This error message will be localized in the future.
Expand Down
214 changes: 106 additions & 108 deletions FirebaseAuth/Sources/User/FIRUser.m
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,6 @@
*/
static NSString *const kMissingUsersErrorMessage = @"users";

/** @var kMissingRecaptchaTokenErrorPrefix
@brief The prefix of the error message of missing recaptcha token during authenticating.
*/
static NSString *const kMissingRecaptchaTokenErrorPrefix = @"MISSING_RECAPTCHA_TOKEN";

/** @typedef CallbackWithError
@brief The type for a callback block that only takes an error parameter.
*/
Expand Down Expand Up @@ -1085,6 +1080,109 @@ - (void)internalVerifyBeforeUpdateEmailWithNewEmail:(NSString *)newEmail
});
}

- (void)linkWithEmailPassword:(FIREmailPasswordAuthCredential *)credential
authResult:(FIRAuthDataResult *)authResult
completion:(nullable FIRAuthDataResultCallback)completion {
[self internalGetTokenWithCallback:^(NSString *_Nullable accessToken, NSError *_Nullable error) {
FIRAuthRequestConfiguration *requestConfiguration = self.auth.requestConfiguration;
FIRSignUpNewUserRequest *request =
[[FIRSignUpNewUserRequest alloc] initWithEmail:credential.email
password:credential.password
displayName:nil
idToken:accessToken
requestConfiguration:requestConfiguration];
FIRSignupNewUserCallback signUpNewUserCallback = ^(FIRSignUpNewUserResponse *_Nullable response,
NSError *_Nullable error) {
if (error) {
[self signOutIfTokenIsInvalidWithError:error];
callInMainThreadWithAuthDataResultAndError(completion, nil, error);
} else {
// Update the new token and refresh user info again.
self->_tokenService = [[FIRSecureTokenService alloc]
initWithRequestConfiguration:requestConfiguration
accessToken:response.IDToken
accessTokenExpirationDate:response.approximateExpirationDate
refreshToken:response.refreshToken];

[self internalGetTokenWithCallback:^(NSString *_Nullable accessToken,
NSError *_Nullable error) {
if (error) {
callInMainThreadWithAuthDataResultAndError(completion, nil, error);
return;
}
FIRGetAccountInfoRequest *getAccountInfoRequest =
[[FIRGetAccountInfoRequest alloc] initWithAccessToken:accessToken
requestConfiguration:requestConfiguration];
[FIRAuthBackend
getAccountInfo:getAccountInfoRequest
callback:^(FIRGetAccountInfoResponse *_Nullable response,
NSError *_Nullable error) {
if (error) {
[self signOutIfTokenIsInvalidWithError:error];
callInMainThreadWithAuthDataResultAndError(completion, nil, error);
return;
}
self.anonymous = NO;
[self updateWithGetAccountInfoResponse:response];
NSError *keychainError;
if (![self updateKeychain:&keychainError]) {
callInMainThreadWithAuthDataResultAndError(completion, nil, keychainError);
return;
}
[self signOutIfTokenIsInvalidWithError:error];
callInMainThreadWithAuthDataResultAndError(completion, authResult, nil);
}];
}];
}
};

#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST && (!defined(TARGET_OS_VISION) || !TARGET_OS_VISION)
if ([[FIRAuthRecaptchaVerifier sharedRecaptchaVerifier:self.auth]
enablementStatusForProvider:FIRAuthRecaptchaProviderPassword]) {
[[FIRAuthRecaptchaVerifier sharedRecaptchaVerifier:self.auth]
injectRecaptchaFields:request
provider:FIRAuthRecaptchaProviderPassword
action:FIRAuthRecaptchaActionSignUpPassword
completion:^(
FIRIdentityToolkitRequest<FIRAuthRPCRequest> *requestWithRecaptchaToken) {
[FIRAuthBackend
signUpNewUser:(FIRSignUpNewUserRequest *)requestWithRecaptchaToken
callback:signUpNewUserCallback];
}];
} else {
[FIRAuthBackend
signUpNewUser:request
callback:^(FIRSignUpNewUserResponse *_Nullable response, NSError *_Nullable error) {
if (!error) {
signUpNewUserCallback(response, nil);
return;
}
NSError *underlyingError = [error.userInfo objectForKey:NSUnderlyingErrorKey];
if (error.code == FIRAuthErrorCodeInternalError &&
[[underlyingError.userInfo
objectForKey:FIRAuthErrorUserInfoDeserializedResponseKey][@"message"]
hasPrefix:kMissingRecaptchaTokenErrorPrefix]) {
[[FIRAuthRecaptchaVerifier sharedRecaptchaVerifier:self.auth]
injectRecaptchaFields:request
provider:FIRAuthRecaptchaProviderPassword
action:FIRAuthRecaptchaActionSignUpPassword
completion:^(FIRIdentityToolkitRequest<FIRAuthRPCRequest>
*requestWithRecaptchaToken) {
[FIRAuthBackend signUpNewUser:(FIRSignUpNewUserRequest *)
requestWithRecaptchaToken
callback:signUpNewUserCallback];
}];
} else {
signUpNewUserCallback(nil, error);
}
}];
}
#else
[FIRAuthBackend signUpNewUser:request callback:signUpNewUserCallback];
#endif
}];
}

- (void)linkWithCredential:(FIRAuthCredential *)credential
completion:(nullable FIRAuthDataResultCallback)completion {
dispatch_async(FIRAuthGlobalWorkQueue(), ^{
Expand All @@ -1104,109 +1202,9 @@ - (void)linkWithCredential:(FIRAuthCredential *)credential
FIREmailPasswordAuthCredential *emailPasswordCredential =
(FIREmailPasswordAuthCredential *)credential;
if (emailPasswordCredential.password) {
[self internalGetTokenWithCallback:^(NSString *_Nullable accessToken,
NSError *_Nullable error) {
FIRAuthRequestConfiguration *requestConfiguration = self.auth.requestConfiguration;
FIRSignUpNewUserRequest *request =
[[FIRSignUpNewUserRequest alloc] initWithEmail:emailPasswordCredential.email
password:emailPasswordCredential.password
displayName:nil
idToken:accessToken
requestConfiguration:requestConfiguration];
FIRSignupNewUserCallback signUpNewUserCallback = ^(
FIRSignUpNewUserResponse *_Nullable response, NSError *_Nullable error) {
if (error) {
[self signOutIfTokenIsInvalidWithError:error];
callInMainThreadWithAuthDataResultAndError(completion, nil, error);
} else {
// Update the new token and refresh user info again.
self->_tokenService = [[FIRSecureTokenService alloc]
initWithRequestConfiguration:requestConfiguration
accessToken:response.IDToken
accessTokenExpirationDate:response.approximateExpirationDate
refreshToken:response.refreshToken];

[self internalGetTokenWithCallback:^(NSString *_Nullable accessToken,
NSError *_Nullable error) {
if (error) {
callInMainThreadWithAuthDataResultAndError(completion, nil, error);
return;
}
FIRGetAccountInfoRequest *getAccountInfoRequest =
[[FIRGetAccountInfoRequest alloc] initWithAccessToken:accessToken
requestConfiguration:requestConfiguration];
[FIRAuthBackend
getAccountInfo:getAccountInfoRequest
callback:^(FIRGetAccountInfoResponse *_Nullable response,
NSError *_Nullable error) {
if (error) {
[self signOutIfTokenIsInvalidWithError:error];
callInMainThreadWithAuthDataResultAndError(completion, nil, error);
return;
}
self.anonymous = NO;
[self updateWithGetAccountInfoResponse:response];
NSError *keychainError;
if (![self updateKeychain:&keychainError]) {
callInMainThreadWithAuthDataResultAndError(completion, nil,
keychainError);
return;
}
[self signOutIfTokenIsInvalidWithError:error];
callInMainThreadWithAuthDataResultAndError(completion, result, nil);
}];
}];
}
};

#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST && (!defined(TARGET_OS_VISION) || !TARGET_OS_VISION)
if ([[FIRAuthRecaptchaVerifier sharedRecaptchaVerifier:self.auth]
enablementStatusForProvider:FIRAuthRecaptchaProviderPassword]) {
[[FIRAuthRecaptchaVerifier sharedRecaptchaVerifier:self.auth]
injectRecaptchaFields:request
provider:FIRAuthRecaptchaProviderPassword
action:FIRAuthRecaptchaActionSignUpPassword
completion:^(FIRIdentityToolkitRequest<FIRAuthRPCRequest>
*requestWithRecaptchaToken) {
[FIRAuthBackend
signUpNewUser:(FIRSignUpNewUserRequest *)requestWithRecaptchaToken
callback:signUpNewUserCallback];
}];
} else {
[FIRAuthBackend
signUpNewUser:request
callback:^(FIRSignUpNewUserResponse *_Nullable response,
NSError *_Nullable error) {
if (!error) {
signUpNewUserCallback(response, nil);
return;
}
NSError *underlyingError =
[error.userInfo objectForKey:NSUnderlyingErrorKey];
if (error.code == FIRAuthErrorCodeInternalError &&
[[underlyingError.userInfo
objectForKey:FIRAuthErrorUserInfoDeserializedResponseKey][@"message"]
hasPrefix:kMissingRecaptchaTokenErrorPrefix]) {
[[FIRAuthRecaptchaVerifier sharedRecaptchaVerifier:self.auth]
injectRecaptchaFields:request
provider:FIRAuthRecaptchaProviderPassword
action:FIRAuthRecaptchaActionSignUpPassword
completion:^(FIRIdentityToolkitRequest<FIRAuthRPCRequest>
*requestWithRecaptchaToken) {
[FIRAuthBackend
signUpNewUser:(FIRSignUpNewUserRequest *)
requestWithRecaptchaToken
callback:signUpNewUserCallback];
}];
} else {
signUpNewUserCallback(nil, error);
}
}];
}
#else
[FIRAuthBackend signUpNewUser:request callback:signUpNewUserCallback];
#endif
}];
[self linkWithEmailPassword:emailPasswordCredential
authResult:result
completion:completion];
} else {
[self internalGetTokenWithCallback:^(NSString *_Nullable accessToken,
NSError *_Nullable error) {
Expand Down
5 changes: 5 additions & 0 deletions FirebaseAuth/Sources/Utilities/FIRAuthErrorUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@

NS_ASSUME_NONNULL_BEGIN

/** @var kMissingRecaptchaTokenErrorPrefix
@brief The prefix of the error message of missing recaptcha token during authenticating.
*/
static NSString *const kMissingRecaptchaTokenErrorPrefix = @"MISSING_RECAPTCHA_TOKEN";

/** @class FIRAuthErrorUtils
@brief Utility class used to construct @c NSError instances.
*/
Expand Down
2 changes: 1 addition & 1 deletion FirebaseAuth/Tests/Unit/FIRSignUpNewUserRequestTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
/** @var kTestIDToken
@brief Testing id token.
*/
static NSString *const kTestIDToken = @"IDToken";
static NSString *const kTestIDToken = @"testIDToken";

/** @var kPasswordKey
@brief the name of the "password" property in the request.
Expand Down

0 comments on commit b38882c

Please sign in to comment.