Skip to content

Commit 93567e0

Browse files
committed
[auth-swift] VerifyPhoneNumber RPC (#10897)
1 parent 5284d65 commit 93567e0

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
@@ -966,7 +966,7 @@ - (void)internalSignInAndRetrieveDataWithCredential:(FIRAuthCredential *)credent
966966
}
967967

968968
[self
969-
completeSignInWithAccessToken:response.IDToken
969+
completeSignInWithAccessToken:response.idToken
970970
accessTokenExpirationDate:response.approximateExpirationDate
971971
refreshToken:response.refreshToken
972972
anonymous:NO
@@ -1629,7 +1629,7 @@ - (void)signInWithPhoneCredential:(FIRPhoneAuthCredential *)credential
16291629
phoneNumber:credential.phoneNumber
16301630
operation:operation
16311631
requestConfiguration:_requestConfiguration];
1632-
[FIRAuthBackend verifyPhoneNumber:request callback:callback];
1632+
[FIRAuthBackend2 postWithRequest:request callback:callback];
16331633
return;
16341634
}
16351635

@@ -1646,7 +1646,7 @@ - (void)signInWithPhoneCredential:(FIRPhoneAuthCredential *)credential
16461646
verificationCode:credential.verificationCode
16471647
operation:operation
16481648
requestConfiguration:_requestConfiguration];
1649-
[FIRAuthBackend verifyPhoneNumber:request callback:callback];
1649+
[FIRAuthBackend2 postWithRequest:request callback:callback];
16501650
}
16511651

16521652
#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
@@ -484,13 +484,6 @@ + (void)setDefaultBackendImplementationWithRPCIssuer:
484484
gBackendImplementation = defaultImplementation;
485485
}
486486

487-
#if TARGET_OS_IOS
488-
+ (void)verifyPhoneNumber:(FIRVerifyPhoneNumberRequest *)request
489-
callback:(FIRVerifyPhoneNumberResponseCallback)callback {
490-
[[self implementation] verifyPhoneNumber:request callback:callback];
491-
}
492-
#endif
493-
494487
+ (NSString *)authUserAgent {
495488
return [NSString stringWithFormat:@"FirebaseAuth.iOS/%@ %@", FIRFirebaseVersion(),
496489
GTMFetcherStandardUserAgentString(nil)];
@@ -581,35 +574,6 @@ - (instancetype)init {
581574
return self;
582575
}
583576

584-
#if TARGET_OS_IOS
585-
- (void)verifyPhoneNumber:(FIRVerifyPhoneNumberRequest *)request
586-
callback:(FIRVerifyPhoneNumberResponseCallback)callback {
587-
FIRVerifyPhoneNumberResponse *response = [[FIRVerifyPhoneNumberResponse alloc] init];
588-
[self
589-
postWithRequest:request
590-
response:response
591-
callback:^(NSError *error) {
592-
if (error) {
593-
callback(nil, error);
594-
return;
595-
}
596-
// Check whether or not the successful response is actually the special case phone
597-
// auth flow that returns a temporary proof and phone number.
598-
if (response.phoneNumber.length && response.temporaryProof.length) {
599-
FIRPhoneAuthCredential *credential = [[FIRPhoneAuthCredential alloc]
600-
initWithTemporaryProof:response.temporaryProof
601-
phoneNumber:response.phoneNumber
602-
providerID:FIRPhoneAuthProvider.id];
603-
callback(nil, [FIRAuthErrorUtils credentialAlreadyInUseErrorWithMessage:nil
604-
credential:credential
605-
email:nil]);
606-
return;
607-
}
608-
callback(response, nil);
609-
}];
610-
}
611-
#endif
612-
613577
#pragma mark - Generic RPC handling methods
614578

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

0 commit comments

Comments
 (0)