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