FIRAuthErrorUtils.m 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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 "Private/FIRAuthErrorUtils.h"
  17. #import "FIRAuthCredential.h"
  18. #import "Private/FIRAuthInternalErrors.h"
  19. NS_ASSUME_NONNULL_BEGIN
  20. NSString *const FIRAuthErrorDomain = @"FIRAuthErrorDomain";
  21. NSString *const FIRAuthInternalErrorDomain = @"FIRAuthInternalErrorDomain";
  22. NSString *const FIRAuthErrorUserInfoDeserializedResponseKey =
  23. @"FIRAuthErrorUserInfoDeserializedResponseKey";
  24. NSString *const FIRAuthErrorUserInfoDataKey = @"FIRAuthErrorUserInfoDataKey";
  25. NSString *const FIRAuthErrorUserInfoEmailKey = @"FIRAuthErrorUserInfoEmailKey";
  26. NSString *const FIRAuthErrorNameKey = @"error_name";
  27. NSString *const FIRAuthUpdatedCredentialKey = @"FIRAuthUpdatedCredentialKey";
  28. /** @var kServerErrorDetailMarker
  29. @brief This marker indicates that the server error message contains a detail error message which
  30. should be used instead of the hardcoded client error message.
  31. */
  32. static NSString *const kServerErrorDetailMarker = @" : ";
  33. #pragma mark - Standard Error Messages
  34. /** @var kFIRAuthErrorMessageInvalidCustomToken
  35. @brief Message for @c FIRAuthErrorCodeInvalidCustomToken error code.
  36. */
  37. static NSString *const kFIRAuthErrorMessageInvalidCustomToken = @"The custom token format is "
  38. "incorrect. Please check the documentation.";
  39. /** @var kFIRAuthErrorMessageCustomTokenMismatch
  40. @brief Message for @c FIRAuthErrorCodeCustomTokenMismatch error code.
  41. */
  42. static NSString *const kFIRAuthErrorMessageCustomTokenMismatch = @"The custom token corresponds to "
  43. "a different audience.";
  44. /** @var kFIRAuthErrorMessageInvalidEmail
  45. @brief Message for @c FIRAuthErrorCodeInvalidEmail error code.
  46. */
  47. static NSString *const kFIRAuthErrorMessageInvalidEmail = @"The email address is badly formatted.";
  48. /** @var kFIRAuthErrorMessageInvalidCredential
  49. @brief Message for @c FIRAuthErrorCodeInvalidCredential error code.
  50. */
  51. static NSString *const kFIRAuthErrorMessageInvalidCredential = @"The supplied auth credential is "
  52. "malformed or has expired.";
  53. /** @var kFIRAuthErrorMessageUserDisabled
  54. @brief Message for @c FIRAuthErrorCodeUserDisabled error code.
  55. */
  56. static NSString *const kFIRAuthErrorMessageUserDisabled = @"The user account has been disabled by "
  57. "an administrator.";
  58. /** @var kFIRAuthErrorMessageEmailAlreadyInUse
  59. @brief Message for @c FIRAuthErrorCodeEmailAlreadyInUse error code.
  60. */
  61. static NSString *const kFIRAuthErrorMessageEmailAlreadyInUse = @"The email address is already in "
  62. "use by another account.";
  63. /** @var kFIRAuthErrorMessageWrongPassword
  64. @brief Message for @c FIRAuthErrorCodeWrongPassword error code.
  65. */
  66. static NSString *const kFIRAuthErrorMessageWrongPassword = @"The password is invalid or the user "
  67. "does not have a password.";
  68. /** @var kFIRAuthErrorMessageTooManyRequests
  69. @brief Message for @c FIRAuthErrorCodeTooManyRequests error code.
  70. */
  71. static NSString *const kFIRAuthErrorMessageTooManyRequests = @"We have blocked all requests from "
  72. "this device due to unusual activity. Try again later.";
  73. /** @var kFIRAuthErrorMessageAccountExistsWithDifferentCredential
  74. @brief Message for @c FIRAuthErrorCodeAccountLinkNeeded error code.
  75. */
  76. static NSString *const kFIRAuthErrorMessageAccountExistsWithDifferentCredential = @"An account "
  77. "already exists with the same email address but different sign-in credentials. Sign in using a "
  78. "provider associated with this email address.";
  79. /** @var kFIRAuthErrorMessageRequiresRecentLogin
  80. @brief Message for @c FIRAuthErrorCodeRequiresRecentLogin error code.
  81. */
  82. static NSString *const kFIRAuthErrorMessageRequiresRecentLogin= @"This operation is sensitive and "
  83. "requires recent authentication. Log in again before retrying this request.";
  84. /** @var kFIRAuthErrorMessageProviderAlreadyLinked
  85. @brief Message for @c FIRAuthErrorCodeProviderAlreadyExists error code.
  86. */
  87. static NSString *const kFIRAuthErrorMessageProviderAlreadyLinked =
  88. @"[ERROR_PROVIDER_ALREADY_LINKED] - User can only be linked to one identity for the given "
  89. "provider.";
  90. /** @var kFIRAuthErrorMessageNoSuchProvider
  91. @brief Message for @c FIRAuthErrorCodeNoSuchProvider error code.
  92. */
  93. static NSString *const kFIRAuthErrorMessageNoSuchProvider = @"User was not linked to an account "
  94. "with the given provider.";
  95. /** @var kFIRAuthErrorMessageInvalidUserToken
  96. @brief Message for @c FIRAuthErrorCodeInvalidUserToken error code.
  97. */
  98. static NSString *const kFIRAuthErrorMessageInvalidUserToken = @"The user's credential is no longer "
  99. "valid. The user must sign in again.";
  100. /** @var kFIRAuthErrorMessageNetworkError
  101. @brief Message for @c FIRAuthErrorCodeNetworkError error code.
  102. */
  103. static NSString *const kFIRAuthErrorMessageNetworkError = @"Network error (such as timeout, "
  104. "interrupted connection or unreachable host) has occurred.";
  105. /** @var kFIRAuthErrorMessageKeychainError
  106. @brief Message for @c FIRAuthErrorCodeKeychainError error code.
  107. */
  108. static NSString *const kFIRAuthErrorMessageKeychainError = @"An error occurred when accessing the "
  109. "keychain. The @c NSLocalizedFailureReasonErrorKey field in the @c NSError.userInfo dictionary "
  110. "will contain more information about the error encountered";
  111. /** @var kFIRAuthErrorMessageUserTokenExpired
  112. @brief Message for @c FIRAuthErrorCodeTokenExpired error code.
  113. */
  114. static NSString *const kFIRAuthErrorMessageUserTokenExpired = @"The user's credential is no longer "
  115. "valid. The user must sign in again.";
  116. /** @var kFIRAuthErrorMessageUserNotFound
  117. @brief Message for @c FIRAuthErrorCodeUserNotFound error code.
  118. */
  119. static NSString *const kFIRAuthErrorMessageUserNotFound = @"There is no user record corresponding "
  120. "to this identifier. The user may have been deleted.";
  121. /** @var kFIRAuthErrorMessageInvalidAPIKey
  122. @brief Message for @c FIRAuthErrorCodeInvalidAPIKey error code.
  123. @remarks This error is not thrown by the server.
  124. */
  125. static NSString *const kFIRAuthErrorMessageInvalidAPIKey = @"An invalid API Key was supplied in "
  126. "the request.";
  127. /** @var kFIRAuthErrorMessageUserMismatch.
  128. @brief Message for @c FIRAuthErrorCodeInvalidAPIKey error code.
  129. */
  130. static NSString *const FIRAuthErrorMessageUserMismatch = @"The supplied credentials do not "
  131. "correspond to the previously signed in user.";
  132. /** @var kFIRAuthErrorMessageCredentialAlreadyInUse
  133. @brief Message for @c FIRAuthErrorCodeCredentialAlreadyInUse error code.
  134. */
  135. static NSString *const kFIRAuthErrorMessageCredentialAlreadyInUse = @"This credential is already "
  136. "associated with a different user account.";
  137. /** @var kFIRAuthErrorMessageOperationNotAllowed
  138. @brief Message for @c FIRAuthErrorCodeOperationNotAllowed error code.
  139. */
  140. static NSString *const kFIRAuthErrorMessageOperationNotAllowed = @"The given sign-in provider is "
  141. "disabled for this Firebase project. Enable it in the Firebase console, under the sign-in "
  142. "method tab of the Auth section.";
  143. /** @var kFIRAuthErrorMessageWeakPassword
  144. @brief Message for @c FIRAuthErrorCodeWeakPassword error code.
  145. */
  146. static NSString *const kFIRAuthErrorMessageWeakPassword = @"The password must be 6 characters long "
  147. "or more.";
  148. /** @var kFIRAuthErrorMessageAppNotAuthorized
  149. @brief Message for @c FIRAuthErrorCodeAppNotAuthorized error code.
  150. */
  151. static NSString *const kFIRAuthErrorMessageAppNotAuthorized = @"This app is not authorized to use "
  152. "Firebase Authentication with the provided API key. Review your key configuration in the "
  153. "Google API console and ensure that it accepts requests from your app's bundle ID.";
  154. /** @var kFIRAuthErrorMessageExpiredActionCode
  155. @brief Message for @c FIRAuthErrorCodeExpiredActionCode error code.
  156. */
  157. static NSString *const kFIRAuthErrorMessageExpiredActionCode = @"The action code has expired.";
  158. /** @var kFIRAuthErrorMessageInvalidActionCode
  159. @brief Message for @c FIRAuthErrorCodeInvalidActionCode error code.
  160. */
  161. static NSString *const kFIRAuthErrorMessageInvalidActionCode = @"The action code is invalid. This "
  162. "can happen if the code is malformed, expired, or has already been used.";
  163. /** @var kFIRAuthErrorMessageInvalidMessagePayload
  164. @brief Message for @c FIRAuthErrorCodeInvalidMessagePayload error code.
  165. */
  166. static NSString *const kFIRAuthErrorMessageInvalidMessagePayload = @"The action code is invalid. "
  167. "This can happen if the code is malformed, expired, or has already been used.";
  168. /** @var kFIRAuthErrorMessageInvalidSender
  169. @brief Message for @c FIRAuthErrorCodeInvalidSender error code.
  170. */
  171. static NSString *const kFIRAuthErrorMessageInvalidSender = @"The email template corresponding to "
  172. "this action contains invalid characters in its message. Please fix by going to the Auth email "
  173. "templates section in the Firebase Console.";
  174. /** @var kFIRAuthErrorMessageInvalidRecipientEmail
  175. @brief Message for @c FIRAuthErrorCodeInvalidRecipient error code.
  176. */
  177. static NSString *const kFIRAuthErrorMessageInvalidRecipientEmail = @"The action code is invalid. "
  178. "This can happen if the code is malformed, expired, or has already been used.";
  179. /** @var kFIRAuthErrorMessageMissingContinueURI
  180. @brief Message for @c FIRAuthErrorCodeMissingContinueURI error code.
  181. */
  182. static NSString *const kFIRAuthErrorMessageMissingContinueURI =
  183. @"A continue URL must be provided in the request.";
  184. /** @var kFIRAuthErrorMessageMissingPhoneNumber
  185. @brief Message for @c FIRAuthErrorCodeMissingPhoneNumber error code.
  186. */
  187. static NSString *const kFIRAuthErrorMessageMissingPhoneNumber =
  188. @"To send verification codes, provide a phone number for the recipient.";
  189. /** @var kFIRAuthErrorMessageInvalidPhoneNumber
  190. @brief Message for @c FIRAuthErrorCodeMissingPhoneNumber error code.
  191. */
  192. static NSString *const kFIRAuthErrorMessageInvalidPhoneNumber =
  193. @"The format of the phone number provided is incorrect. Please enter the phone number in a "
  194. "format that can be parsed into E.164 format. E.164 phone numbers are written in the format "
  195. "[+][country code][subscriber number including area code].";
  196. /** @var kFIRAuthErrorMessageMissingVerificationCode
  197. @brief Message for @c FIRAuthErrorCodeMissingVerificationCode error code.
  198. */
  199. static NSString *const kFIRAuthErrorMessageMissingVerificationCode =
  200. @"The Phone Auth Credential was created with an empty SMS verification Code.";
  201. /** @var kFIRAuthErrorMessageInvalidVerificationCode
  202. @brief Message for @c FIRAuthErrorCodeInvalidVerificationCode error code.
  203. */
  204. static NSString *const kFIRAuthErrorMessageInvalidVerificationCode =
  205. @"The SMS verification code used to create the phone auth credential is invalid. Please resend "
  206. "the verification code sms and be sure use the verification code provided by the user.";
  207. /** @var kFIRAuthErrorMessageMissingVerificationID
  208. @brief Message for @c FIRAuthErrorCodeInvalidVerificationID error code.
  209. */
  210. static NSString *const kFIRAuthErrorMessageMissingVerificationID =
  211. @"The Phone Auth Credential was created with an empty verification ID.";
  212. /** @var kFIRAuthErrorMessageInvalidVerificationID
  213. @brief Message for @c FIRAuthErrorCodeInvalidVerificationID error code.
  214. */
  215. static NSString *const kFIRAuthErrorMessageInvalidVerificationID =
  216. @"The verification ID used to create the phone auth credential is invalid.";
  217. /** @var kFIRAuthErrorMessageSessionExpired
  218. @brief Message for @c FIRAuthErrorCodeSessionExpired error code.
  219. */
  220. static NSString *const kFIRAuthErrorMessageSessionExpired = @"The SMS code has expired. Please "
  221. @"re-send the verification code to try again.";
  222. /** @var kFIRAuthErrorMessageMissingAppCredential
  223. @brief Message for @c FIRAuthErrorCodeMissingAppCredential error code.
  224. */
  225. static NSString *const kFIRAuthErrorMessageMissingAppCredential = @"The phone verification request "
  226. "is missing an APNs Device token. Firebase Auth automatically detects APNs Device Tokens, "
  227. "however, if method swizzling is disabled, the APNs token must be set via the APNSToken "
  228. "property on FIRAuth or by calling setAPNSToken:type on FIRAuth.";
  229. /** @var kFIRAuthErrorMessageInvalidAppCredential
  230. @brief Message for @c FIRAuthErrorCodeInvalidAppCredential error code.
  231. */
  232. static NSString *const kFIRAuthErrorMessageInvalidAppCredential = @"The APNs device token provided "
  233. "may be incorrect or does not match the private certificate uploaded to the Firebase Console.";
  234. /** @var kFIRAuthErrorMessageQuotaExceeded
  235. @brief Message for @c FIRAuthErrorCodeQuotaExceeded error code.
  236. */
  237. static NSString *const kFIRAuthErrorMessageQuotaExceeded = @"The SMS quota for this project has "
  238. "been exceeded.";
  239. /** @var kFIRAuthErrorMessageMissingAppToken
  240. @brief Message for @c FIRAuthErrorCodeMissingAppToken error code.
  241. */
  242. static NSString *const kFIRAuthErrorMessageMissingAppToken = @"Remote notification and background "
  243. "fetching need to be set up for the app. If app delegate swizzling is disabled, the APNs "
  244. "device token received by UIApplicationDelegate needs to be forwarded to FIRAuth's APNSToken "
  245. "property.";
  246. /** @var kFIRAuthErrorMessageMissingAppToken
  247. @brief Message for @c FIRAuthErrorCodeMissingAppToken error code.
  248. */
  249. static NSString *const kFIRAuthErrorMessageNotificationNotForwarded = @"If app delegate swizzling "
  250. "is disabled, remote notifications received by UIApplicationDelegate need to be forwarded to "
  251. "FIRAuth's canHandleNotificaton: method.";
  252. /** @var kFIRAuthErrorMessageAppNotVerified
  253. @brief Message for @c FIRAuthErrorCodeMissingAppToken error code.
  254. */
  255. static NSString *const kFIRAuthErrorMessageAppNotVerified = @"Firebase could not retrieve the "
  256. "silent push notification and therefore could not verify your app. Ensure that you configured "
  257. "your app correctly to recieve push notifications.";
  258. /** @var kFIRAuthErrorMessageInternalError
  259. @brief Message for @c FIRAuthErrorCodeInternalError error code.
  260. */
  261. static NSString *const kFIRAuthErrorMessageInternalError = @"An internal error has occurred, "
  262. "print and inspect the error details for more information.";
  263. /** @var FIRAuthErrorDescription
  264. @brief The error descrioption, based on the error code.
  265. @remarks No default case so that we get a compiler warning if a new value was added to the enum.
  266. */
  267. static NSString *FIRAuthErrorDescription(FIRAuthErrorCode code) {
  268. switch (code) {
  269. case FIRAuthErrorCodeInvalidCustomToken:
  270. return kFIRAuthErrorMessageInvalidCustomToken;
  271. case FIRAuthErrorCodeCustomTokenMismatch:
  272. return kFIRAuthErrorMessageCustomTokenMismatch;
  273. case FIRAuthErrorCodeInvalidEmail:
  274. return kFIRAuthErrorMessageInvalidEmail;
  275. case FIRAuthErrorCodeInvalidCredential:
  276. return kFIRAuthErrorMessageInvalidCredential;
  277. case FIRAuthErrorCodeUserDisabled:
  278. return kFIRAuthErrorMessageUserDisabled;
  279. case FIRAuthErrorCodeEmailAlreadyInUse:
  280. return kFIRAuthErrorMessageEmailAlreadyInUse;
  281. case FIRAuthErrorCodeWrongPassword:
  282. return kFIRAuthErrorMessageWrongPassword;
  283. case FIRAuthErrorCodeTooManyRequests:
  284. return kFIRAuthErrorMessageTooManyRequests;
  285. case FIRAuthErrorCodeAccountExistsWithDifferentCredential:
  286. return kFIRAuthErrorMessageAccountExistsWithDifferentCredential;
  287. case FIRAuthErrorCodeRequiresRecentLogin:
  288. return kFIRAuthErrorMessageRequiresRecentLogin;
  289. case FIRAuthErrorCodeProviderAlreadyLinked:
  290. return kFIRAuthErrorMessageProviderAlreadyLinked;
  291. case FIRAuthErrorCodeNoSuchProvider:
  292. return kFIRAuthErrorMessageNoSuchProvider;
  293. case FIRAuthErrorCodeInvalidUserToken:
  294. return kFIRAuthErrorMessageInvalidUserToken;
  295. case FIRAuthErrorCodeNetworkError:
  296. return kFIRAuthErrorMessageNetworkError;
  297. case FIRAuthErrorCodeKeychainError:
  298. return kFIRAuthErrorMessageKeychainError;
  299. case FIRAuthErrorCodeUserTokenExpired:
  300. return kFIRAuthErrorMessageUserTokenExpired;
  301. case FIRAuthErrorCodeUserNotFound:
  302. return kFIRAuthErrorMessageUserNotFound;
  303. case FIRAuthErrorCodeInvalidAPIKey:
  304. return kFIRAuthErrorMessageInvalidAPIKey;
  305. case FIRAuthErrorCodeCredentialAlreadyInUse:
  306. return kFIRAuthErrorMessageCredentialAlreadyInUse;
  307. case FIRAuthErrorCodeInternalError:
  308. return kFIRAuthErrorMessageInternalError;
  309. case FIRAuthErrorCodeUserMismatch:
  310. return FIRAuthErrorMessageUserMismatch;
  311. case FIRAuthErrorCodeOperationNotAllowed:
  312. return kFIRAuthErrorMessageOperationNotAllowed;
  313. case FIRAuthErrorCodeWeakPassword:
  314. return kFIRAuthErrorMessageWeakPassword;
  315. case FIRAuthErrorCodeAppNotAuthorized:
  316. return kFIRAuthErrorMessageAppNotAuthorized;
  317. case FIRAuthErrorCodeExpiredActionCode:
  318. return kFIRAuthErrorMessageExpiredActionCode;
  319. case FIRAuthErrorCodeInvalidActionCode:
  320. return kFIRAuthErrorMessageInvalidActionCode;
  321. case FIRAuthErrorCodeInvalidSender:
  322. return kFIRAuthErrorMessageInvalidSender;
  323. case FIRAuthErrorCodeInvalidMessagePayload:
  324. return kFIRAuthErrorMessageInvalidMessagePayload;
  325. case FIRAuthErrorCodeInvalidRecipientEmail:
  326. return kFIRAuthErrorMessageInvalidRecipientEmail;
  327. case FIRAuthErrorCodeMissingPhoneNumber:
  328. return kFIRAuthErrorMessageMissingPhoneNumber;
  329. case FIRAuthErrorCodeInvalidPhoneNumber:
  330. return kFIRAuthErrorMessageInvalidPhoneNumber;
  331. case FIRAuthErrorCodeMissingVerificationCode:
  332. return kFIRAuthErrorMessageMissingVerificationCode;
  333. case FIRAuthErrorCodeInvalidVerificationCode:
  334. return kFIRAuthErrorMessageInvalidVerificationCode;
  335. case FIRAuthErrorCodeMissingVerificationID:
  336. return kFIRAuthErrorMessageMissingVerificationID;
  337. case FIRAuthErrorCodeInvalidVerificationID:
  338. return kFIRAuthErrorMessageInvalidVerificationID;
  339. case FIRAuthErrorCodeSessionExpired:
  340. return kFIRAuthErrorMessageSessionExpired;
  341. case FIRAuthErrorCodeMissingAppCredential:
  342. return kFIRAuthErrorMessageMissingAppCredential;
  343. case FIRAuthErrorCodeInvalidAppCredential:
  344. return kFIRAuthErrorMessageInvalidAppCredential;
  345. case FIRAuthErrorCodeQuotaExceeded:
  346. return kFIRAuthErrorMessageQuotaExceeded;
  347. case FIRAuthErrorCodeMissingAppToken:
  348. return kFIRAuthErrorMessageMissingAppToken;
  349. case FIRAuthErrorCodeNotificationNotForwarded:
  350. return kFIRAuthErrorMessageNotificationNotForwarded;
  351. case FIRAuthErrorCodeAppNotVerified:
  352. return kFIRAuthErrorMessageAppNotVerified;
  353. }
  354. }
  355. /** @var FIRAuthErrorCodeString
  356. @brief The the error short string, based on the error code.
  357. @remarks No default case so that we get a compiler warning if a new value was added to the enum.
  358. */
  359. static NSString *const FIRAuthErrorCodeString(FIRAuthErrorCode code) {
  360. switch (code) {
  361. case FIRAuthErrorCodeInvalidCustomToken:
  362. return @"ERROR_INVALID_CUSTOM_TOKEN";
  363. case FIRAuthErrorCodeCustomTokenMismatch:
  364. return @"ERROR_CUSTOM_TOKEN_MISMATCH";
  365. case FIRAuthErrorCodeInvalidEmail:
  366. return @"ERROR_INVALID_EMAIL";
  367. case FIRAuthErrorCodeInvalidCredential:
  368. return @"ERROR_INVALID_CREDENTIAL";
  369. case FIRAuthErrorCodeUserDisabled:
  370. return @"ERROR_USER_DISABLED";
  371. case FIRAuthErrorCodeEmailAlreadyInUse:
  372. return @"ERROR_EMAIL_ALREADY_IN_USE";
  373. case FIRAuthErrorCodeWrongPassword:
  374. return @"ERROR_WRONG_PASSWORD";
  375. case FIRAuthErrorCodeTooManyRequests:
  376. return @"ERROR_TOO_MANY_REQUESTS";
  377. case FIRAuthErrorCodeAccountExistsWithDifferentCredential:
  378. return @"ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL";
  379. case FIRAuthErrorCodeRequiresRecentLogin:
  380. return @"ERROR_REQUIRES_RECENT_LOGIN";
  381. case FIRAuthErrorCodeProviderAlreadyLinked:
  382. return @"ERROR_PROVIDER_ALREADY_LINKED";
  383. case FIRAuthErrorCodeNoSuchProvider:
  384. return @"ERROR_NO_SUCH_PROVIDER";
  385. case FIRAuthErrorCodeInvalidUserToken:
  386. return @"ERROR_INVALID_USER_TOKEN";
  387. case FIRAuthErrorCodeNetworkError:
  388. return @"ERROR_NETWORK_REQUEST_FAILED";
  389. case FIRAuthErrorCodeKeychainError:
  390. return @"ERROR_KEYCHAIN_ERROR";
  391. case FIRAuthErrorCodeUserTokenExpired:
  392. return @"ERROR_USER_TOKEN_EXPIRED";
  393. case FIRAuthErrorCodeUserNotFound:
  394. return @"ERROR_USER_NOT_FOUND";
  395. case FIRAuthErrorCodeInvalidAPIKey:
  396. return @"ERROR_INVALID_API_KEY";
  397. case FIRAuthErrorCodeCredentialAlreadyInUse:
  398. return @"ERROR_CREDENTIAL_ALREADY_IN_USE";
  399. case FIRAuthErrorCodeInternalError:
  400. return @"ERROR_INTERNAL_ERROR";
  401. case FIRAuthErrorCodeUserMismatch:
  402. return @"ERROR_USER_MISMATCH";
  403. case FIRAuthErrorCodeOperationNotAllowed:
  404. return @"ERROR_OPERATION_NOT_ALLOWED";
  405. case FIRAuthErrorCodeWeakPassword:
  406. return @"ERROR_WEAK_PASSWORD";
  407. case FIRAuthErrorCodeAppNotAuthorized:
  408. return @"ERROR_APP_NOT_AUTHORIZED";
  409. case FIRAuthErrorCodeExpiredActionCode:
  410. return @"ERROR_EXPIRED_ACTION_CODE";
  411. case FIRAuthErrorCodeInvalidActionCode:
  412. return @"ERROR_INVALID_ACTION_CODE";
  413. case FIRAuthErrorCodeInvalidMessagePayload:
  414. return @"ERROR_INVALID_MESSAGE_PAYLOAD";
  415. case FIRAuthErrorCodeInvalidSender:
  416. return @"ERROR_INVALID_SENDER";
  417. case FIRAuthErrorCodeInvalidRecipientEmail:
  418. return @"ERROR_INVALID_RECIPIENT_EMAIL";
  419. case FIRAuthErrorCodeMissingPhoneNumber:
  420. return @"ERROR_MISSING_PHONE_NUMBER";
  421. case FIRAuthErrorCodeInvalidPhoneNumber:
  422. return @"ERROR_INVALID_PHONE_NUMBER";
  423. case FIRAuthErrorCodeMissingVerificationCode:
  424. return @"ERROR_MISSING_VERIFICATION_CODE";
  425. case FIRAuthErrorCodeInvalidVerificationCode:
  426. return @"ERROR_INVALID_VERIFICATION_CODE";
  427. case FIRAuthErrorCodeMissingVerificationID:
  428. return @"ERROR_MISSING_VERIFICATION_ID";
  429. case FIRAuthErrorCodeInvalidVerificationID:
  430. return @"ERROR_INVALID_VERIFICATION_ID";
  431. case FIRAuthErrorCodeSessionExpired:
  432. return @"ERROR_SESSION_EXPIRED";
  433. case FIRAuthErrorCodeMissingAppCredential:
  434. return @"MISSING_APP_CREDENTIAL";
  435. case FIRAuthErrorCodeInvalidAppCredential:
  436. return @"INVALID_APP_CREDENTIAL";
  437. case FIRAuthErrorCodeQuotaExceeded:
  438. return @"ERROR_QUOTA_EXCEEDED";
  439. case FIRAuthErrorCodeMissingAppToken:
  440. return @"ERROR_MISSING_APP_TOKEN";
  441. case FIRAuthErrorCodeNotificationNotForwarded:
  442. return @"ERROR_NOTIFICATION_NOT_FORWARDED";
  443. case FIRAuthErrorCodeAppNotVerified:
  444. return @"ERROR_APP_NOT_VERIFIED";
  445. }
  446. }
  447. @implementation FIRAuthErrorUtils
  448. + (NSError *)errorWithCode:(FIRAuthInternalErrorCode)code {
  449. return [self errorWithCode:code message:nil];
  450. }
  451. + (NSError *)errorWithCode:(FIRAuthInternalErrorCode)code
  452. message:(nullable NSString *)message {
  453. NSDictionary *userInfo = nil;
  454. if (message.length) {
  455. userInfo = @{
  456. NSLocalizedDescriptionKey : message
  457. };
  458. }
  459. return [self errorWithCode:code userInfo:userInfo];
  460. }
  461. + (NSError *)errorWithCode:(FIRAuthInternalErrorCode)code
  462. underlyingError:(nullable NSError *)underlyingError {
  463. NSDictionary *errorUserInfo = nil;
  464. if (underlyingError) {
  465. errorUserInfo = @{
  466. NSUnderlyingErrorKey : underlyingError
  467. };
  468. }
  469. return [self errorWithCode:code userInfo:errorUserInfo];
  470. }
  471. + (NSError *)errorWithCode:(FIRAuthInternalErrorCode)code userInfo:(NSDictionary *)userInfo {
  472. BOOL isPublic = (code & FIRAuthPublicErrorCodeFlag) == FIRAuthPublicErrorCodeFlag;
  473. if (isPublic) {
  474. // This is a public error. Return it as a public error and add a description.
  475. NSInteger errorCode = code & ~FIRAuthPublicErrorCodeFlag;
  476. NSMutableDictionary *errorUserInfo = [NSMutableDictionary dictionaryWithDictionary:userInfo];
  477. if (!errorUserInfo[NSLocalizedDescriptionKey]) {
  478. errorUserInfo[NSLocalizedDescriptionKey] = FIRAuthErrorDescription(errorCode);
  479. }
  480. errorUserInfo[FIRAuthErrorNameKey] = FIRAuthErrorCodeString(errorCode);
  481. return [NSError errorWithDomain:FIRAuthErrorDomain code:errorCode userInfo:errorUserInfo];
  482. } else {
  483. // This is an internal error. Wrap it in an internal error.
  484. NSError *error =
  485. [NSError errorWithDomain:FIRAuthInternalErrorDomain code:code userInfo:userInfo];
  486. return [self errorWithCode:FIRAuthInternalErrorCodeInternalError underlyingError:error];
  487. }
  488. }
  489. + (NSError *)RPCRequestEncodingErrorWithUnderlyingError:(NSError *)underlyingError {
  490. return [self errorWithCode:FIRAuthInternalErrorCodeRPCRequestEncodingError
  491. underlyingError:underlyingError];
  492. }
  493. + (NSError *)JSONSerializationErrorForUnencodableType {
  494. return [self errorWithCode:FIRAuthInternalErrorCodeJSONSerializationError];
  495. }
  496. + (NSError *)JSONSerializationErrorWithUnderlyingError:(NSError *)underlyingError {
  497. return [self errorWithCode:FIRAuthInternalErrorCodeJSONSerializationError
  498. underlyingError:underlyingError];
  499. }
  500. + (NSError *)networkErrorWithUnderlyingError:(NSError *)underlyingError {
  501. return [self errorWithCode:FIRAuthInternalErrorCodeNetworkError
  502. underlyingError:underlyingError];
  503. }
  504. + (NSError *)unexpectedErrorResponseWithData:(NSData *)data
  505. underlyingError:(NSError *)underlyingError {
  506. return [self errorWithCode:FIRAuthInternalErrorCodeUnexpectedErrorResponse userInfo:@{
  507. FIRAuthErrorUserInfoDataKey : data,
  508. NSUnderlyingErrorKey : underlyingError
  509. }];
  510. }
  511. + (NSError *)unexpectedErrorResponseWithDeserializedResponse:(id)deserializedResponse {
  512. return [self errorWithCode:FIRAuthInternalErrorCodeUnexpectedErrorResponse userInfo:@{
  513. FIRAuthErrorUserInfoDeserializedResponseKey : deserializedResponse
  514. }];
  515. }
  516. + (NSError *)unexpectedResponseWithData:(NSData *)data
  517. underlyingError:(NSError *)underlyingError {
  518. return [self errorWithCode:FIRAuthInternalErrorCodeUnexpectedResponse userInfo:@{
  519. FIRAuthErrorUserInfoDataKey : data,
  520. NSUnderlyingErrorKey : underlyingError
  521. }];
  522. }
  523. + (NSError *)unexpectedResponseWithDeserializedResponse:(id)deserializedResponse {
  524. return [self errorWithCode:FIRAuthInternalErrorCodeUnexpectedResponse userInfo:@{
  525. FIRAuthErrorUserInfoDeserializedResponseKey : deserializedResponse
  526. }];
  527. }
  528. + (NSError *)unexpectedResponseWithDeserializedResponse:(nullable id)deserializedResponse
  529. underlyingError:(NSError *)underlyingError {
  530. NSMutableDictionary *userInfo =
  531. [NSMutableDictionary dictionaryWithDictionary:@{ NSUnderlyingErrorKey : underlyingError }];
  532. if (deserializedResponse) {
  533. userInfo[FIRAuthErrorUserInfoDeserializedResponseKey] = deserializedResponse;
  534. }
  535. return [self errorWithCode:FIRAuthInternalErrorCodeUnexpectedResponse userInfo:userInfo];
  536. }
  537. + (NSError *)RPCResponseDecodingErrorWithDeserializedResponse:(id)deserializedResponse
  538. underlyingError:(NSError *)underlyingError {
  539. return [self errorWithCode:FIRAuthInternalErrorCodeRPCResponseDecodingError userInfo:@{
  540. FIRAuthErrorUserInfoDeserializedResponseKey : deserializedResponse,
  541. NSUnderlyingErrorKey : underlyingError
  542. }];
  543. }
  544. + (NSError *)emailAlreadyInUseErrorWithEmail:(nullable NSString *)email {
  545. NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
  546. if (email.length) {
  547. userInfo[FIRAuthErrorUserInfoEmailKey] = email;
  548. }
  549. return [self errorWithCode:FIRAuthInternalErrorCodeEmailAlreadyInUse userInfo:userInfo];
  550. }
  551. + (NSError *)userDisabledErrorWithMessage:(nullable NSString *)message {
  552. return [self errorWithCode:FIRAuthInternalErrorCodeUserDisabled message:message];
  553. }
  554. + (NSError *)wrongPasswordErrorWithMessage:(nullable NSString *)message {
  555. return [self errorWithCode:FIRAuthInternalErrorCodeWrongPassword message:message];
  556. }
  557. + (NSError *)tooManyRequestsErrorWithMessage:(nullable NSString *)message {
  558. return [self errorWithCode:FIRAuthInternalErrorCodeTooManyRequests message:message];
  559. }
  560. + (NSError *)invalidCustomTokenErrorWithMessage:(nullable NSString *)message {
  561. return [self errorWithCode:FIRAuthInternalErrorCodeInvalidCustomToken message:message];
  562. }
  563. + (NSError *)customTokenMistmatchErrorWithMessage:(nullable NSString *)message {
  564. return [self errorWithCode:FIRAuthInternalErrorCodeCustomTokenMismatch message:message];
  565. }
  566. + (NSError *)invalidCredentialErrorWithMessage:(nullable NSString *)message {
  567. return [self errorWithCode:FIRAuthInternalErrorCodeInvalidCredential message:message];
  568. }
  569. + (NSError *)requiresRecentLoginErrorWithMessage:(nullable NSString *)message {
  570. return [self errorWithCode:FIRAuthInternalErrorCodeRequiresRecentLogin message:message];
  571. }
  572. + (NSError *)invalidUserTokenErrorWithMessage:(nullable NSString *)message {
  573. return [self errorWithCode:FIRAuthInternalErrorCodeInvalidUserToken message:message];
  574. }
  575. + (NSError *)invalidEmailErrorWithMessage:(nullable NSString *)message {
  576. return [self errorWithCode:FIRAuthInternalErrorCodeInvalidEmail message:message];
  577. }
  578. + (NSError *)accountExistsWithDifferentCredentialErrorWithEmail:(nullable NSString *)email {
  579. return [self errorWithCode:FIRAuthInternalErrorCodeAccountExistsWithDifferentCredential
  580. userInfo:@{ FIRAuthErrorUserInfoEmailKey : email }];
  581. }
  582. + (NSError *)providerAlreadyLinkedError {
  583. return [self errorWithCode:FIRAuthInternalErrorCodeProviderAlreadyLinked];
  584. }
  585. + (NSError *)noSuchProviderError {
  586. return [self errorWithCode:FIRAuthInternalErrorCodeNoSuchProvider];
  587. }
  588. + (NSError *)userTokenExpiredErrorWithMessage:(nullable NSString *)message {
  589. return [self errorWithCode:FIRAuthInternalErrorCodeUserTokenExpired message:message];
  590. }
  591. + (NSError *)userNotFoundErrorWithMessage:(nullable NSString *)message {
  592. return [self errorWithCode:FIRAuthInternalErrorCodeUserNotFound message:message];
  593. }
  594. + (NSError *)invalidAPIKeyError {
  595. return [self errorWithCode:FIRAuthInternalErrorCodeInvalidAPIKey];
  596. }
  597. + (NSError *)userMismatchError {
  598. return [self errorWithCode:FIRAuthInternalErrorCodeUserMismatch];
  599. }
  600. + (NSError *)credentialAlreadyInUseErrorWithMessage:(nullable NSString *)message
  601. credential:(nullable FIRPhoneAuthCredential *)credential {
  602. if (credential) {
  603. return [self errorWithCode:FIRAuthInternalErrorCodeCredentialAlreadyInUse
  604. userInfo:@{ FIRAuthUpdatedCredentialKey : credential }];
  605. }
  606. return [self errorWithCode:FIRAuthInternalErrorCodeCredentialAlreadyInUse message:message];
  607. }
  608. + (NSError *)operationNotAllowedErrorWithMessage:(nullable NSString *)message {
  609. return [self errorWithCode:FIRAuthInternalErrorCodeOperationNotAllowed message:message];
  610. }
  611. + (NSError *)weakPasswordErrorWithServerResponseReason:(NSString *)reason {
  612. return [self errorWithCode:FIRAuthInternalErrorCodeWeakPassword userInfo:@{
  613. NSLocalizedFailureReasonErrorKey : reason
  614. }];
  615. }
  616. + (NSError *)appNotAuthorizedError {
  617. return [self errorWithCode:FIRAuthInternalErrorCodeAppNotAuthorized];
  618. }
  619. + (NSError *)expiredActionCodeErrorWithMessage:(nullable NSString *)message {
  620. return [self errorWithCode:FIRAuthInternalErrorCodeExpiredActionCode message:message];
  621. }
  622. + (NSError *)invalidActionCodeErrorWithMessage:(nullable NSString *)message {
  623. return [self errorWithCode:FIRAuthInternalErrorCodeInvalidActionCode message:message];
  624. }
  625. + (NSError *)invalidMessagePayloadErrorWithMessage:(nullable NSString *)message {
  626. return [self errorWithCode:FIRAuthInternalErrorCodeInvalidMessagePayload message:message];
  627. }
  628. + (NSError *)invalidSenderErrorWithMessage:(nullable NSString *)message {
  629. return [self errorWithCode:FIRAuthInternalErrorCodeInvalidSender message:message];
  630. }
  631. + (NSError *)invalidRecipientEmailErrorWithMessage:(nullable NSString *)message {
  632. return [self errorWithCode:FIRAuthInternalErrorCodeInvalidRecipientEmail message:message];
  633. }
  634. + (NSError *)missingPhoneNumberErrorWithMessage:(nullable NSString *)message {
  635. return [self errorWithCode:FIRAuthInternalErrorCodeMissingPhoneNumber message:message];
  636. }
  637. + (NSError *)invalidPhoneNumberErrorWithMessage:(nullable NSString *)message {
  638. return [self errorWithCode:FIRAuthInternalErrorCodeInvalidPhoneNumber message:message];
  639. }
  640. + (NSError *)missingVerificationCodeErrorWithMessage:(nullable NSString *)message {
  641. return [self errorWithCode:FIRAuthInternalErrorCodeMissingVerificationCode message:message];
  642. }
  643. + (NSError *)invalidVerificationCodeErrorWithMessage:(nullable NSString *)message {
  644. return [self errorWithCode:FIRAuthInternalErrorCodeInvalidVerificationCode message:message];
  645. }
  646. + (NSError *)missingVerificationIDErrorWithMessage:(nullable NSString *)message {
  647. return [self errorWithCode:FIRAuthInternalErrorCodeMissingVerificationID message:message];
  648. }
  649. + (NSError *)invalidVerificationIDErrorWithMessage:(nullable NSString *)message {
  650. return [self errorWithCode:FIRAuthInternalErrorCodeInvalidVerificationID message:message];
  651. }
  652. + (NSError *)sessionExpiredErrorWithMessage:(nullable NSString *)message {
  653. return [self errorWithCode:FIRAuthInternalErrorCodeSessionExpired message:message];
  654. }
  655. + (NSError *)missingAppCredentialWithMessage:(nullable NSString *)message {
  656. return [self errorWithCode:FIRAuthInternalErrorCodeMissingAppCredential message:message];
  657. }
  658. + (NSError *)invalidAppCredentialWithMessage:(nullable NSString *)message {
  659. return [self errorWithCode:FIRAuthInternalErrorCodeInvalidAppCredential message:message];
  660. }
  661. + (NSError *)quotaExceededErrorWithMessage:(nullable NSString *)message {
  662. return [self errorWithCode:FIRAuthInternalErrorCodeQuotaExceeded message:message];
  663. }
  664. + (NSError *)missingAppTokenError {
  665. return [self errorWithCode:FIRAuthInternalErrorCodeMissingAppToken];
  666. }
  667. + (NSError *)notificationNotForwardedError {
  668. return [self errorWithCode:FIRAuthInternalErrorCodeNotificationNotForwarded];
  669. }
  670. + (NSError *)appNotVerifiedErrorWithMessage:(nullable NSString *)message {
  671. return [self errorWithCode:FIRAuthInternalErrorCodeAppNotVerified message:message];
  672. }
  673. + (NSError *)keychainErrorWithFunction:(NSString *)keychainFunction status:(OSStatus)status {
  674. NSString *failureReason = [NSString stringWithFormat:@"%@ (%li)", keychainFunction, (long)status];
  675. return [self errorWithCode:FIRAuthInternalErrorCodeKeychainError userInfo:@{
  676. NSLocalizedFailureReasonErrorKey : failureReason,
  677. }];
  678. }
  679. @end
  680. NS_ASSUME_NONNULL_END