From c56857ea816d49d594ce3380fe9239b74e288516 Mon Sep 17 00:00:00 2001 From: Martin Walsh Date: Thu, 18 May 2017 13:55:46 +0100 Subject: [PATCH] Added reason to failedRefresh error --- Auth0/CredentialsManager.swift | 8 ++++---- Auth0/CredentialsManagerError.swift | 2 +- Auth0Tests/CredentialsManagerSpec.swift | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Auth0/CredentialsManager.swift b/Auth0/CredentialsManager.swift index 7971103c..12db2d96 100644 --- a/Auth0/CredentialsManager.swift +++ b/Auth0/CredentialsManager.swift @@ -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 { @@ -62,12 +61,13 @@ 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) } @@ -75,8 +75,8 @@ public struct CredentialsManager { switch $0 { case .success(let credentials): callback(nil, credentials) - case .failure(_): - callback(.failedRefresh, nil) + case .failure(let error): + callback(.failedRefresh(error.localizedDescription), nil) } } } diff --git a/Auth0/CredentialsManagerError.swift b/Auth0/CredentialsManagerError.swift index a1a40e06..152e0fb3 100644 --- a/Auth0/CredentialsManagerError.swift +++ b/Auth0/CredentialsManagerError.swift @@ -25,5 +25,5 @@ import Foundation public enum CredentialsManagerError: Error { case noCredentials case noRefreshToken - case failedRefresh + case failedRefresh(String) } diff --git a/Auth0Tests/CredentialsManagerSpec.swift b/Auth0Tests/CredentialsManagerSpec.swift index acf515bc..1f99d221 100644 --- a/Auth0Tests/CredentialsManagerSpec.swift +++ b/Auth0Tests/CredentialsManagerSpec.swift @@ -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()) } @@ -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() }