FIRAuthBackend.m 64 KB

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