FIRAppCheckErrorUtil.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright 2020 Google LLC
  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 "FirebaseAppCheck/Sources/Core/Errors/FIRAppCheckErrorUtil.h"
  17. #import <GoogleUtilities/GULKeychainUtils.h>
  18. #import "FirebaseAppCheck/Sources/Core/Errors/FIRAppCheckHTTPError.h"
  19. #import "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRAppCheckErrors.h"
  20. @implementation FIRAppCheckErrorUtil
  21. + (NSError *)publicDomainErrorWithError:(NSError *)error {
  22. if ([error.domain isEqualToString:FIRAppCheckErrorDomain]) {
  23. return error;
  24. }
  25. return [self unknownErrorWithError:error];
  26. }
  27. #pragma mark - Internal errors
  28. + (NSError *)cachedTokenNotFound {
  29. NSString *failureReason = [NSString stringWithFormat:@"Cached token not found."];
  30. return [self appCheckErrorWithCode:FIRAppCheckErrorCodeUnknown
  31. failureReason:failureReason
  32. underlyingError:nil];
  33. }
  34. + (NSError *)cachedTokenExpired {
  35. NSString *failureReason = [NSString stringWithFormat:@"Cached token expired."];
  36. return [self appCheckErrorWithCode:FIRAppCheckErrorCodeUnknown
  37. failureReason:failureReason
  38. underlyingError:nil];
  39. }
  40. + (NSError *)keychainErrorWithError:(NSError *)error {
  41. if ([error.domain isEqualToString:kGULKeychainUtilsErrorDomain]) {
  42. NSString *failureReason = [NSString stringWithFormat:@"Keychain access error."];
  43. return [self appCheckErrorWithCode:FIRAppCheckErrorCodeKeychain
  44. failureReason:failureReason
  45. underlyingError:error];
  46. }
  47. return [self unknownErrorWithError:error];
  48. }
  49. + (FIRAppCheckHTTPError *)APIErrorWithHTTPResponse:(NSHTTPURLResponse *)HTTPResponse
  50. data:(nullable NSData *)data {
  51. return [[FIRAppCheckHTTPError alloc] initWithHTTPResponse:HTTPResponse data:data];
  52. }
  53. + (NSError *)APIErrorWithNetworkError:(NSError *)networkError {
  54. NSString *failureReason = [NSString stringWithFormat:@"API request error."];
  55. return [self appCheckErrorWithCode:FIRAppCheckErrorCodeServerUnreachable
  56. failureReason:failureReason
  57. underlyingError:networkError];
  58. }
  59. + (NSError *)appCheckTokenResponseErrorWithMissingField:(NSString *)fieldName {
  60. NSString *failureReason = [NSString
  61. stringWithFormat:@"Unexpected app check token response format. Field `%@` is missing.",
  62. fieldName];
  63. return [self appCheckErrorWithCode:FIRAppCheckErrorCodeUnknown
  64. failureReason:failureReason
  65. underlyingError:nil];
  66. }
  67. + (NSError *)appAttestAttestationResponseErrorWithMissingField:(NSString *)fieldName {
  68. NSString *failureReason =
  69. [NSString stringWithFormat:@"Unexpected attestation response format. Field `%@` is missing.",
  70. fieldName];
  71. return [self appCheckErrorWithCode:FIRAppCheckErrorCodeUnknown
  72. failureReason:failureReason
  73. underlyingError:nil];
  74. }
  75. + (NSError *)JSONSerializationError:(NSError *)error {
  76. NSString *failureReason = [NSString stringWithFormat:@"JSON serialization error."];
  77. return [self appCheckErrorWithCode:FIRAppCheckErrorCodeUnknown
  78. failureReason:failureReason
  79. underlyingError:error];
  80. }
  81. + (NSError *)unsupportedAttestationProvider:(NSString *)providerName {
  82. NSString *failureReason = [NSString
  83. stringWithFormat:
  84. @"The attestation provider %@ is not supported on current platform and OS version.",
  85. providerName];
  86. return [self appCheckErrorWithCode:FIRAppCheckErrorCodeUnsupported
  87. failureReason:failureReason
  88. underlyingError:nil];
  89. }
  90. + (NSError *)appAttestKeyIDNotFound {
  91. NSString *failureReason = [NSString stringWithFormat:@"App attest key ID not found."];
  92. return [self appCheckErrorWithCode:FIRAppCheckErrorCodeUnknown
  93. failureReason:failureReason
  94. underlyingError:nil];
  95. }
  96. + (NSError *)errorWithFailureReason:(NSString *)failureReason {
  97. return [self appCheckErrorWithCode:FIRAppCheckErrorCodeUnknown
  98. failureReason:failureReason
  99. underlyingError:nil];
  100. }
  101. #pragma mark - Helpers
  102. + (NSError *)unknownErrorWithError:(NSError *)error {
  103. NSString *failureReason = error.userInfo[NSLocalizedFailureReasonErrorKey];
  104. return [self appCheckErrorWithCode:FIRAppCheckErrorCodeUnknown
  105. failureReason:failureReason
  106. underlyingError:error];
  107. }
  108. + (NSError *)appCheckErrorWithCode:(FIRAppCheckErrorCode)code
  109. failureReason:(nullable NSString *)failureReason
  110. underlyingError:(nullable NSError *)underlyingError {
  111. NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
  112. userInfo[NSUnderlyingErrorKey] = underlyingError;
  113. userInfo[NSLocalizedFailureReasonErrorKey] = failureReason;
  114. return [NSError errorWithDomain:FIRAppCheckErrorDomain code:code userInfo:userInfo];
  115. }
  116. @end
  117. void FIRAppCheckSetErrorToPointer(NSError *error, NSError **pointer) {
  118. if (pointer != NULL) {
  119. *pointer = error;
  120. }
  121. }