FIRPhoneAuthProvider.m 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <TargetConditionals.h>
  17. #if TARGET_OS_IOS
  18. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRAuthSettings.h"
  19. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRMultiFactorResolver.h"
  20. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRPhoneAuthProvider.h"
  21. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  22. #import "FirebaseAuth/Sources/Auth/FIRAuthGlobalWorkQueue.h"
  23. #import "FirebaseAuth/Sources/Auth/FIRAuth_Internal.h"
  24. #import "FirebaseAuth/Sources/Backend/FIRAuthBackend+MultiFactor.h"
  25. #import "FirebaseAuth/Sources/Backend/FIRAuthBackend.h"
  26. #import "FirebaseAuth/Sources/Backend/RPC/FIRGetProjectConfigRequest.h"
  27. #import "FirebaseAuth/Sources/Backend/RPC/FIRGetProjectConfigResponse.h"
  28. #import "FirebaseAuth/Sources/Backend/RPC/FIRSendVerificationCodeRequest.h"
  29. #import "FirebaseAuth/Sources/Backend/RPC/FIRSendVerificationCodeResponse.h"
  30. #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyClientRequest.h"
  31. #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyClientResponse.h"
  32. #import "FirebaseAuth/Sources/Backend/RPC/MultiFactor/Enroll/FIRStartMFAEnrollmentRequest.h"
  33. #import "FirebaseAuth/Sources/Backend/RPC/MultiFactor/Enroll/FIRStartMFAEnrollmentResponse.h"
  34. #import "FirebaseAuth/Sources/Backend/RPC/Proto/Phone/FIRAuthProtoStartMFAPhoneRequestInfo.h"
  35. #import "FirebaseAuth/Sources/MultiFactor/FIRMultiFactorSession+Internal.h"
  36. #import "FirebaseAuth/Sources/SystemService/FIRAuthAPNSToken.h"
  37. #import "FirebaseAuth/Sources/SystemService/FIRAuthAPNSTokenManager.h"
  38. #import "FirebaseAuth/Sources/SystemService/FIRAuthAppCredential.h"
  39. #import "FirebaseAuth/Sources/SystemService/FIRAuthAppCredentialManager.h"
  40. #import "FirebaseAuth/Sources/SystemService/FIRAuthNotificationManager.h"
  41. #import "FirebaseAuth/Sources/Utilities/FIRAuthErrorUtils.h"
  42. #import "FirebaseAuth/Sources/Utilities/FIRAuthURLPresenter.h"
  43. #import "FirebaseAuth/Sources/Utilities/FIRAuthWebUtils.h"
  44. #if TARGET_OS_IOS
  45. #import "FirebaseAuth/Sources/AuthProvider/Phone/FIRPhoneAuthCredential_Internal.h"
  46. #import "FirebaseAuth/Sources/MultiFactor/Phone/FIRPhoneMultiFactorInfo+Internal.h"
  47. #endif
  48. NS_ASSUME_NONNULL_BEGIN
  49. /** @typedef FIRReCAPTCHAURLCallBack
  50. @brief The callback invoked at the end of the flow to fetch a reCAPTCHA URL.
  51. @param reCAPTCHAURL The reCAPTCHA URL.
  52. @param error The error that occurred while fetching the reCAPTCHAURL, if any.
  53. */
  54. typedef void (^FIRReCAPTCHAURLCallBack)(NSURL *_Nullable reCAPTCHAURL, NSError *_Nullable error);
  55. /** @typedef FIRVerifyClientCallback
  56. @brief The callback invoked at the end of a client verification flow.
  57. @param appCredential credential that proves the identity of the app during a phone
  58. authentication flow.
  59. @param error The error that occurred while verifying the app, if any.
  60. */
  61. typedef void (^FIRVerifyClientCallback)(FIRAuthAppCredential *_Nullable appCredential,
  62. NSString *_Nullable reCAPTCHAToken,
  63. NSError *_Nullable error);
  64. /** @typedef FIRFetchAuthDomainCallback
  65. @brief The callback invoked at the end of the flow to fetch the Auth domain.
  66. @param authDomain The Auth domain.
  67. @param error The error that occurred while fetching the auth domain, if any.
  68. */
  69. typedef void (^FIRFetchAuthDomainCallback)(NSString *_Nullable authDomain,
  70. NSError *_Nullable error);
  71. /** @var kauthTypeVerifyApp
  72. @brief The auth type to be specified in the app verification request.
  73. */
  74. static NSString *const kAuthTypeVerifyApp = @"verifyApp";
  75. /** @var kCustomUrlSchemePrefix
  76. @brief The prefix to append to the Firebase app ID custom callback scheme..
  77. */
  78. static NSString *const kCustomUrlSchemePrefix = @"app-";
  79. /** @var kReCAPTCHAURLStringFormat
  80. @brief The format of the URL used to open the reCAPTCHA page during app verification.
  81. */
  82. NSString *const kReCAPTCHAURLStringFormat = @"https://%@/__/auth/handler?";
  83. extern NSString *const FIRPhoneMultiFactorID;
  84. @implementation FIRPhoneAuthProvider {
  85. /** @var _auth
  86. @brief The auth instance used for verifying the phone number.
  87. */
  88. FIRAuth *_auth;
  89. /** @var _callbackScheme
  90. @brief The callback URL scheme used for reCAPTCHA fallback.
  91. */
  92. NSString *_callbackScheme;
  93. /** @var _usingClientIDScheme
  94. @brief True if the reverse client ID is registered as a custom URL scheme, and false
  95. otherwise.
  96. */
  97. BOOL _usingClientIDScheme;
  98. }
  99. /** @fn initWithAuth:
  100. @brief returns an instance of @c FIRPhoneAuthProvider associated with the provided auth
  101. instance.
  102. @return An Instance of @c FIRPhoneAuthProvider.
  103. */
  104. - (nullable instancetype)initWithAuth:(FIRAuth *)auth {
  105. self = [super init];
  106. if (self) {
  107. _auth = auth;
  108. if (_auth.app.options.clientID) {
  109. NSString *reverseClientIDScheme =
  110. [[[_auth.app.options.clientID componentsSeparatedByString:@"."]
  111. reverseObjectEnumerator].allObjects componentsJoinedByString:@"."];
  112. if ([FIRAuthWebUtils isCallbackSchemeRegisteredForCustomURLScheme:reverseClientIDScheme]) {
  113. _callbackScheme = reverseClientIDScheme;
  114. _usingClientIDScheme = YES;
  115. }
  116. }
  117. if (!_usingClientIDScheme) {
  118. _callbackScheme = [kCustomUrlSchemePrefix
  119. stringByAppendingString:[_auth.app.options.googleAppID
  120. stringByReplacingOccurrencesOfString:@":"
  121. withString:@"-"]];
  122. }
  123. }
  124. return self;
  125. }
  126. - (void)verifyPhoneNumber:(NSString *)phoneNumber
  127. UIDelegate:(nullable id<FIRAuthUIDelegate>)UIDelegate
  128. completion:(nullable FIRVerificationResultCallback)completion {
  129. if (![FIRAuthWebUtils isCallbackSchemeRegisteredForCustomURLScheme:_callbackScheme]) {
  130. [NSException raise:NSInternalInconsistencyException
  131. format:@"Please register custom URL scheme '%@' in the app's Info.plist file.",
  132. _callbackScheme];
  133. }
  134. dispatch_async(FIRAuthGlobalWorkQueue(), ^{
  135. FIRVerificationResultCallback callBackOnMainThread =
  136. ^(NSString *_Nullable verificationID, NSError *_Nullable error) {
  137. if (completion) {
  138. dispatch_async(dispatch_get_main_queue(), ^{
  139. completion(verificationID, error);
  140. });
  141. }
  142. };
  143. [self
  144. internalVerifyPhoneNumber:phoneNumber
  145. UIDelegate:UIDelegate
  146. completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
  147. if (!error) {
  148. callBackOnMainThread(verificationID, nil);
  149. return;
  150. } else {
  151. callBackOnMainThread(nil, error);
  152. return;
  153. }
  154. }];
  155. });
  156. }
  157. - (void)verifyPhoneNumberWithMultiFactorInfo:(FIRPhoneMultiFactorInfo *)phoneMultiFactorInfo
  158. UIDelegate:(nullable id<FIRAuthUIDelegate>)UIDelegate
  159. multiFactorSession:(nullable FIRMultiFactorSession *)session
  160. completion:(nullable FIRVerificationResultCallback)completion {
  161. session.multiFactorInfo = phoneMultiFactorInfo;
  162. [self verifyPhoneNumber:phoneMultiFactorInfo.phoneNumber
  163. UIDelegate:UIDelegate
  164. multiFactorSession:session
  165. completion:completion];
  166. }
  167. - (void)verifyPhoneNumber:(NSString *)phoneNumber
  168. UIDelegate:(nullable id<FIRAuthUIDelegate>)UIDelegate
  169. multiFactorSession:(nullable FIRMultiFactorSession *)session
  170. completion:(nullable FIRVerificationResultCallback)completion {
  171. if (!session) {
  172. [self verifyPhoneNumber:phoneNumber UIDelegate:UIDelegate completion:completion];
  173. return;
  174. }
  175. if (![FIRAuthWebUtils isCallbackSchemeRegisteredForCustomURLScheme:_callbackScheme]) {
  176. [NSException raise:NSInternalInconsistencyException
  177. format:@"Please register custom URL scheme '%@' in the app's Info.plist file.",
  178. _callbackScheme];
  179. }
  180. dispatch_async(FIRAuthGlobalWorkQueue(), ^{
  181. FIRVerificationResultCallback callBackOnMainThread =
  182. ^(NSString *_Nullable verificationID, NSError *_Nullable error) {
  183. if (completion) {
  184. dispatch_async(dispatch_get_main_queue(), ^{
  185. completion(verificationID, error);
  186. });
  187. }
  188. };
  189. [self
  190. internalVerifyPhoneNumber:phoneNumber
  191. UIDelegate:UIDelegate
  192. multiFactorSession:session
  193. completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
  194. if (!error) {
  195. callBackOnMainThread(verificationID, nil);
  196. return;
  197. } else {
  198. callBackOnMainThread(nil, error);
  199. return;
  200. }
  201. }];
  202. });
  203. }
  204. - (FIRPhoneAuthCredential *)credentialWithVerificationID:(NSString *)verificationID
  205. verificationCode:(NSString *)verificationCode {
  206. return [[FIRPhoneAuthCredential alloc] initWithProviderID:FIRPhoneAuthProviderID
  207. verificationID:verificationID
  208. verificationCode:verificationCode];
  209. }
  210. + (instancetype)provider {
  211. return [[self alloc] initWithAuth:[FIRAuth auth]];
  212. }
  213. + (instancetype)providerWithAuth:(FIRAuth *)auth {
  214. return [[self alloc] initWithAuth:auth];
  215. }
  216. #pragma mark - Internal Methods
  217. /** @fn reCAPTCHATokenForURL:error:
  218. @brief Parses the reCAPTCHA URL and returns the reCAPTCHA token.
  219. @param URL The url to be parsed for a reCAPTCHA token.
  220. @param error The error that occurred if any.
  221. @return The reCAPTCHA token if successful.
  222. */
  223. - (nullable NSString *)reCAPTCHATokenForURL:(NSURL *)URL error:(NSError **_Nonnull)error {
  224. NSURLComponents *actualURLComponents = [NSURLComponents componentsWithURL:URL
  225. resolvingAgainstBaseURL:NO];
  226. NSArray<NSURLQueryItem *> *queryItems = [actualURLComponents queryItems];
  227. NSString *deepLinkURL = [FIRAuthWebUtils queryItemValue:@"deep_link_id" from:queryItems];
  228. NSData *errorData;
  229. if (deepLinkURL) {
  230. actualURLComponents = [NSURLComponents componentsWithString:deepLinkURL];
  231. queryItems = [actualURLComponents queryItems];
  232. NSString *recaptchaToken = [FIRAuthWebUtils queryItemValue:@"recaptchaToken" from:queryItems];
  233. if (recaptchaToken) {
  234. return recaptchaToken;
  235. }
  236. NSString *firebaseError = [FIRAuthWebUtils queryItemValue:@"firebaseError" from:queryItems];
  237. errorData = [firebaseError dataUsingEncoding:NSUTF8StringEncoding];
  238. } else {
  239. errorData = nil;
  240. }
  241. if (error != NULL && errorData != nil) {
  242. NSError *jsonError;
  243. NSDictionary *errorDict = [NSJSONSerialization JSONObjectWithData:errorData
  244. options:0
  245. error:&jsonError];
  246. if (jsonError) {
  247. *error = [FIRAuthErrorUtils JSONSerializationErrorWithUnderlyingError:jsonError];
  248. return nil;
  249. }
  250. *error = [FIRAuthErrorUtils URLResponseErrorWithCode:errorDict[@"code"]
  251. message:errorDict[@"message"]];
  252. if (!*error) {
  253. NSString *reason;
  254. if (errorDict[@"code"] && errorDict[@"message"]) {
  255. reason =
  256. [NSString stringWithFormat:@"[%@] - %@", errorDict[@"code"], errorDict[@"message"]];
  257. } else {
  258. reason = [NSString stringWithFormat:@"An unknown error occurred with the following "
  259. "response: %@",
  260. deepLinkURL];
  261. }
  262. *error = [FIRAuthErrorUtils appVerificationUserInteractionFailureWithReason:reason];
  263. }
  264. }
  265. return nil;
  266. }
  267. /** @fn internalVerifyPhoneNumber:completion:
  268. @brief Starts the phone number authentication flow by sending a verifcation code to the
  269. specified phone number.
  270. @param phoneNumber The phone number to be verified.
  271. @param completion The callback to be invoked when the verification flow is finished.
  272. */
  273. - (void)internalVerifyPhoneNumber:(NSString *)phoneNumber
  274. UIDelegate:(nullable id<FIRAuthUIDelegate>)UIDelegate
  275. completion:(nullable FIRVerificationResultCallback)completion {
  276. if (!phoneNumber.length) {
  277. completion(nil, [FIRAuthErrorUtils missingPhoneNumberErrorWithMessage:nil]);
  278. return;
  279. }
  280. [_auth.notificationManager
  281. checkNotificationForwardingWithCallback:^(BOOL isNotificationBeingForwarded) {
  282. if (!isNotificationBeingForwarded) {
  283. completion(nil, [FIRAuthErrorUtils notificationNotForwardedError]);
  284. return;
  285. }
  286. FIRVerificationResultCallback callback =
  287. ^(NSString *_Nullable verificationID, NSError *_Nullable error) {
  288. if (completion) {
  289. completion(verificationID, error);
  290. }
  291. };
  292. [self verifyClientAndSendVerificationCodeToPhoneNumber:phoneNumber
  293. retryOnInvalidAppCredential:YES
  294. UIDelegate:UIDelegate
  295. callback:callback];
  296. }];
  297. }
  298. - (void)internalVerifyPhoneNumber:(NSString *)phoneNumber
  299. UIDelegate:(nullable id<FIRAuthUIDelegate>)UIDelegate
  300. multiFactorSession:(nullable FIRMultiFactorSession *)session
  301. completion:(nullable FIRVerificationResultCallback)completion {
  302. if (!phoneNumber.length) {
  303. if (completion) {
  304. completion(nil, [FIRAuthErrorUtils missingPhoneNumberErrorWithMessage:nil]);
  305. }
  306. return;
  307. }
  308. [_auth.notificationManager
  309. checkNotificationForwardingWithCallback:^(BOOL isNotificationBeingForwarded) {
  310. if (!isNotificationBeingForwarded) {
  311. if (completion) {
  312. completion(nil, [FIRAuthErrorUtils notificationNotForwardedError]);
  313. }
  314. return;
  315. }
  316. FIRVerificationResultCallback callback =
  317. ^(NSString *_Nullable verificationID, NSError *_Nullable error) {
  318. if (completion) {
  319. completion(verificationID, error);
  320. }
  321. };
  322. [self verifyClientAndSendVerificationCodeToPhoneNumber:phoneNumber
  323. retryOnInvalidAppCredential:YES
  324. UIDelegate:UIDelegate
  325. multiFactorSession:session
  326. callback:callback];
  327. }];
  328. }
  329. /** @fn verifyClientAndSendVerificationCodeToPhoneNumber:retryOnInvalidAppCredential:callback:
  330. @brief Starts the flow to verify the client via silent push notification.
  331. @param retryOnInvalidAppCredential Whether of not the flow should be retried if an
  332. FIRAuthErrorCodeInvalidAppCredential error is returned from the backend.
  333. @param phoneNumber The phone number to be verified.
  334. @param callback The callback to be invoked on the global work queue when the flow is
  335. finished.
  336. */
  337. - (void)verifyClientAndSendVerificationCodeToPhoneNumber:(NSString *)phoneNumber
  338. retryOnInvalidAppCredential:(BOOL)retryOnInvalidAppCredential
  339. UIDelegate:(nullable id<FIRAuthUIDelegate>)UIDelegate
  340. callback:(FIRVerificationResultCallback)callback {
  341. if (_auth.settings.isAppVerificationDisabledForTesting) {
  342. FIRSendVerificationCodeRequest *request =
  343. [[FIRSendVerificationCodeRequest alloc] initWithPhoneNumber:phoneNumber
  344. appCredential:nil
  345. reCAPTCHAToken:nil
  346. requestConfiguration:_auth.requestConfiguration];
  347. [FIRAuthBackend sendVerificationCode:request
  348. callback:^(FIRSendVerificationCodeResponse *_Nullable response,
  349. NSError *_Nullable error) {
  350. callback(response.verificationID, error);
  351. }];
  352. return;
  353. }
  354. [self
  355. verifyClientWithUIDelegate:UIDelegate
  356. completion:^(FIRAuthAppCredential *_Nullable appCredential,
  357. NSString *_Nullable reCAPTCHAToken, NSError *_Nullable error) {
  358. if (error) {
  359. callback(nil, error);
  360. return;
  361. }
  362. FIRSendVerificationCodeRequest *_Nullable request;
  363. if (appCredential) {
  364. request = [[FIRSendVerificationCodeRequest alloc]
  365. initWithPhoneNumber:phoneNumber
  366. appCredential:appCredential
  367. reCAPTCHAToken:nil
  368. requestConfiguration:self->_auth.requestConfiguration];
  369. } else if (reCAPTCHAToken) {
  370. request = [[FIRSendVerificationCodeRequest alloc]
  371. initWithPhoneNumber:phoneNumber
  372. appCredential:nil
  373. reCAPTCHAToken:reCAPTCHAToken
  374. requestConfiguration:self->_auth.requestConfiguration];
  375. }
  376. if (request) {
  377. [FIRAuthBackend
  378. sendVerificationCode:request
  379. callback:^(
  380. FIRSendVerificationCodeResponse *_Nullable response,
  381. NSError *_Nullable error) {
  382. if (error) {
  383. if (error.code ==
  384. FIRAuthErrorCodeInvalidAppCredential) {
  385. if (retryOnInvalidAppCredential) {
  386. [self->_auth.appCredentialManager clearCredential];
  387. [self
  388. verifyClientAndSendVerificationCodeToPhoneNumber:
  389. phoneNumber
  390. retryOnInvalidAppCredential:
  391. NO
  392. UIDelegate:
  393. UIDelegate
  394. callback:
  395. callback];
  396. return;
  397. }
  398. callback(
  399. nil,
  400. [FIRAuthErrorUtils
  401. unexpectedResponseWithDeserializedResponse:nil
  402. underlyingError:
  403. error]);
  404. return;
  405. }
  406. callback(nil, error);
  407. return;
  408. }
  409. callback(response.verificationID, nil);
  410. }];
  411. }
  412. }];
  413. }
  414. - (void)verifyClientAndSendVerificationCodeToPhoneNumber:(NSString *)phoneNumber
  415. retryOnInvalidAppCredential:(BOOL)retryOnInvalidAppCredential
  416. UIDelegate:(nullable id<FIRAuthUIDelegate>)UIDelegate
  417. multiFactorSession:(nullable FIRMultiFactorSession *)session
  418. callback:(FIRVerificationResultCallback)callback {
  419. if (_auth.settings.isAppVerificationDisabledForTesting) {
  420. FIRSendVerificationCodeRequest *request =
  421. [[FIRSendVerificationCodeRequest alloc] initWithPhoneNumber:phoneNumber
  422. appCredential:nil
  423. reCAPTCHAToken:nil
  424. requestConfiguration:_auth.requestConfiguration];
  425. [FIRAuthBackend sendVerificationCode:request
  426. callback:^(FIRSendVerificationCodeResponse *_Nullable response,
  427. NSError *_Nullable error) {
  428. callback(response.verificationID, error);
  429. }];
  430. return;
  431. }
  432. [self
  433. verifyClientWithUIDelegate:UIDelegate
  434. completion:^(FIRAuthAppCredential *_Nullable appCredential,
  435. NSString *_Nullable reCAPTCHAToken, NSError *_Nullable error) {
  436. if (error) {
  437. if (callback) {
  438. callback(nil, error);
  439. }
  440. return;
  441. }
  442. NSString *IDToken = session.IDToken;
  443. FIRAuthProtoStartMFAPhoneRequestInfo *startMFARequestInfo =
  444. [[FIRAuthProtoStartMFAPhoneRequestInfo alloc]
  445. initWithPhoneNumber:phoneNumber
  446. appCredential:appCredential
  447. reCAPTCHAToken:reCAPTCHAToken];
  448. if (session.IDToken) {
  449. FIRStartMFAEnrollmentRequest *request =
  450. [[FIRStartMFAEnrollmentRequest alloc]
  451. initWithIDToken:IDToken
  452. enrollmentInfo:startMFARequestInfo
  453. requestConfiguration:self->_auth.requestConfiguration];
  454. [FIRAuthBackend
  455. startMultiFactorEnrollment:request
  456. callback:^(
  457. FIRStartMFAEnrollmentResponse *_Nullable response,
  458. NSError *_Nullable error) {
  459. if (error) {
  460. if (error.code ==
  461. FIRAuthErrorCodeInvalidAppCredential) {
  462. if (retryOnInvalidAppCredential) {
  463. [self->_auth.appCredentialManager
  464. clearCredential];
  465. [self
  466. verifyClientAndSendVerificationCodeToPhoneNumber:
  467. phoneNumber
  468. retryOnInvalidAppCredential:
  469. NO
  470. UIDelegate:
  471. UIDelegate
  472. multiFactorSession:
  473. session
  474. callback:
  475. callback];
  476. return;
  477. }
  478. if (callback) {
  479. callback(
  480. nil,
  481. [FIRAuthErrorUtils
  482. unexpectedResponseWithDeserializedResponse:
  483. nil
  484. underlyingError:
  485. error]);
  486. }
  487. return;
  488. } else {
  489. if (callback) {
  490. callback(nil, error);
  491. }
  492. }
  493. } else {
  494. if (callback) {
  495. callback(
  496. response.enrollmentResponse.sessionInfo,
  497. nil);
  498. }
  499. }
  500. }];
  501. } else {
  502. FIRStartMFASignInRequest *request = [[FIRStartMFASignInRequest alloc]
  503. initWithMFAPendingCredential:session.MFAPendingCredential
  504. MFAEnrollmentID:session.multiFactorInfo.UID
  505. signInInfo:startMFARequestInfo
  506. requestConfiguration:self->_auth.requestConfiguration];
  507. [FIRAuthBackend
  508. startMultiFactorSignIn:request
  509. callback:^(FIRStartMFASignInResponse *_Nullable response,
  510. NSError *_Nullable error) {
  511. if (error) {
  512. if (error.code ==
  513. FIRAuthErrorCodeInvalidAppCredential) {
  514. if (retryOnInvalidAppCredential) {
  515. [self->_auth
  516. .appCredentialManager clearCredential];
  517. [self
  518. verifyClientAndSendVerificationCodeToPhoneNumber:
  519. phoneNumber
  520. retryOnInvalidAppCredential:
  521. NO
  522. UIDelegate:
  523. UIDelegate
  524. multiFactorSession:
  525. session
  526. callback:
  527. callback];
  528. return;
  529. }
  530. if (callback) {
  531. callback(
  532. nil,
  533. [FIRAuthErrorUtils
  534. unexpectedResponseWithDeserializedResponse:
  535. nil
  536. underlyingError:
  537. error]);
  538. }
  539. return;
  540. } else {
  541. if (callback) {
  542. callback(nil, error);
  543. }
  544. }
  545. } else {
  546. if (callback) {
  547. callback(response.responseInfo.sessionInfo, nil);
  548. }
  549. }
  550. }];
  551. }
  552. }];
  553. }
  554. /** @fn verifyClientWithCompletion:completion:
  555. @brief Continues the flow to verify the client via silent push notification.
  556. @param completion The callback to be invoked when the client verification flow is finished.
  557. */
  558. - (void)verifyClientWithUIDelegate:(nullable id<FIRAuthUIDelegate>)UIDelegate
  559. completion:(FIRVerifyClientCallback)completion {
  560. if (_auth.appCredentialManager.credential) {
  561. completion(_auth.appCredentialManager.credential, nil, nil);
  562. return;
  563. }
  564. [_auth.tokenManager getTokenWithCallback:^(FIRAuthAPNSToken *_Nullable token,
  565. NSError *_Nullable error) {
  566. if (!token) {
  567. [self reCAPTCHAFlowWithUIDelegate:UIDelegate completion:completion];
  568. return;
  569. }
  570. FIRVerifyClientRequest *request =
  571. [[FIRVerifyClientRequest alloc] initWithAppToken:token.string
  572. isSandbox:token.type == FIRAuthAPNSTokenTypeSandbox
  573. requestConfiguration:self->_auth.requestConfiguration];
  574. [FIRAuthBackend
  575. verifyClient:request
  576. callback:^(FIRVerifyClientResponse *_Nullable response, NSError *_Nullable error) {
  577. if (error) {
  578. NSError *underlyingError = error.userInfo[NSUnderlyingErrorKey];
  579. BOOL isInvalidAppCredential =
  580. error.code == FIRAuthErrorCodeInternalError &&
  581. underlyingError.code == FIRAuthErrorCodeInvalidAppCredential;
  582. if (error.code != FIRAuthErrorCodeMissingAppToken && !isInvalidAppCredential) {
  583. completion(nil, nil, error);
  584. return;
  585. } else {
  586. [self reCAPTCHAFlowWithUIDelegate:UIDelegate completion:completion];
  587. return;
  588. }
  589. }
  590. NSTimeInterval timeout = [response.suggestedTimeOutDate timeIntervalSinceNow];
  591. [self->_auth.appCredentialManager
  592. didStartVerificationWithReceipt:response.receipt
  593. timeout:timeout
  594. callback:^(FIRAuthAppCredential *credential) {
  595. if (!credential.secret) {
  596. FIRLogWarning(kFIRLoggerAuth, @"I-AUT000014",
  597. @"Failed to receive remote notification "
  598. @"to verify app identity within "
  599. @"%.0f second(s), falling back to "
  600. @"reCAPTCHA verification.",
  601. timeout);
  602. [self reCAPTCHAFlowWithUIDelegate:UIDelegate
  603. completion:completion];
  604. return;
  605. }
  606. completion(credential, nil, nil);
  607. }];
  608. }];
  609. }];
  610. }
  611. - (void)reCAPTCHAFlowWithUIDelegate:(nullable id<FIRAuthUIDelegate>)UIDelegate
  612. completion:(FIRVerifyClientCallback)completion {
  613. NSString *eventID = [FIRAuthWebUtils randomStringWithLength:10];
  614. [self
  615. reCAPTCHAURLWithEventID:eventID
  616. completion:^(NSURL *_Nullable reCAPTCHAURL, NSError *_Nullable error) {
  617. if (error) {
  618. completion(nil, nil, error);
  619. return;
  620. }
  621. FIRAuthURLCallbackMatcher callbackMatcher =
  622. ^BOOL(NSURL *_Nullable callbackURL) {
  623. return [FIRAuthWebUtils isExpectedCallbackURL:callbackURL
  624. eventID:eventID
  625. authType:kAuthTypeVerifyApp
  626. callbackScheme:self->_callbackScheme];
  627. };
  628. [self->_auth.authURLPresenter
  629. presentURL:reCAPTCHAURL
  630. UIDelegate:UIDelegate
  631. callbackMatcher:callbackMatcher
  632. completion:^(NSURL *_Nullable callbackURL, NSError *_Nullable error) {
  633. if (error) {
  634. completion(nil, nil, error);
  635. return;
  636. }
  637. NSError *reCAPTCHAError;
  638. NSString *reCAPTCHAToken =
  639. [self reCAPTCHATokenForURL:callbackURL error:&reCAPTCHAError];
  640. if (!reCAPTCHAToken) {
  641. completion(nil, nil, reCAPTCHAError);
  642. return;
  643. } else {
  644. completion(nil, reCAPTCHAToken, nil);
  645. return;
  646. }
  647. }];
  648. }];
  649. }
  650. /** @fn reCAPTCHAURLWithEventID:completion:
  651. @brief Constructs a URL used for opening a reCAPTCHA app verification flow using a given event
  652. ID.
  653. @param eventID The event ID used for this purpose.
  654. @param completion The callback invoked after the URL has been constructed or an error
  655. has been encountered.
  656. */
  657. - (void)reCAPTCHAURLWithEventID:(NSString *)eventID completion:(FIRReCAPTCHAURLCallBack)completion {
  658. [FIRAuthWebUtils
  659. fetchAuthDomainWithRequestConfiguration:_auth.requestConfiguration
  660. completion:^(NSString *_Nullable authDomain,
  661. NSError *_Nullable error) {
  662. if (error) {
  663. if (completion) {
  664. completion(nil, error);
  665. return;
  666. }
  667. }
  668. NSString *bundleID = [NSBundle mainBundle].bundleIdentifier;
  669. NSString *clientID = self->_auth.app.options.clientID;
  670. NSString *appID = self->_auth.app.options.googleAppID;
  671. NSString *apiKey = self->_auth.requestConfiguration.APIKey;
  672. NSMutableArray<NSURLQueryItem *> *queryItems = [@[
  673. [NSURLQueryItem queryItemWithName:@"apiKey" value:apiKey],
  674. [NSURLQueryItem queryItemWithName:@"authType"
  675. value:kAuthTypeVerifyApp],
  676. [NSURLQueryItem queryItemWithName:@"ibi"
  677. value:bundleID ?: @""],
  678. [NSURLQueryItem
  679. queryItemWithName:@"v"
  680. value:[FIRAuthBackend authUserAgent]],
  681. [NSURLQueryItem queryItemWithName:@"eventId" value:eventID]
  682. ] mutableCopy];
  683. if (self->_usingClientIDScheme) {
  684. [queryItems
  685. addObject:[NSURLQueryItem queryItemWithName:@"clientId"
  686. value:clientID]];
  687. } else {
  688. [queryItems
  689. addObject:[NSURLQueryItem queryItemWithName:@"appId"
  690. value:appID]];
  691. }
  692. if (self->_auth.requestConfiguration.languageCode) {
  693. [queryItems
  694. addObject:[NSURLQueryItem
  695. queryItemWithName:@"hl"
  696. value:self->_auth
  697. .requestConfiguration
  698. .languageCode]];
  699. }
  700. NSURLComponents *components = [[NSURLComponents alloc]
  701. initWithString:
  702. [NSString stringWithFormat:kReCAPTCHAURLStringFormat,
  703. authDomain]];
  704. [components setQueryItems:queryItems];
  705. if (completion) {
  706. completion([components URL], nil);
  707. }
  708. }];
  709. }
  710. @end
  711. NS_ASSUME_NONNULL_END
  712. #endif