FIRAuthBackend.m 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677
  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 "FirebaseAuth/Sources/Backend/FIRAuthBackend.h"
  17. #if SWIFT_PACKAGE
  18. @import GTMSessionFetcherCore;
  19. #else
  20. #import <GTMSessionFetcher/GTMSessionFetcher.h>
  21. #import <GTMSessionFetcher/GTMSessionFetcherService.h>
  22. #endif
  23. #import <FirebaseAppCheckInterop/FirebaseAppCheckInterop.h>
  24. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FirebaseAuth.h"
  25. #import "FirebaseAuth/Sources/Auth/FIRAuthGlobalWorkQueue.h"
  26. #import "FirebaseAuth/Sources/Auth/FIRAuth_Internal.h"
  27. #import "FirebaseAuth/Sources/AuthProvider/OAuth/FIROAuthCredential_Internal.h"
  28. #import "FirebaseAuth/Sources/Backend/RPC/FIRCreateAuthURIRequest.h"
  29. #import "FirebaseAuth/Sources/Backend/RPC/FIRCreateAuthURIResponse.h"
  30. #import "FirebaseAuth/Sources/Backend/RPC/FIRDeleteAccountRequest.h"
  31. #import "FirebaseAuth/Sources/Backend/RPC/FIRDeleteAccountResponse.h"
  32. #import "FirebaseAuth/Sources/Backend/RPC/FIREmailLinkSignInRequest.h"
  33. #import "FirebaseAuth/Sources/Backend/RPC/FIREmailLinkSignInResponse.h"
  34. #import "FirebaseAuth/Sources/Backend/RPC/FIRGetAccountInfoRequest.h"
  35. #import "FirebaseAuth/Sources/Backend/RPC/FIRGetAccountInfoResponse.h"
  36. #import "FirebaseAuth/Sources/Backend/RPC/FIRGetOOBConfirmationCodeRequest.h"
  37. #import "FirebaseAuth/Sources/Backend/RPC/FIRGetOOBConfirmationCodeResponse.h"
  38. #import "FirebaseAuth/Sources/Backend/RPC/FIRGetProjectConfigRequest.h"
  39. #import "FirebaseAuth/Sources/Backend/RPC/FIRGetProjectConfigResponse.h"
  40. #import "FirebaseAuth/Sources/Backend/RPC/FIRGetRecaptchaConfigRequest.h"
  41. #import "FirebaseAuth/Sources/Backend/RPC/FIRGetRecaptchaConfigResponse.h"
  42. #import "FirebaseAuth/Sources/Backend/RPC/FIRResetPasswordRequest.h"
  43. #import "FirebaseAuth/Sources/Backend/RPC/FIRResetPasswordResponse.h"
  44. #import "FirebaseAuth/Sources/Backend/RPC/FIRRevokeTokenRequest.h"
  45. #import "FirebaseAuth/Sources/Backend/RPC/FIRRevokeTokenResponse.h"
  46. #import "FirebaseAuth/Sources/Backend/RPC/FIRSecureTokenRequest.h"
  47. #import "FirebaseAuth/Sources/Backend/RPC/FIRSecureTokenResponse.h"
  48. #import "FirebaseAuth/Sources/Backend/RPC/FIRSendVerificationCodeRequest.h"
  49. #import "FirebaseAuth/Sources/Backend/RPC/FIRSendVerificationCodeResponse.h"
  50. #import "FirebaseAuth/Sources/Backend/RPC/FIRSetAccountInfoRequest.h"
  51. #import "FirebaseAuth/Sources/Backend/RPC/FIRSetAccountInfoResponse.h"
  52. #import "FirebaseAuth/Sources/Backend/RPC/FIRSignInWithGameCenterRequest.h"
  53. #import "FirebaseAuth/Sources/Backend/RPC/FIRSignInWithGameCenterResponse.h"
  54. #import "FirebaseAuth/Sources/Backend/RPC/FIRSignUpNewUserRequest.h"
  55. #import "FirebaseAuth/Sources/Backend/RPC/FIRSignUpNewUserResponse.h"
  56. #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyAssertionRequest.h"
  57. #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyAssertionResponse.h"
  58. #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyClientRequest.h"
  59. #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyClientResponse.h"
  60. #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyCustomTokenRequest.h"
  61. #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyCustomTokenResponse.h"
  62. #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyPasswordRequest.h"
  63. #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyPasswordResponse.h"
  64. #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyPhoneNumberRequest.h"
  65. #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyPhoneNumberResponse.h"
  66. #import "FirebaseAuth/Sources/Utilities/FIRAuthErrorUtils.h"
  67. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  68. #if TARGET_OS_IOS
  69. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRPhoneAuthProvider.h"
  70. #import "FirebaseAuth/Sources/AuthProvider/Phone/FIRPhoneAuthCredential_Internal.h"
  71. #import "FirebaseAuth/Sources/MultiFactor/Phone/FIRPhoneMultiFactorInfo+Internal.h"
  72. #import "FirebaseAuth/Sources/MultiFactor/TOTP/FIRTOTPMultiFactorInfo.h"
  73. #endif
  74. NS_ASSUME_NONNULL_BEGIN
  75. /** @var kClientVersionHeader
  76. @brief HTTP header name for the client version.
  77. */
  78. static NSString *const kClientVersionHeader = @"X-Client-Version";
  79. /** @var kIosBundleIdentifierHeader
  80. @brief HTTP header name for iOS bundle ID.
  81. */
  82. static NSString *const kIosBundleIdentifierHeader = @"X-Ios-Bundle-Identifier";
  83. /** @var kFirebaseLocalHeader
  84. @brief HTTP header name for the firebase locale.
  85. */
  86. static NSString *const kFirebaseLocalHeader = @"X-Firebase-Locale";
  87. /** @var kFirebaseAppIDHeader
  88. @brief HTTP header name for the Firebase app ID.
  89. */
  90. static NSString *const kFirebaseAppIDHeader = @"X-Firebase-GMPID";
  91. /** @var kFirebaseHeartbeatHeader
  92. @brief HTTP header name for a Firebase heartbeats payload.
  93. */
  94. static NSString *const kFirebaseHeartbeatHeader = @"X-Firebase-Client";
  95. /** @var kFirebaseAuthCoreFrameworkMarker
  96. @brief The marker in the HTTP header that indicates the request comes from Firebase Auth Core.
  97. */
  98. static NSString *const kFirebaseAuthCoreFrameworkMarker = @"FirebaseCore-iOS";
  99. /** @var kJSONContentType
  100. @brief The value of the HTTP content-type header for JSON payloads.
  101. */
  102. static NSString *const kJSONContentType = @"application/json";
  103. /** @var kErrorDataKey
  104. @brief Key for error data in NSError returned by @c GTMSessionFetcher.
  105. */
  106. static NSString *const kErrorDataKey = @"data";
  107. /** @var kErrorKey
  108. @brief The key for the "error" value in JSON responses from the server.
  109. */
  110. static NSString *const kErrorKey = @"error";
  111. /** @var kErrorsKey
  112. @brief The key for the "errors" value in JSON responses from the server.
  113. */
  114. static NSString *const kErrorsKey = @"errors";
  115. /** @var kReasonKey
  116. @brief The key for the "reason" value in JSON responses from the server.
  117. */
  118. static NSString *const kReasonKey = @"reason";
  119. /** @var kInvalidKeyReasonValue
  120. @brief The value for the "reason" key indicating an invalid API Key was received by the server.
  121. */
  122. static NSString *const kInvalidKeyReasonValue = @"keyInvalid";
  123. /** @var kAppNotAuthorizedReasonValue
  124. @brief The value for the "reason" key indicating the App is not authorized to use Firebase
  125. Authentication.
  126. */
  127. static NSString *const kAppNotAuthorizedReasonValue = @"ipRefererBlocked";
  128. /** @var kErrorMessageKey
  129. @brief The key for an error's "message" value in JSON responses from the server.
  130. */
  131. static NSString *const kErrorMessageKey = @"message";
  132. /** @var kReturnIDPCredentialErrorMessageKey
  133. @brief The key for "errorMessage" value in JSON responses from the server, In case
  134. returnIDPCredential of a verifyAssertion request is set to @YES.
  135. */
  136. static NSString *const kReturnIDPCredentialErrorMessageKey = @"errorMessage";
  137. /** @var kUserNotFoundErrorMessage
  138. @brief This is the error message returned when the user is not found, which means the user
  139. account has been deleted given the token was once valid.
  140. */
  141. static NSString *const kUserNotFoundErrorMessage = @"USER_NOT_FOUND";
  142. /** @var kUserDeletedErrorMessage
  143. @brief This is the error message the server will respond with if the user entered an invalid
  144. email address.
  145. */
  146. static NSString *const kUserDeletedErrorMessage = @"EMAIL_NOT_FOUND";
  147. /** @var kInvalidLocalIDErrorMessage
  148. @brief This is the error message the server responds with if the user local id in the id token
  149. does not exit.
  150. */
  151. static NSString *const kInvalidLocalIDErrorMessage = @"INVALID_LOCAL_ID";
  152. /** @var kUserTokenExpiredErrorMessage
  153. @brief The error returned by the server if the token issue time is older than the account's
  154. valid_since time.
  155. */
  156. static NSString *const kUserTokenExpiredErrorMessage = @"TOKEN_EXPIRED";
  157. /** @var kTooManyRequestsErrorMessage
  158. @brief This is the error message the server will respond with if too many requests were made to
  159. a server method.
  160. */
  161. static NSString *const kTooManyRequestsErrorMessage = @"TOO_MANY_ATTEMPTS_TRY_LATER";
  162. /** @var kInvalidCustomTokenErrorMessage
  163. @brief This is the error message the server will respond with if there is a validation error
  164. with the custom token.
  165. */
  166. static NSString *const kInvalidCustomTokenErrorMessage = @"INVALID_CUSTOM_TOKEN";
  167. /** @var kCustomTokenMismatch
  168. @brief This is the error message the server will respond with if the service account and API key
  169. belong to different projects.
  170. */
  171. static NSString *const kCustomTokenMismatch = @"CREDENTIAL_MISMATCH";
  172. /** @var kInvalidCredentialErrorMessage
  173. @brief This is the error message the server responds with if the IDP token or requestUri is
  174. invalid.
  175. */
  176. static NSString *const kInvalidCredentialErrorMessage = @"INVALID_IDP_RESPONSE";
  177. /** @var kUserDisabledErrorMessage
  178. @brief The error returned by the server if the user account is diabled.
  179. */
  180. static NSString *const kUserDisabledErrorMessage = @"USER_DISABLED";
  181. /** @var kOperationNotAllowedErrorMessage
  182. @brief This is the error message the server will respond with if Admin disables IDP specified by
  183. provider.
  184. */
  185. static NSString *const kOperationNotAllowedErrorMessage = @"OPERATION_NOT_ALLOWED";
  186. /** @var kPasswordLoginDisabledErrorMessage
  187. @brief This is the error message the server responds with if password login is disabled.
  188. */
  189. static NSString *const kPasswordLoginDisabledErrorMessage = @"PASSWORD_LOGIN_DISABLED";
  190. /** @var kEmailAlreadyInUseErrorMessage
  191. @brief This is the error message the server responds with if the email address already exists.
  192. */
  193. static NSString *const kEmailAlreadyInUseErrorMessage = @"EMAIL_EXISTS";
  194. /** @var kInvalidEmailErrorMessage
  195. @brief The error returned by the server if the email is invalid.
  196. */
  197. static NSString *const kInvalidEmailErrorMessage = @"INVALID_EMAIL";
  198. /** @var kInvalidIdentifierErrorMessage
  199. @brief The error returned by the server if the identifier is invalid.
  200. */
  201. static NSString *const kInvalidIdentifierErrorMessage = @"INVALID_IDENTIFIER";
  202. /** @var kWrongPasswordErrorMessage
  203. @brief This is the error message the server will respond with if the user entered a wrong
  204. password.
  205. */
  206. static NSString *const kWrongPasswordErrorMessage = @"INVALID_PASSWORD";
  207. /** @var kCredentialTooOldErrorMessage
  208. @brief This is the error message the server responds with if account change is attempted 5
  209. minutes after signing in.
  210. */
  211. static NSString *const kCredentialTooOldErrorMessage = @"CREDENTIAL_TOO_OLD_LOGIN_AGAIN";
  212. /** @var kFederatedUserIDAlreadyLinkedMessage
  213. @brief This is the error message the server will respond with if the federated user ID has been
  214. already linked with another account.
  215. */
  216. static NSString *const kFederatedUserIDAlreadyLinkedMessage = @"FEDERATED_USER_ID_ALREADY_LINKED";
  217. /** @var kInvalidUserTokenErrorMessage
  218. @brief This is the error message the server responds with if user's saved auth credential is
  219. invalid, and the user needs to sign in again.
  220. */
  221. static NSString *const kInvalidUserTokenErrorMessage = @"INVALID_ID_TOKEN";
  222. /** @var kWeakPasswordErrorMessagePrefix
  223. @brief This is the prefix for the error message the server responds with if user's new password
  224. to be set is too weak.
  225. */
  226. static NSString *const kWeakPasswordErrorMessagePrefix = @"WEAK_PASSWORD";
  227. /** @var kExpiredActionCodeErrorMessage
  228. @brief This is the error message the server will respond with if the action code is expired.
  229. */
  230. static NSString *const kExpiredActionCodeErrorMessage = @"EXPIRED_OOB_CODE";
  231. /** @var kInvalidActionCodeErrorMessage
  232. @brief This is the error message the server will respond with if the action code is invalid.
  233. */
  234. static NSString *const kInvalidActionCodeErrorMessage = @"INVALID_OOB_CODE";
  235. /** @var kMissingEmailErrorMessage
  236. @brief This is the error message the server will respond with if the email address is missing
  237. during a "send password reset email" attempt.
  238. */
  239. static NSString *const kMissingEmailErrorMessage = @"MISSING_EMAIL";
  240. /** @var kInvalidSenderEmailErrorMessage
  241. @brief This is the error message the server will respond with if the sender email is invalid
  242. during a "send password reset email" attempt.
  243. */
  244. static NSString *const kInvalidSenderEmailErrorMessage = @"INVALID_SENDER";
  245. /** @var kInvalidMessagePayloadErrorMessage
  246. @brief This is the error message the server will respond with if there are invalid parameters in
  247. the payload during a "send password reset email" attempt.
  248. */
  249. static NSString *const kInvalidMessagePayloadErrorMessage = @"INVALID_MESSAGE_PAYLOAD";
  250. /** @var kInvalidRecipientEmailErrorMessage
  251. @brief This is the error message the server will respond with if the recipient email is invalid.
  252. */
  253. static NSString *const kInvalidRecipientEmailErrorMessage = @"INVALID_RECIPIENT_EMAIL";
  254. /** @var kMissingIosBundleIDErrorMessage
  255. @brief This is the error message the server will respond with if iOS bundle ID is missing but
  256. the iOS App store ID is provided.
  257. */
  258. static NSString *const kMissingIosBundleIDErrorMessage = @"MISSING_IOS_BUNDLE_ID";
  259. /** @var kMissingAndroidPackageNameErrorMessage
  260. @brief This is the error message the server will respond with if Android Package Name is missing
  261. but the flag indicating the app should be installed is set to true.
  262. */
  263. static NSString *const kMissingAndroidPackageNameErrorMessage = @"MISSING_ANDROID_PACKAGE_NAME";
  264. /** @var kUnauthorizedDomainErrorMessage
  265. @brief This is the error message the server will respond with if the domain of the continue URL
  266. specified is not allowlisted in the Firebase console.
  267. */
  268. static NSString *const kUnauthorizedDomainErrorMessage = @"UNAUTHORIZED_DOMAIN";
  269. /** @var kInvalidProviderIDErrorMessage
  270. @brief This is the error message the server will respond with if the provider id given for the
  271. web operation is invalid.
  272. */
  273. static NSString *const kInvalidProviderIDErrorMessage = @"INVALID_PROVIDER_ID";
  274. /** @var kInvalidDynamicLinkDomainErrorMessage
  275. @brief This is the error message the server will respond with if the dynamic link domain
  276. provided in the request is invalid.
  277. */
  278. static NSString *const kInvalidDynamicLinkDomainErrorMessage = @"INVALID_DYNAMIC_LINK_DOMAIN";
  279. /** @var kInvalidContinueURIErrorMessage
  280. @brief This is the error message the server will respond with if the continue URL provided in
  281. the request is invalid.
  282. */
  283. static NSString *const kInvalidContinueURIErrorMessage = @"INVALID_CONTINUE_URI";
  284. /** @var kMissingContinueURIErrorMessage
  285. @brief This is the error message the server will respond with if there was no continue URI
  286. present in a request that required one.
  287. */
  288. static NSString *const kMissingContinueURIErrorMessage = @"MISSING_CONTINUE_URI";
  289. /** @var kInvalidPhoneNumberErrorMessage
  290. @brief This is the error message the server will respond with if an incorrectly formatted phone
  291. number is provided.
  292. */
  293. static NSString *const kInvalidPhoneNumberErrorMessage = @"INVALID_PHONE_NUMBER";
  294. /** @var kInvalidVerificationCodeErrorMessage
  295. @brief This is the error message the server will respond with if an invalid verification code is
  296. provided.
  297. */
  298. static NSString *const kInvalidVerificationCodeErrorMessage = @"INVALID_CODE";
  299. /** @var kInvalidSessionInfoErrorMessage
  300. @brief This is the error message the server will respond with if an invalid session info
  301. (verification ID) is provided.
  302. */
  303. static NSString *const kInvalidSessionInfoErrorMessage = @"INVALID_SESSION_INFO";
  304. /** @var kSessionExpiredErrorMessage
  305. @brief This is the error message the server will respond with if the SMS code has expired before
  306. it is used.
  307. */
  308. static NSString *const kSessionExpiredErrorMessage = @"SESSION_EXPIRED";
  309. /** @var kMissingOrInvalidNonceErrorMessage
  310. @brief This is the error message the server will respond with if the nonce is missing or
  311. invalid.
  312. */
  313. static NSString *const kMissingOrInvalidNonceErrorMessage = @"MISSING_OR_INVALID_NONCE";
  314. /** @var kMissingAppTokenErrorMessage
  315. @brief This is the error message the server will respond with if the APNS token is missing in a
  316. verifyClient request.
  317. */
  318. static NSString *const kMissingAppTokenErrorMessage = @"MISSING_IOS_APP_TOKEN";
  319. /** @var kMissingAppCredentialErrorMessage
  320. @brief This is the error message the server will respond with if the app token is missing in a
  321. sendVerificationCode request.
  322. */
  323. static NSString *const kMissingAppCredentialErrorMessage = @"MISSING_APP_CREDENTIAL";
  324. /** @var kInvalidAppCredentialErrorMessage
  325. @brief This is the error message the server will respond with if the app credential in a
  326. sendVerificationCode request is invalid.
  327. */
  328. static NSString *const kInvalidAppCredentialErrorMessage = @"INVALID_APP_CREDENTIAL";
  329. /** @var kQuoutaExceededErrorMessage
  330. @brief This is the error message the server will respond with if the quota for SMS text messages
  331. has been exceeded for the project.
  332. */
  333. static NSString *const kQuoutaExceededErrorMessage = @"QUOTA_EXCEEDED";
  334. /** @var kAppNotVerifiedErrorMessage
  335. @brief This is the error message the server will respond with if Firebase could not verify the
  336. app during a phone authentication flow.
  337. */
  338. static NSString *const kAppNotVerifiedErrorMessage = @"APP_NOT_VERIFIED";
  339. /** @var kCaptchaCheckFailedErrorMessage
  340. @brief This is the error message the server will respond with if the reCAPTCHA token provided is
  341. invalid.
  342. */
  343. static NSString *const kCaptchaCheckFailedErrorMessage = @"CAPTCHA_CHECK_FAILED";
  344. /** @var kTenantIDMismatch
  345. @brief This is the error message the server will respond with if the tenant id mismatches.
  346. */
  347. static NSString *const kTenantIDMismatch = @"TENANT_ID_MISMATCH";
  348. /** @var kUnsupportedTenantOperation
  349. @brief This is the error message the server will respond with if the operation does not support
  350. multi-tenant.
  351. */
  352. static NSString *const kUnsupportedTenantOperation = @"UNSUPPORTED_TENANT_OPERATION";
  353. /** @var kMissingMFAPendingCredentialErrorMessage
  354. @brief This is the error message the server will respond with if the MFA pending credential is
  355. missing.
  356. */
  357. static NSString *const kMissingMFAPendingCredentialErrorMessage = @"MISSING_MFA_PENDING_CREDENTIAL";
  358. /** @var kMissingMFAEnrollmentIDErrorMessage
  359. @brief This is the error message the server will respond with if the MFA enrollment ID is missing.
  360. */
  361. static NSString *const kMissingMFAEnrollmentIDErrorMessage = @"MISSING_MFA_ENROLLMENT_ID";
  362. /** @var kInvalidMFAPendingCredentialErrorMessage
  363. @brief This is the error message the server will respond with if the MFA pending credential is
  364. invalid.
  365. */
  366. static NSString *const kInvalidMFAPendingCredentialErrorMessage = @"INVALID_MFA_PENDING_CREDENTIAL";
  367. /** @var kMFAEnrollmentNotFoundErrorMessage
  368. @brief This is the error message the server will respond with if the MFA enrollment info is not
  369. found.
  370. */
  371. static NSString *const kMFAEnrollmentNotFoundErrorMessage = @"MFA_ENROLLMENT_NOT_FOUND";
  372. /** @var kAdminOnlyOperationErrorMessage
  373. @brief This is the error message the server will respond with if the operation is admin only.
  374. */
  375. static NSString *const kAdminOnlyOperationErrorMessage = @"ADMIN_ONLY_OPERATION";
  376. /** @var kUnverifiedEmailErrorMessage
  377. @brief This is the error message the server will respond with if the email is unverified.
  378. */
  379. static NSString *const kUnverifiedEmailErrorMessage = @"UNVERIFIED_EMAIL";
  380. /** @var kSecondFactorExistsErrorMessage
  381. @brief This is the error message the server will respond with if the second factor already exsists.
  382. */
  383. static NSString *const kSecondFactorExistsErrorMessage = @"SECOND_FACTOR_EXISTS";
  384. /** @var kSecondFactorLimitExceededErrorMessage
  385. @brief This is the error message the server will respond with if the number of second factor
  386. reaches the limit.
  387. */
  388. static NSString *const kSecondFactorLimitExceededErrorMessage = @"SECOND_FACTOR_LIMIT_EXCEEDED";
  389. /** @var kUnsupportedFirstFactorErrorMessage
  390. @brief This is the error message the server will respond with if the first factor doesn't support
  391. MFA.
  392. */
  393. static NSString *const kUnsupportedFirstFactorErrorMessage = @"UNSUPPORTED_FIRST_FACTOR";
  394. /** @var kBlockingCloudFunctionErrorResponse
  395. @brief This is the error message blocking Cloud Functions.
  396. */
  397. static NSString *const kBlockingCloudFunctionErrorResponse = @"BLOCKING_FUNCTION_ERROR_RESPONSE";
  398. /** @var kEmailChangeNeedsVerificationErrorMessage
  399. @brief This is the error message the server will respond with if changing an unverified email.
  400. */
  401. static NSString *const kEmailChangeNeedsVerificationErrorMessage =
  402. @"EMAIL_CHANGE_NEEDS_VERIFICATION";
  403. /** @var kInvalidPendingToken
  404. @brief Generic IDP error codes.
  405. */
  406. static NSString *const kInvalidPendingToken = @"INVALID_PENDING_TOKEN";
  407. /** @var kInvalidRecaptchaScore
  408. @brief This is the error message the server will respond with if the recaptcha score is invalid.
  409. */
  410. static NSString *const kInvalidRecaptchaScore = @"INVALID_RECAPTCHA_SCORE";
  411. /** @var kMissingRecaptchaToken
  412. @brief This is the error message the server will respond with if the recaptcha token is missing
  413. in the request.
  414. */
  415. static NSString *const kMissingRecaptchaToken = @"MISSING_RECAPTCHA_TOKEN";
  416. /** @var kInvalidRecaptchaToken
  417. @brief This is the error message the server will respond with if the recaptcha token is invalid.
  418. */
  419. static NSString *const kInvalidRecaptchaToken = @"INVALID_RECAPTCHA_TOKEN";
  420. /** @var kInvalidRecaptchaAction
  421. @brief This is the error message the server will respond with if the recaptcha action is
  422. invalid.
  423. */
  424. static NSString *const kInvalidRecaptchaAction = @"INVALID_RECAPTCHA_ACTION";
  425. /** @var kInvalidRecaptchaEnforcementState
  426. @brief This is the error message the server will respond with if the recaptcha enforcement state
  427. is invalid.
  428. */
  429. static NSString *const kInvalidRecaptchaEnforcementState = @"INVALID_RECAPTCHA_ENFORCEMENT_STATE";
  430. /** @var kRecaptchaNotEnabled
  431. @brief This is the error message the server will respond with if recaptcha is not enabled.
  432. */
  433. static NSString *const kRecaptchaNotEnabled = @"RECAPTCHA_NOT_ENABLED";
  434. /** @var kMissingClientIdentifier
  435. @brief This is the error message the server will respond with if Firebase could not verify the
  436. app during a phone authentication flow when a real phone number is used and app verification
  437. is disabled for testing.
  438. */
  439. static NSString *const kMissingClientIdentifier = @"MISSING_CLIENT_IDENTIFIER";
  440. /** @var kMissingClientType
  441. @brief This is the error message the server will respond with if Firebase could not verify the
  442. app during a phone authentication flow when a real phone number is used and app verification
  443. is disabled for testing.
  444. */
  445. static NSString *const kMissingClientType = @"MISSING_CLIENT_TYPE";
  446. /** @var kMissingRecaptchaToken
  447. @brief This is the error message the server will respond with if the recaptcha token is missing
  448. in the request.
  449. */
  450. static NSString *const kMissingRecaptchaVersion = @"MISSING_RECAPTCHA_VERSION";
  451. /** @var kMissingRecaptchaToken
  452. @brief This is the error message the server will respond with if the recaptcha token is missing
  453. in the request.
  454. */
  455. static NSString *const kMissingInvalidReqType = @"INVALID_REQ_TYPE";
  456. /** @var kMissingRecaptchaToken
  457. @brief This is the error message the server will respond with if the recaptcha token is missing
  458. in the request.
  459. */
  460. static NSString *const kInvalidRecaptchaVersion = @"INVALID_RECAPTCHA_VERSION";
  461. /** @var kInvalidLoginCredentials
  462. @brief This is the error message the server will respond with if the login credentials is
  463. invalid. in the request.
  464. */
  465. static NSString *const kInvalidLoginCredentials = @"INVALID_LOGIN_CREDENTIALS";
  466. /** @var gBackendImplementation
  467. @brief The singleton FIRAuthBackendImplementation instance to use.
  468. */
  469. static id<FIRAuthBackendImplementation> gBackendImplementation;
  470. /** @class FIRAuthBackendRPCImplementation
  471. @brief The default RPC-based backend implementation.
  472. */
  473. @interface FIRAuthBackendRPCImplementation : NSObject <FIRAuthBackendImplementation>
  474. /** @property RPCIssuer
  475. @brief An instance of FIRAuthBackendRPCIssuer for making RPC requests. Allows the RPC
  476. requests/responses to be easily faked.
  477. */
  478. @property(nonatomic, strong) id<FIRAuthBackendRPCIssuer> RPCIssuer;
  479. @end
  480. @implementation FIRAuthBackend
  481. + (id<FIRAuthBackendImplementation>)implementation {
  482. if (!gBackendImplementation) {
  483. gBackendImplementation = [[FIRAuthBackendRPCImplementation alloc] init];
  484. }
  485. return gBackendImplementation;
  486. }
  487. + (void)setBackendImplementation:(id<FIRAuthBackendImplementation>)backendImplementation {
  488. gBackendImplementation = backendImplementation;
  489. }
  490. + (void)setDefaultBackendImplementationWithRPCIssuer:
  491. (nullable id<FIRAuthBackendRPCIssuer>)RPCIssuer {
  492. FIRAuthBackendRPCImplementation *defaultImplementation =
  493. [[FIRAuthBackendRPCImplementation alloc] init];
  494. if (RPCIssuer) {
  495. defaultImplementation.RPCIssuer = RPCIssuer;
  496. }
  497. gBackendImplementation = defaultImplementation;
  498. }
  499. + (void)createAuthURI:(FIRCreateAuthURIRequest *)request
  500. callback:(FIRCreateAuthURIResponseCallback)callback {
  501. [[self implementation] createAuthURI:request callback:callback];
  502. }
  503. + (void)getAccountInfo:(FIRGetAccountInfoRequest *)request
  504. callback:(FIRGetAccountInfoResponseCallback)callback {
  505. [[self implementation] getAccountInfo:request callback:callback];
  506. }
  507. + (void)getProjectConfig:(FIRGetProjectConfigRequest *)request
  508. callback:(FIRGetProjectConfigResponseCallback)callback {
  509. [[self implementation] getProjectConfig:request callback:callback];
  510. }
  511. + (void)setAccountInfo:(FIRSetAccountInfoRequest *)request
  512. callback:(FIRSetAccountInfoResponseCallback)callback {
  513. [[self implementation] setAccountInfo:request callback:callback];
  514. }
  515. + (void)verifyAssertion:(FIRVerifyAssertionRequest *)request
  516. callback:(FIRVerifyAssertionResponseCallback)callback {
  517. [[self implementation] verifyAssertion:request callback:callback];
  518. }
  519. + (void)verifyCustomToken:(FIRVerifyCustomTokenRequest *)request
  520. callback:(FIRVerifyCustomTokenResponseCallback)callback {
  521. [[self implementation] verifyCustomToken:request callback:callback];
  522. }
  523. + (void)verifyPassword:(FIRVerifyPasswordRequest *)request
  524. callback:(FIRVerifyPasswordResponseCallback)callback {
  525. [[self implementation] verifyPassword:request callback:callback];
  526. }
  527. + (void)emailLinkSignin:(FIREmailLinkSignInRequest *)request
  528. callback:(FIREmailLinkSigninResponseCallback)callback {
  529. [[self implementation] emailLinkSignin:request callback:callback];
  530. }
  531. + (void)secureToken:(FIRSecureTokenRequest *)request
  532. callback:(FIRSecureTokenResponseCallback)callback {
  533. [[self implementation] secureToken:request callback:callback];
  534. }
  535. + (void)getOOBConfirmationCode:(FIRGetOOBConfirmationCodeRequest *)request
  536. callback:(FIRGetOOBConfirmationCodeResponseCallback)callback {
  537. [[self implementation] getOOBConfirmationCode:request callback:callback];
  538. }
  539. + (void)signUpNewUser:(FIRSignUpNewUserRequest *)request
  540. callback:(FIRSignupNewUserCallback)callback {
  541. [[self implementation] signUpNewUser:request callback:callback];
  542. }
  543. + (void)deleteAccount:(FIRDeleteAccountRequest *)request callback:(FIRDeleteCallBack)callback {
  544. [[self implementation] deleteAccount:request callback:callback];
  545. }
  546. + (void)signInWithGameCenter:(FIRSignInWithGameCenterRequest *)request
  547. callback:(FIRSignInWithGameCenterResponseCallback)callback {
  548. [[self implementation] signInWithGameCenter:request callback:callback];
  549. }
  550. #if TARGET_OS_IOS
  551. + (void)sendVerificationCode:(FIRSendVerificationCodeRequest *)request
  552. callback:(FIRSendVerificationCodeResponseCallback)callback {
  553. [[self implementation] sendVerificationCode:request callback:callback];
  554. }
  555. + (void)verifyPhoneNumber:(FIRVerifyPhoneNumberRequest *)request
  556. callback:(FIRVerifyPhoneNumberResponseCallback)callback {
  557. [[self implementation] verifyPhoneNumber:request callback:callback];
  558. }
  559. + (void)verifyClient:(id)request callback:(FIRVerifyClientResponseCallback)callback {
  560. [[self implementation] verifyClient:request callback:callback];
  561. }
  562. #endif
  563. + (void)revokeToken:(FIRRevokeTokenRequest *)request
  564. callback:(FIRRevokeTokenResponseCallback)callback {
  565. [[self implementation] revokeToken:request callback:callback];
  566. }
  567. + (void)resetPassword:(FIRResetPasswordRequest *)request
  568. callback:(FIRResetPasswordCallback)callback {
  569. [[self implementation] resetPassword:request callback:callback];
  570. }
  571. + (void)getRecaptchaConfig:(FIRGetRecaptchaConfigRequest *)request
  572. callback:(FIRGetRecaptchaConfigResponseCallback)callback {
  573. [[self implementation] getRecaptchaConfig:request callback:callback];
  574. }
  575. + (NSString *)authUserAgent {
  576. return [NSString stringWithFormat:@"FirebaseAuth.iOS/%@ %@", FIRFirebaseVersion(),
  577. GTMFetcherStandardUserAgentString(nil)];
  578. }
  579. + (void)requestWithURL:(NSURL *)URL
  580. contentType:(NSString *)contentType
  581. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration
  582. completionHandler:(void (^)(NSMutableURLRequest *_Nullable))completionHandler {
  583. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
  584. [request setValue:contentType forHTTPHeaderField:@"Content-Type"];
  585. NSString *additionalFrameworkMarker =
  586. requestConfiguration.additionalFrameworkMarker ?: kFirebaseAuthCoreFrameworkMarker;
  587. NSString *clientVersion = [NSString
  588. stringWithFormat:@"iOS/FirebaseSDK/%@/%@", FIRFirebaseVersion(), additionalFrameworkMarker];
  589. [request setValue:clientVersion forHTTPHeaderField:kClientVersionHeader];
  590. NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];
  591. [request setValue:bundleID forHTTPHeaderField:kIosBundleIdentifierHeader];
  592. NSString *appID = requestConfiguration.appID;
  593. [request setValue:appID forHTTPHeaderField:kFirebaseAppIDHeader];
  594. [request setValue:FIRHeaderValueFromHeartbeatsPayload(
  595. [requestConfiguration.heartbeatLogger flushHeartbeatsIntoPayload])
  596. forHTTPHeaderField:kFirebaseHeartbeatHeader];
  597. NSString *HTTPMethod = requestConfiguration.HTTPMethod;
  598. [request setValue:HTTPMethod forKey:@"HTTPMethod"];
  599. NSArray<NSString *> *preferredLocalizations = [NSBundle mainBundle].preferredLocalizations;
  600. if (preferredLocalizations.count) {
  601. NSString *acceptLanguage = preferredLocalizations.firstObject;
  602. [request setValue:acceptLanguage forHTTPHeaderField:@"Accept-Language"];
  603. }
  604. NSString *languageCode = requestConfiguration.languageCode;
  605. if (languageCode.length) {
  606. [request setValue:languageCode forHTTPHeaderField:kFirebaseLocalHeader];
  607. }
  608. if (requestConfiguration.appCheck) {
  609. [requestConfiguration.appCheck
  610. getTokenForcingRefresh:false
  611. completion:^(id<FIRAppCheckTokenResultInterop> _Nonnull tokenResult) {
  612. if (tokenResult.error) {
  613. FIRLogWarning(kFIRLoggerAuth, @"I-AUT000018",
  614. @"Error getting App Check token; using placeholder token "
  615. @"instead. Error: %@",
  616. tokenResult.error);
  617. }
  618. [request setValue:tokenResult.token
  619. forHTTPHeaderField:@"X-Firebase-AppCheck"];
  620. completionHandler(request);
  621. }];
  622. } else {
  623. completionHandler(request);
  624. }
  625. }
  626. @end
  627. @interface FIRAuthBackendRPCIssuerImplementation : NSObject <FIRAuthBackendRPCIssuer>
  628. @end
  629. @implementation FIRAuthBackendRPCIssuerImplementation {
  630. /** @var The session fetcher service.
  631. */
  632. GTMSessionFetcherService *_fetcherService;
  633. }
  634. - (instancetype)init {
  635. self = [super init];
  636. if (self) {
  637. _fetcherService = [[GTMSessionFetcherService alloc] init];
  638. _fetcherService.userAgent = [FIRAuthBackend authUserAgent];
  639. _fetcherService.callbackQueue = FIRAuthGlobalWorkQueue();
  640. // Avoid reusing the session to prevent
  641. // https://github.com/firebase/firebase-ios-sdk/issues/1261
  642. _fetcherService.reuseSession = NO;
  643. }
  644. return self;
  645. }
  646. - (void)asyncCallToURLWithRequestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration
  647. URL:(NSURL *)URL
  648. body:(nullable NSData *)body
  649. contentType:(NSString *)contentType
  650. completionHandler:
  651. (void (^)(NSData *_Nullable, NSError *_Nullable))handler {
  652. [FIRAuthBackend requestWithURL:URL
  653. contentType:contentType
  654. requestConfiguration:requestConfiguration
  655. completionHandler:^(NSMutableURLRequest *request) {
  656. GTMSessionFetcher *fetcher = [self->_fetcherService fetcherWithRequest:request];
  657. NSString *emulatorHostAndPort = requestConfiguration.emulatorHostAndPort;
  658. if (emulatorHostAndPort) {
  659. fetcher.allowLocalhostRequest = YES;
  660. fetcher.allowedInsecureSchemes = @[ @"http" ];
  661. }
  662. fetcher.bodyData = body;
  663. [fetcher beginFetchWithCompletionHandler:handler];
  664. }];
  665. }
  666. @end
  667. @implementation FIRAuthBackendRPCImplementation
  668. - (instancetype)init {
  669. self = [super init];
  670. if (self) {
  671. _RPCIssuer = [[FIRAuthBackendRPCIssuerImplementation alloc] init];
  672. }
  673. return self;
  674. }
  675. - (void)createAuthURI:(FIRCreateAuthURIRequest *)request
  676. callback:(FIRCreateAuthURIResponseCallback)callback {
  677. FIRCreateAuthURIResponse *response = [[FIRCreateAuthURIResponse alloc] init];
  678. [self callWithRequest:request
  679. response:response
  680. callback:^(NSError *error) {
  681. if (error) {
  682. callback(nil, error);
  683. } else {
  684. callback(response, nil);
  685. }
  686. }];
  687. }
  688. - (void)getAccountInfo:(FIRGetAccountInfoRequest *)request
  689. callback:(FIRGetAccountInfoResponseCallback)callback {
  690. FIRGetAccountInfoResponse *response = [[FIRGetAccountInfoResponse alloc] init];
  691. [self callWithRequest:request
  692. response:response
  693. callback:^(NSError *error) {
  694. if (error) {
  695. callback(nil, error);
  696. } else {
  697. callback(response, nil);
  698. }
  699. }];
  700. }
  701. - (void)getProjectConfig:(FIRGetProjectConfigRequest *)request
  702. callback:(FIRGetProjectConfigResponseCallback)callback {
  703. FIRGetProjectConfigResponse *response = [[FIRGetProjectConfigResponse alloc] init];
  704. [self callWithRequest:request
  705. response:response
  706. callback:^(NSError *error) {
  707. if (error) {
  708. callback(nil, error);
  709. } else {
  710. callback(response, nil);
  711. }
  712. }];
  713. }
  714. - (void)setAccountInfo:(FIRSetAccountInfoRequest *)request
  715. callback:(FIRSetAccountInfoResponseCallback)callback {
  716. FIRSetAccountInfoResponse *response = [[FIRSetAccountInfoResponse alloc] init];
  717. [self callWithRequest:request
  718. response:response
  719. callback:^(NSError *error) {
  720. if (error) {
  721. callback(nil, error);
  722. } else {
  723. callback(response, nil);
  724. }
  725. }];
  726. }
  727. - (void)verifyAssertion:(FIRVerifyAssertionRequest *)request
  728. callback:(FIRVerifyAssertionResponseCallback)callback {
  729. FIRVerifyAssertionResponse *response = [[FIRVerifyAssertionResponse alloc] init];
  730. [self
  731. callWithRequest:request
  732. response:response
  733. callback:^(NSError *error) {
  734. if (error) {
  735. callback(nil, error);
  736. } else {
  737. if (!response.IDToken && response.MFAInfo) {
  738. #if TARGET_OS_IOS
  739. NSMutableArray<FIRMultiFactorInfo *> *multiFactorInfoArray =
  740. [[NSMutableArray alloc] init];
  741. for (FIRAuthProtoMFAEnrollment *MFAEnrollment in response.MFAInfo) {
  742. if (MFAEnrollment.phoneInfo) {
  743. FIRMultiFactorInfo *multiFactorInfo =
  744. [[FIRPhoneMultiFactorInfo alloc] initWithProto:MFAEnrollment];
  745. [multiFactorInfoArray addObject:multiFactorInfo];
  746. } else if (MFAEnrollment.TOTPInfo) {
  747. FIRMultiFactorInfo *multiFactorInfo =
  748. [[FIRTOTPMultiFactorInfo alloc] initWithProto:MFAEnrollment];
  749. [multiFactorInfoArray addObject:multiFactorInfo];
  750. } else {
  751. FIRLogError(kFIRLoggerAuth, @"I-AUT000020",
  752. @"Multifactor type is not supported");
  753. }
  754. }
  755. NSError *multiFactorRequiredError = [FIRAuthErrorUtils
  756. secondFactorRequiredErrorWithPendingCredential:response.MFAPendingCredential
  757. hints:multiFactorInfoArray
  758. auth:request.requestConfiguration
  759. .auth];
  760. callback(nil, multiFactorRequiredError);
  761. #endif
  762. } else {
  763. callback(response, nil);
  764. }
  765. }
  766. }];
  767. }
  768. - (void)verifyCustomToken:(FIRVerifyCustomTokenRequest *)request
  769. callback:(FIRVerifyCustomTokenResponseCallback)callback {
  770. FIRVerifyCustomTokenResponse *response = [[FIRVerifyCustomTokenResponse alloc] init];
  771. [self callWithRequest:request
  772. response:response
  773. callback:^(NSError *error) {
  774. if (error) {
  775. callback(nil, error);
  776. } else {
  777. callback(response, nil);
  778. }
  779. }];
  780. }
  781. - (void)verifyPassword:(FIRVerifyPasswordRequest *)request
  782. callback:(FIRVerifyPasswordResponseCallback)callback {
  783. FIRVerifyPasswordResponse *response = [[FIRVerifyPasswordResponse alloc] init];
  784. [self
  785. callWithRequest:request
  786. response:response
  787. callback:^(NSError *error) {
  788. if (error) {
  789. callback(nil, error);
  790. } else {
  791. if (!response.IDToken && response.MFAInfo) {
  792. #if TARGET_OS_IOS
  793. NSMutableArray<FIRMultiFactorInfo *> *multiFactorInfo = [NSMutableArray array];
  794. for (FIRAuthProtoMFAEnrollment *MFAEnrollment in response.MFAInfo) {
  795. // check which MFA factors are enabled.
  796. if (MFAEnrollment.phoneInfo != nil) {
  797. FIRPhoneMultiFactorInfo *info =
  798. [[FIRPhoneMultiFactorInfo alloc] initWithProto:MFAEnrollment];
  799. [multiFactorInfo addObject:info];
  800. } else if (MFAEnrollment.TOTPInfo != nil) {
  801. FIRTOTPMultiFactorInfo *info =
  802. [[FIRTOTPMultiFactorInfo alloc] initWithProto:MFAEnrollment];
  803. [multiFactorInfo addObject:info];
  804. } else {
  805. FIRLogError(kFIRLoggerAuth, @"I-AUT000021",
  806. @"Multifactor type is not supported");
  807. }
  808. }
  809. NSError *multiFactorRequiredError = [FIRAuthErrorUtils
  810. secondFactorRequiredErrorWithPendingCredential:response.MFAPendingCredential
  811. hints:multiFactorInfo
  812. auth:request.requestConfiguration
  813. .auth];
  814. callback(nil, multiFactorRequiredError);
  815. #endif
  816. } else {
  817. callback(response, nil);
  818. }
  819. }
  820. }];
  821. }
  822. - (void)emailLinkSignin:(FIREmailLinkSignInRequest *)request
  823. callback:(FIREmailLinkSigninResponseCallback)callback {
  824. FIREmailLinkSignInResponse *response = [[FIREmailLinkSignInResponse alloc] init];
  825. [self
  826. callWithRequest:request
  827. response:response
  828. callback:^(NSError *error) {
  829. if (error) {
  830. callback(nil, error);
  831. } else {
  832. if (!response.IDToken && response.MFAInfo) {
  833. #if TARGET_OS_IOS
  834. NSMutableArray<FIRMultiFactorInfo *> *multiFactorInfoArray =
  835. [[NSMutableArray alloc] init];
  836. for (FIRAuthProtoMFAEnrollment *MFAEnrollment in response.MFAInfo) {
  837. if (MFAEnrollment.phoneInfo) {
  838. FIRMultiFactorInfo *multiFactorInfo =
  839. [[FIRPhoneMultiFactorInfo alloc] initWithProto:MFAEnrollment];
  840. [multiFactorInfoArray addObject:multiFactorInfo];
  841. } else if (MFAEnrollment.TOTPInfo) {
  842. FIRMultiFactorInfo *multiFactorInfo =
  843. [[FIRTOTPMultiFactorInfo alloc] initWithProto:MFAEnrollment];
  844. [multiFactorInfoArray addObject:multiFactorInfo];
  845. } else {
  846. FIRLogError(kFIRLoggerAuth, @"I-AUT000022",
  847. @"Multifactor type is not supported");
  848. }
  849. }
  850. NSError *multiFactorRequiredError = [FIRAuthErrorUtils
  851. secondFactorRequiredErrorWithPendingCredential:response.MFAPendingCredential
  852. hints:multiFactorInfoArray
  853. auth:request.requestConfiguration
  854. .auth];
  855. callback(nil, multiFactorRequiredError);
  856. #endif
  857. } else {
  858. callback(response, nil);
  859. }
  860. }
  861. }];
  862. }
  863. - (void)secureToken:(FIRSecureTokenRequest *)request
  864. callback:(FIRSecureTokenResponseCallback)callback {
  865. FIRSecureTokenResponse *response = [[FIRSecureTokenResponse alloc] init];
  866. [self callWithRequest:request
  867. response:response
  868. callback:^(NSError *error) {
  869. if (error) {
  870. callback(nil, error);
  871. } else {
  872. callback(response, nil);
  873. }
  874. }];
  875. }
  876. - (void)getOOBConfirmationCode:(FIRGetOOBConfirmationCodeRequest *)request
  877. callback:(FIRGetOOBConfirmationCodeResponseCallback)callback {
  878. FIRGetOOBConfirmationCodeResponse *response = [[FIRGetOOBConfirmationCodeResponse alloc] init];
  879. [self callWithRequest:request
  880. response:response
  881. callback:^(NSError *error) {
  882. if (error) {
  883. callback(nil, error);
  884. } else {
  885. callback(response, nil);
  886. }
  887. }];
  888. }
  889. - (void)signUpNewUser:(FIRSignUpNewUserRequest *)request
  890. callback:(FIRSignupNewUserCallback)callback {
  891. FIRSignUpNewUserResponse *response = [[FIRSignUpNewUserResponse alloc] init];
  892. [self callWithRequest:request
  893. response:response
  894. callback:^(NSError *error) {
  895. if (error) {
  896. callback(nil, error);
  897. } else {
  898. callback(response, nil);
  899. }
  900. }];
  901. }
  902. - (void)deleteAccount:(FIRDeleteAccountRequest *)request callback:(FIRDeleteCallBack)callback {
  903. FIRDeleteAccountResponse *response = [[FIRDeleteAccountResponse alloc] init];
  904. [self callWithRequest:request response:response callback:callback];
  905. }
  906. #if TARGET_OS_IOS
  907. - (void)sendVerificationCode:(FIRSendVerificationCodeRequest *)request
  908. callback:(FIRSendVerificationCodeResponseCallback)callback {
  909. FIRSendVerificationCodeResponse *response = [[FIRSendVerificationCodeResponse alloc] init];
  910. [self callWithRequest:request
  911. response:response
  912. callback:^(NSError *error) {
  913. if (error) {
  914. callback(nil, error);
  915. } else {
  916. callback(response, error);
  917. }
  918. }];
  919. }
  920. - (void)verifyPhoneNumber:(FIRVerifyPhoneNumberRequest *)request
  921. callback:(FIRVerifyPhoneNumberResponseCallback)callback {
  922. FIRVerifyPhoneNumberResponse *response = [[FIRVerifyPhoneNumberResponse alloc] init];
  923. [self
  924. callWithRequest:request
  925. response:response
  926. callback:^(NSError *error) {
  927. if (error) {
  928. callback(nil, error);
  929. return;
  930. }
  931. // Check whether or not the successful response is actually the special case phone
  932. // auth flow that returns a temporary proof and phone number.
  933. if (response.phoneNumber.length && response.temporaryProof.length) {
  934. FIRPhoneAuthCredential *credential =
  935. [[FIRPhoneAuthCredential alloc] initWithTemporaryProof:response.temporaryProof
  936. phoneNumber:response.phoneNumber
  937. providerID:FIRPhoneAuthProviderID];
  938. callback(nil, [FIRAuthErrorUtils credentialAlreadyInUseErrorWithMessage:nil
  939. credential:credential
  940. email:nil]);
  941. return;
  942. }
  943. callback(response, nil);
  944. }];
  945. }
  946. - (void)verifyClient:(id)request callback:(FIRVerifyClientResponseCallback)callback {
  947. FIRVerifyClientResponse *response = [[FIRVerifyClientResponse alloc] init];
  948. [self callWithRequest:request
  949. response:response
  950. callback:^(NSError *error) {
  951. if (error) {
  952. callback(nil, error);
  953. return;
  954. }
  955. callback(response, nil);
  956. }];
  957. }
  958. #endif
  959. - (void)revokeToken:(FIRRevokeTokenRequest *)request
  960. callback:(FIRRevokeTokenResponseCallback)callback {
  961. FIRRevokeTokenResponse *response = [[FIRRevokeTokenResponse alloc] init];
  962. [self
  963. callWithRequest:request
  964. response:response
  965. callback:^(NSError *error) {
  966. if (error) {
  967. callback(nil, [FIRAuthErrorUtils
  968. invalidCredentialErrorWithMessage:[error localizedDescription]]);
  969. return;
  970. }
  971. callback(response, nil);
  972. }];
  973. }
  974. - (void)resetPassword:(FIRResetPasswordRequest *)request
  975. callback:(FIRResetPasswordCallback)callback {
  976. FIRResetPasswordResponse *response = [[FIRResetPasswordResponse alloc] init];
  977. [self callWithRequest:request
  978. response:response
  979. callback:^(NSError *error) {
  980. if (error) {
  981. callback(nil, error);
  982. return;
  983. }
  984. callback(response, nil);
  985. }];
  986. }
  987. - (void)signInWithGameCenter:(FIRSignInWithGameCenterRequest *)request
  988. callback:(FIRSignInWithGameCenterResponseCallback)callback {
  989. FIRSignInWithGameCenterResponse *response = [[FIRSignInWithGameCenterResponse alloc] init];
  990. [self callWithRequest:request
  991. response:response
  992. callback:^(NSError *error) {
  993. if (error) {
  994. if (callback) {
  995. callback(nil, error);
  996. }
  997. } else {
  998. if (callback) {
  999. callback(response, nil);
  1000. }
  1001. }
  1002. }];
  1003. }
  1004. - (void)getRecaptchaConfig:(FIRGetRecaptchaConfigRequest *)request
  1005. callback:(FIRGetRecaptchaConfigResponseCallback)callback {
  1006. FIRGetRecaptchaConfigResponse *response = [[FIRGetRecaptchaConfigResponse alloc] init];
  1007. [self callWithRequest:request
  1008. response:response
  1009. callback:^(NSError *error) {
  1010. if (error) {
  1011. if (callback) {
  1012. callback(nil, error);
  1013. }
  1014. } else {
  1015. if (callback) {
  1016. callback(response, nil);
  1017. }
  1018. }
  1019. }];
  1020. }
  1021. #pragma mark - Generic RPC handling methods
  1022. /** @fn callWithRequest:response:callback:
  1023. @brief Calls the RPC using HTTP request.
  1024. @remarks Possible error responses:
  1025. @see FIRAuthInternalErrorCodeRPCRequestEncodingError
  1026. @see FIRAuthInternalErrorCodeJSONSerializationError
  1027. @see FIRAuthInternalErrorCodeNetworkError
  1028. @see FIRAuthInternalErrorCodeUnexpectedErrorResponse
  1029. @see FIRAuthInternalErrorCodeUnexpectedResponse
  1030. @see FIRAuthInternalErrorCodeRPCResponseDecodingError
  1031. @param request The request.
  1032. @param response The empty response to be filled.
  1033. @param callback The callback for both success and failure.
  1034. */
  1035. - (void)callWithRequest:(id<FIRAuthRPCRequest>)request
  1036. response:(id<FIRAuthRPCResponse>)response
  1037. callback:(void (^)(NSError *_Nullable error))callback {
  1038. NSError *error;
  1039. NSData *bodyData;
  1040. if ([request containsPostBody]) {
  1041. id postBody = [request unencodedHTTPRequestBodyWithError:&error];
  1042. if (!postBody) {
  1043. callback([FIRAuthErrorUtils RPCRequestEncodingErrorWithUnderlyingError:error]);
  1044. return;
  1045. }
  1046. NSJSONWritingOptions JSONWritingOptions = 0;
  1047. #if DEBUG
  1048. JSONWritingOptions |= NSJSONWritingPrettyPrinted;
  1049. #endif
  1050. if ([NSJSONSerialization isValidJSONObject:postBody]) {
  1051. bodyData = [NSJSONSerialization dataWithJSONObject:postBody
  1052. options:JSONWritingOptions
  1053. error:&error];
  1054. if (!bodyData) {
  1055. // This is an untested case. This happens exclusively when there is an error in the
  1056. // framework implementation of dataWithJSONObject:options:error:. This shouldn't normally
  1057. // occur as isValidJSONObject: should return NO in any case we should encounter an error.
  1058. error = [FIRAuthErrorUtils JSONSerializationErrorWithUnderlyingError:error];
  1059. }
  1060. } else {
  1061. error = [FIRAuthErrorUtils JSONSerializationErrorForUnencodableType];
  1062. }
  1063. if (!bodyData) {
  1064. callback(error);
  1065. return;
  1066. }
  1067. }
  1068. [_RPCIssuer
  1069. asyncCallToURLWithRequestConfiguration:[request requestConfiguration]
  1070. URL:[request requestURL]
  1071. body:bodyData
  1072. contentType:kJSONContentType
  1073. completionHandler:^(NSData *data, NSError *error) {
  1074. // If there is an error with no body data at all, then this must be a
  1075. // network error.
  1076. if (error && !data) {
  1077. callback([FIRAuthErrorUtils networkErrorWithUnderlyingError:error]);
  1078. return;
  1079. }
  1080. // Try to decode the HTTP response data which may contain either a
  1081. // successful response or error message.
  1082. NSError *jsonError;
  1083. NSDictionary *dictionary =
  1084. [NSJSONSerialization JSONObjectWithData:data
  1085. options:NSJSONReadingMutableLeaves
  1086. error:&jsonError];
  1087. if (!dictionary) {
  1088. if (error) {
  1089. // We have an error, but we couldn't decode the body, so we have no
  1090. // additional information other than the raw response and the
  1091. // original NSError (the jsonError is infered by the error code
  1092. // (FIRAuthErrorCodeUnexpectedHTTPResponse, and is irrelevant.)
  1093. callback([FIRAuthErrorUtils
  1094. unexpectedErrorResponseWithData:data
  1095. underlyingError:error]);
  1096. } else {
  1097. // This is supposed to be a "successful" response, but we couldn't
  1098. // deserialize the body.
  1099. callback([FIRAuthErrorUtils unexpectedResponseWithData:data
  1100. underlyingError:jsonError]);
  1101. }
  1102. return;
  1103. }
  1104. if (![dictionary isKindOfClass:[NSDictionary class]]) {
  1105. if (error) {
  1106. callback([FIRAuthErrorUtils
  1107. unexpectedErrorResponseWithDeserializedResponse:dictionary
  1108. underlyingError:error]);
  1109. } else {
  1110. callback([FIRAuthErrorUtils
  1111. unexpectedResponseWithDeserializedResponse:dictionary]);
  1112. }
  1113. return;
  1114. }
  1115. // At this point we either have an error with successfully decoded
  1116. // details in the body, or we have a response which must pass further
  1117. // validation before we know it's truly successful. We deal with the
  1118. // case where we have an error with successfully decoded error details
  1119. // first:
  1120. if (error) {
  1121. NSDictionary *errorDictionary = dictionary[kErrorKey];
  1122. if ([errorDictionary isKindOfClass:[NSDictionary class]]) {
  1123. id<NSObject> errorMessage = errorDictionary[kErrorMessageKey];
  1124. if ([errorMessage isKindOfClass:[NSString class]]) {
  1125. NSString *errorMessageString = (NSString *)errorMessage;
  1126. // Contruct client error.
  1127. NSError *clientError = [[self class]
  1128. clientErrorWithServerErrorMessage:errorMessageString
  1129. errorDictionary:errorDictionary
  1130. response:response];
  1131. if (clientError) {
  1132. callback(clientError);
  1133. return;
  1134. }
  1135. }
  1136. // Not a message we know, return the message directly.
  1137. if (errorMessage) {
  1138. NSError *unexpecterErrorResponse = [FIRAuthErrorUtils
  1139. unexpectedErrorResponseWithDeserializedResponse:
  1140. errorDictionary
  1141. underlyingError:error];
  1142. callback(unexpecterErrorResponse);
  1143. return;
  1144. }
  1145. }
  1146. // No error message at all, return the decoded response.
  1147. callback([FIRAuthErrorUtils
  1148. unexpectedErrorResponseWithDeserializedResponse:dictionary
  1149. underlyingError:error]);
  1150. return;
  1151. }
  1152. // Finally, we try to populate the response object with the JSON
  1153. // values.
  1154. if (![response setWithDictionary:dictionary error:&error]) {
  1155. callback([FIRAuthErrorUtils
  1156. RPCResponseDecodingErrorWithDeserializedResponse:dictionary
  1157. underlyingError:error]);
  1158. return;
  1159. }
  1160. // In case returnIDPCredential of a verifyAssertion request is set to
  1161. // @YES, the server may return a 200 with a response that may contain a
  1162. // server error.
  1163. if ([request isKindOfClass:[FIRVerifyAssertionRequest class]]) {
  1164. FIRVerifyAssertionRequest *verifyAssertionRequest =
  1165. (FIRVerifyAssertionRequest *)request;
  1166. if (verifyAssertionRequest.returnIDPCredential) {
  1167. NSString *errorMessage =
  1168. dictionary[kReturnIDPCredentialErrorMessageKey];
  1169. if ([errorMessage isKindOfClass:[NSString class]]) {
  1170. NSString *errorString = (NSString *)errorMessage;
  1171. NSError *clientError =
  1172. [[self class] clientErrorWithServerErrorMessage:errorString
  1173. errorDictionary:@{}
  1174. response:response];
  1175. if (clientError) {
  1176. callback(clientError);
  1177. return;
  1178. }
  1179. }
  1180. }
  1181. }
  1182. // Success! The response object originally passed in can be used by the
  1183. // caller.
  1184. callback(nil);
  1185. }];
  1186. }
  1187. /** @fn clientErrorWithServerErrorMessage:errorDictionary:
  1188. @brief Translates known server errors to client errors.
  1189. @param serverErrorMessage The error message from the server.
  1190. @param errorDictionary The error part of the response from the server.
  1191. @param response The response from the server RPC.
  1192. @return A client error, if any.
  1193. */
  1194. + (nullable NSError *)clientErrorWithServerErrorMessage:(NSString *)serverErrorMessage
  1195. errorDictionary:(NSDictionary *)errorDictionary
  1196. response:(id<FIRAuthRPCResponse>)response {
  1197. NSString *shortErrorMessage = serverErrorMessage;
  1198. NSString *serverDetailErrorMessage;
  1199. NSRange colonRange = [serverErrorMessage rangeOfString:@":"];
  1200. if (colonRange.location != NSNotFound) {
  1201. shortErrorMessage = [serverErrorMessage substringToIndex:colonRange.location];
  1202. shortErrorMessage =
  1203. [shortErrorMessage stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
  1204. serverDetailErrorMessage = [serverErrorMessage substringFromIndex:colonRange.location + 1];
  1205. serverDetailErrorMessage = [serverDetailErrorMessage
  1206. stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
  1207. }
  1208. // Delegate the responsibility for constructing the client error to the response object,
  1209. // if possible.
  1210. SEL clientErrorWithServerErrorMessageSelector = @selector(clientErrorWithShortErrorMessage:
  1211. detailErrorMessage:);
  1212. if ([response respondsToSelector:clientErrorWithServerErrorMessageSelector]) {
  1213. NSError *error = [response clientErrorWithShortErrorMessage:shortErrorMessage
  1214. detailErrorMessage:serverDetailErrorMessage];
  1215. if (error) {
  1216. return error;
  1217. }
  1218. }
  1219. if ([shortErrorMessage isEqualToString:kUserNotFoundErrorMessage]) {
  1220. return [FIRAuthErrorUtils userNotFoundErrorWithMessage:serverDetailErrorMessage];
  1221. }
  1222. if ([shortErrorMessage isEqualToString:kUserDeletedErrorMessage]) {
  1223. return [FIRAuthErrorUtils userNotFoundErrorWithMessage:serverDetailErrorMessage];
  1224. }
  1225. if ([shortErrorMessage isEqualToString:kInvalidLocalIDErrorMessage]) {
  1226. // This case shouldn't be necessary but it is for now: b/27908364 .
  1227. return [FIRAuthErrorUtils userNotFoundErrorWithMessage:serverDetailErrorMessage];
  1228. }
  1229. if ([shortErrorMessage isEqualToString:kUserTokenExpiredErrorMessage]) {
  1230. return [FIRAuthErrorUtils userTokenExpiredErrorWithMessage:serverDetailErrorMessage];
  1231. }
  1232. if ([shortErrorMessage isEqualToString:kTooManyRequestsErrorMessage]) {
  1233. return [FIRAuthErrorUtils tooManyRequestsErrorWithMessage:serverDetailErrorMessage];
  1234. }
  1235. if ([shortErrorMessage isEqualToString:kInvalidCustomTokenErrorMessage]) {
  1236. return [FIRAuthErrorUtils invalidCustomTokenErrorWithMessage:serverDetailErrorMessage];
  1237. }
  1238. if ([shortErrorMessage isEqualToString:kCustomTokenMismatch]) {
  1239. return [FIRAuthErrorUtils customTokenMistmatchErrorWithMessage:serverDetailErrorMessage];
  1240. }
  1241. if ([shortErrorMessage isEqualToString:kInvalidCredentialErrorMessage] ||
  1242. [shortErrorMessage isEqualToString:kInvalidPendingToken] ||
  1243. [shortErrorMessage isEqualToString:kInvalidLoginCredentials]) {
  1244. return [FIRAuthErrorUtils invalidCredentialErrorWithMessage:serverDetailErrorMessage];
  1245. }
  1246. if ([shortErrorMessage isEqualToString:kUserDisabledErrorMessage]) {
  1247. return [FIRAuthErrorUtils userDisabledErrorWithMessage:serverDetailErrorMessage];
  1248. }
  1249. if ([shortErrorMessage isEqualToString:kOperationNotAllowedErrorMessage]) {
  1250. return [FIRAuthErrorUtils operationNotAllowedErrorWithMessage:serverDetailErrorMessage];
  1251. }
  1252. if ([shortErrorMessage isEqualToString:kPasswordLoginDisabledErrorMessage]) {
  1253. return [FIRAuthErrorUtils operationNotAllowedErrorWithMessage:serverDetailErrorMessage];
  1254. }
  1255. if ([shortErrorMessage isEqualToString:kEmailAlreadyInUseErrorMessage]) {
  1256. return [FIRAuthErrorUtils emailAlreadyInUseErrorWithEmail:nil];
  1257. }
  1258. if ([shortErrorMessage isEqualToString:kInvalidEmailErrorMessage]) {
  1259. return [FIRAuthErrorUtils invalidEmailErrorWithMessage:serverDetailErrorMessage];
  1260. }
  1261. // "INVALID_IDENTIFIER" can be returned by createAuthURI RPC. Considering email addresses are
  1262. // currently the only identifiers, we surface the FIRAuthErrorCodeInvalidEmail error code in this
  1263. // case.
  1264. if ([shortErrorMessage isEqualToString:kInvalidIdentifierErrorMessage]) {
  1265. return [FIRAuthErrorUtils invalidEmailErrorWithMessage:serverDetailErrorMessage];
  1266. }
  1267. if ([shortErrorMessage isEqualToString:kWrongPasswordErrorMessage]) {
  1268. return [FIRAuthErrorUtils wrongPasswordErrorWithMessage:serverDetailErrorMessage];
  1269. }
  1270. if ([shortErrorMessage isEqualToString:kCredentialTooOldErrorMessage]) {
  1271. return [FIRAuthErrorUtils requiresRecentLoginErrorWithMessage:serverDetailErrorMessage];
  1272. }
  1273. if ([shortErrorMessage isEqualToString:kInvalidUserTokenErrorMessage]) {
  1274. return [FIRAuthErrorUtils invalidUserTokenErrorWithMessage:serverDetailErrorMessage];
  1275. }
  1276. if ([shortErrorMessage isEqualToString:kFederatedUserIDAlreadyLinkedMessage]) {
  1277. FIROAuthCredential *credential;
  1278. NSString *email;
  1279. if ([response isKindOfClass:[FIRVerifyAssertionResponse class]]) {
  1280. FIRVerifyAssertionResponse *verifyAssertion = (FIRVerifyAssertionResponse *)response;
  1281. credential = [[FIROAuthCredential alloc] initWithVerifyAssertionResponse:verifyAssertion];
  1282. email = verifyAssertion.email;
  1283. }
  1284. return [FIRAuthErrorUtils credentialAlreadyInUseErrorWithMessage:serverDetailErrorMessage
  1285. credential:credential
  1286. email:email];
  1287. }
  1288. if ([shortErrorMessage isEqualToString:kWeakPasswordErrorMessagePrefix]) {
  1289. return [FIRAuthErrorUtils weakPasswordErrorWithServerResponseReason:serverDetailErrorMessage];
  1290. }
  1291. if ([shortErrorMessage isEqualToString:kExpiredActionCodeErrorMessage]) {
  1292. return [FIRAuthErrorUtils expiredActionCodeErrorWithMessage:serverDetailErrorMessage];
  1293. }
  1294. if ([shortErrorMessage isEqualToString:kInvalidActionCodeErrorMessage]) {
  1295. return [FIRAuthErrorUtils invalidActionCodeErrorWithMessage:serverDetailErrorMessage];
  1296. }
  1297. if ([shortErrorMessage isEqualToString:kMissingEmailErrorMessage]) {
  1298. return [FIRAuthErrorUtils missingEmailErrorWithMessage:serverDetailErrorMessage];
  1299. }
  1300. if ([shortErrorMessage isEqualToString:kInvalidSenderEmailErrorMessage]) {
  1301. return [FIRAuthErrorUtils invalidSenderErrorWithMessage:serverDetailErrorMessage];
  1302. }
  1303. if ([shortErrorMessage isEqualToString:kInvalidMessagePayloadErrorMessage]) {
  1304. return [FIRAuthErrorUtils invalidMessagePayloadErrorWithMessage:serverDetailErrorMessage];
  1305. }
  1306. if ([shortErrorMessage isEqualToString:kInvalidRecipientEmailErrorMessage]) {
  1307. return [FIRAuthErrorUtils invalidRecipientEmailErrorWithMessage:serverDetailErrorMessage];
  1308. }
  1309. if ([shortErrorMessage isEqualToString:kMissingIosBundleIDErrorMessage]) {
  1310. return [FIRAuthErrorUtils missingIosBundleIDErrorWithMessage:serverDetailErrorMessage];
  1311. }
  1312. if ([shortErrorMessage isEqualToString:kMissingAndroidPackageNameErrorMessage]) {
  1313. return [FIRAuthErrorUtils missingAndroidPackageNameErrorWithMessage:serverDetailErrorMessage];
  1314. }
  1315. if ([shortErrorMessage isEqualToString:kUnauthorizedDomainErrorMessage]) {
  1316. return [FIRAuthErrorUtils unauthorizedDomainErrorWithMessage:serverDetailErrorMessage];
  1317. }
  1318. if ([shortErrorMessage isEqualToString:kInvalidContinueURIErrorMessage]) {
  1319. return [FIRAuthErrorUtils invalidContinueURIErrorWithMessage:serverDetailErrorMessage];
  1320. }
  1321. if ([shortErrorMessage isEqualToString:kInvalidProviderIDErrorMessage]) {
  1322. return [FIRAuthErrorUtils invalidProviderIDErrorWithMessage:serverDetailErrorMessage];
  1323. }
  1324. if ([shortErrorMessage isEqualToString:kInvalidDynamicLinkDomainErrorMessage]) {
  1325. return [FIRAuthErrorUtils invalidDynamicLinkDomainErrorWithMessage:serverDetailErrorMessage];
  1326. }
  1327. if ([shortErrorMessage isEqualToString:kMissingContinueURIErrorMessage]) {
  1328. return [FIRAuthErrorUtils missingContinueURIErrorWithMessage:serverDetailErrorMessage];
  1329. }
  1330. if ([shortErrorMessage isEqualToString:kInvalidPhoneNumberErrorMessage]) {
  1331. return [FIRAuthErrorUtils invalidPhoneNumberErrorWithMessage:serverDetailErrorMessage];
  1332. }
  1333. if ([shortErrorMessage isEqualToString:kInvalidSessionInfoErrorMessage]) {
  1334. return [FIRAuthErrorUtils invalidVerificationIDErrorWithMessage:serverDetailErrorMessage];
  1335. }
  1336. if ([shortErrorMessage isEqualToString:kInvalidVerificationCodeErrorMessage]) {
  1337. return [FIRAuthErrorUtils invalidVerificationCodeErrorWithMessage:serverDetailErrorMessage];
  1338. }
  1339. if ([shortErrorMessage isEqualToString:kSessionExpiredErrorMessage]) {
  1340. return [FIRAuthErrorUtils sessionExpiredErrorWithMessage:serverDetailErrorMessage];
  1341. }
  1342. if ([shortErrorMessage isEqualToString:kMissingAppTokenErrorMessage]) {
  1343. return [FIRAuthErrorUtils missingAppTokenErrorWithUnderlyingError:nil];
  1344. }
  1345. if ([shortErrorMessage isEqualToString:kMissingAppCredentialErrorMessage]) {
  1346. return [FIRAuthErrorUtils missingAppCredentialWithMessage:serverDetailErrorMessage];
  1347. }
  1348. if ([shortErrorMessage isEqualToString:kInvalidAppCredentialErrorMessage]) {
  1349. return [FIRAuthErrorUtils invalidAppCredentialWithMessage:serverDetailErrorMessage];
  1350. }
  1351. if ([shortErrorMessage isEqualToString:kQuoutaExceededErrorMessage]) {
  1352. return [FIRAuthErrorUtils quotaExceededErrorWithMessage:serverErrorMessage];
  1353. }
  1354. if ([shortErrorMessage isEqualToString:kAppNotVerifiedErrorMessage]) {
  1355. return [FIRAuthErrorUtils appNotVerifiedErrorWithMessage:serverErrorMessage];
  1356. }
  1357. if ([shortErrorMessage isEqualToString:kMissingClientIdentifier]) {
  1358. return [FIRAuthErrorUtils missingClientIdentifierErrorWithMessage:serverErrorMessage];
  1359. }
  1360. if ([shortErrorMessage isEqualToString:kMissingClientType]) {
  1361. return [FIRAuthErrorUtils missingClientTypeErrorWithMessage:serverErrorMessage];
  1362. }
  1363. if ([shortErrorMessage isEqualToString:kCaptchaCheckFailedErrorMessage]) {
  1364. return [FIRAuthErrorUtils captchaCheckFailedErrorWithMessage:serverErrorMessage];
  1365. }
  1366. if ([shortErrorMessage isEqualToString:kMissingOrInvalidNonceErrorMessage]) {
  1367. return [FIRAuthErrorUtils missingOrInvalidNonceErrorWithMessage:serverDetailErrorMessage];
  1368. }
  1369. if ([shortErrorMessage isEqualToString:kMissingMFAPendingCredentialErrorMessage]) {
  1370. return [FIRAuthErrorUtils errorWithCode:FIRAuthInternalErrorCodeMissingMultiFactorSession
  1371. message:serverErrorMessage];
  1372. }
  1373. if ([shortErrorMessage isEqualToString:kMissingMFAEnrollmentIDErrorMessage]) {
  1374. return [FIRAuthErrorUtils errorWithCode:FIRAuthInternalErrorCodeMissingMultiFactorInfo
  1375. message:serverErrorMessage];
  1376. }
  1377. if ([shortErrorMessage isEqualToString:kInvalidMFAPendingCredentialErrorMessage]) {
  1378. return [FIRAuthErrorUtils errorWithCode:FIRAuthInternalErrorCodeInvalidMultiFactorSession
  1379. message:serverErrorMessage];
  1380. }
  1381. if ([shortErrorMessage isEqualToString:kMFAEnrollmentNotFoundErrorMessage]) {
  1382. return [FIRAuthErrorUtils errorWithCode:FIRAuthInternalErrorCodeMultiFactorInfoNotFound
  1383. message:serverErrorMessage];
  1384. }
  1385. if ([shortErrorMessage isEqualToString:kAdminOnlyOperationErrorMessage]) {
  1386. return [FIRAuthErrorUtils errorWithCode:FIRAuthInternalErrorCodeAdminRestrictedOperation
  1387. message:serverErrorMessage];
  1388. }
  1389. if ([shortErrorMessage isEqualToString:kUnverifiedEmailErrorMessage]) {
  1390. return [FIRAuthErrorUtils errorWithCode:FIRAuthInternalErrorCodeUnverifiedEmail
  1391. message:serverErrorMessage];
  1392. }
  1393. if ([shortErrorMessage isEqualToString:kSecondFactorExistsErrorMessage]) {
  1394. return [FIRAuthErrorUtils errorWithCode:FIRAuthInternalErrorCodeSecondFactorAlreadyEnrolled
  1395. message:serverErrorMessage];
  1396. }
  1397. if ([shortErrorMessage isEqualToString:kSecondFactorLimitExceededErrorMessage]) {
  1398. return [FIRAuthErrorUtils errorWithCode:FIRAuthInternalErrorCodeMaximumSecondFactorCountExceeded
  1399. message:serverErrorMessage];
  1400. }
  1401. if ([shortErrorMessage isEqualToString:kUnsupportedFirstFactorErrorMessage]) {
  1402. return [FIRAuthErrorUtils errorWithCode:FIRAuthInternalErrorCodeUnsupportedFirstFactor
  1403. message:serverErrorMessage];
  1404. }
  1405. if ([shortErrorMessage isEqualToString:kEmailChangeNeedsVerificationErrorMessage]) {
  1406. return [FIRAuthErrorUtils errorWithCode:FIRAuthInternalErrorCodeEmailChangeNeedsVerification
  1407. message:serverErrorMessage];
  1408. }
  1409. if ([shortErrorMessage isEqualToString:kTenantIDMismatch]) {
  1410. return [FIRAuthErrorUtils tenantIDMismatchError];
  1411. }
  1412. if ([shortErrorMessage isEqualToString:kUnsupportedTenantOperation]) {
  1413. return [FIRAuthErrorUtils unsupportedTenantOperationError];
  1414. }
  1415. if ([shortErrorMessage isEqualToString:kBlockingCloudFunctionErrorResponse]) {
  1416. return
  1417. [FIRAuthErrorUtils blockingCloudFunctionServerResponseWithMessage:serverDetailErrorMessage];
  1418. }
  1419. if ([shortErrorMessage isEqualToString:kInvalidRecaptchaScore]) {
  1420. return [FIRAuthErrorUtils errorWithCode:FIRAuthInternalErrorCodeCaptchaCheckFailed
  1421. message:serverErrorMessage];
  1422. }
  1423. if ([shortErrorMessage isEqualToString:kRecaptchaNotEnabled]) {
  1424. return [FIRAuthErrorUtils errorWithCode:FIRAuthInternalErrorCodeRecaptchaNotEnabled
  1425. message:serverErrorMessage];
  1426. }
  1427. // In this case we handle an error that might be specified in the underlying errors dictionary,
  1428. // the error message in determined based on the @c reason key in the dictionary.
  1429. if (errorDictionary[kErrorsKey]) {
  1430. // Check for underlying error with reason = keyInvalid;
  1431. id underlyingErrors = errorDictionary[kErrorsKey];
  1432. if ([underlyingErrors isKindOfClass:[NSArray class]]) {
  1433. NSArray *underlyingErrorsArray = (NSArray *)underlyingErrors;
  1434. for (id underlyingError in underlyingErrorsArray) {
  1435. if ([underlyingError isKindOfClass:[NSDictionary class]]) {
  1436. NSDictionary *underlyingErrorDictionary = (NSDictionary *)underlyingError;
  1437. NSString *reason = underlyingErrorDictionary[kReasonKey];
  1438. if ([reason hasPrefix:kInvalidKeyReasonValue]) {
  1439. return [FIRAuthErrorUtils invalidAPIKeyError];
  1440. }
  1441. if ([reason isEqualToString:kAppNotAuthorizedReasonValue]) {
  1442. return [FIRAuthErrorUtils appNotAuthorizedError];
  1443. }
  1444. }
  1445. }
  1446. }
  1447. }
  1448. return nil;
  1449. }
  1450. @end
  1451. NS_ASSUME_NONNULL_END