FIRAuthBackend.m 73 KB

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