Skip to content

Commit

Permalink
Streamline CredentialManagerErrors
Browse files Browse the repository at this point in the history
Update Tests
  • Loading branch information
cocojoe committed May 17, 2017
1 parent 8350850 commit 52ccc59
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
10 changes: 5 additions & 5 deletions Auth0/CredentialsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public struct CredentialsManager {
return self.storage.setData(NSKeyedArchiver.archivedData(withRootObject: credentials), forKey: storeKey)
}

/// Retrieve credentials from keychain and yield new credentials if the accessToken has expired
/// 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)
Expand All @@ -67,16 +67,16 @@ public struct CredentialsManager {
let data = self.storage.data(forKey:self.storeKey),
let credentials = NSKeyedUnarchiver.unarchiveObject(with: data) as? Credentials
else { return callback(.noCredentials, nil) }
guard let refreshToken = credentials.refreshToken else { return callback(.noRefreshToken, nil) }
guard let expiresIn = credentials.expiresIn else { return callback(.noExpiresIn, nil) }
guard let expiresIn = credentials.expiresIn else { return callback(.failedRefresh, 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(let error):
callback(.renewFailed(error), nil)
case .failure(_):
callback(.failedRefresh, nil)
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions Auth0/CredentialsManagerError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@ import Foundation
public enum CredentialsManagerError: Error {
case noCredentials
case noRefreshToken
case noExpiresIn
case renewFailed(Error)
case failedRefresh
}
21 changes: 17 additions & 4 deletions Auth0Tests/CredentialsManagerSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class CredentialsManagerSpec: QuickSpec {
beforeEach {
error = nil
newCredentials = nil
stub(condition: isToken(Domain) && hasAtLeast(["refresh_token": RefreshToken])) { _ in return authResponse(accessToken: AccessToken) }.name = "refresh_token login"
stub(condition: isToken(Domain) && hasAtLeast(["refresh_token": RefreshToken])) { _ in return authResponse(accessToken: AccessToken) }.name = "renew success"
}

afterEach {
Expand All @@ -78,8 +78,8 @@ class CredentialsManagerSpec: QuickSpec {
expect(newCredentials).toEventually(beNil())
}

it("should error when no refresh_token present") {
credentials = Credentials(accessToken: AccessToken, tokenType: TokenType, idToken: IdToken, refreshToken: nil, expiresIn: Date(timeIntervalSinceNow: ExpiresIn))
it("should error when no refresh_token present and token expired") {
credentials = Credentials(accessToken: AccessToken, tokenType: TokenType, idToken: IdToken, refreshToken: nil, expiresIn: Date(timeIntervalSinceNow: -ExpiresIn))
_ = credentialsManager.store(credentials: credentials)
credentialsManager.credentials { error = $0; newCredentials = $1 }
expect(error).to(matchError(CredentialsManagerError.noRefreshToken))
Expand All @@ -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.noExpiresIn))
expect(error).to(matchError(CredentialsManagerError.failedRefresh))
expect(newCredentials).toEventually(beNil())
}

Expand All @@ -112,6 +112,19 @@ class CredentialsManagerSpec: QuickSpec {
}
}
}

it("should yield error on failed renew") {
stub(condition: isToken(Domain) && hasAtLeast(["refresh_token": RefreshToken])) { _ in return authFailure(code: "invalid_request", description: "missing_params") }.name = "renew failed"
credentials = Credentials(accessToken: AccessToken, tokenType: TokenType, idToken: IdToken, refreshToken: RefreshToken, expiresIn: Date(timeIntervalSinceNow: -3600))
_ = credentialsManager.store(credentials: credentials)
waitUntil(timeout: 2) { done in
credentialsManager.credentials { error = $0; newCredentials = $1
expect(error).to(matchError(CredentialsManagerError.failedRefresh))
expect(newCredentials).to(beNil())
done()
}
}
}

}
}
Expand Down

0 comments on commit 52ccc59

Please sign in to comment.