Skip to content

Commit

Permalink
Enable full phone MFA flow for emulator requests
Browse files Browse the repository at this point in the history
  • Loading branch information
dantheli committed Apr 30, 2024
1 parent b5c4044 commit 254afdb
Showing 1 changed file with 114 additions and 132 deletions.
246 changes: 114 additions & 132 deletions FirebaseAuth/Sources/AuthProvider/Phone/FIRPhoneAuthProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -445,148 +445,130 @@ - (void)verifyClientAndSendVerificationCodeToPhoneNumber:(NSString *)phoneNumber
}];
}

- (void)sendVerificationCodeToPhoneNumber:(NSString *)phoneNumber
retryOnInvalidAppCredential:(BOOL)retryOnInvalidAppCredential
UIDelegate:(nullable id<FIRAuthUIDelegate>)UIDelegate
multiFactorSession:(nullable FIRMultiFactorSession *)session
appCredential:(nullable FIRAuthAppCredential *)appCredential
reCAPTCHAToken:(nullable NSString *)reCAPTCHAToken
callback:(FIRVerificationResultCallback)callback {
NSString *IDToken = session.IDToken;
FIRAuthProtoStartMFAPhoneRequestInfo *startMFARequestInfo =
[[FIRAuthProtoStartMFAPhoneRequestInfo alloc] initWithPhoneNumber:phoneNumber
appCredential:appCredential
reCAPTCHAToken:reCAPTCHAToken];
if (session.IDToken) {
FIRStartMFAEnrollmentRequest *request =
[[FIRStartMFAEnrollmentRequest alloc] initWithIDToken:IDToken
enrollmentInfo:startMFARequestInfo
requestConfiguration:self->_auth.requestConfiguration];
[FIRAuthBackend
startMultiFactorEnrollment:request
callback:^(FIRStartMFAEnrollmentResponse *_Nullable response,
NSError *_Nullable error) {
if (error) {
if (error.code == FIRAuthErrorCodeInvalidAppCredential) {
if (retryOnInvalidAppCredential) {
[self->_auth.appCredentialManager clearCredential];
[self verifyClientAndSendVerificationCodeToPhoneNumber:phoneNumber
retryOnInvalidAppCredential:NO
UIDelegate:UIDelegate
multiFactorSession:session
callback:callback];
return;
}
if (callback) {
callback(nil,
[FIRAuthErrorUtils
unexpectedResponseWithDeserializedResponse:nil
underlyingError:error]);
}
return;
} else {
if (callback) {
callback(nil, error);
}
}
} else {
if (callback) {
callback(response.phoneSessionInfo.sessionInfo, nil);
}
}
}];
} else {
FIRStartMFASignInRequest *request = [[FIRStartMFASignInRequest alloc]
initWithMFAPendingCredential:session.MFAPendingCredential
MFAEnrollmentID:session.multiFactorInfo.UID
signInInfo:startMFARequestInfo
requestConfiguration:self->_auth.requestConfiguration];
[FIRAuthBackend
startMultiFactorSignIn:request
callback:^(FIRStartMFASignInResponse *_Nullable response,
NSError *_Nullable error) {
if (error) {
if (error.code == FIRAuthErrorCodeInvalidAppCredential) {
if (retryOnInvalidAppCredential) {
[self->_auth.appCredentialManager clearCredential];
[self verifyClientAndSendVerificationCodeToPhoneNumber:phoneNumber
retryOnInvalidAppCredential:NO
UIDelegate:UIDelegate
multiFactorSession:session
callback:callback];
return;
}
if (callback) {
callback(nil, [FIRAuthErrorUtils
unexpectedResponseWithDeserializedResponse:nil
underlyingError:error]);
}
return;
} else {
if (callback) {
callback(nil, error);
}
}
} else {
if (callback) {
callback(response.responseInfo.sessionInfo, nil);
}
}
}];
}
}

- (void)verifyClientAndSendVerificationCodeToPhoneNumber:(NSString *)phoneNumber
retryOnInvalidAppCredential:(BOOL)retryOnInvalidAppCredential
UIDelegate:(nullable id<FIRAuthUIDelegate>)UIDelegate
multiFactorSession:(nullable FIRMultiFactorSession *)session
callback:(FIRVerificationResultCallback)callback {
if (_auth.settings.isAppVerificationDisabledForTesting) {
FIRSendVerificationCodeRequest *request =
[[FIRSendVerificationCodeRequest alloc] initWithPhoneNumber:phoneNumber
appCredential:nil
reCAPTCHAToken:nil
requestConfiguration:_auth.requestConfiguration];
[FIRAuthBackend sendVerificationCode:request
callback:^(FIRSendVerificationCodeResponse *_Nullable response,
NSError *_Nullable error) {
callback(response.verificationID, error);
}];
[self sendVerificationCodeToPhoneNumber:phoneNumber
retryOnInvalidAppCredential:retryOnInvalidAppCredential
UIDelegate:UIDelegate
multiFactorSession:session
appCredential:nil
reCAPTCHAToken:nil
callback:callback];
return;
}

[self
verifyClientWithUIDelegate:UIDelegate
completion:^(FIRAuthAppCredential *_Nullable appCredential,
NSString *_Nullable reCAPTCHAToken, NSError *_Nullable error) {
if (error) {
if (callback) {
callback(nil, error);
[self verifyClientWithUIDelegate:UIDelegate
completion:^(FIRAuthAppCredential *_Nullable appCredential,
NSString *_Nullable reCAPTCHAToken, NSError *_Nullable error) {
if (error) {
if (callback) {
callback(nil, error);
}
return;
}
return;
}

NSString *IDToken = session.IDToken;
FIRAuthProtoStartMFAPhoneRequestInfo *startMFARequestInfo =
[[FIRAuthProtoStartMFAPhoneRequestInfo alloc]
initWithPhoneNumber:phoneNumber
appCredential:appCredential
reCAPTCHAToken:reCAPTCHAToken];
if (session.IDToken) {
FIRStartMFAEnrollmentRequest *request =
[[FIRStartMFAEnrollmentRequest alloc]
initWithIDToken:IDToken
enrollmentInfo:startMFARequestInfo
requestConfiguration:self->_auth.requestConfiguration];
[FIRAuthBackend
startMultiFactorEnrollment:request
callback:^(FIRStartMFAEnrollmentResponse
*_Nullable response,
NSError *_Nullable error) {
if (error) {
if (error.code ==
FIRAuthErrorCodeInvalidAppCredential) {
if (retryOnInvalidAppCredential) {
[self->_auth.appCredentialManager
clearCredential];
[self
verifyClientAndSendVerificationCodeToPhoneNumber:
phoneNumber
retryOnInvalidAppCredential:
NO
UIDelegate:
UIDelegate
multiFactorSession:
session
callback:
callback];
return;
}
if (callback) {
callback(
nil,
[FIRAuthErrorUtils
unexpectedResponseWithDeserializedResponse:
nil
underlyingError:
error]);
}
return;
} else {
if (callback) {
callback(nil, error);
}
}
} else {
if (callback) {
callback(
response.phoneSessionInfo.sessionInfo,
nil);
}
}
}];
} else {
FIRStartMFASignInRequest *request = [[FIRStartMFASignInRequest alloc]
initWithMFAPendingCredential:session.MFAPendingCredential
MFAEnrollmentID:session.multiFactorInfo.UID
signInInfo:startMFARequestInfo
requestConfiguration:self->_auth.requestConfiguration];
[FIRAuthBackend
startMultiFactorSignIn:request
callback:^(
FIRStartMFASignInResponse *_Nullable response,
NSError *_Nullable error) {
if (error) {
if (error.code ==
FIRAuthErrorCodeInvalidAppCredential) {
if (retryOnInvalidAppCredential) {
[self->_auth
.appCredentialManager clearCredential];
[self
verifyClientAndSendVerificationCodeToPhoneNumber:
phoneNumber
retryOnInvalidAppCredential:
NO
UIDelegate:
UIDelegate
multiFactorSession:
session
callback:
callback];
return;
}
if (callback) {
callback(
nil,
[FIRAuthErrorUtils
unexpectedResponseWithDeserializedResponse:
nil
underlyingError:
error]);
}
return;
} else {
if (callback) {
callback(nil, error);
}
}
} else {
if (callback) {
callback(response.responseInfo.sessionInfo, nil);
}
}
}];
}
}];
[self sendVerificationCodeToPhoneNumber:phoneNumber
retryOnInvalidAppCredential:retryOnInvalidAppCredential
UIDelegate:UIDelegate
multiFactorSession:session
appCredential:appCredential
reCAPTCHAToken:reCAPTCHAToken
callback:callback];
}];
}

/** @fn verifyClientWithCompletion:completion:
Expand Down

0 comments on commit 254afdb

Please sign in to comment.