Skip to content

Commit aecbf63

Browse files
committed
user profile() error refactored
1 parent d52dfee commit aecbf63

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

Auth0/CredentialsManager.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public struct CredentialsManager: Sendable {
7979
/// ```
8080
///
8181
/// - Throws: A ``CredentialsManagerError`` if the credentials cannot be read from storage or the ID token cannot be decoded.
82+
/// The underlying `Error` can be accessed via the ``Auth0Error/cause-swift.property`` property.
8283
/// - Returns: The ``UserProfile`` extracted from the stored ID token, or `nil` if the ID token payload cannot be parsed.
8384
/// - Important: Access to this method will not be protected by biometric authentication.
8485
public func userProfile() throws -> UserProfile? {
@@ -89,7 +90,7 @@ public struct CredentialsManager: Sendable {
8990
let jwt = try decode(jwt: credentials.idToken)
9091
return UserProfile(json: jwt.body)
9192
} catch {
92-
throw CredentialsManagerError(code: .noCredentials, cause: error)
93+
throw CredentialsManagerError(code: .unknown, cause: error)
9394
}
9495
}
9596

Auth0/CredentialsManagerError.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public struct CredentialsManagerError: Auth0Error, Sendable {
1313
case clearFailed
1414
case biometricsFailed
1515
case revokeFailed
16+
case unknown
1617
case largeMinTTL(minTTL: Int, lifetime: Int)
1718
}
1819

@@ -71,7 +72,11 @@ public struct CredentialsManagerError: Auth0Error, Sendable {
7172
/// The underlying ``AuthenticationError`` can be accessed via the ``Auth0Error/cause-9wuyi`` property.
7273
public static let revokeFailed: CredentialsManagerError = .init(code: .revokeFailed)
7374

74-
/// The `minTTL` requested is greater than the lifetime of the renewed access token. Request a lower `minTTL` or
75+
/// An unknown error occurred.
76+
/// The underlying `Error` can be accessed via the ``Auth0Error/cause-9wuyi`` property.
77+
public static let unknown: CredentialsManagerError = .init(code: .unknown)
78+
79+
/// The `minTTL` requested is greater than the lifetime of the renewed access token. Request a lower `minTTL` or
7580
/// increase the **Token Expiration** value in the settings page of your [Auth0 API](https://manage.auth0.com/#/apis/).
7681
/// This error does not include a ``Auth0Error/cause-9wuyi``.
7782
public static let largeMinTTL: CredentialsManagerError = .init(code: .largeMinTTL(minTTL: 0, lifetime: 0))
@@ -92,6 +97,7 @@ public extension CredentialsManagerError {
9297
return "Clearing of the credentials failed"
9398
case .biometricsFailed: return "The biometric authentication failed."
9499
case .revokeFailed: return "The revocation of the refresh token failed."
100+
case .unknown: return "An unknown error occurred."
95101
case .largeMinTTL(let minTTL, let lifetime): return "The minTTL requested (\(minTTL)s) is greater than the"
96102
+ " lifetime of the renewed access token (\(lifetime)s). Request a lower minTTL or increase the"
97103
+ " 'Token Expiration' value in the settings page of your Auth0 API."

Auth0Tests/CredentialsManagerErrorSpec.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ class CredentialsManagerErrorSpec: QuickSpec {
127127
expect(error.localizedDescription) == message
128128
}
129129

130+
it("should return message for unknown error") {
131+
let message = "An unknown error occurred."
132+
let error = CredentialsManagerError(code: .unknown)
133+
expect(error.localizedDescription) == message
134+
}
135+
130136
it("should return message when minTTL is too big") {
131137
let minTTL = 7200
132138
let lifetime = 3600

Auth0Tests/CredentialsManagerSpec.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ class CredentialsManagerSpec: QuickSpec {
374374
expect { try failingManager.store(apiCredentials: apiCreds, forAudience: Audience) }.to(throwError())
375375
}
376376

377-
it("should return nil for user when storage throws") {
377+
it("should throw unknown error with cause when storage throws") {
378378
class FailingReadStore: CredentialsStorage {
379379
func getEntry(forKey key: String) throws -> Data {
380380
throw SimpleKeychainError.itemNotFound
@@ -384,7 +384,14 @@ class CredentialsManagerSpec: QuickSpec {
384384
}
385385

386386
let failingManager = CredentialsManager(authentication: authentication, storage: FailingReadStore())
387-
expect(try? failingManager.userProfile()).to(beNil())
387+
var thrownError: CredentialsManagerError?
388+
expect {
389+
try failingManager.userProfile()
390+
}.to(throwError { error in
391+
thrownError = error as? CredentialsManagerError
392+
})
393+
expect(thrownError?.code) == CredentialsManagerError.Code.unknown
394+
expect(thrownError?.cause).notTo(beNil())
388395
}
389396

390397
it("should return false for hasValid when storage throws") {
@@ -652,7 +659,14 @@ class CredentialsManagerSpec: QuickSpec {
652659
it("should not retrieve the user profile when the id token is not a jwt") {
653660
let credentials = Credentials(accessToken: AccessToken, idToken: "not a jwt", expiresAt: Date(timeIntervalSinceNow: ExpiresIn))
654661
expect { try credentialsManager.store(credentials: credentials) }.toNot(throwError())
655-
expect { try credentialsManager.userProfile() }.to(throwError())
662+
var thrownError: CredentialsManagerError?
663+
expect {
664+
try credentialsManager.userProfile()
665+
}.to(throwError { error in
666+
thrownError = error as? CredentialsManagerError
667+
})
668+
expect(thrownError?.code) == CredentialsManagerError.Code.unknown
669+
expect(thrownError?.cause).notTo(beNil())
656670
}
657671

658672
}

0 commit comments

Comments
 (0)