Skip to content

Commit

Permalink
Added reason to failedRefresh error
Browse files Browse the repository at this point in the history
  • Loading branch information
cocojoe committed May 18, 2017
1 parent 52ccc59 commit c56857e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
8 changes: 4 additions & 4 deletions Auth0/CredentialsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public struct CredentialsManager {
/// Retrieve credentials from keychain and yield new credentials using refreshToken if accessToken has expired
/// otherwise the retrieved credentails will be returned as they have not expired.
///
/// - seeAlso: [Auth0 Refresh Tokens Docs](https://auth0.com/docs/tokens/refresh-token)
///
/// ```
/// credentialsManager.credentials {
Expand All @@ -62,21 +61,22 @@ public struct CredentialsManager {
/// - scope: scopes to request for the new tokens. By default is nil which will ask for the same ones requested during original Auth
/// - callback: callback with the user's credentials or the cause of the error.
/// - Important: This method only works for a refresh token obtained after auth with OAuth 2.0 API Authorization.
/// - Note: [Auth0 Refresh Tokens Docs](https://auth0.com/docs/tokens/refresh-token)
public func credentials(withScope scope: String? = nil, callback: @escaping (CredentialsManagerError?, Credentials?) -> Void) {
guard
let data = self.storage.data(forKey:self.storeKey),
let credentials = NSKeyedUnarchiver.unarchiveObject(with: data) as? Credentials
else { return callback(.noCredentials, nil) }
guard let expiresIn = credentials.expiresIn else { return callback(.failedRefresh, nil) }
guard let expiresIn = credentials.expiresIn else { return callback(.failedRefresh("no_expiry"), nil) }
guard expiresIn < Date() else { return callback(nil, credentials) }
guard let refreshToken = credentials.refreshToken else { return callback(.noRefreshToken, nil) }

self.authentication.renew(withRefreshToken: refreshToken, scope: scope).start {
switch $0 {
case .success(let credentials):
callback(nil, credentials)
case .failure(_):
callback(.failedRefresh, nil)
case .failure(let error):
callback(.failedRefresh(error.localizedDescription), nil)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Auth0/CredentialsManagerError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ import Foundation
public enum CredentialsManagerError: Error {
case noCredentials
case noRefreshToken
case failedRefresh
case failedRefresh(String)
}
4 changes: 2 additions & 2 deletions Auth0Tests/CredentialsManagerSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class CredentialsManagerSpec: QuickSpec {
credentials = Credentials(accessToken: AccessToken, tokenType: TokenType, idToken: IdToken, refreshToken: RefreshToken, expiresIn: nil)
_ = credentialsManager.store(credentials: credentials)
credentialsManager.credentials { error = $0; newCredentials = $1 }
expect(error).to(matchError(CredentialsManagerError.failedRefresh))
expect(error).to(matchError(CredentialsManagerError.failedRefresh("no_expiry")))
expect(newCredentials).toEventually(beNil())
}

Expand Down Expand Up @@ -119,7 +119,7 @@ class CredentialsManagerSpec: QuickSpec {
_ = credentialsManager.store(credentials: credentials)
waitUntil(timeout: 2) { done in
credentialsManager.credentials { error = $0; newCredentials = $1
expect(error).to(matchError(CredentialsManagerError.failedRefresh))
expect(error).to(matchError(CredentialsManagerError.failedRefresh("invalid_request")))
expect(newCredentials).to(beNil())
done()
}
Expand Down

0 comments on commit c56857e

Please sign in to comment.