No Description

FIRAuthBackend.m 48KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  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 "FIRAuthBackend.h"
  17. #import <GTMSessionFetcher/GTMSessionFetcher.h>
  18. #import <GTMSessionFetcher/GTMSessionFetcherService.h>
  19. #import "FIRAuthErrorUtils.h"
  20. #import "FIRAuthGlobalWorkQueue.h"
  21. #import "FirebaseAuth.h"
  22. #import "FIRAuthRPCRequest.h"
  23. #import "FIRAuthRPCResponse.h"
  24. #import "FIRCreateAuthURIRequest.h"
  25. #import "FIRCreateAuthURIResponse.h"
  26. #import "FIRDeleteAccountRequest.h"
  27. #import "FIRDeleteAccountResponse.h"
  28. #import "FIRGetAccountInfoRequest.h"
  29. #import "FIRGetAccountInfoResponse.h"
  30. #import "FIRSignInWithGameCenterRequest.h"
  31. #import "FIRSignInWithGameCenterResponse.h"
  32. #import "FIRGetOOBConfirmationCodeRequest.h"
  33. #import "FIRGetOOBConfirmationCodeResponse.h"
  34. #import "FIRGetProjectConfigRequest.h"
  35. #import "FIRGetProjectConfigResponse.h"
  36. #import "FIRResetPasswordRequest.h"
  37. #import "FIRResetPasswordResponse.h"
  38. #import "FIRSendVerificationCodeRequest.h"
  39. #import "FIRSendVerificationCodeResponse.h"
  40. #import "FIRSecureTokenRequest.h"
  41. #import "FIRSecureTokenResponse.h"
  42. #import "FIRSetAccountInfoRequest.h"
  43. #import "FIRSetAccountInfoResponse.h"
  44. #import "FIRSignUpNewUserRequest.h"
  45. #import "FIRSignUpNewUserResponse.h"
  46. #import "FIRVerifyAssertionRequest.h"
  47. #import "FIRVerifyAssertionResponse.h"
  48. #import "FIRVerifyClientRequest.h"
  49. #import "FIRVerifyClientResponse.h"
  50. #import "FIRVerifyCustomTokenRequest.h"
  51. #import "FIRVerifyCustomTokenResponse.h"
  52. #import "FIRVerifyPasswordRequest.h"
  53. #import "FIRVerifyPasswordResponse.h"
  54. #import "FIREmailLinkSignInRequest.h"
  55. #import "FIREmailLinkSignInResponse.h"
  56. #import "FIRVerifyPhoneNumberRequest.h"
  57. #import "FIRVerifyPhoneNumberResponse.h"
  58. #import "../AuthProviders/OAuth/FIROAuthCredential_Internal.h"
  59. #if TARGET_OS_IOS
  60. #import "../AuthProviders/Phone/FIRPhoneAuthCredential_Internal.h"
  61. #import "FIRPhoneAuthProvider.h"
  62. #endif
  63. NS_ASSUME_NONNULL_BEGIN
  64. /** @var kClientVersionHeader
  65. @brief HTTP header name for the client version.
  66. */
  67. static NSString *const kClientVersionHeader = @"X-Client-Version";
  68. /** @var kIosBundleIdentifierHeader
  69. @brief HTTP header name for iOS bundle ID.
  70. */
  71. static NSString *const kIosBundleIdentifierHeader = @"X-Ios-Bundle-Identifier";
  72. /** @var kFirebaseLocalHeader
  73. @brief HTTP header name for the firebase locale.
  74. */
  75. static NSString *const kFirebaseLocalHeader = @"X-Firebase-Locale";
  76. /** @var kFirebaseAuthCoreFrameworkMarker
  77. @brief The marker in the HTTP header that indicates the request comes from Firebase Auth Core.
  78. */
  79. static NSString *const kFirebaseAuthCoreFrameworkMarker = @"FirebaseCore-iOS";
  80. /** @var kJSONContentType
  81. @brief The value of the HTTP content-type header for JSON payloads.
  82. */
  83. static NSString *const kJSONContentType = @"application/json";
  84. /** @var kErrorDataKey
  85. @brief Key for error data in NSError returned by @c GTMSessionFetcher.
  86. */
  87. static NSString * const kErrorDataKey = @"data";
  88. /** @var kErrorKey
  89. @brief The key for the "error" value in JSON responses from the server.
  90. */
  91. static NSString *const kErrorKey = @"error";
  92. /** @var kErrorsKey
  93. @brief The key for the "errors" value in JSON responses from the server.
  94. */
  95. static NSString *const kErrorsKey = @"errors";
  96. /** @var kReasonKey
  97. @brief The key for the "reason" value in JSON responses from the server.
  98. */
  99. static NSString *const kReasonKey = @"reason";
  100. /** @var kInvalidKeyReasonValue
  101. @brief The value for the "reason" key indicating an invalid API Key was received by the server.
  102. */
  103. static NSString *const kInvalidKeyReasonValue = @"keyInvalid";
  104. /** @var kAppNotAuthorizedReasonValue
  105. @brief The value for the "reason" key indicating the App is not authorized to use Firebase
  106. Authentication.
  107. */
  108. static NSString *const kAppNotAuthorizedReasonValue = @"ipRefererBlocked";
  109. /** @var kErrorMessageKey
  110. @brief The key for an error's "message" value in JSON responses from the server.
  111. */
  112. static NSString *const kErrorMessageKey = @"message";
  113. /** @var kReturnIDPCredentialErrorMessageKey
  114. @brief The key for "errorMessage" value in JSON responses from the server, In case
  115. returnIDPCredential of a verifyAssertion request is set to @YES.
  116. */
  117. static NSString *const kReturnIDPCredentialErrorMessageKey = @"errorMessage";
  118. /** @var kUserNotFoundErrorMessage
  119. @brief This is the error message returned when the user is not found, which means the user
  120. account has been deleted given the token was once valid.
  121. */
  122. static NSString *const kUserNotFoundErrorMessage = @"USER_NOT_FOUND";
  123. /** @var kUserDeletedErrorMessage
  124. @brief This is the error message the server will respond with if the user entered an invalid
  125. email address.
  126. */
  127. static NSString *const kUserDeletedErrorMessage = @"EMAIL_NOT_FOUND";
  128. /** @var kInvalidLocalIDErrorMessage
  129. @brief This is the error message the server responds with if the user local id in the id token
  130. does not exit.
  131. */
  132. static NSString *const kInvalidLocalIDErrorMessage = @"INVALID_LOCAL_ID";
  133. /** @var kUserTokenExpiredErrorMessage
  134. @brief The error returned by the server if the token issue time is older than the account's
  135. valid_since time.
  136. */
  137. static NSString *const kUserTokenExpiredErrorMessage = @"TOKEN_EXPIRED";
  138. /** @var kTooManyRequestsErrorMessage
  139. @brief This is the error message the server will respond with if too many requests were made to
  140. a server method.
  141. */
  142. static NSString *const kTooManyRequestsErrorMessage = @"TOO_MANY_ATTEMPTS_TRY_LATER";
  143. /** @var kInvalidTokenCustomErrorMessage
  144. @brief This is the error message the server will respond with if there is a validation error
  145. with the custom token.
  146. */
  147. static NSString *const kInvalidCustomTokenErrorMessage = @"INVALID_CUSTOM_TOKEN";
  148. /** @var kCustomTokenMismatch
  149. @brief This is the error message the server will respond with if the service account and API key
  150. belong to different projects.
  151. */
  152. static NSString *const kCustomTokenMismatch = @"CREDENTIAL_MISMATCH";
  153. /** @var kInvalidCredentialErrorMessage
  154. @brief This is the error message the server responds with if the IDP token or requestUri is
  155. invalid.
  156. */
  157. static NSString *const kInvalidCredentialErrorMessage = @"INVALID_IDP_RESPONSE";
  158. /** @var kUserDisabledErrorMessage
  159. @brief The error returned by the server if the user account is diabled.
  160. */
  161. static NSString *const kUserDisabledErrorMessage = @"USER_DISABLED";
  162. /** @var kOperationNotAllowedErrorMessage
  163. @brief This is the error message the server will respond with if Admin disables IDP specified by
  164. provider.
  165. */
  166. static NSString *const kOperationNotAllowedErrorMessage = @"OPERATION_NOT_ALLOWED";
  167. /** @var kPasswordLoginDisabledErrorMessage
  168. @brief This is the error message the server responds with if password login is disabled.
  169. */
  170. static NSString *const kPasswordLoginDisabledErrorMessage = @"PASSWORD_LOGIN_DISABLED";
  171. /** @var kEmailAlreadyInUseErrorMessage
  172. @brief This is the error message the server responds with if the email address already exists.
  173. */
  174. static NSString *const kEmailAlreadyInUseErrorMessage = @"EMAIL_EXISTS";
  175. /** @var kInvalidEmailErrorMessage
  176. @brief The error returned by the server if the email is invalid.
  177. */
  178. static NSString *const kInvalidEmailErrorMessage = @"INVALID_EMAIL";
  179. /** @var kInvalidIdentifierErrorMessage
  180. @brief The error returned by the server if the identifier is invalid.
  181. */
  182. static NSString *const kInvalidIdentifierErrorMessage = @"INVALID_IDENTIFIER";
  183. /** @var kWrongPasswordErrorMessage
  184. @brief This is the error message the server will respond with if the user entered a wrong
  185. password.
  186. */
  187. static NSString *const kWrongPasswordErrorMessage = @"INVALID_PASSWORD";
  188. /** @var kCredentialTooOldErrorMessage
  189. @brief This is the error message the server responds with if account change is attempted 5
  190. minutes after signing in.
  191. */
  192. static NSString *const kCredentialTooOldErrorMessage = @"CREDENTIAL_TOO_OLD_LOGIN_AGAIN";
  193. /** @var kFederatedUserIDAlreadyLinkedMessage
  194. @brief This is the error message the server will respond with if the federated user ID has been
  195. already linked with another account.
  196. */
  197. static NSString *const kFederatedUserIDAlreadyLinkedMessage = @"FEDERATED_USER_ID_ALREADY_LINKED";
  198. /** @var kInvalidUserTokenErrorMessage
  199. @brief This is the error message the server responds with if user's saved auth credential is
  200. invalid, and the user needs to sign in again.
  201. */
  202. static NSString *const kInvalidUserTokenErrorMessage = @"INVALID_ID_TOKEN";
  203. /** @var kWeakPasswordErrorMessagePrefix
  204. @brief This is the prefix for the error message the server responds with if user's new password
  205. to be set is too weak.
  206. */
  207. static NSString *const kWeakPasswordErrorMessagePrefix = @"WEAK_PASSWORD";
  208. /** @var kExpiredActionCodeErrorMessage
  209. @brief This is the error message the server will respond with if the action code is expired.
  210. */
  211. static NSString *const kExpiredActionCodeErrorMessage = @"EXPIRED_OOB_CODE";
  212. /** @var kInvalidActionCodeErrorMessage
  213. @brief This is the error message the server will respond with if the action code is invalid.
  214. */
  215. static NSString *const kInvalidActionCodeErrorMessage = @"INVALID_OOB_CODE";
  216. /** @var kMissingEmailErrorMessage
  217. @brief This is the error message the server will respond with if the email address is missing
  218. during a "send password reset email" attempt.
  219. */
  220. static NSString *const kMissingEmailErrorMessage = @"MISSING_EMAIL";
  221. /** @var kInvalidSenderEmailErrorMessage
  222. @brief This is the error message the server will respond with if the sender email is invalid
  223. during a "send password reset email" attempt.
  224. */
  225. static NSString *const kInvalidSenderEmailErrorMessage = @"INVALID_SENDER";
  226. /** @var kInvalidMessagePayloadErrorMessage
  227. @brief This is the error message the server will respond with if there are invalid parameters in
  228. the payload during a "send password reset email" attempt.
  229. */
  230. static NSString *const kInvalidMessagePayloadErrorMessage = @"INVALID_MESSAGE_PAYLOAD";
  231. /** @var kInvalidRecipientEmailErrorMessage
  232. @brief This is the error message the server will respond with if the recipient email is invalid.
  233. */
  234. static NSString *const kInvalidRecipientEmailErrorMessage = @"INVALID_RECIPIENT_EMAIL";
  235. /** @var kMissingIosBundleIDErrorMessage
  236. @brief This is the error message the server will respond with if iOS bundle ID is missing but
  237. the iOS App store ID is provided.
  238. */
  239. static NSString *const kMissingIosBundleIDErrorMessage = @"MISSING_IOS_BUNDLE_ID";
  240. /** @var kMissingAndroidPackageNameErrorMessage
  241. @brief This is the error message the server will respond with if Android Package Name is missing
  242. but the flag indicating the app should be installed is set to true.
  243. */
  244. static NSString *const kMissingAndroidPackageNameErrorMessage = @"MISSING_ANDROID_PACKAGE_NAME";
  245. /** @var kUnauthorizedDomainErrorMessage
  246. @brief This is the error message the server will respond with if the domain of the continue URL
  247. specified is not whitelisted in the firebase console.
  248. */
  249. static NSString *const kUnauthorizedDomainErrorMessage = @"UNAUTHORIZED_DOMAIN";
  250. /** @var kInvalidDynamicLinkDomainErrorMessage
  251. @brief This is the error message the server will respond with if the dynamic link domain provided
  252. in the request is invalid.
  253. */
  254. static NSString *const kInvalidDynamicLinkDomainErrorMessage = @"INVALID_DYNAMIC_LINK_DOMAIN";
  255. /** @var kInvalidContinueURIErrorMessage
  256. @brief This is the error message the server will respond with if the continue URL provided in
  257. the request is invalid.
  258. */
  259. static NSString *const kInvalidContinueURIErrorMessage = @"INVALID_CONTINUE_URI";
  260. /** @var kMissingContinueURIErrorMessage
  261. @brief This is the error message the server will respond with if there was no continue URI
  262. present in a request that required one.
  263. */
  264. static NSString *const kMissingContinueURIErrorMessage = @"MISSING_CONTINUE_URI";
  265. /** @var kInvalidPhoneNumberErrorMessage
  266. @brief This is the error message the server will respond with if an incorrectly formatted phone
  267. number is provided.
  268. */
  269. static NSString *const kInvalidPhoneNumberErrorMessage = @"INVALID_PHONE_NUMBER";
  270. /** @var kInvalidVerificationCodeErrorMessage
  271. @brief This is the error message the server will respond with if an invalid verification code is
  272. provided.
  273. */
  274. static NSString *const kInvalidVerificationCodeErrorMessage = @"INVALID_CODE";
  275. /** @var kInvalidSessionInfoErrorMessage
  276. @brief This is the error message the server will respond with if an invalid session info
  277. (verification ID) is provided.
  278. */
  279. static NSString *const kInvalidSessionInfoErrorMessage = @"INVALID_SESSION_INFO";
  280. /** @var kSessionExpiredErrorMessage
  281. @brief This is the error message the server will respond with if the SMS code has expired before
  282. it is used.
  283. */
  284. static NSString *const kSessionExpiredErrorMessage = @"SESSION_EXPIRED";
  285. /** @var kMissingAppTokenErrorMessage
  286. @brief This is the error message the server will respond with if the APNS token is missing in a
  287. verifyClient request.
  288. */
  289. static NSString *const kMissingAppTokenErrorMessage = @"MISSING_IOS_APP_TOKEN";
  290. /** @var kMissingAppCredentialErrorMessage
  291. @brief This is the error message the server will respond with if the app token is missing in a
  292. sendVerificationCode request.
  293. */
  294. static NSString *const kMissingAppCredentialErrorMessage = @"MISSING_APP_CREDENTIAL";
  295. /** @var kInvalidAppCredentialErrorMessage
  296. @brief This is the error message the server will respond with if the app credential in a
  297. sendVerificationCode request is invalid.
  298. */
  299. static NSString *const kInvalidAppCredentialErrorMessage = @"INVALID_APP_CREDENTIAL";
  300. /** @var kQuoutaExceededErrorMessage
  301. @brief This is the error message the server will respond with if the quota for SMS text messages
  302. has been exceeded for the project.
  303. */
  304. static NSString *const kQuoutaExceededErrorMessage = @"QUOTA_EXCEEDED";
  305. /** @var kAppNotVerifiedErrorMessage
  306. @brief This is the error message the server will respond with if Firebase could not verify the
  307. app during a phone authentication flow.
  308. */
  309. static NSString *const kAppNotVerifiedErrorMessage = @"APP_NOT_VERIFIED";
  310. /** @var kMissingClientIdentifier
  311. @brief This is the error message the server will respond with if Firebase could not verify the
  312. app during a phone authentication flow when a real phone number is used and app verification
  313. is disabled for testing.
  314. */
  315. static NSString *const kMissingClientIdentifier = @"MISSING_CLIENT_IDENTIFIER";
  316. /** @var kCaptchaCheckFailedErrorMessage
  317. @brief This is the error message the server will respond with if the reCAPTCHA token provided is
  318. invalid.
  319. */
  320. static NSString *const kCaptchaCheckFailedErrorMessage = @"CAPTCHA_CHECK_FAILED";
  321. /** @var kInvalidPendingToken
  322. @brief Generic IDP error codes.
  323. */
  324. static NSString *const kInvalidPendingToken = @"INVALID_PENDING_TOKEN";
  325. /** @var gBackendImplementation
  326. @brief The singleton FIRAuthBackendImplementation instance to use.
  327. */
  328. static id<FIRAuthBackendImplementation> gBackendImplementation;
  329. /** @class FIRAuthBackendRPCImplementation
  330. @brief The default RPC-based backend implementation.
  331. */
  332. @interface FIRAuthBackendRPCImplementation : NSObject <FIRAuthBackendImplementation>
  333. /** @property RPCIssuer
  334. @brief An instance of FIRAuthBackendRPCIssuer for making RPC requests. Allows the RPC
  335. requests/responses to be easily faked.
  336. */
  337. @property(nonatomic, strong) id<FIRAuthBackendRPCIssuer> RPCIssuer;
  338. @end
  339. @implementation FIRAuthBackend
  340. + (id<FIRAuthBackendImplementation>)implementation {
  341. if (!gBackendImplementation) {
  342. gBackendImplementation = [[FIRAuthBackendRPCImplementation alloc] init];
  343. }
  344. return gBackendImplementation;
  345. }
  346. + (void)setBackendImplementation:(id<FIRAuthBackendImplementation>)backendImplementation {
  347. gBackendImplementation = backendImplementation;
  348. }
  349. + (void)setDefaultBackendImplementationWithRPCIssuer:
  350. (nullable id<FIRAuthBackendRPCIssuer>)RPCIssuer {
  351. FIRAuthBackendRPCImplementation *defaultImplementation =
  352. [[FIRAuthBackendRPCImplementation alloc] init];
  353. if (RPCIssuer) {
  354. defaultImplementation.RPCIssuer = RPCIssuer;
  355. }
  356. gBackendImplementation = defaultImplementation;
  357. }
  358. + (void)createAuthURI:(FIRCreateAuthURIRequest *)request
  359. callback:(FIRCreateAuthURIResponseCallback)callback {
  360. [[self implementation] createAuthURI:request callback:callback];
  361. }
  362. + (void)getAccountInfo:(FIRGetAccountInfoRequest *)request
  363. callback:(FIRGetAccountInfoResponseCallback)callback {
  364. [[self implementation] getAccountInfo:request callback:callback];
  365. }
  366. + (void)getProjectConfig:(FIRGetProjectConfigRequest *)request
  367. callback:(FIRGetProjectConfigResponseCallback)callback {
  368. [[self implementation] getProjectConfig:request callback:callback];
  369. }
  370. + (void)setAccountInfo:(FIRSetAccountInfoRequest *)request
  371. callback:(FIRSetAccountInfoResponseCallback)callback {
  372. [[self implementation] setAccountInfo:request callback:callback];
  373. }
  374. + (void)verifyAssertion:(FIRVerifyAssertionRequest *)request
  375. callback:(FIRVerifyAssertionResponseCallback)callback {
  376. [[self implementation] verifyAssertion:request callback:callback];
  377. }
  378. + (void)verifyCustomToken:(FIRVerifyCustomTokenRequest *)request
  379. callback:(FIRVerifyCustomTokenResponseCallback)callback {
  380. [[self implementation] verifyCustomToken:request callback:callback];
  381. }
  382. + (void)verifyPassword:(FIRVerifyPasswordRequest *)request
  383. callback:(FIRVerifyPasswordResponseCallback)callback {
  384. [[self implementation] verifyPassword:request callback:callback];
  385. }
  386. + (void)emailLinkSignin:(FIREmailLinkSignInRequest *)request
  387. callback:(FIREmailLinkSigninResponseCallback)callback {
  388. [[self implementation] emailLinkSignin:request callback:callback];
  389. }
  390. + (void)secureToken:(FIRSecureTokenRequest *)request
  391. callback:(FIRSecureTokenResponseCallback)callback {
  392. [[self implementation] secureToken:request callback:callback];
  393. }
  394. + (void)getOOBConfirmationCode:(FIRGetOOBConfirmationCodeRequest *)request
  395. callback:(FIRGetOOBConfirmationCodeResponseCallback)callback {
  396. [[self implementation] getOOBConfirmationCode:request callback:callback];
  397. }
  398. + (void)signUpNewUser:(FIRSignUpNewUserRequest *)request
  399. callback:(FIRSignupNewUserCallback)callback {
  400. [[self implementation] signUpNewUser:request callback:callback];
  401. }
  402. + (void)deleteAccount:(FIRDeleteAccountRequest *)request callback:(FIRDeleteCallBack)callback {
  403. [[self implementation] deleteAccount:request callback:callback];
  404. }
  405. + (void)signInWithGameCenter:(FIRSignInWithGameCenterRequest *)request
  406. callback:(FIRSignInWithGameCenterResponseCallback)callback {
  407. [[self implementation] signInWithGameCenter:request callback:callback];
  408. }
  409. #if TARGET_OS_IOS
  410. + (void)sendVerificationCode:(FIRSendVerificationCodeRequest *)request
  411. callback:(FIRSendVerificationCodeResponseCallback)callback {
  412. [[self implementation] sendVerificationCode:request callback:callback];
  413. }
  414. + (void)verifyPhoneNumber:(FIRVerifyPhoneNumberRequest *)request
  415. callback:(FIRVerifyPhoneNumberResponseCallback)callback {
  416. [[self implementation] verifyPhoneNumber:request callback:callback];
  417. }
  418. + (void)verifyClient:(id)request callback:(FIRVerifyClientResponseCallback)callback {
  419. [[self implementation] verifyClient:request callback:callback];
  420. }
  421. #endif
  422. + (void)resetPassword:(FIRResetPasswordRequest *)request
  423. callback:(FIRResetPasswordCallback)callback {
  424. [[self implementation] resetPassword:request callback:callback];
  425. }
  426. + (NSString *)authUserAgent {
  427. return [NSString stringWithFormat:@"FirebaseAuth.iOS/%s %@",
  428. FirebaseAuthVersionStr, GTMFetcherStandardUserAgentString(nil)];
  429. }
  430. @end
  431. @interface FIRAuthBackendRPCIssuerImplementation : NSObject <FIRAuthBackendRPCIssuer>
  432. @end
  433. @implementation FIRAuthBackendRPCIssuerImplementation {
  434. /** @var The session fetcher service.
  435. */
  436. GTMSessionFetcherService *_fetcherService;
  437. }
  438. - (instancetype)init {
  439. self = [super init];
  440. if (self) {
  441. _fetcherService = [[GTMSessionFetcherService alloc] init];
  442. _fetcherService.userAgent = [FIRAuthBackend authUserAgent];
  443. _fetcherService.callbackQueue = FIRAuthGlobalWorkQueue();
  444. // Avoid reusing the session to prevent
  445. // https://github.com/firebase/firebase-ios-sdk/issues/1261
  446. _fetcherService.reuseSession = NO;
  447. }
  448. return self;
  449. }
  450. - (void)asyncPostToURLWithRequestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration
  451. URL:(NSURL *)URL
  452. body:(nullable NSData *)body
  453. contentType:(NSString *)contentType
  454. completionHandler:(void (^)(NSData *_Nullable,
  455. NSError *_Nullable))handler {
  456. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
  457. [request setValue:contentType forHTTPHeaderField:@"Content-Type"];
  458. NSString *additionalFrameworkMarker = requestConfiguration.additionalFrameworkMarker ?:
  459. kFirebaseAuthCoreFrameworkMarker;
  460. NSString *clientVersion = [NSString stringWithFormat:@"iOS/FirebaseSDK/%s/%@",
  461. FirebaseAuthVersionStr,
  462. additionalFrameworkMarker];
  463. [request setValue:clientVersion forHTTPHeaderField:kClientVersionHeader];
  464. NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];
  465. [request setValue:bundleID forHTTPHeaderField:kIosBundleIdentifierHeader];
  466. NSArray<NSString *> *preferredLocalizations = [NSBundle mainBundle].preferredLocalizations;
  467. if (preferredLocalizations.count) {
  468. NSString *acceptLanguage = preferredLocalizations.firstObject;
  469. [request setValue:acceptLanguage forHTTPHeaderField:@"Accept-Language"];
  470. }
  471. NSString *languageCode = requestConfiguration.languageCode;
  472. if (languageCode.length) {
  473. [request setValue:languageCode forHTTPHeaderField:kFirebaseLocalHeader];
  474. }
  475. GTMSessionFetcher *fetcher = [_fetcherService fetcherWithRequest:request];
  476. fetcher.bodyData = body;
  477. [fetcher beginFetchWithCompletionHandler:handler];
  478. }
  479. @end
  480. @implementation FIRAuthBackendRPCImplementation
  481. - (instancetype)init {
  482. self = [super init];
  483. if (self) {
  484. _RPCIssuer = [[FIRAuthBackendRPCIssuerImplementation alloc] init];
  485. }
  486. return self;
  487. }
  488. - (void)createAuthURI:(FIRCreateAuthURIRequest *)request
  489. callback:(FIRCreateAuthURIResponseCallback)callback {
  490. FIRCreateAuthURIResponse *response = [[FIRCreateAuthURIResponse alloc] init];
  491. [self postWithRequest:request response:response callback:^(NSError *error) {
  492. if (error) {
  493. callback(nil, error);
  494. } else {
  495. callback(response, nil);
  496. }
  497. }];
  498. }
  499. - (void)getAccountInfo:(FIRGetAccountInfoRequest *)request
  500. callback:(FIRGetAccountInfoResponseCallback)callback {
  501. FIRGetAccountInfoResponse *response = [[FIRGetAccountInfoResponse alloc] init];
  502. [self postWithRequest:request response:response callback:^(NSError *error) {
  503. if (error) {
  504. callback(nil, error);
  505. } else {
  506. callback(response, nil);
  507. }
  508. }];
  509. }
  510. - (void)getProjectConfig:(FIRGetProjectConfigRequest *)request
  511. callback:(FIRGetProjectConfigResponseCallback)callback {
  512. FIRGetProjectConfigResponse *response = [[FIRGetProjectConfigResponse alloc] init];
  513. [self postWithRequest:request response:response callback:^(NSError *error) {
  514. if (error) {
  515. callback(nil, error);
  516. } else {
  517. callback(response, nil);
  518. }
  519. }];
  520. }
  521. - (void)setAccountInfo:(FIRSetAccountInfoRequest *)request
  522. callback:(FIRSetAccountInfoResponseCallback)callback {
  523. FIRSetAccountInfoResponse *response = [[FIRSetAccountInfoResponse alloc] init];
  524. [self postWithRequest:request response:response callback:^(NSError *error) {
  525. if (error) {
  526. callback(nil, error);
  527. } else {
  528. callback(response, nil);
  529. }
  530. }];
  531. }
  532. - (void)verifyAssertion:(FIRVerifyAssertionRequest *)request
  533. callback:(FIRVerifyAssertionResponseCallback)callback {
  534. FIRVerifyAssertionResponse *response = [[FIRVerifyAssertionResponse alloc] init];
  535. [self postWithRequest:request response:response callback:^(NSError *error) {
  536. if (error) {
  537. callback(nil, error);
  538. return;
  539. }
  540. callback(response, nil);
  541. }];
  542. }
  543. - (void)verifyCustomToken:(FIRVerifyCustomTokenRequest *)request
  544. callback:(FIRVerifyCustomTokenResponseCallback)callback {
  545. FIRVerifyCustomTokenResponse *response = [[FIRVerifyCustomTokenResponse alloc] init];
  546. [self postWithRequest:request response:response callback:^(NSError *error) {
  547. if (error) {
  548. callback(nil, error);
  549. } else {
  550. callback(response, nil);
  551. }
  552. }];
  553. }
  554. - (void)verifyPassword:(FIRVerifyPasswordRequest *)request
  555. callback:(FIRVerifyPasswordResponseCallback)callback {
  556. FIRVerifyPasswordResponse *response = [[FIRVerifyPasswordResponse alloc] init];
  557. [self postWithRequest:request response:response callback:^(NSError *error) {
  558. if (error) {
  559. callback(nil, error);
  560. } else {
  561. callback(response, nil);
  562. }
  563. }];
  564. }
  565. - (void)emailLinkSignin:(FIREmailLinkSignInRequest *)request
  566. callback:(FIREmailLinkSigninResponseCallback)callback {
  567. FIREmailLinkSignInResponse *response = [[FIREmailLinkSignInResponse alloc] init];
  568. [self postWithRequest:request response:response callback:^(NSError *error) {
  569. if (error) {
  570. callback(nil, error);
  571. } else {
  572. callback(response, nil);
  573. }
  574. }];
  575. }
  576. - (void)secureToken:(FIRSecureTokenRequest *)request
  577. callback:(FIRSecureTokenResponseCallback)callback {
  578. FIRSecureTokenResponse *response = [[FIRSecureTokenResponse alloc] init];
  579. [self postWithRequest:request response:response callback:^(NSError *error) {
  580. if (error) {
  581. callback(nil, error);
  582. } else {
  583. callback(response, nil);
  584. }
  585. }];
  586. }
  587. - (void)getOOBConfirmationCode:(FIRGetOOBConfirmationCodeRequest *)request
  588. callback:(FIRGetOOBConfirmationCodeResponseCallback)callback {
  589. FIRGetOOBConfirmationCodeResponse *response = [[FIRGetOOBConfirmationCodeResponse alloc] init];
  590. [self postWithRequest:request response:response callback:^(NSError *error) {
  591. if (error) {
  592. callback(nil, error);
  593. } else {
  594. callback(response, nil);
  595. }
  596. }];
  597. }
  598. - (void)signUpNewUser:(FIRSignUpNewUserRequest *)request
  599. callback:(FIRSignupNewUserCallback)callback{
  600. FIRSignUpNewUserResponse *response = [[FIRSignUpNewUserResponse alloc] init];
  601. [self postWithRequest:request response:response callback:^(NSError *error) {
  602. if (error) {
  603. callback(nil, error);
  604. } else {
  605. callback(response, nil);
  606. }
  607. }];
  608. }
  609. - (void)deleteAccount:(FIRDeleteAccountRequest *)request callback:(FIRDeleteCallBack)callback {
  610. FIRDeleteAccountResponse *response = [[FIRDeleteAccountResponse alloc] init];
  611. [self postWithRequest:request response:response callback:callback];
  612. }
  613. #if TARGET_OS_IOS
  614. - (void)sendVerificationCode:(FIRSendVerificationCodeRequest *)request
  615. callback:(FIRSendVerificationCodeResponseCallback)callback {
  616. FIRSendVerificationCodeResponse *response = [[FIRSendVerificationCodeResponse alloc] init];
  617. [self postWithRequest:request response:response callback:^(NSError *error) {
  618. if (error) {
  619. callback(nil, error);
  620. } else {
  621. callback(response, error);
  622. }
  623. }];
  624. }
  625. - (void)verifyPhoneNumber:(FIRVerifyPhoneNumberRequest *)request
  626. callback:(FIRVerifyPhoneNumberResponseCallback)callback {
  627. FIRVerifyPhoneNumberResponse *response = [[FIRVerifyPhoneNumberResponse alloc] init];
  628. [self postWithRequest:request response:response callback:^(NSError *error) {
  629. if (error) {
  630. callback(nil, error);
  631. return;
  632. }
  633. // Check whether or not the successful response is actually the special case phone auth flow
  634. // that returns a temporary proof and phone number.
  635. if (response.phoneNumber.length && response.temporaryProof.length) {
  636. FIRPhoneAuthCredential *credential =
  637. [[FIRPhoneAuthCredential alloc] initWithTemporaryProof:response.temporaryProof
  638. phoneNumber:response.phoneNumber
  639. providerID:FIRPhoneAuthProviderID];
  640. callback(nil,
  641. [FIRAuthErrorUtils credentialAlreadyInUseErrorWithMessage:nil
  642. credential:credential
  643. email:nil]);
  644. return;
  645. }
  646. callback(response, nil);
  647. }];
  648. }
  649. - (void)verifyClient:(id)request callback:(FIRVerifyClientResponseCallback)callback {
  650. FIRVerifyClientResponse *response = [[FIRVerifyClientResponse alloc] init];
  651. [self postWithRequest:request response:response callback:^(NSError *error) {
  652. if (error) {
  653. callback(nil, error);
  654. return;
  655. }
  656. callback(response, nil);
  657. }];
  658. }
  659. #endif
  660. - (void)resetPassword:(FIRResetPasswordRequest *)request
  661. callback:(FIRResetPasswordCallback)callback {
  662. FIRResetPasswordResponse *response = [[FIRResetPasswordResponse alloc] init];
  663. [self postWithRequest:request response:response callback:^(NSError *error) {
  664. if (error) {
  665. callback(nil, error);
  666. return;
  667. }
  668. callback(response, nil);
  669. }];
  670. }
  671. - (void)signInWithGameCenter:(FIRSignInWithGameCenterRequest *)request
  672. callback:(FIRSignInWithGameCenterResponseCallback)callback {
  673. FIRSignInWithGameCenterResponse *response = [[FIRSignInWithGameCenterResponse alloc] init];
  674. [self postWithRequest:request response:response callback:^(NSError *error) {
  675. if (error) {
  676. if (callback) {
  677. callback(nil, error);
  678. }
  679. } else {
  680. if (callback) {
  681. callback(response, nil);
  682. }
  683. }
  684. }];
  685. }
  686. #pragma mark - Generic RPC handling methods
  687. /** @fn postWithRequest:response:callback:
  688. @brief Calls the RPC using HTTP POST.
  689. @remarks Possible error responses:
  690. @see FIRAuthInternalErrorCodeRPCRequestEncodingError
  691. @see FIRAuthInternalErrorCodeJSONSerializationError
  692. @see FIRAuthInternalErrorCodeNetworkError
  693. @see FIRAuthInternalErrorCodeUnexpectedErrorResponse
  694. @see FIRAuthInternalErrorCodeUnexpectedResponse
  695. @see FIRAuthInternalErrorCodeRPCResponseDecodingError
  696. @param request The request.
  697. @param response The empty response to be filled.
  698. @param callback The callback for both success and failure.
  699. */
  700. - (void)postWithRequest:(id<FIRAuthRPCRequest>)request
  701. response:(id<FIRAuthRPCResponse>)response
  702. callback:(void (^)(NSError * _Nullable error))callback {
  703. NSError *error;
  704. NSData *bodyData;
  705. if ([request containsPostBody]) {
  706. id postBody = [request unencodedHTTPRequestBodyWithError:&error];
  707. if (!postBody) {
  708. callback([FIRAuthErrorUtils RPCRequestEncodingErrorWithUnderlyingError:error]);
  709. return;
  710. }
  711. NSJSONWritingOptions JSONWritingOptions = 0;
  712. #if DEBUG
  713. JSONWritingOptions |= NSJSONWritingPrettyPrinted;
  714. #endif
  715. if ([NSJSONSerialization isValidJSONObject:postBody]) {
  716. bodyData = [NSJSONSerialization dataWithJSONObject:postBody
  717. options:JSONWritingOptions
  718. error:&error];
  719. if (!bodyData) {
  720. // This is an untested case. This happens exclusively when there is an error in the framework
  721. // implementation of dataWithJSONObject:options:error:. This shouldn't normally occur as
  722. // isValidJSONObject: should return NO in any case we should encounter an error.
  723. error = [FIRAuthErrorUtils JSONSerializationErrorWithUnderlyingError:error];
  724. }
  725. } else {
  726. error = [FIRAuthErrorUtils JSONSerializationErrorForUnencodableType];
  727. }
  728. if (!bodyData) {
  729. callback(error);
  730. return;
  731. }
  732. }
  733. [_RPCIssuer asyncPostToURLWithRequestConfiguration:[request requestConfiguration]
  734. URL:[request requestURL]
  735. body:bodyData
  736. contentType:kJSONContentType
  737. completionHandler:^(NSData *data, NSError *error) {
  738. // If there is an error with no body data at all, then this must be a network error.
  739. if (error && !data) {
  740. callback([FIRAuthErrorUtils networkErrorWithUnderlyingError:error]);
  741. return;
  742. }
  743. // Try to decode the HTTP response data which may contain either a successful response or error
  744. // message.
  745. NSError *jsonError;
  746. NSDictionary * dictionary =
  747. [NSJSONSerialization JSONObjectWithData:data
  748. options:NSJSONReadingMutableLeaves
  749. error:&jsonError];
  750. if (!dictionary) {
  751. if (error) {
  752. // We have an error, but we couldn't decode the body, so we have no additional information
  753. // other than the raw response and the original NSError (the jsonError is infered by the
  754. // error code (FIRAuthErrorCodeUnexpectedHTTPResponse, and is irrelevant.)
  755. callback([FIRAuthErrorUtils unexpectedErrorResponseWithData:data underlyingError:error]);
  756. } else {
  757. // This is supposed to be a "successful" response, but we couldn't deserialize the body.
  758. callback([FIRAuthErrorUtils unexpectedResponseWithData:data underlyingError:jsonError]);
  759. }
  760. return;
  761. }
  762. if (![dictionary isKindOfClass:[NSDictionary class]]) {
  763. if (error) {
  764. callback([FIRAuthErrorUtils unexpectedErrorResponseWithDeserializedResponse:dictionary]);
  765. } else {
  766. callback([FIRAuthErrorUtils unexpectedResponseWithDeserializedResponse:dictionary]);
  767. }
  768. return;
  769. }
  770. // At this point we either have an error with successfully decoded details in the body, or we
  771. // have a response which must pass further validation before we know it's truly successful.
  772. // We deal with the case where we have an error with successfully decoded error details first:
  773. if (error) {
  774. NSDictionary *errorDictionary = dictionary[kErrorKey];
  775. if ([errorDictionary isKindOfClass:[NSDictionary class]]) {
  776. id<NSObject> errorMessage = errorDictionary[kErrorMessageKey];
  777. if ([errorMessage isKindOfClass:[NSString class]]) {
  778. NSString *errorMessageString = (NSString *)errorMessage;
  779. // Contruct client error.
  780. NSError *clientError = [[self class] clientErrorWithServerErrorMessage:errorMessageString
  781. errorDictionary:errorDictionary
  782. response:response];
  783. if (clientError) {
  784. callback(clientError);
  785. return;
  786. }
  787. }
  788. // Not a message we know, return the message directly.
  789. if (errorMessage) {
  790. NSError *unexpecterErrorResponse =
  791. [FIRAuthErrorUtils unexpectedErrorResponseWithDeserializedResponse:errorDictionary];
  792. callback(unexpecterErrorResponse);
  793. return;
  794. }
  795. }
  796. // No error message at all, return the decoded response.
  797. callback([FIRAuthErrorUtils unexpectedErrorResponseWithDeserializedResponse:dictionary]);
  798. return;
  799. }
  800. // Finally, we try to populate the response object with the JSON values.
  801. if (![response setWithDictionary:dictionary error:&error]) {
  802. callback([FIRAuthErrorUtils RPCResponseDecodingErrorWithDeserializedResponse:dictionary
  803. underlyingError:error]);
  804. return;
  805. }
  806. // In case returnIDPCredential of a verifyAssertion request is set to @YES, the server may
  807. // return a 200 with a response that may contain a server error.
  808. if ([request isKindOfClass:[FIRVerifyAssertionRequest class]]) {
  809. FIRVerifyAssertionRequest *verifyAssertionRequest = (FIRVerifyAssertionRequest *)request;
  810. if (verifyAssertionRequest.returnIDPCredential) {
  811. NSString *errorMessage = dictionary[kReturnIDPCredentialErrorMessageKey];
  812. if ([errorMessage isKindOfClass:[NSString class]]) {
  813. NSString *errorString = (NSString *)errorMessage;
  814. NSError *clientError = [[self class] clientErrorWithServerErrorMessage:errorString
  815. errorDictionary:@{}
  816. response:response];
  817. if (clientError) {
  818. callback(clientError);
  819. return;
  820. }
  821. }
  822. }
  823. }
  824. // Success! The response object originally passed in can be used by the caller.
  825. callback(nil);
  826. }];
  827. }
  828. /** @fn clientErrorWithServerErrorMessage:errorDictionary:
  829. @brief Translates known server errors to client errors.
  830. @param serverErrorMessage The error message from the server.
  831. @param errorDictionary The error part of the response from the server.
  832. @param response The response from the server RPC.
  833. @return A client error, if any.
  834. */
  835. + (nullable NSError *)clientErrorWithServerErrorMessage:(NSString *)serverErrorMessage
  836. errorDictionary:(NSDictionary *)errorDictionary
  837. response:(id<FIRAuthRPCResponse>)response {
  838. NSString *shortErrorMessage = serverErrorMessage;
  839. NSString *serverDetailErrorMessage;
  840. NSRange colonRange = [serverErrorMessage rangeOfString:@":"];
  841. if (colonRange.location != NSNotFound) {
  842. shortErrorMessage = [serverErrorMessage substringToIndex:colonRange.location];
  843. shortErrorMessage =
  844. [shortErrorMessage stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
  845. serverDetailErrorMessage = [serverErrorMessage substringFromIndex:colonRange.location + 1];
  846. serverDetailErrorMessage = [serverDetailErrorMessage stringByTrimmingCharactersInSet:
  847. [NSCharacterSet whitespaceCharacterSet]];
  848. }
  849. // Delegate the responsibility for constructing the client error to the response object,
  850. // if possible.
  851. SEL clientErrorWithServerErrorMessageSelector =
  852. @selector(clientErrorWithShortErrorMessage:detailErrorMessage:);
  853. if ([response respondsToSelector:clientErrorWithServerErrorMessageSelector]) {
  854. NSError *error = [response clientErrorWithShortErrorMessage:shortErrorMessage
  855. detailErrorMessage:serverDetailErrorMessage];
  856. if (error) {
  857. return error;
  858. }
  859. }
  860. if ([shortErrorMessage isEqualToString:kUserNotFoundErrorMessage]) {
  861. return [FIRAuthErrorUtils userNotFoundErrorWithMessage:serverDetailErrorMessage];
  862. }
  863. if ([shortErrorMessage isEqualToString:kUserDeletedErrorMessage]) {
  864. return [FIRAuthErrorUtils userNotFoundErrorWithMessage:serverDetailErrorMessage];
  865. }
  866. if ([shortErrorMessage isEqualToString:kInvalidLocalIDErrorMessage]) {
  867. // This case shouldn't be necessary but it is for now: b/27908364 .
  868. return [FIRAuthErrorUtils userNotFoundErrorWithMessage:serverDetailErrorMessage];
  869. }
  870. if ([shortErrorMessage isEqualToString:kUserTokenExpiredErrorMessage]) {
  871. return [FIRAuthErrorUtils userTokenExpiredErrorWithMessage:serverDetailErrorMessage];
  872. }
  873. if ([shortErrorMessage isEqualToString:kTooManyRequestsErrorMessage]) {
  874. return [FIRAuthErrorUtils tooManyRequestsErrorWithMessage:serverDetailErrorMessage];
  875. }
  876. if ([shortErrorMessage isEqualToString:kInvalidCustomTokenErrorMessage]) {
  877. return [FIRAuthErrorUtils invalidCustomTokenErrorWithMessage:serverDetailErrorMessage];
  878. }
  879. if ([shortErrorMessage isEqualToString:kCustomTokenMismatch]) {
  880. return [FIRAuthErrorUtils customTokenMistmatchErrorWithMessage:serverDetailErrorMessage];
  881. }
  882. if ([shortErrorMessage isEqualToString:kInvalidCredentialErrorMessage] ||
  883. [shortErrorMessage isEqualToString:kInvalidPendingToken]) {
  884. return [FIRAuthErrorUtils invalidCredentialErrorWithMessage:serverDetailErrorMessage];
  885. }
  886. if ([shortErrorMessage isEqualToString:kUserDisabledErrorMessage]) {
  887. return [FIRAuthErrorUtils userDisabledErrorWithMessage:serverDetailErrorMessage];
  888. }
  889. if ([shortErrorMessage isEqualToString:kOperationNotAllowedErrorMessage]) {
  890. return [FIRAuthErrorUtils operationNotAllowedErrorWithMessage:serverDetailErrorMessage];
  891. }
  892. if ([shortErrorMessage isEqualToString:kPasswordLoginDisabledErrorMessage]) {
  893. return [FIRAuthErrorUtils operationNotAllowedErrorWithMessage:serverDetailErrorMessage];
  894. }
  895. if ([shortErrorMessage isEqualToString:kEmailAlreadyInUseErrorMessage]) {
  896. return [FIRAuthErrorUtils emailAlreadyInUseErrorWithEmail:nil];
  897. }
  898. if ([shortErrorMessage isEqualToString:kInvalidEmailErrorMessage]) {
  899. return [FIRAuthErrorUtils invalidEmailErrorWithMessage:serverDetailErrorMessage];
  900. }
  901. // "INVALID_IDENTIFIER" can be returned by createAuthURI RPC. Considering email addresses are
  902. // currently the only identifiers, we surface the FIRAuthErrorCodeInvalidEmail error code in this
  903. // case.
  904. if ([shortErrorMessage isEqualToString:kInvalidIdentifierErrorMessage]) {
  905. return [FIRAuthErrorUtils invalidEmailErrorWithMessage:serverDetailErrorMessage];
  906. }
  907. if ([shortErrorMessage isEqualToString:kWrongPasswordErrorMessage]) {
  908. return [FIRAuthErrorUtils wrongPasswordErrorWithMessage:serverDetailErrorMessage];
  909. }
  910. if ([shortErrorMessage isEqualToString:kCredentialTooOldErrorMessage]) {
  911. return [FIRAuthErrorUtils requiresRecentLoginErrorWithMessage:serverDetailErrorMessage];
  912. }
  913. if ([shortErrorMessage isEqualToString:kInvalidUserTokenErrorMessage]) {
  914. return [FIRAuthErrorUtils invalidUserTokenErrorWithMessage:serverDetailErrorMessage];
  915. }
  916. if ([shortErrorMessage isEqualToString:kFederatedUserIDAlreadyLinkedMessage]) {
  917. FIROAuthCredential *credential;
  918. NSString *email;
  919. if ([response isKindOfClass:[FIRVerifyAssertionResponse class]]) {
  920. FIRVerifyAssertionResponse *verifyAssertion = (FIRVerifyAssertionResponse *)response;
  921. if (verifyAssertion.oauthIDToken.length || verifyAssertion.oauthAccessToken.length) {
  922. credential =
  923. [[FIROAuthCredential alloc] initWithProviderID:verifyAssertion.providerID
  924. IDToken:verifyAssertion.oauthIDToken
  925. accessToken:verifyAssertion.oauthAccessToken
  926. pendingToken:verifyAssertion.pendingToken];
  927. }
  928. email = verifyAssertion.email;
  929. }
  930. return [FIRAuthErrorUtils credentialAlreadyInUseErrorWithMessage:serverDetailErrorMessage
  931. credential:credential
  932. email:email];
  933. }
  934. if ([shortErrorMessage isEqualToString:kWeakPasswordErrorMessagePrefix]) {
  935. return [FIRAuthErrorUtils weakPasswordErrorWithServerResponseReason:serverDetailErrorMessage];
  936. }
  937. if ([shortErrorMessage isEqualToString:kExpiredActionCodeErrorMessage]) {
  938. return [FIRAuthErrorUtils expiredActionCodeErrorWithMessage:serverDetailErrorMessage];
  939. }
  940. if ([shortErrorMessage isEqualToString:kInvalidActionCodeErrorMessage]) {
  941. return [FIRAuthErrorUtils invalidActionCodeErrorWithMessage:serverDetailErrorMessage];
  942. }
  943. if ([shortErrorMessage isEqualToString:kMissingEmailErrorMessage]) {
  944. return [FIRAuthErrorUtils missingEmailErrorWithMessage:serverDetailErrorMessage];
  945. }
  946. if ([shortErrorMessage isEqualToString:kInvalidSenderEmailErrorMessage]) {
  947. return [FIRAuthErrorUtils invalidSenderErrorWithMessage:serverDetailErrorMessage];
  948. }
  949. if ([shortErrorMessage isEqualToString:kInvalidMessagePayloadErrorMessage]) {
  950. return [FIRAuthErrorUtils invalidMessagePayloadErrorWithMessage:serverDetailErrorMessage];
  951. }
  952. if ([shortErrorMessage isEqualToString:kInvalidRecipientEmailErrorMessage]) {
  953. return [FIRAuthErrorUtils invalidRecipientEmailErrorWithMessage:serverDetailErrorMessage];
  954. }
  955. if ([shortErrorMessage isEqualToString:kMissingIosBundleIDErrorMessage]) {
  956. return [FIRAuthErrorUtils missingIosBundleIDErrorWithMessage:serverDetailErrorMessage];
  957. }
  958. if ([shortErrorMessage isEqualToString:kMissingAndroidPackageNameErrorMessage]) {
  959. return [FIRAuthErrorUtils missingAndroidPackageNameErrorWithMessage:serverDetailErrorMessage];
  960. }
  961. if ([shortErrorMessage isEqualToString:kUnauthorizedDomainErrorMessage]) {
  962. return [FIRAuthErrorUtils unauthorizedDomainErrorWithMessage:serverDetailErrorMessage];
  963. }
  964. if ([shortErrorMessage isEqualToString:kInvalidContinueURIErrorMessage]) {
  965. return [FIRAuthErrorUtils invalidContinueURIErrorWithMessage:serverDetailErrorMessage];
  966. }
  967. if ([shortErrorMessage isEqualToString:kInvalidDynamicLinkDomainErrorMessage]) {
  968. return [FIRAuthErrorUtils invalidDynamicLinkDomainErrorWithMessage:serverDetailErrorMessage];
  969. }
  970. if ([shortErrorMessage isEqualToString:kMissingContinueURIErrorMessage]) {
  971. return [FIRAuthErrorUtils missingContinueURIErrorWithMessage:serverDetailErrorMessage];
  972. }
  973. if ([shortErrorMessage isEqualToString:kInvalidPhoneNumberErrorMessage]) {
  974. return [FIRAuthErrorUtils invalidPhoneNumberErrorWithMessage:serverDetailErrorMessage];
  975. }
  976. if ([shortErrorMessage isEqualToString:kInvalidSessionInfoErrorMessage]) {
  977. return [FIRAuthErrorUtils invalidVerificationIDErrorWithMessage:serverDetailErrorMessage];
  978. }
  979. if ([shortErrorMessage isEqualToString:kInvalidVerificationCodeErrorMessage]) {
  980. return [FIRAuthErrorUtils invalidVerificationCodeErrorWithMessage:serverDetailErrorMessage];
  981. }
  982. if ([shortErrorMessage isEqualToString:kSessionExpiredErrorMessage]) {
  983. return [FIRAuthErrorUtils sessionExpiredErrorWithMessage:serverDetailErrorMessage];
  984. }
  985. if ([shortErrorMessage isEqualToString:kMissingAppTokenErrorMessage]) {
  986. return [FIRAuthErrorUtils missingAppTokenErrorWithUnderlyingError:nil];
  987. }
  988. if ([shortErrorMessage isEqualToString:kMissingAppCredentialErrorMessage]) {
  989. return [FIRAuthErrorUtils missingAppCredentialWithMessage:serverDetailErrorMessage];
  990. }
  991. if ([shortErrorMessage isEqualToString:kInvalidAppCredentialErrorMessage]) {
  992. return [FIRAuthErrorUtils invalidAppCredentialWithMessage:serverDetailErrorMessage];
  993. }
  994. if ([shortErrorMessage isEqualToString:kQuoutaExceededErrorMessage]) {
  995. return [FIRAuthErrorUtils quotaExceededErrorWithMessage:serverErrorMessage];
  996. }
  997. if ([shortErrorMessage isEqualToString:kAppNotVerifiedErrorMessage]) {
  998. return [FIRAuthErrorUtils appNotVerifiedErrorWithMessage:serverErrorMessage];
  999. }
  1000. if ([shortErrorMessage isEqualToString:kMissingClientIdentifier]) {
  1001. return [FIRAuthErrorUtils appNotVerifiedErrorWithMessage:@"Missing app verification via"
  1002. " reCAPTCHA or APNS token. Please verify that appVerificationDisabledForTesting is not"
  1003. " enabled when testing with a phone number that is not marked as a test Phone number in the"
  1004. " app console."];
  1005. }
  1006. if ([shortErrorMessage isEqualToString:kCaptchaCheckFailedErrorMessage]) {
  1007. return [FIRAuthErrorUtils captchaCheckFailedErrorWithMessage:serverErrorMessage];
  1008. }
  1009. // In this case we handle an error that might be specified in the underlying errors dictionary,
  1010. // the error message in determined based on the @c reason key in the dictionary.
  1011. if (errorDictionary[kErrorsKey]) {
  1012. // Check for underlying error with reason = keyInvalid;
  1013. id underlyingErrors = errorDictionary[kErrorsKey];
  1014. if ([underlyingErrors isKindOfClass:[NSArray class]]) {
  1015. NSArray *underlyingErrorsArray = (NSArray *)underlyingErrors;
  1016. for (id underlyingError in underlyingErrorsArray) {
  1017. if ([underlyingError isKindOfClass:[NSDictionary class]]) {
  1018. NSDictionary *underlyingErrorDictionary = (NSDictionary *)underlyingError;
  1019. NSString *reason = underlyingErrorDictionary[kReasonKey];
  1020. if ([reason hasPrefix:kInvalidKeyReasonValue]) {
  1021. return [FIRAuthErrorUtils invalidAPIKeyError];
  1022. }
  1023. if ([reason isEqualToString:kAppNotAuthorizedReasonValue]) {
  1024. return [FIRAuthErrorUtils appNotAuthorizedError];
  1025. }
  1026. }
  1027. }
  1028. }
  1029. }
  1030. return nil;
  1031. }
  1032. @end
  1033. NS_ASSUME_NONNULL_END