FIRAuthBackend.m 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  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 "FIROAuthCredential_Internal.h"
  59. #if TARGET_OS_IOS
  60. #import "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 kInvalidProviderIDErrorMessage
  251. @brief This is the error message the server will respond with if the provider id given for the
  252. web operation is invalid.
  253. */
  254. static NSString *const kInvalidProviderIDErrorMessage = @"INVALID_PROVIDER_ID";
  255. /** @var kInvalidDynamicLinkDomainErrorMessage
  256. @brief This is the error message the server will respond with if the dynamic link domain
  257. provided in the request is invalid.
  258. */
  259. static NSString *const kInvalidDynamicLinkDomainErrorMessage = @"INVALID_DYNAMIC_LINK_DOMAIN";
  260. /** @var kInvalidContinueURIErrorMessage
  261. @brief This is the error message the server will respond with if the continue URL provided in
  262. the request is invalid.
  263. */
  264. static NSString *const kInvalidContinueURIErrorMessage = @"INVALID_CONTINUE_URI";
  265. /** @var kMissingContinueURIErrorMessage
  266. @brief This is the error message the server will respond with if there was no continue URI
  267. present in a request that required one.
  268. */
  269. static NSString *const kMissingContinueURIErrorMessage = @"MISSING_CONTINUE_URI";
  270. /** @var kInvalidPhoneNumberErrorMessage
  271. @brief This is the error message the server will respond with if an incorrectly formatted phone
  272. number is provided.
  273. */
  274. static NSString *const kInvalidPhoneNumberErrorMessage = @"INVALID_PHONE_NUMBER";
  275. /** @var kInvalidVerificationCodeErrorMessage
  276. @brief This is the error message the server will respond with if an invalid verification code is
  277. provided.
  278. */
  279. static NSString *const kInvalidVerificationCodeErrorMessage = @"INVALID_CODE";
  280. /** @var kInvalidSessionInfoErrorMessage
  281. @brief This is the error message the server will respond with if an invalid session info
  282. (verification ID) is provided.
  283. */
  284. static NSString *const kInvalidSessionInfoErrorMessage = @"INVALID_SESSION_INFO";
  285. /** @var kSessionExpiredErrorMessage
  286. @brief This is the error message the server will respond with if the SMS code has expired before
  287. it is used.
  288. */
  289. static NSString *const kSessionExpiredErrorMessage = @"SESSION_EXPIRED";
  290. /** @var kMissingAppTokenErrorMessage
  291. @brief This is the error message the server will respond with if the APNS token is missing in a
  292. verifyClient request.
  293. */
  294. static NSString *const kMissingAppTokenErrorMessage = @"MISSING_IOS_APP_TOKEN";
  295. /** @var kMissingAppCredentialErrorMessage
  296. @brief This is the error message the server will respond with if the app token is missing in a
  297. sendVerificationCode request.
  298. */
  299. static NSString *const kMissingAppCredentialErrorMessage = @"MISSING_APP_CREDENTIAL";
  300. /** @var kInvalidAppCredentialErrorMessage
  301. @brief This is the error message the server will respond with if the app credential in a
  302. sendVerificationCode request is invalid.
  303. */
  304. static NSString *const kInvalidAppCredentialErrorMessage = @"INVALID_APP_CREDENTIAL";
  305. /** @var kQuoutaExceededErrorMessage
  306. @brief This is the error message the server will respond with if the quota for SMS text messages
  307. has been exceeded for the project.
  308. */
  309. static NSString *const kQuoutaExceededErrorMessage = @"QUOTA_EXCEEDED";
  310. /** @var kAppNotVerifiedErrorMessage
  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.
  313. */
  314. static NSString *const kAppNotVerifiedErrorMessage = @"APP_NOT_VERIFIED";
  315. /** @var kMissingClientIdentifier
  316. @brief This is the error message the server will respond with if Firebase could not verify the
  317. app during a phone authentication flow when a real phone number is used and app verification
  318. is disabled for testing.
  319. */
  320. static NSString *const kMissingClientIdentifier = @"MISSING_CLIENT_IDENTIFIER";
  321. /** @var kCaptchaCheckFailedErrorMessage
  322. @brief This is the error message the server will respond with if the reCAPTCHA token provided is
  323. invalid.
  324. */
  325. static NSString *const kCaptchaCheckFailedErrorMessage = @"CAPTCHA_CHECK_FAILED";
  326. /** @var kInvalidPendingToken
  327. @brief Generic IDP error codes.
  328. */
  329. static NSString *const kInvalidPendingToken = @"INVALID_PENDING_TOKEN";
  330. /** @var gBackendImplementation
  331. @brief The singleton FIRAuthBackendImplementation instance to use.
  332. */
  333. static id<FIRAuthBackendImplementation> gBackendImplementation;
  334. /** @class FIRAuthBackendRPCImplementation
  335. @brief The default RPC-based backend implementation.
  336. */
  337. @interface FIRAuthBackendRPCImplementation : NSObject <FIRAuthBackendImplementation>
  338. /** @property RPCIssuer
  339. @brief An instance of FIRAuthBackendRPCIssuer for making RPC requests. Allows the RPC
  340. requests/responses to be easily faked.
  341. */
  342. @property(nonatomic, strong) id<FIRAuthBackendRPCIssuer> RPCIssuer;
  343. @end
  344. @implementation FIRAuthBackend
  345. + (id<FIRAuthBackendImplementation>)implementation {
  346. if (!gBackendImplementation) {
  347. gBackendImplementation = [[FIRAuthBackendRPCImplementation alloc] init];
  348. }
  349. return gBackendImplementation;
  350. }
  351. + (void)setBackendImplementation:(id<FIRAuthBackendImplementation>)backendImplementation {
  352. gBackendImplementation = backendImplementation;
  353. }
  354. + (void)setDefaultBackendImplementationWithRPCIssuer:
  355. (nullable id<FIRAuthBackendRPCIssuer>)RPCIssuer {
  356. FIRAuthBackendRPCImplementation *defaultImplementation =
  357. [[FIRAuthBackendRPCImplementation alloc] init];
  358. if (RPCIssuer) {
  359. defaultImplementation.RPCIssuer = RPCIssuer;
  360. }
  361. gBackendImplementation = defaultImplementation;
  362. }
  363. + (void)createAuthURI:(FIRCreateAuthURIRequest *)request
  364. callback:(FIRCreateAuthURIResponseCallback)callback {
  365. [[self implementation] createAuthURI:request callback:callback];
  366. }
  367. + (void)getAccountInfo:(FIRGetAccountInfoRequest *)request
  368. callback:(FIRGetAccountInfoResponseCallback)callback {
  369. [[self implementation] getAccountInfo:request callback:callback];
  370. }
  371. + (void)getProjectConfig:(FIRGetProjectConfigRequest *)request
  372. callback:(FIRGetProjectConfigResponseCallback)callback {
  373. [[self implementation] getProjectConfig:request callback:callback];
  374. }
  375. + (void)setAccountInfo:(FIRSetAccountInfoRequest *)request
  376. callback:(FIRSetAccountInfoResponseCallback)callback {
  377. [[self implementation] setAccountInfo:request callback:callback];
  378. }
  379. + (void)verifyAssertion:(FIRVerifyAssertionRequest *)request
  380. callback:(FIRVerifyAssertionResponseCallback)callback {
  381. [[self implementation] verifyAssertion:request callback:callback];
  382. }
  383. + (void)verifyCustomToken:(FIRVerifyCustomTokenRequest *)request
  384. callback:(FIRVerifyCustomTokenResponseCallback)callback {
  385. [[self implementation] verifyCustomToken:request callback:callback];
  386. }
  387. + (void)verifyPassword:(FIRVerifyPasswordRequest *)request
  388. callback:(FIRVerifyPasswordResponseCallback)callback {
  389. [[self implementation] verifyPassword:request callback:callback];
  390. }
  391. + (void)emailLinkSignin:(FIREmailLinkSignInRequest *)request
  392. callback:(FIREmailLinkSigninResponseCallback)callback {
  393. [[self implementation] emailLinkSignin:request callback:callback];
  394. }
  395. + (void)secureToken:(FIRSecureTokenRequest *)request
  396. callback:(FIRSecureTokenResponseCallback)callback {
  397. [[self implementation] secureToken:request callback:callback];
  398. }
  399. + (void)getOOBConfirmationCode:(FIRGetOOBConfirmationCodeRequest *)request
  400. callback:(FIRGetOOBConfirmationCodeResponseCallback)callback {
  401. [[self implementation] getOOBConfirmationCode:request callback:callback];
  402. }
  403. + (void)signUpNewUser:(FIRSignUpNewUserRequest *)request
  404. callback:(FIRSignupNewUserCallback)callback {
  405. [[self implementation] signUpNewUser:request callback:callback];
  406. }
  407. + (void)deleteAccount:(FIRDeleteAccountRequest *)request callback:(FIRDeleteCallBack)callback {
  408. [[self implementation] deleteAccount:request callback:callback];
  409. }
  410. + (void)signInWithGameCenter:(FIRSignInWithGameCenterRequest *)request
  411. callback:(FIRSignInWithGameCenterResponseCallback)callback {
  412. [[self implementation] signInWithGameCenter:request callback:callback];
  413. }
  414. #if TARGET_OS_IOS
  415. + (void)sendVerificationCode:(FIRSendVerificationCodeRequest *)request
  416. callback:(FIRSendVerificationCodeResponseCallback)callback {
  417. [[self implementation] sendVerificationCode:request callback:callback];
  418. }
  419. + (void)verifyPhoneNumber:(FIRVerifyPhoneNumberRequest *)request
  420. callback:(FIRVerifyPhoneNumberResponseCallback)callback {
  421. [[self implementation] verifyPhoneNumber:request callback:callback];
  422. }
  423. + (void)verifyClient:(id)request callback:(FIRVerifyClientResponseCallback)callback {
  424. [[self implementation] verifyClient:request callback:callback];
  425. }
  426. #endif
  427. + (void)resetPassword:(FIRResetPasswordRequest *)request
  428. callback:(FIRResetPasswordCallback)callback {
  429. [[self implementation] resetPassword:request callback:callback];
  430. }
  431. + (NSString *)authUserAgent {
  432. return [NSString stringWithFormat:@"FirebaseAuth.iOS/%s %@",
  433. FirebaseAuthVersionStr, GTMFetcherStandardUserAgentString(nil)];
  434. }
  435. @end
  436. @interface FIRAuthBackendRPCIssuerImplementation : NSObject <FIRAuthBackendRPCIssuer>
  437. @end
  438. @implementation FIRAuthBackendRPCIssuerImplementation {
  439. /** @var The session fetcher service.
  440. */
  441. GTMSessionFetcherService *_fetcherService;
  442. }
  443. - (instancetype)init {
  444. self = [super init];
  445. if (self) {
  446. _fetcherService = [[GTMSessionFetcherService alloc] init];
  447. _fetcherService.userAgent = [FIRAuthBackend authUserAgent];
  448. _fetcherService.callbackQueue = FIRAuthGlobalWorkQueue();
  449. // Avoid reusing the session to prevent
  450. // https://github.com/firebase/firebase-ios-sdk/issues/1261
  451. _fetcherService.reuseSession = NO;
  452. }
  453. return self;
  454. }
  455. - (void)asyncPostToURLWithRequestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration
  456. URL:(NSURL *)URL
  457. body:(nullable NSData *)body
  458. contentType:(NSString *)contentType
  459. completionHandler:(void (^)(NSData *_Nullable,
  460. NSError *_Nullable))handler {
  461. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
  462. [request setValue:contentType forHTTPHeaderField:@"Content-Type"];
  463. NSString *additionalFrameworkMarker = requestConfiguration.additionalFrameworkMarker ?:
  464. kFirebaseAuthCoreFrameworkMarker;
  465. NSString *clientVersion = [NSString stringWithFormat:@"iOS/FirebaseSDK/%s/%@",
  466. FirebaseAuthVersionStr,
  467. additionalFrameworkMarker];
  468. [request setValue:clientVersion forHTTPHeaderField:kClientVersionHeader];
  469. NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];
  470. [request setValue:bundleID forHTTPHeaderField:kIosBundleIdentifierHeader];
  471. NSArray<NSString *> *preferredLocalizations = [NSBundle mainBundle].preferredLocalizations;
  472. if (preferredLocalizations.count) {
  473. NSString *acceptLanguage = preferredLocalizations.firstObject;
  474. [request setValue:acceptLanguage forHTTPHeaderField:@"Accept-Language"];
  475. }
  476. NSString *languageCode = requestConfiguration.languageCode;
  477. if (languageCode.length) {
  478. [request setValue:languageCode forHTTPHeaderField:kFirebaseLocalHeader];
  479. }
  480. GTMSessionFetcher *fetcher = [_fetcherService fetcherWithRequest:request];
  481. fetcher.bodyData = body;
  482. [fetcher beginFetchWithCompletionHandler:handler];
  483. }
  484. @end
  485. @implementation FIRAuthBackendRPCImplementation
  486. - (instancetype)init {
  487. self = [super init];
  488. if (self) {
  489. _RPCIssuer = [[FIRAuthBackendRPCIssuerImplementation alloc] init];
  490. }
  491. return self;
  492. }
  493. - (void)createAuthURI:(FIRCreateAuthURIRequest *)request
  494. callback:(FIRCreateAuthURIResponseCallback)callback {
  495. FIRCreateAuthURIResponse *response = [[FIRCreateAuthURIResponse alloc] init];
  496. [self postWithRequest:request response:response callback:^(NSError *error) {
  497. if (error) {
  498. callback(nil, error);
  499. } else {
  500. callback(response, nil);
  501. }
  502. }];
  503. }
  504. - (void)getAccountInfo:(FIRGetAccountInfoRequest *)request
  505. callback:(FIRGetAccountInfoResponseCallback)callback {
  506. FIRGetAccountInfoResponse *response = [[FIRGetAccountInfoResponse alloc] init];
  507. [self postWithRequest:request response:response callback:^(NSError *error) {
  508. if (error) {
  509. callback(nil, error);
  510. } else {
  511. callback(response, nil);
  512. }
  513. }];
  514. }
  515. - (void)getProjectConfig:(FIRGetProjectConfigRequest *)request
  516. callback:(FIRGetProjectConfigResponseCallback)callback {
  517. FIRGetProjectConfigResponse *response = [[FIRGetProjectConfigResponse alloc] init];
  518. [self postWithRequest:request response:response callback:^(NSError *error) {
  519. if (error) {
  520. callback(nil, error);
  521. } else {
  522. callback(response, nil);
  523. }
  524. }];
  525. }
  526. - (void)setAccountInfo:(FIRSetAccountInfoRequest *)request
  527. callback:(FIRSetAccountInfoResponseCallback)callback {
  528. FIRSetAccountInfoResponse *response = [[FIRSetAccountInfoResponse alloc] init];
  529. [self postWithRequest:request response:response callback:^(NSError *error) {
  530. if (error) {
  531. callback(nil, error);
  532. } else {
  533. callback(response, nil);
  534. }
  535. }];
  536. }
  537. - (void)verifyAssertion:(FIRVerifyAssertionRequest *)request
  538. callback:(FIRVerifyAssertionResponseCallback)callback {
  539. FIRVerifyAssertionResponse *response = [[FIRVerifyAssertionResponse alloc] init];
  540. [self postWithRequest:request response:response callback:^(NSError *error) {
  541. if (error) {
  542. callback(nil, error);
  543. return;
  544. }
  545. callback(response, nil);
  546. }];
  547. }
  548. - (void)verifyCustomToken:(FIRVerifyCustomTokenRequest *)request
  549. callback:(FIRVerifyCustomTokenResponseCallback)callback {
  550. FIRVerifyCustomTokenResponse *response = [[FIRVerifyCustomTokenResponse alloc] init];
  551. [self postWithRequest:request response:response callback:^(NSError *error) {
  552. if (error) {
  553. callback(nil, error);
  554. } else {
  555. callback(response, nil);
  556. }
  557. }];
  558. }
  559. - (void)verifyPassword:(FIRVerifyPasswordRequest *)request
  560. callback:(FIRVerifyPasswordResponseCallback)callback {
  561. FIRVerifyPasswordResponse *response = [[FIRVerifyPasswordResponse alloc] init];
  562. [self postWithRequest:request response:response callback:^(NSError *error) {
  563. if (error) {
  564. callback(nil, error);
  565. } else {
  566. callback(response, nil);
  567. }
  568. }];
  569. }
  570. - (void)emailLinkSignin:(FIREmailLinkSignInRequest *)request
  571. callback:(FIREmailLinkSigninResponseCallback)callback {
  572. FIREmailLinkSignInResponse *response = [[FIREmailLinkSignInResponse alloc] init];
  573. [self postWithRequest:request response:response callback:^(NSError *error) {
  574. if (error) {
  575. callback(nil, error);
  576. } else {
  577. callback(response, nil);
  578. }
  579. }];
  580. }
  581. - (void)secureToken:(FIRSecureTokenRequest *)request
  582. callback:(FIRSecureTokenResponseCallback)callback {
  583. FIRSecureTokenResponse *response = [[FIRSecureTokenResponse alloc] init];
  584. [self postWithRequest:request response:response callback:^(NSError *error) {
  585. if (error) {
  586. callback(nil, error);
  587. } else {
  588. callback(response, nil);
  589. }
  590. }];
  591. }
  592. - (void)getOOBConfirmationCode:(FIRGetOOBConfirmationCodeRequest *)request
  593. callback:(FIRGetOOBConfirmationCodeResponseCallback)callback {
  594. FIRGetOOBConfirmationCodeResponse *response = [[FIRGetOOBConfirmationCodeResponse alloc] init];
  595. [self postWithRequest:request response:response callback:^(NSError *error) {
  596. if (error) {
  597. callback(nil, error);
  598. } else {
  599. callback(response, nil);
  600. }
  601. }];
  602. }
  603. - (void)signUpNewUser:(FIRSignUpNewUserRequest *)request
  604. callback:(FIRSignupNewUserCallback)callback{
  605. FIRSignUpNewUserResponse *response = [[FIRSignUpNewUserResponse alloc] init];
  606. [self postWithRequest:request response:response callback:^(NSError *error) {
  607. if (error) {
  608. callback(nil, error);
  609. } else {
  610. callback(response, nil);
  611. }
  612. }];
  613. }
  614. - (void)deleteAccount:(FIRDeleteAccountRequest *)request callback:(FIRDeleteCallBack)callback {
  615. FIRDeleteAccountResponse *response = [[FIRDeleteAccountResponse alloc] init];
  616. [self postWithRequest:request response:response callback:callback];
  617. }
  618. #if TARGET_OS_IOS
  619. - (void)sendVerificationCode:(FIRSendVerificationCodeRequest *)request
  620. callback:(FIRSendVerificationCodeResponseCallback)callback {
  621. FIRSendVerificationCodeResponse *response = [[FIRSendVerificationCodeResponse alloc] init];
  622. [self postWithRequest:request response:response callback:^(NSError *error) {
  623. if (error) {
  624. callback(nil, error);
  625. } else {
  626. callback(response, error);
  627. }
  628. }];
  629. }
  630. - (void)verifyPhoneNumber:(FIRVerifyPhoneNumberRequest *)request
  631. callback:(FIRVerifyPhoneNumberResponseCallback)callback {
  632. FIRVerifyPhoneNumberResponse *response = [[FIRVerifyPhoneNumberResponse alloc] init];
  633. [self postWithRequest:request response:response callback:^(NSError *error) {
  634. if (error) {
  635. callback(nil, error);
  636. return;
  637. }
  638. // Check whether or not the successful response is actually the special case phone auth flow
  639. // that returns a temporary proof and phone number.
  640. if (response.phoneNumber.length && response.temporaryProof.length) {
  641. FIRPhoneAuthCredential *credential =
  642. [[FIRPhoneAuthCredential alloc] initWithTemporaryProof:response.temporaryProof
  643. phoneNumber:response.phoneNumber
  644. providerID:FIRPhoneAuthProviderID];
  645. callback(nil,
  646. [FIRAuthErrorUtils credentialAlreadyInUseErrorWithMessage:nil
  647. credential:credential
  648. email:nil]);
  649. return;
  650. }
  651. callback(response, nil);
  652. }];
  653. }
  654. - (void)verifyClient:(id)request callback:(FIRVerifyClientResponseCallback)callback {
  655. FIRVerifyClientResponse *response = [[FIRVerifyClientResponse alloc] init];
  656. [self postWithRequest:request response:response callback:^(NSError *error) {
  657. if (error) {
  658. callback(nil, error);
  659. return;
  660. }
  661. callback(response, nil);
  662. }];
  663. }
  664. #endif
  665. - (void)resetPassword:(FIRResetPasswordRequest *)request
  666. callback:(FIRResetPasswordCallback)callback {
  667. FIRResetPasswordResponse *response = [[FIRResetPasswordResponse alloc] init];
  668. [self postWithRequest:request response:response callback:^(NSError *error) {
  669. if (error) {
  670. callback(nil, error);
  671. return;
  672. }
  673. callback(response, nil);
  674. }];
  675. }
  676. - (void)signInWithGameCenter:(FIRSignInWithGameCenterRequest *)request
  677. callback:(FIRSignInWithGameCenterResponseCallback)callback {
  678. FIRSignInWithGameCenterResponse *response = [[FIRSignInWithGameCenterResponse alloc] init];
  679. [self postWithRequest:request response:response callback:^(NSError *error) {
  680. if (error) {
  681. if (callback) {
  682. callback(nil, error);
  683. }
  684. } else {
  685. if (callback) {
  686. callback(response, nil);
  687. }
  688. }
  689. }];
  690. }
  691. #pragma mark - Generic RPC handling methods
  692. /** @fn postWithRequest:response:callback:
  693. @brief Calls the RPC using HTTP POST.
  694. @remarks Possible error responses:
  695. @see FIRAuthInternalErrorCodeRPCRequestEncodingError
  696. @see FIRAuthInternalErrorCodeJSONSerializationError
  697. @see FIRAuthInternalErrorCodeNetworkError
  698. @see FIRAuthInternalErrorCodeUnexpectedErrorResponse
  699. @see FIRAuthInternalErrorCodeUnexpectedResponse
  700. @see FIRAuthInternalErrorCodeRPCResponseDecodingError
  701. @param request The request.
  702. @param response The empty response to be filled.
  703. @param callback The callback for both success and failure.
  704. */
  705. - (void)postWithRequest:(id<FIRAuthRPCRequest>)request
  706. response:(id<FIRAuthRPCResponse>)response
  707. callback:(void (^)(NSError * _Nullable error))callback {
  708. NSError *error;
  709. NSData *bodyData;
  710. if ([request containsPostBody]) {
  711. id postBody = [request unencodedHTTPRequestBodyWithError:&error];
  712. if (!postBody) {
  713. callback([FIRAuthErrorUtils RPCRequestEncodingErrorWithUnderlyingError:error]);
  714. return;
  715. }
  716. NSJSONWritingOptions JSONWritingOptions = 0;
  717. #if DEBUG
  718. JSONWritingOptions |= NSJSONWritingPrettyPrinted;
  719. #endif
  720. if ([NSJSONSerialization isValidJSONObject:postBody]) {
  721. bodyData = [NSJSONSerialization dataWithJSONObject:postBody
  722. options:JSONWritingOptions
  723. error:&error];
  724. if (!bodyData) {
  725. // This is an untested case. This happens exclusively when there is an error in the framework
  726. // implementation of dataWithJSONObject:options:error:. This shouldn't normally occur as
  727. // isValidJSONObject: should return NO in any case we should encounter an error.
  728. error = [FIRAuthErrorUtils JSONSerializationErrorWithUnderlyingError:error];
  729. }
  730. } else {
  731. error = [FIRAuthErrorUtils JSONSerializationErrorForUnencodableType];
  732. }
  733. if (!bodyData) {
  734. callback(error);
  735. return;
  736. }
  737. }
  738. [_RPCIssuer asyncPostToURLWithRequestConfiguration:[request requestConfiguration]
  739. URL:[request requestURL]
  740. body:bodyData
  741. contentType:kJSONContentType
  742. completionHandler:^(NSData *data, NSError *error) {
  743. // If there is an error with no body data at all, then this must be a network error.
  744. if (error && !data) {
  745. callback([FIRAuthErrorUtils networkErrorWithUnderlyingError:error]);
  746. return;
  747. }
  748. // Try to decode the HTTP response data which may contain either a successful response or error
  749. // message.
  750. NSError *jsonError;
  751. NSDictionary * dictionary =
  752. [NSJSONSerialization JSONObjectWithData:data
  753. options:NSJSONReadingMutableLeaves
  754. error:&jsonError];
  755. if (!dictionary) {
  756. if (error) {
  757. // We have an error, but we couldn't decode the body, so we have no additional information
  758. // other than the raw response and the original NSError (the jsonError is infered by the
  759. // error code (FIRAuthErrorCodeUnexpectedHTTPResponse, and is irrelevant.)
  760. callback([FIRAuthErrorUtils unexpectedErrorResponseWithData:data underlyingError:error]);
  761. } else {
  762. // This is supposed to be a "successful" response, but we couldn't deserialize the body.
  763. callback([FIRAuthErrorUtils unexpectedResponseWithData:data underlyingError:jsonError]);
  764. }
  765. return;
  766. }
  767. if (![dictionary isKindOfClass:[NSDictionary class]]) {
  768. if (error) {
  769. callback([FIRAuthErrorUtils unexpectedErrorResponseWithDeserializedResponse:dictionary]);
  770. } else {
  771. callback([FIRAuthErrorUtils unexpectedResponseWithDeserializedResponse:dictionary]);
  772. }
  773. return;
  774. }
  775. // At this point we either have an error with successfully decoded details in the body, or we
  776. // have a response which must pass further validation before we know it's truly successful.
  777. // We deal with the case where we have an error with successfully decoded error details first:
  778. if (error) {
  779. NSDictionary *errorDictionary = dictionary[kErrorKey];
  780. if ([errorDictionary isKindOfClass:[NSDictionary class]]) {
  781. id<NSObject> errorMessage = errorDictionary[kErrorMessageKey];
  782. if ([errorMessage isKindOfClass:[NSString class]]) {
  783. NSString *errorMessageString = (NSString *)errorMessage;
  784. // Contruct client error.
  785. NSError *clientError = [[self class] clientErrorWithServerErrorMessage:errorMessageString
  786. errorDictionary:errorDictionary
  787. response:response];
  788. if (clientError) {
  789. callback(clientError);
  790. return;
  791. }
  792. }
  793. // Not a message we know, return the message directly.
  794. if (errorMessage) {
  795. NSError *unexpecterErrorResponse =
  796. [FIRAuthErrorUtils unexpectedErrorResponseWithDeserializedResponse:errorDictionary];
  797. callback(unexpecterErrorResponse);
  798. return;
  799. }
  800. }
  801. // No error message at all, return the decoded response.
  802. callback([FIRAuthErrorUtils unexpectedErrorResponseWithDeserializedResponse:dictionary]);
  803. return;
  804. }
  805. // Finally, we try to populate the response object with the JSON values.
  806. if (![response setWithDictionary:dictionary error:&error]) {
  807. callback([FIRAuthErrorUtils RPCResponseDecodingErrorWithDeserializedResponse:dictionary
  808. underlyingError:error]);
  809. return;
  810. }
  811. // In case returnIDPCredential of a verifyAssertion request is set to @YES, the server may
  812. // return a 200 with a response that may contain a server error.
  813. if ([request isKindOfClass:[FIRVerifyAssertionRequest class]]) {
  814. FIRVerifyAssertionRequest *verifyAssertionRequest = (FIRVerifyAssertionRequest *)request;
  815. if (verifyAssertionRequest.returnIDPCredential) {
  816. NSString *errorMessage = dictionary[kReturnIDPCredentialErrorMessageKey];
  817. if ([errorMessage isKindOfClass:[NSString class]]) {
  818. NSString *errorString = (NSString *)errorMessage;
  819. NSError *clientError = [[self class] clientErrorWithServerErrorMessage:errorString
  820. errorDictionary:@{}
  821. response:response];
  822. if (clientError) {
  823. callback(clientError);
  824. return;
  825. }
  826. }
  827. }
  828. }
  829. // Success! The response object originally passed in can be used by the caller.
  830. callback(nil);
  831. }];
  832. }
  833. /** @fn clientErrorWithServerErrorMessage:errorDictionary:
  834. @brief Translates known server errors to client errors.
  835. @param serverErrorMessage The error message from the server.
  836. @param errorDictionary The error part of the response from the server.
  837. @param response The response from the server RPC.
  838. @return A client error, if any.
  839. */
  840. + (nullable NSError *)clientErrorWithServerErrorMessage:(NSString *)serverErrorMessage
  841. errorDictionary:(NSDictionary *)errorDictionary
  842. response:(id<FIRAuthRPCResponse>)response {
  843. NSString *shortErrorMessage = serverErrorMessage;
  844. NSString *serverDetailErrorMessage;
  845. NSRange colonRange = [serverErrorMessage rangeOfString:@":"];
  846. if (colonRange.location != NSNotFound) {
  847. shortErrorMessage = [serverErrorMessage substringToIndex:colonRange.location];
  848. shortErrorMessage =
  849. [shortErrorMessage stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
  850. serverDetailErrorMessage = [serverErrorMessage substringFromIndex:colonRange.location + 1];
  851. serverDetailErrorMessage = [serverDetailErrorMessage stringByTrimmingCharactersInSet:
  852. [NSCharacterSet whitespaceCharacterSet]];
  853. }
  854. // Delegate the responsibility for constructing the client error to the response object,
  855. // if possible.
  856. SEL clientErrorWithServerErrorMessageSelector =
  857. @selector(clientErrorWithShortErrorMessage:detailErrorMessage:);
  858. if ([response respondsToSelector:clientErrorWithServerErrorMessageSelector]) {
  859. NSError *error = [response clientErrorWithShortErrorMessage:shortErrorMessage
  860. detailErrorMessage:serverDetailErrorMessage];
  861. if (error) {
  862. return error;
  863. }
  864. }
  865. if ([shortErrorMessage isEqualToString:kUserNotFoundErrorMessage]) {
  866. return [FIRAuthErrorUtils userNotFoundErrorWithMessage:serverDetailErrorMessage];
  867. }
  868. if ([shortErrorMessage isEqualToString:kUserDeletedErrorMessage]) {
  869. return [FIRAuthErrorUtils userNotFoundErrorWithMessage:serverDetailErrorMessage];
  870. }
  871. if ([shortErrorMessage isEqualToString:kInvalidLocalIDErrorMessage]) {
  872. // This case shouldn't be necessary but it is for now: b/27908364 .
  873. return [FIRAuthErrorUtils userNotFoundErrorWithMessage:serverDetailErrorMessage];
  874. }
  875. if ([shortErrorMessage isEqualToString:kUserTokenExpiredErrorMessage]) {
  876. return [FIRAuthErrorUtils userTokenExpiredErrorWithMessage:serverDetailErrorMessage];
  877. }
  878. if ([shortErrorMessage isEqualToString:kTooManyRequestsErrorMessage]) {
  879. return [FIRAuthErrorUtils tooManyRequestsErrorWithMessage:serverDetailErrorMessage];
  880. }
  881. if ([shortErrorMessage isEqualToString:kInvalidCustomTokenErrorMessage]) {
  882. return [FIRAuthErrorUtils invalidCustomTokenErrorWithMessage:serverDetailErrorMessage];
  883. }
  884. if ([shortErrorMessage isEqualToString:kCustomTokenMismatch]) {
  885. return [FIRAuthErrorUtils customTokenMistmatchErrorWithMessage:serverDetailErrorMessage];
  886. }
  887. if ([shortErrorMessage isEqualToString:kInvalidCredentialErrorMessage] ||
  888. [shortErrorMessage isEqualToString:kInvalidPendingToken]) {
  889. return [FIRAuthErrorUtils invalidCredentialErrorWithMessage:serverDetailErrorMessage];
  890. }
  891. if ([shortErrorMessage isEqualToString:kUserDisabledErrorMessage]) {
  892. return [FIRAuthErrorUtils userDisabledErrorWithMessage:serverDetailErrorMessage];
  893. }
  894. if ([shortErrorMessage isEqualToString:kOperationNotAllowedErrorMessage]) {
  895. return [FIRAuthErrorUtils operationNotAllowedErrorWithMessage:serverDetailErrorMessage];
  896. }
  897. if ([shortErrorMessage isEqualToString:kPasswordLoginDisabledErrorMessage]) {
  898. return [FIRAuthErrorUtils operationNotAllowedErrorWithMessage:serverDetailErrorMessage];
  899. }
  900. if ([shortErrorMessage isEqualToString:kEmailAlreadyInUseErrorMessage]) {
  901. return [FIRAuthErrorUtils emailAlreadyInUseErrorWithEmail:nil];
  902. }
  903. if ([shortErrorMessage isEqualToString:kInvalidEmailErrorMessage]) {
  904. return [FIRAuthErrorUtils invalidEmailErrorWithMessage:serverDetailErrorMessage];
  905. }
  906. // "INVALID_IDENTIFIER" can be returned by createAuthURI RPC. Considering email addresses are
  907. // currently the only identifiers, we surface the FIRAuthErrorCodeInvalidEmail error code in this
  908. // case.
  909. if ([shortErrorMessage isEqualToString:kInvalidIdentifierErrorMessage]) {
  910. return [FIRAuthErrorUtils invalidEmailErrorWithMessage:serverDetailErrorMessage];
  911. }
  912. if ([shortErrorMessage isEqualToString:kWrongPasswordErrorMessage]) {
  913. return [FIRAuthErrorUtils wrongPasswordErrorWithMessage:serverDetailErrorMessage];
  914. }
  915. if ([shortErrorMessage isEqualToString:kCredentialTooOldErrorMessage]) {
  916. return [FIRAuthErrorUtils requiresRecentLoginErrorWithMessage:serverDetailErrorMessage];
  917. }
  918. if ([shortErrorMessage isEqualToString:kInvalidUserTokenErrorMessage]) {
  919. return [FIRAuthErrorUtils invalidUserTokenErrorWithMessage:serverDetailErrorMessage];
  920. }
  921. if ([shortErrorMessage isEqualToString:kFederatedUserIDAlreadyLinkedMessage]) {
  922. FIROAuthCredential *credential;
  923. NSString *email;
  924. if ([response isKindOfClass:[FIRVerifyAssertionResponse class]]) {
  925. FIRVerifyAssertionResponse *verifyAssertion = (FIRVerifyAssertionResponse *)response;
  926. credential =
  927. [[FIROAuthCredential alloc] initWithVerifyAssertionResponse:verifyAssertion];
  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:kInvalidProviderIDErrorMessage]) {
  968. return [FIRAuthErrorUtils invalidProviderIDErrorWithMessage:serverDetailErrorMessage];
  969. }
  970. if ([shortErrorMessage isEqualToString:kInvalidDynamicLinkDomainErrorMessage]) {
  971. return [FIRAuthErrorUtils invalidDynamicLinkDomainErrorWithMessage:serverDetailErrorMessage];
  972. }
  973. if ([shortErrorMessage isEqualToString:kMissingContinueURIErrorMessage]) {
  974. return [FIRAuthErrorUtils missingContinueURIErrorWithMessage:serverDetailErrorMessage];
  975. }
  976. if ([shortErrorMessage isEqualToString:kInvalidPhoneNumberErrorMessage]) {
  977. return [FIRAuthErrorUtils invalidPhoneNumberErrorWithMessage:serverDetailErrorMessage];
  978. }
  979. if ([shortErrorMessage isEqualToString:kInvalidSessionInfoErrorMessage]) {
  980. return [FIRAuthErrorUtils invalidVerificationIDErrorWithMessage:serverDetailErrorMessage];
  981. }
  982. if ([shortErrorMessage isEqualToString:kInvalidVerificationCodeErrorMessage]) {
  983. return [FIRAuthErrorUtils invalidVerificationCodeErrorWithMessage:serverDetailErrorMessage];
  984. }
  985. if ([shortErrorMessage isEqualToString:kSessionExpiredErrorMessage]) {
  986. return [FIRAuthErrorUtils sessionExpiredErrorWithMessage:serverDetailErrorMessage];
  987. }
  988. if ([shortErrorMessage isEqualToString:kMissingAppTokenErrorMessage]) {
  989. return [FIRAuthErrorUtils missingAppTokenErrorWithUnderlyingError:nil];
  990. }
  991. if ([shortErrorMessage isEqualToString:kMissingAppCredentialErrorMessage]) {
  992. return [FIRAuthErrorUtils missingAppCredentialWithMessage:serverDetailErrorMessage];
  993. }
  994. if ([shortErrorMessage isEqualToString:kInvalidAppCredentialErrorMessage]) {
  995. return [FIRAuthErrorUtils invalidAppCredentialWithMessage:serverDetailErrorMessage];
  996. }
  997. if ([shortErrorMessage isEqualToString:kQuoutaExceededErrorMessage]) {
  998. return [FIRAuthErrorUtils quotaExceededErrorWithMessage:serverErrorMessage];
  999. }
  1000. if ([shortErrorMessage isEqualToString:kAppNotVerifiedErrorMessage]) {
  1001. return [FIRAuthErrorUtils appNotVerifiedErrorWithMessage:serverErrorMessage];
  1002. }
  1003. if ([shortErrorMessage isEqualToString:kMissingClientIdentifier]) {
  1004. return [FIRAuthErrorUtils missingClientIdentifierErrorWithMessage:serverErrorMessage];
  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