Skip to content

Commit 3364b56

Browse files
committed
[auth-swift] VerifyPhoneNumber RPC (#10897)
1 parent 55d0220 commit 3364b56

File tree

10 files changed

+252
-621
lines changed

10 files changed

+252
-621
lines changed

FirebaseAuth/Sources/Auth/FIRAuth.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,7 @@ - (void)internalSignInAndRetrieveDataWithCredential:(FIRAuthCredential *)credent
970970
}
971971

972972
[self
973-
completeSignInWithAccessToken:response.IDToken
973+
completeSignInWithAccessToken:response.idToken
974974
accessTokenExpirationDate:response.approximateExpirationDate
975975
refreshToken:response.refreshToken
976976
anonymous:NO
@@ -1633,7 +1633,7 @@ - (void)signInWithPhoneCredential:(FIRPhoneAuthCredential *)credential
16331633
phoneNumber:credential.phoneNumber
16341634
operation:operation
16351635
requestConfiguration:_requestConfiguration];
1636-
[FIRAuthBackend verifyPhoneNumber:request callback:callback];
1636+
[FIRAuthBackend2 postWithRequest:request callback:callback];
16371637
return;
16381638
}
16391639

@@ -1650,7 +1650,7 @@ - (void)signInWithPhoneCredential:(FIRPhoneAuthCredential *)credential
16501650
verificationCode:credential.verificationCode
16511651
operation:operation
16521652
requestConfiguration:_requestConfiguration];
1653-
[FIRAuthBackend verifyPhoneNumber:request callback:callback];
1653+
[FIRAuthBackend2 postWithRequest:request callback:callback];
16541654
}
16551655

16561656
#endif

FirebaseAuth/Sources/Backend/FIRAuthBackend.h

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
@protocol FIRAuthRPCRequest;
2020
@protocol FIRAuthRPCResponse;
2121
@class FIRAuthRequestConfiguration;
22-
@class FIRVerifyPhoneNumberRequest;
2322
@class FIRVerifyPhoneNumberResponse;
2423
// TODO: FIRSignUpNewUserResponse Used in extra internal functions in FIRAuth.m
2524
@class FIRSignUpNewUserResponse;
@@ -38,12 +37,6 @@ NS_ASSUME_NONNULL_BEGIN
3837
typedef void (^FIRAuthBackendRPCIssuerCompletionHandler)(NSData *_Nullable data,
3938
NSError *_Nullable error);
4039

41-
/** @typedef FIRDeleteCallBack
42-
@brief The type of block called when a request delete account has finished.
43-
@param error The error which occurred, or nil if the request was successful.
44-
*/
45-
typedef void (^FIRDeleteCallBack)(NSError *_Nullable error);
46-
4740
/** @typedef FIRSignupNewUserCallback
4841
@brief The type of block used to return the result of a call to the signupNewUser endpoint.
4942
@param response The received response, if any.
@@ -96,17 +89,6 @@ typedef void (^FIRVerifyPhoneNumberResponseCallback)(
9689
+ (void)setDefaultBackendImplementationWithRPCIssuer:
9790
(nullable id<FIRAuthBackendRPCIssuer>)RPCIssuer;
9891

99-
#if TARGET_OS_IOS
100-
/** @fn verifyPhoneNumber:callback:
101-
@brief Calls the verifyPhoneNumber endpoint, which is responsible for sending the verification
102-
code to a phone number specified in the request parameters.
103-
@param request The request parameters.
104-
@param callback The callback.
105-
*/
106-
+ (void)verifyPhoneNumber:(FIRVerifyPhoneNumberRequest *)request
107-
callback:(FIRVerifyPhoneNumberResponseCallback)callback;
108-
#endif
109-
11092
@end
11193

11294
/** @protocol FIRAuthBackendRPCIssuer
@@ -137,17 +119,6 @@ typedef void (^FIRVerifyPhoneNumberResponseCallback)(
137119
*/
138120
@protocol FIRAuthBackendImplementation <NSObject>
139121

140-
#if TARGET_OS_IOS
141-
/** @fn verifyPhoneNumber:callback:
142-
@brief Calls the verifyPhoneNumber endpoint, which is responsible for sending the verification
143-
code to a phone number specified in the request parameters.
144-
@param request The request parameters.
145-
@param callback The callback.
146-
*/
147-
- (void)verifyPhoneNumber:(FIRVerifyPhoneNumberRequest *)request
148-
callback:(FIRVerifyPhoneNumberResponseCallback)callback;
149-
#endif
150-
151122
/** @fn postWithRequest:response:callback:
152123
@brief Calls the RPC using HTTP POST.
153124
@remarks Possible error responses:

FirebaseAuth/Sources/Backend/FIRAuthBackend.m

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -486,13 +486,6 @@ + (void)setDefaultBackendImplementationWithRPCIssuer:
486486
gBackendImplementation = defaultImplementation;
487487
}
488488

489-
#if TARGET_OS_IOS
490-
+ (void)verifyPhoneNumber:(FIRVerifyPhoneNumberRequest *)request
491-
callback:(FIRVerifyPhoneNumberResponseCallback)callback {
492-
[[self implementation] verifyPhoneNumber:request callback:callback];
493-
}
494-
#endif
495-
496489
+ (NSString *)authUserAgent {
497490
return [NSString stringWithFormat:@"FirebaseAuth.iOS/%@ %@", FIRFirebaseVersion(),
498491
GTMFetcherStandardUserAgentString(nil)];
@@ -602,35 +595,6 @@ - (instancetype)init {
602595
return self;
603596
}
604597

605-
#if TARGET_OS_IOS
606-
- (void)verifyPhoneNumber:(FIRVerifyPhoneNumberRequest *)request
607-
callback:(FIRVerifyPhoneNumberResponseCallback)callback {
608-
FIRVerifyPhoneNumberResponse *response = [[FIRVerifyPhoneNumberResponse alloc] init];
609-
[self
610-
postWithRequest:request
611-
response:response
612-
callback:^(NSError *error) {
613-
if (error) {
614-
callback(nil, error);
615-
return;
616-
}
617-
// Check whether or not the successful response is actually the special case phone
618-
// auth flow that returns a temporary proof and phone number.
619-
if (response.phoneNumber.length && response.temporaryProof.length) {
620-
FIRPhoneAuthCredential *credential = [[FIRPhoneAuthCredential alloc]
621-
initWithTemporaryProof:response.temporaryProof
622-
phoneNumber:response.phoneNumber
623-
providerID:FIRPhoneAuthProvider.id];
624-
callback(nil, [FIRAuthErrorUtils credentialAlreadyInUseErrorWithMessage:nil
625-
credential:credential
626-
email:nil]);
627-
return;
628-
}
629-
callback(response, nil);
630-
}];
631-
}
632-
#endif
633-
634598
#pragma mark - Generic RPC handling methods
635599

636600
/** @fn postWithRequest:response:callback:

FirebaseAuth/Sources/Swift/Backend/AuthBackend.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ private class AuthBackendRPCImplementation: NSObject, AuthBackendImplementation
175175
.generateMFAError(response: response,
176176
auth: request.requestConfiguration().auth) {
177177
callback(nil, mfaError)
178+
} else if let error = AuthBackendRPCImplementation.phoneCredentialInUse(response: response) {
179+
callback(nil, error)
178180
} else {
179181
callback(response, nil)
180182
}
@@ -205,6 +207,31 @@ private class AuthBackendRPCImplementation: NSObject, AuthBackendImplementation
205207
}
206208
#endif
207209

210+
#if os(iOS)
211+
// Check whether or not the successful response is actually the special case phone
212+
// auth flow that returns a temporary proof and phone number.
213+
private class func phoneCredentialInUse(response: AuthRPCResponse) -> Error? {
214+
if let phoneAuthResponse = response as? VerifyPhoneNumberResponse,
215+
let phoneNumber = phoneAuthResponse.phoneNumber,
216+
phoneNumber.count > 0,
217+
let temporaryProof = phoneAuthResponse.temporaryProof,
218+
temporaryProof.count > 0 {
219+
let credential = PhoneAuthCredential(withTemporaryProof: temporaryProof,
220+
phoneNumber: phoneNumber,
221+
providerID: PhoneAuthProvider.id)
222+
return AuthErrorUtils.credentialAlreadyInUseError(message: nil,
223+
credential: credential,
224+
email: nil)
225+
} else {
226+
return nil
227+
}
228+
}
229+
#else
230+
private class func phoneCredentialInUse(response: AuthRPCResponse) -> Error? {
231+
return nil
232+
}
233+
#endif
234+
208235
/** @fn postWithRequest:response:callback:
209236
@brief Calls the RPC using HTTP POST.
210237
@remarks Possible error responses:
@@ -446,6 +473,12 @@ private class AuthBackendRPCImplementation: NSObject, AuthBackendImplementation
446473
.invalidAppCredential(message: serverDetailErrorMessage)
447474
case "MISSING_APP_CREDENTIAL": return AuthErrorUtils
448475
.missingAppCredential(message: serverDetailErrorMessage)
476+
case "INVALID_CODE": return AuthErrorUtils
477+
.invalidVerificationCodeError(message: serverDetailErrorMessage)
478+
case "INVALID_SESSION_INFO": return AuthErrorUtils
479+
.invalidVerificationIDError(message: serverDetailErrorMessage)
480+
case "SESSION_EXPIRED": return AuthErrorUtils
481+
.sessionExpiredError(message: serverDetailErrorMessage)
449482
case "FEDERATED_USER_ID_ALREADY_LINKED":
450483
guard let verifyAssertion = response as? VerifyAssertionResponse else {
451484
return AuthErrorUtils.credentialAlreadyInUseError(

FirebaseAuth/Sources/Swift/Backend/RPC/VerifyPhoneNumberResponse.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import Foundation
2121
access token from Secure Token Service, depending on whether @c returnSecureToken is set
2222
on the request.
2323
*/
24-
@objc public var IDToken: String?
24+
@objc public var idToken: String?
2525

2626
/** @property refreshToken
2727
@brief The refresh token from Secure Token Service.
@@ -60,7 +60,7 @@ import Foundation
6060
}
6161

6262
public func setFields(dictionary: [String: Any]) throws {
63-
IDToken = dictionary["idToken"] as? String
63+
idToken = dictionary["idToken"] as? String
6464
refreshToken = dictionary["refreshToken"] as? String
6565
isNewUser = (dictionary["isNewUser"] as? Bool) ?? false
6666
localID = dictionary["localId"] as? String

FirebaseAuth/Sources/Swift/Utilities/AuthErrors.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import Foundation
4747
exists.
4848
*/
4949
@objc(FIRAuthErrorUserInfoEmailKey)
50-
public static var AuthErrorUserInfoEmailKey: String =
50+
public static let AuthErrorUserInfoEmailKey: String =
5151
"FIRAuthErrorUserInfoEmailKey"
5252

5353
/**
@@ -56,15 +56,15 @@ import Foundation
5656
recovery if applicable.
5757
*/
5858
@objc(FIRAuthErrorUserInfoUpdatedCredentialKey)
59-
public static var AuthErrorUserInfoUpdatedCredentialKey: String =
59+
public static let AuthErrorUserInfoUpdatedCredentialKey: String =
6060
"FIRAuthErrorUserInfoUpdatedCredentialKey"
6161

6262
/**
6363
@brief The key used to read the MFA resolver from the userInfo dictionary of the NSError object
6464
returned when 2FA is required for sign-incompletion.
6565
*/
6666
@objc(FIRAuthErrorUserInfoMultiFactorResolverKey)
67-
static var AuthErrorUserInfoMultiFactorResolverKey: String =
67+
static let AuthErrorUserInfoMultiFactorResolverKey: String =
6868
"FIRAuthErrorUserInfoMultiFactorResolverKey"
6969
}
7070

FirebaseAuth/Sources/User/FIRUser.m

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -688,39 +688,39 @@ - (void)internalUpdateOrLinkPhoneNumberCredential:(FIRPhoneAuthCredential *)phon
688688
operation:operation
689689
requestConfiguration:self->_auth.requestConfiguration];
690690
request.accessToken = accessToken;
691-
[FIRAuthBackend verifyPhoneNumber:request
692-
callback:^(FIRVerifyPhoneNumberResponse *_Nullable response,
693-
NSError *_Nullable error) {
694-
if (error) {
695-
[self signOutIfTokenIsInvalidWithError:error];
696-
completion(error);
697-
return;
698-
}
699-
FIRAuthRequestConfiguration *requestConfiguration =
700-
self.auth.requestConfiguration;
701-
// Update the new token and refresh user info again.
702-
self->_tokenService = [[FIRSecureTokenService alloc]
703-
initWithRequestConfiguration:requestConfiguration
704-
accessToken:response.IDToken
705-
accessTokenExpirationDate:response.approximateExpirationDate
706-
refreshToken:response.refreshToken];
707-
// Get account info to update cached user info.
708-
[self getAccountInfoRefreshingCache:^(
709-
FIRGetAccountInfoResponseUser *_Nullable user,
710-
NSError *_Nullable error) {
711-
if (error) {
712-
[self signOutIfTokenIsInvalidWithError:error];
713-
completion(error);
714-
return;
715-
}
716-
self.anonymous = NO;
717-
if (![self updateKeychain:&error]) {
718-
completion(error);
719-
return;
720-
}
721-
completion(nil);
722-
}];
723-
}];
691+
[FIRAuthBackend2
692+
postWithRequest:request
693+
callback:^(FIRVerifyPhoneNumberResponse *_Nullable response,
694+
NSError *_Nullable error) {
695+
if (error) {
696+
[self signOutIfTokenIsInvalidWithError:error];
697+
completion(error);
698+
return;
699+
}
700+
FIRAuthRequestConfiguration *requestConfiguration = self.auth.requestConfiguration;
701+
// Update the new token and refresh user info again.
702+
self->_tokenService = [[FIRSecureTokenService alloc]
703+
initWithRequestConfiguration:requestConfiguration
704+
accessToken:response.idToken
705+
accessTokenExpirationDate:response.approximateExpirationDate
706+
refreshToken:response.refreshToken];
707+
// Get account info to update cached user info.
708+
[self
709+
getAccountInfoRefreshingCache:^(FIRGetAccountInfoResponseUser *_Nullable user,
710+
NSError *_Nullable error) {
711+
if (error) {
712+
[self signOutIfTokenIsInvalidWithError:error];
713+
completion(error);
714+
return;
715+
}
716+
self.anonymous = NO;
717+
if (![self updateKeychain:&error]) {
718+
completion(error);
719+
return;
720+
}
721+
completion(nil);
722+
}];
723+
}];
724724
}];
725725
}
726726

0 commit comments

Comments
 (0)