FIRAuthBackend.m 69 KB

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