FIRAuthBackend.m 77 KB

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