FIRAuthErrorUtils.m 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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 kFIRAuthErrorMessageMissingEmail
  180. @brief Message for @c FIRAuthErrorCodeMissingEmail error code.
  181. */
  182. static NSString *const kFIRAuthErrorMessageMissingEmail = @"An email address must be provided.";
  183. /** @var kFIRAuthErrorMessageMissingContinueURI
  184. @brief Message for @c FIRAuthErrorCodeMissingContinueURI error code.
  185. */
  186. static NSString *const kFIRAuthErrorMessageMissingContinueURI =
  187. @"A continue URL must be provided in the request.";
  188. /** @var kFIRAuthErrorMessageMissingPhoneNumber
  189. @brief Message for @c FIRAuthErrorCodeMissingPhoneNumber error code.
  190. */
  191. static NSString *const kFIRAuthErrorMessageMissingPhoneNumber =
  192. @"To send verification codes, provide a phone number for the recipient.";
  193. /** @var kFIRAuthErrorMessageInvalidPhoneNumber
  194. @brief Message for @c FIRAuthErrorCodeInvalidPhoneNumber error code.
  195. */
  196. static NSString *const kFIRAuthErrorMessageInvalidPhoneNumber =
  197. @"The format of the phone number provided is incorrect. Please enter the phone number in a "
  198. "format that can be parsed into E.164 format. E.164 phone numbers are written in the format "
  199. "[+][country code][subscriber number including area code].";
  200. /** @var kFIRAuthErrorMessageMissingVerificationCode
  201. @brief Message for @c FIRAuthErrorCodeMissingVerificationCode error code.
  202. */
  203. static NSString *const kFIRAuthErrorMessageMissingVerificationCode =
  204. @"The phone auth credential was created with an empty SMS verification Code.";
  205. /** @var kFIRAuthErrorMessageInvalidVerificationCode
  206. @brief Message for @c FIRAuthErrorCodeInvalidVerificationCode error code.
  207. */
  208. static NSString *const kFIRAuthErrorMessageInvalidVerificationCode =
  209. @"The SMS verification code used to create the phone auth credential is invalid. Please resend "
  210. "the verification code SMS and be sure to use the verification code provided by the user.";
  211. /** @var kFIRAuthErrorMessageMissingVerificationID
  212. @brief Message for @c FIRAuthErrorCodeInvalidVerificationID error code.
  213. */
  214. static NSString *const kFIRAuthErrorMessageMissingVerificationID =
  215. @"The phone auth credential was created with an empty verification ID.";
  216. /** @var kFIRAuthErrorMessageInvalidVerificationID
  217. @brief Message for @c FIRAuthErrorCodeInvalidVerificationID error code.
  218. */
  219. static NSString *const kFIRAuthErrorMessageInvalidVerificationID =
  220. @"The verification ID used to create the phone auth credential is invalid.";
  221. /** @var kFIRAuthErrorMessageSessionExpired
  222. @brief Message for @c FIRAuthErrorCodeSessionExpired error code.
  223. */
  224. static NSString *const kFIRAuthErrorMessageSessionExpired = @"The SMS code has expired. Please "
  225. @"re-send the verification code to try again.";
  226. /** @var kFIRAuthErrorMessageMissingAppCredential
  227. @brief Message for @c FIRAuthErrorCodeMissingAppCredential error code.
  228. */
  229. static NSString *const kFIRAuthErrorMessageMissingAppCredential = @"The phone verification request "
  230. "is missing an APNs Device token. Firebase Auth automatically detects APNs Device Tokens, "
  231. "however, if method swizzling is disabled, the APNs token must be set via the APNSToken "
  232. "property on FIRAuth or by calling setAPNSToken:type on FIRAuth.";
  233. /** @var kFIRAuthErrorMessageInvalidAppCredential
  234. @brief Message for @c FIRAuthErrorCodeInvalidAppCredential error code.
  235. */
  236. static NSString *const kFIRAuthErrorMessageInvalidAppCredential = @"The APNs device token provided "
  237. "is either incorrect or does not match the private certificate uploaded to the Firebase "
  238. "Console.";
  239. /** @var kFIRAuthErrorMessageQuotaExceeded
  240. @brief Message for @c FIRAuthErrorCodeQuotaExceeded error code.
  241. */
  242. static NSString *const kFIRAuthErrorMessageQuotaExceeded = @"The phone verification quota for this "
  243. "project has been exceeded.";
  244. /** @var kFIRAuthErrorMessageMissingAppToken
  245. @brief Message for @c FIRAuthErrorCodeMissingAppToken error code.
  246. */
  247. static NSString *const kFIRAuthErrorMessageMissingAppToken = @"Remote notification and background "
  248. "fetching need to be set up for the app. If app delegate swizzling is disabled, the APNs "
  249. "device token received by UIApplicationDelegate needs to be forwarded to FIRAuth's APNSToken "
  250. "property.";
  251. /** @var kFIRAuthErrorMessageMissingAppToken
  252. @brief Message for @c FIRAuthErrorCodeMissingAppToken error code.
  253. */
  254. static NSString *const kFIRAuthErrorMessageNotificationNotForwarded = @"If app delegate swizzling "
  255. "is disabled, remote notifications received by UIApplicationDelegate need to be forwarded to "
  256. "FIRAuth's canHandleNotificaton: method.";
  257. /** @var kFIRAuthErrorMessageAppNotVerified
  258. @brief Message for @c FIRAuthErrorCodeMissingAppToken error code.
  259. */
  260. static NSString *const kFIRAuthErrorMessageAppNotVerified = @"Firebase could not retrieve the "
  261. "silent push notification and therefore could not verify your app. Ensure that you configured "
  262. "your app correctly to recieve push notifications.";
  263. /** @var kFIRAuthErrorMessageInternalError
  264. @brief Message for @c FIRAuthErrorCodeInternalError error code.
  265. */
  266. static NSString *const kFIRAuthErrorMessageInternalError = @"An internal error has occurred, "
  267. "print and inspect the error details for more information.";
  268. /** @var FIRAuthErrorDescription
  269. @brief The error descrioption, based on the error code.
  270. @remarks No default case so that we get a compiler warning if a new value was added to the enum.
  271. */
  272. static NSString *FIRAuthErrorDescription(FIRAuthErrorCode code) {
  273. switch (code) {
  274. case FIRAuthErrorCodeInvalidCustomToken:
  275. return kFIRAuthErrorMessageInvalidCustomToken;
  276. case FIRAuthErrorCodeCustomTokenMismatch:
  277. return kFIRAuthErrorMessageCustomTokenMismatch;
  278. case FIRAuthErrorCodeInvalidEmail:
  279. return kFIRAuthErrorMessageInvalidEmail;
  280. case FIRAuthErrorCodeInvalidCredential:
  281. return kFIRAuthErrorMessageInvalidCredential;
  282. case FIRAuthErrorCodeUserDisabled:
  283. return kFIRAuthErrorMessageUserDisabled;
  284. case FIRAuthErrorCodeEmailAlreadyInUse:
  285. return kFIRAuthErrorMessageEmailAlreadyInUse;
  286. case FIRAuthErrorCodeWrongPassword:
  287. return kFIRAuthErrorMessageWrongPassword;
  288. case FIRAuthErrorCodeTooManyRequests:
  289. return kFIRAuthErrorMessageTooManyRequests;
  290. case FIRAuthErrorCodeAccountExistsWithDifferentCredential:
  291. return kFIRAuthErrorMessageAccountExistsWithDifferentCredential;
  292. case FIRAuthErrorCodeRequiresRecentLogin:
  293. return kFIRAuthErrorMessageRequiresRecentLogin;
  294. case FIRAuthErrorCodeProviderAlreadyLinked:
  295. return kFIRAuthErrorMessageProviderAlreadyLinked;
  296. case FIRAuthErrorCodeNoSuchProvider:
  297. return kFIRAuthErrorMessageNoSuchProvider;
  298. case FIRAuthErrorCodeInvalidUserToken:
  299. return kFIRAuthErrorMessageInvalidUserToken;
  300. case FIRAuthErrorCodeNetworkError:
  301. return kFIRAuthErrorMessageNetworkError;
  302. case FIRAuthErrorCodeKeychainError:
  303. return kFIRAuthErrorMessageKeychainError;
  304. case FIRAuthErrorCodeUserTokenExpired:
  305. return kFIRAuthErrorMessageUserTokenExpired;
  306. case FIRAuthErrorCodeUserNotFound:
  307. return kFIRAuthErrorMessageUserNotFound;
  308. case FIRAuthErrorCodeInvalidAPIKey:
  309. return kFIRAuthErrorMessageInvalidAPIKey;
  310. case FIRAuthErrorCodeCredentialAlreadyInUse:
  311. return kFIRAuthErrorMessageCredentialAlreadyInUse;
  312. case FIRAuthErrorCodeInternalError:
  313. return kFIRAuthErrorMessageInternalError;
  314. case FIRAuthErrorCodeUserMismatch:
  315. return FIRAuthErrorMessageUserMismatch;
  316. case FIRAuthErrorCodeOperationNotAllowed:
  317. return kFIRAuthErrorMessageOperationNotAllowed;
  318. case FIRAuthErrorCodeWeakPassword:
  319. return kFIRAuthErrorMessageWeakPassword;
  320. case FIRAuthErrorCodeAppNotAuthorized:
  321. return kFIRAuthErrorMessageAppNotAuthorized;
  322. case FIRAuthErrorCodeExpiredActionCode:
  323. return kFIRAuthErrorMessageExpiredActionCode;
  324. case FIRAuthErrorCodeInvalidActionCode:
  325. return kFIRAuthErrorMessageInvalidActionCode;
  326. case FIRAuthErrorCodeInvalidSender:
  327. return kFIRAuthErrorMessageInvalidSender;
  328. case FIRAuthErrorCodeInvalidMessagePayload:
  329. return kFIRAuthErrorMessageInvalidMessagePayload;
  330. case FIRAuthErrorCodeInvalidRecipientEmail:
  331. return kFIRAuthErrorMessageInvalidRecipientEmail;
  332. case FIRAuthErrorCodeMissingEmail:
  333. return kFIRAuthErrorMessageMissingEmail;
  334. case FIRAuthErrorCodeMissingPhoneNumber:
  335. return kFIRAuthErrorMessageMissingPhoneNumber;
  336. case FIRAuthErrorCodeInvalidPhoneNumber:
  337. return kFIRAuthErrorMessageInvalidPhoneNumber;
  338. case FIRAuthErrorCodeMissingVerificationCode:
  339. return kFIRAuthErrorMessageMissingVerificationCode;
  340. case FIRAuthErrorCodeInvalidVerificationCode:
  341. return kFIRAuthErrorMessageInvalidVerificationCode;
  342. case FIRAuthErrorCodeMissingVerificationID:
  343. return kFIRAuthErrorMessageMissingVerificationID;
  344. case FIRAuthErrorCodeInvalidVerificationID:
  345. return kFIRAuthErrorMessageInvalidVerificationID;
  346. case FIRAuthErrorCodeSessionExpired:
  347. return kFIRAuthErrorMessageSessionExpired;
  348. case FIRAuthErrorCodeMissingAppCredential:
  349. return kFIRAuthErrorMessageMissingAppCredential;
  350. case FIRAuthErrorCodeInvalidAppCredential:
  351. return kFIRAuthErrorMessageInvalidAppCredential;
  352. case FIRAuthErrorCodeQuotaExceeded:
  353. return kFIRAuthErrorMessageQuotaExceeded;
  354. case FIRAuthErrorCodeMissingAppToken:
  355. return kFIRAuthErrorMessageMissingAppToken;
  356. case FIRAuthErrorCodeNotificationNotForwarded:
  357. return kFIRAuthErrorMessageNotificationNotForwarded;
  358. case FIRAuthErrorCodeAppNotVerified:
  359. return kFIRAuthErrorMessageAppNotVerified;
  360. }
  361. }
  362. /** @var FIRAuthErrorCodeString
  363. @brief The the error short string, based on the error code.
  364. @remarks No default case so that we get a compiler warning if a new value was added to the enum.
  365. */
  366. static NSString *const FIRAuthErrorCodeString(FIRAuthErrorCode code) {
  367. switch (code) {
  368. case FIRAuthErrorCodeInvalidCustomToken:
  369. return @"ERROR_INVALID_CUSTOM_TOKEN";
  370. case FIRAuthErrorCodeCustomTokenMismatch:
  371. return @"ERROR_CUSTOM_TOKEN_MISMATCH";
  372. case FIRAuthErrorCodeInvalidEmail:
  373. return @"ERROR_INVALID_EMAIL";
  374. case FIRAuthErrorCodeInvalidCredential:
  375. return @"ERROR_INVALID_CREDENTIAL";
  376. case FIRAuthErrorCodeUserDisabled:
  377. return @"ERROR_USER_DISABLED";
  378. case FIRAuthErrorCodeEmailAlreadyInUse:
  379. return @"ERROR_EMAIL_ALREADY_IN_USE";
  380. case FIRAuthErrorCodeWrongPassword:
  381. return @"ERROR_WRONG_PASSWORD";
  382. case FIRAuthErrorCodeTooManyRequests:
  383. return @"ERROR_TOO_MANY_REQUESTS";
  384. case FIRAuthErrorCodeAccountExistsWithDifferentCredential:
  385. return @"ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL";
  386. case FIRAuthErrorCodeRequiresRecentLogin:
  387. return @"ERROR_REQUIRES_RECENT_LOGIN";
  388. case FIRAuthErrorCodeProviderAlreadyLinked:
  389. return @"ERROR_PROVIDER_ALREADY_LINKED";
  390. case FIRAuthErrorCodeNoSuchProvider:
  391. return @"ERROR_NO_SUCH_PROVIDER";
  392. case FIRAuthErrorCodeInvalidUserToken:
  393. return @"ERROR_INVALID_USER_TOKEN";
  394. case FIRAuthErrorCodeNetworkError:
  395. return @"ERROR_NETWORK_REQUEST_FAILED";
  396. case FIRAuthErrorCodeKeychainError:
  397. return @"ERROR_KEYCHAIN_ERROR";
  398. case FIRAuthErrorCodeUserTokenExpired:
  399. return @"ERROR_USER_TOKEN_EXPIRED";
  400. case FIRAuthErrorCodeUserNotFound:
  401. return @"ERROR_USER_NOT_FOUND";
  402. case FIRAuthErrorCodeInvalidAPIKey:
  403. return @"ERROR_INVALID_API_KEY";
  404. case FIRAuthErrorCodeCredentialAlreadyInUse:
  405. return @"ERROR_CREDENTIAL_ALREADY_IN_USE";
  406. case FIRAuthErrorCodeInternalError:
  407. return @"ERROR_INTERNAL_ERROR";
  408. case FIRAuthErrorCodeUserMismatch:
  409. return @"ERROR_USER_MISMATCH";
  410. case FIRAuthErrorCodeOperationNotAllowed:
  411. return @"ERROR_OPERATION_NOT_ALLOWED";
  412. case FIRAuthErrorCodeWeakPassword:
  413. return @"ERROR_WEAK_PASSWORD";
  414. case FIRAuthErrorCodeAppNotAuthorized:
  415. return @"ERROR_APP_NOT_AUTHORIZED";
  416. case FIRAuthErrorCodeExpiredActionCode:
  417. return @"ERROR_EXPIRED_ACTION_CODE";
  418. case FIRAuthErrorCodeInvalidActionCode:
  419. return @"ERROR_INVALID_ACTION_CODE";
  420. case FIRAuthErrorCodeInvalidMessagePayload:
  421. return @"ERROR_INVALID_MESSAGE_PAYLOAD";
  422. case FIRAuthErrorCodeInvalidSender:
  423. return @"ERROR_INVALID_SENDER";
  424. case FIRAuthErrorCodeInvalidRecipientEmail:
  425. return @"ERROR_INVALID_RECIPIENT_EMAIL";
  426. case FIRAuthErrorCodeMissingEmail:
  427. return @"MISSING_EMAIL";
  428. case FIRAuthErrorCodeMissingPhoneNumber:
  429. return @"ERROR_MISSING_PHONE_NUMBER";
  430. case FIRAuthErrorCodeInvalidPhoneNumber:
  431. return @"ERROR_INVALID_PHONE_NUMBER";
  432. case FIRAuthErrorCodeMissingVerificationCode:
  433. return @"ERROR_MISSING_VERIFICATION_CODE";
  434. case FIRAuthErrorCodeInvalidVerificationCode:
  435. return @"ERROR_INVALID_VERIFICATION_CODE";
  436. case FIRAuthErrorCodeMissingVerificationID:
  437. return @"ERROR_MISSING_VERIFICATION_ID";
  438. case FIRAuthErrorCodeInvalidVerificationID:
  439. return @"ERROR_INVALID_VERIFICATION_ID";
  440. case FIRAuthErrorCodeSessionExpired:
  441. return @"ERROR_SESSION_EXPIRED";
  442. case FIRAuthErrorCodeMissingAppCredential:
  443. return @"MISSING_APP_CREDENTIAL";
  444. case FIRAuthErrorCodeInvalidAppCredential:
  445. return @"INVALID_APP_CREDENTIAL";
  446. case FIRAuthErrorCodeQuotaExceeded:
  447. return @"ERROR_QUOTA_EXCEEDED";
  448. case FIRAuthErrorCodeMissingAppToken:
  449. return @"ERROR_MISSING_APP_TOKEN";
  450. case FIRAuthErrorCodeNotificationNotForwarded:
  451. return @"ERROR_NOTIFICATION_NOT_FORWARDED";
  452. case FIRAuthErrorCodeAppNotVerified:
  453. return @"ERROR_APP_NOT_VERIFIED";
  454. }
  455. }
  456. @implementation FIRAuthErrorUtils
  457. + (NSError *)errorWithCode:(FIRAuthInternalErrorCode)code {
  458. return [self errorWithCode:code message:nil];
  459. }
  460. + (NSError *)errorWithCode:(FIRAuthInternalErrorCode)code
  461. message:(nullable NSString *)message {
  462. NSDictionary *userInfo = nil;
  463. if (message.length) {
  464. userInfo = @{
  465. NSLocalizedDescriptionKey : message
  466. };
  467. }
  468. return [self errorWithCode:code userInfo:userInfo];
  469. }
  470. + (NSError *)errorWithCode:(FIRAuthInternalErrorCode)code
  471. underlyingError:(nullable NSError *)underlyingError {
  472. NSDictionary *errorUserInfo = nil;
  473. if (underlyingError) {
  474. errorUserInfo = @{
  475. NSUnderlyingErrorKey : underlyingError
  476. };
  477. }
  478. return [self errorWithCode:code userInfo:errorUserInfo];
  479. }
  480. + (NSError *)errorWithCode:(FIRAuthInternalErrorCode)code userInfo:(NSDictionary *)userInfo {
  481. BOOL isPublic = (code & FIRAuthPublicErrorCodeFlag) == FIRAuthPublicErrorCodeFlag;
  482. if (isPublic) {
  483. // This is a public error. Return it as a public error and add a description.
  484. NSInteger errorCode = code & ~FIRAuthPublicErrorCodeFlag;
  485. NSMutableDictionary *errorUserInfo = [NSMutableDictionary dictionaryWithDictionary:userInfo];
  486. if (!errorUserInfo[NSLocalizedDescriptionKey]) {
  487. errorUserInfo[NSLocalizedDescriptionKey] = FIRAuthErrorDescription(errorCode);
  488. }
  489. errorUserInfo[FIRAuthErrorNameKey] = FIRAuthErrorCodeString(errorCode);
  490. return [NSError errorWithDomain:FIRAuthErrorDomain code:errorCode userInfo:errorUserInfo];
  491. } else {
  492. // This is an internal error. Wrap it in an internal error.
  493. NSError *error =
  494. [NSError errorWithDomain:FIRAuthInternalErrorDomain code:code userInfo:userInfo];
  495. return [self errorWithCode:FIRAuthInternalErrorCodeInternalError underlyingError:error];
  496. }
  497. }
  498. + (NSError *)RPCRequestEncodingErrorWithUnderlyingError:(NSError *)underlyingError {
  499. return [self errorWithCode:FIRAuthInternalErrorCodeRPCRequestEncodingError
  500. underlyingError:underlyingError];
  501. }
  502. + (NSError *)JSONSerializationErrorForUnencodableType {
  503. return [self errorWithCode:FIRAuthInternalErrorCodeJSONSerializationError];
  504. }
  505. + (NSError *)JSONSerializationErrorWithUnderlyingError:(NSError *)underlyingError {
  506. return [self errorWithCode:FIRAuthInternalErrorCodeJSONSerializationError
  507. underlyingError:underlyingError];
  508. }
  509. + (NSError *)networkErrorWithUnderlyingError:(NSError *)underlyingError {
  510. return [self errorWithCode:FIRAuthInternalErrorCodeNetworkError
  511. underlyingError:underlyingError];
  512. }
  513. + (NSError *)unexpectedErrorResponseWithData:(NSData *)data
  514. underlyingError:(NSError *)underlyingError {
  515. return [self errorWithCode:FIRAuthInternalErrorCodeUnexpectedErrorResponse userInfo:@{
  516. FIRAuthErrorUserInfoDataKey : data,
  517. NSUnderlyingErrorKey : underlyingError
  518. }];
  519. }
  520. + (NSError *)unexpectedErrorResponseWithDeserializedResponse:(id)deserializedResponse {
  521. return [self errorWithCode:FIRAuthInternalErrorCodeUnexpectedErrorResponse userInfo:@{
  522. FIRAuthErrorUserInfoDeserializedResponseKey : deserializedResponse
  523. }];
  524. }
  525. + (NSError *)unexpectedResponseWithData:(NSData *)data
  526. underlyingError:(NSError *)underlyingError {
  527. return [self errorWithCode:FIRAuthInternalErrorCodeUnexpectedResponse userInfo:@{
  528. FIRAuthErrorUserInfoDataKey : data,
  529. NSUnderlyingErrorKey : underlyingError
  530. }];
  531. }
  532. + (NSError *)unexpectedResponseWithDeserializedResponse:(id)deserializedResponse {
  533. return [self errorWithCode:FIRAuthInternalErrorCodeUnexpectedResponse userInfo:@{
  534. FIRAuthErrorUserInfoDeserializedResponseKey : deserializedResponse
  535. }];
  536. }
  537. + (NSError *)unexpectedResponseWithDeserializedResponse:(nullable id)deserializedResponse
  538. underlyingError:(NSError *)underlyingError {
  539. NSMutableDictionary *userInfo =
  540. [NSMutableDictionary dictionaryWithDictionary:@{ NSUnderlyingErrorKey : underlyingError }];
  541. if (deserializedResponse) {
  542. userInfo[FIRAuthErrorUserInfoDeserializedResponseKey] = deserializedResponse;
  543. }
  544. return [self errorWithCode:FIRAuthInternalErrorCodeUnexpectedResponse userInfo:userInfo];
  545. }
  546. + (NSError *)RPCResponseDecodingErrorWithDeserializedResponse:(id)deserializedResponse
  547. underlyingError:(NSError *)underlyingError {
  548. return [self errorWithCode:FIRAuthInternalErrorCodeRPCResponseDecodingError userInfo:@{
  549. FIRAuthErrorUserInfoDeserializedResponseKey : deserializedResponse,
  550. NSUnderlyingErrorKey : underlyingError
  551. }];
  552. }
  553. + (NSError *)emailAlreadyInUseErrorWithEmail:(nullable NSString *)email {
  554. NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
  555. if (email.length) {
  556. userInfo[FIRAuthErrorUserInfoEmailKey] = email;
  557. }
  558. return [self errorWithCode:FIRAuthInternalErrorCodeEmailAlreadyInUse userInfo:userInfo];
  559. }
  560. + (NSError *)userDisabledErrorWithMessage:(nullable NSString *)message {
  561. return [self errorWithCode:FIRAuthInternalErrorCodeUserDisabled message:message];
  562. }
  563. + (NSError *)wrongPasswordErrorWithMessage:(nullable NSString *)message {
  564. return [self errorWithCode:FIRAuthInternalErrorCodeWrongPassword message:message];
  565. }
  566. + (NSError *)tooManyRequestsErrorWithMessage:(nullable NSString *)message {
  567. return [self errorWithCode:FIRAuthInternalErrorCodeTooManyRequests message:message];
  568. }
  569. + (NSError *)invalidCustomTokenErrorWithMessage:(nullable NSString *)message {
  570. return [self errorWithCode:FIRAuthInternalErrorCodeInvalidCustomToken message:message];
  571. }
  572. + (NSError *)customTokenMistmatchErrorWithMessage:(nullable NSString *)message {
  573. return [self errorWithCode:FIRAuthInternalErrorCodeCustomTokenMismatch message:message];
  574. }
  575. + (NSError *)invalidCredentialErrorWithMessage:(nullable NSString *)message {
  576. return [self errorWithCode:FIRAuthInternalErrorCodeInvalidCredential message:message];
  577. }
  578. + (NSError *)requiresRecentLoginErrorWithMessage:(nullable NSString *)message {
  579. return [self errorWithCode:FIRAuthInternalErrorCodeRequiresRecentLogin message:message];
  580. }
  581. + (NSError *)invalidUserTokenErrorWithMessage:(nullable NSString *)message {
  582. return [self errorWithCode:FIRAuthInternalErrorCodeInvalidUserToken message:message];
  583. }
  584. + (NSError *)invalidEmailErrorWithMessage:(nullable NSString *)message {
  585. return [self errorWithCode:FIRAuthInternalErrorCodeInvalidEmail message:message];
  586. }
  587. + (NSError *)accountExistsWithDifferentCredentialErrorWithEmail:(nullable NSString *)email {
  588. return [self errorWithCode:FIRAuthInternalErrorCodeAccountExistsWithDifferentCredential
  589. userInfo:@{ FIRAuthErrorUserInfoEmailKey : email }];
  590. }
  591. + (NSError *)providerAlreadyLinkedError {
  592. return [self errorWithCode:FIRAuthInternalErrorCodeProviderAlreadyLinked];
  593. }
  594. + (NSError *)noSuchProviderError {
  595. return [self errorWithCode:FIRAuthInternalErrorCodeNoSuchProvider];
  596. }
  597. + (NSError *)userTokenExpiredErrorWithMessage:(nullable NSString *)message {
  598. return [self errorWithCode:FIRAuthInternalErrorCodeUserTokenExpired message:message];
  599. }
  600. + (NSError *)userNotFoundErrorWithMessage:(nullable NSString *)message {
  601. return [self errorWithCode:FIRAuthInternalErrorCodeUserNotFound message:message];
  602. }
  603. + (NSError *)invalidAPIKeyError {
  604. return [self errorWithCode:FIRAuthInternalErrorCodeInvalidAPIKey];
  605. }
  606. + (NSError *)userMismatchError {
  607. return [self errorWithCode:FIRAuthInternalErrorCodeUserMismatch];
  608. }
  609. + (NSError *)credentialAlreadyInUseErrorWithMessage:(nullable NSString *)message
  610. credential:(nullable FIRPhoneAuthCredential *)credential {
  611. if (credential) {
  612. return [self errorWithCode:FIRAuthInternalErrorCodeCredentialAlreadyInUse
  613. userInfo:@{ FIRAuthUpdatedCredentialKey : credential }];
  614. }
  615. return [self errorWithCode:FIRAuthInternalErrorCodeCredentialAlreadyInUse message:message];
  616. }
  617. + (NSError *)operationNotAllowedErrorWithMessage:(nullable NSString *)message {
  618. return [self errorWithCode:FIRAuthInternalErrorCodeOperationNotAllowed message:message];
  619. }
  620. + (NSError *)weakPasswordErrorWithServerResponseReason:(NSString *)reason {
  621. return [self errorWithCode:FIRAuthInternalErrorCodeWeakPassword userInfo:@{
  622. NSLocalizedFailureReasonErrorKey : reason
  623. }];
  624. }
  625. + (NSError *)appNotAuthorizedError {
  626. return [self errorWithCode:FIRAuthInternalErrorCodeAppNotAuthorized];
  627. }
  628. + (NSError *)expiredActionCodeErrorWithMessage:(nullable NSString *)message {
  629. return [self errorWithCode:FIRAuthInternalErrorCodeExpiredActionCode message:message];
  630. }
  631. + (NSError *)invalidActionCodeErrorWithMessage:(nullable NSString *)message {
  632. return [self errorWithCode:FIRAuthInternalErrorCodeInvalidActionCode message:message];
  633. }
  634. + (NSError *)invalidMessagePayloadErrorWithMessage:(nullable NSString *)message {
  635. return [self errorWithCode:FIRAuthInternalErrorCodeInvalidMessagePayload message:message];
  636. }
  637. + (NSError *)invalidSenderErrorWithMessage:(nullable NSString *)message {
  638. return [self errorWithCode:FIRAuthInternalErrorCodeInvalidSender message:message];
  639. }
  640. + (NSError *)invalidRecipientEmailErrorWithMessage:(nullable NSString *)message {
  641. return [self errorWithCode:FIRAuthInternalErrorCodeInvalidRecipientEmail message:message];
  642. }
  643. + (NSError *)missingEmail {
  644. return [self errorWithCode:FIRAuthInternalErrorCodeMissingEmail];
  645. }
  646. + (NSError *)missingPhoneNumberErrorWithMessage:(nullable NSString *)message {
  647. return [self errorWithCode:FIRAuthInternalErrorCodeMissingPhoneNumber message:message];
  648. }
  649. + (NSError *)invalidPhoneNumberErrorWithMessage:(nullable NSString *)message {
  650. return [self errorWithCode:FIRAuthInternalErrorCodeInvalidPhoneNumber message:message];
  651. }
  652. + (NSError *)missingVerificationCodeErrorWithMessage:(nullable NSString *)message {
  653. return [self errorWithCode:FIRAuthInternalErrorCodeMissingVerificationCode message:message];
  654. }
  655. + (NSError *)invalidVerificationCodeErrorWithMessage:(nullable NSString *)message {
  656. return [self errorWithCode:FIRAuthInternalErrorCodeInvalidVerificationCode message:message];
  657. }
  658. + (NSError *)missingVerificationIDErrorWithMessage:(nullable NSString *)message {
  659. return [self errorWithCode:FIRAuthInternalErrorCodeMissingVerificationID message:message];
  660. }
  661. + (NSError *)invalidVerificationIDErrorWithMessage:(nullable NSString *)message {
  662. return [self errorWithCode:FIRAuthInternalErrorCodeInvalidVerificationID message:message];
  663. }
  664. + (NSError *)sessionExpiredErrorWithMessage:(nullable NSString *)message {
  665. return [self errorWithCode:FIRAuthInternalErrorCodeSessionExpired message:message];
  666. }
  667. + (NSError *)missingAppCredentialWithMessage:(nullable NSString *)message {
  668. return [self errorWithCode:FIRAuthInternalErrorCodeMissingAppCredential message:message];
  669. }
  670. + (NSError *)invalidAppCredentialWithMessage:(nullable NSString *)message {
  671. return [self errorWithCode:FIRAuthInternalErrorCodeInvalidAppCredential message:message];
  672. }
  673. + (NSError *)quotaExceededErrorWithMessage:(nullable NSString *)message {
  674. return [self errorWithCode:FIRAuthInternalErrorCodeQuotaExceeded message:message];
  675. }
  676. + (NSError *)missingAppTokenError {
  677. return [self errorWithCode:FIRAuthInternalErrorCodeMissingAppToken];
  678. }
  679. + (NSError *)notificationNotForwardedError {
  680. return [self errorWithCode:FIRAuthInternalErrorCodeNotificationNotForwarded];
  681. }
  682. + (NSError *)appNotVerifiedErrorWithMessage:(nullable NSString *)message {
  683. return [self errorWithCode:FIRAuthInternalErrorCodeAppNotVerified message:message];
  684. }
  685. + (NSError *)keychainErrorWithFunction:(NSString *)keychainFunction status:(OSStatus)status {
  686. NSString *failureReason = [NSString stringWithFormat:@"%@ (%li)", keychainFunction, (long)status];
  687. return [self errorWithCode:FIRAuthInternalErrorCodeKeychainError userInfo:@{
  688. NSLocalizedFailureReasonErrorKey : failureReason,
  689. }];
  690. }
  691. @end
  692. NS_ASSUME_NONNULL_END