Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CredentialManager: add hasValidRefreshToken() method #701

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions Auth0/CredentialsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ public struct CredentialsManager {
/// - See: [Refresh tokens](https://auth0.com/docs/secure/tokens/refresh-tokens)
/// - See: [Authentication API Endpoint](https://auth0.com/docs/api/authentication#revoke-refresh-token)
public func revoke(headers: [String: String] = [:], _ callback: @escaping (CredentialsManagerResult<Void>) -> Void) {
guard let data = self.storage.getEntry(forKey: self.storeKey),
let credentials = try? NSKeyedUnarchiver.unarchivedObject(ofClass: Credentials.self, from: data),
guard let credentials = self.retrieveCredentials(),
let refreshToken = credentials.refreshToken else {
_ = self.clear()
return callback(.success(()))
Expand Down Expand Up @@ -168,11 +167,19 @@ public struct CredentialsManager {
/// - Returns: If there are credentials stored containing an access token that is neither expired or about to expire.
/// - See: ``Credentials/expiresIn``
public func hasValid(minTTL: Int = 0) -> Bool {
guard let data = self.storage.getEntry(forKey: self.storeKey),
let credentials = try? NSKeyedUnarchiver.unarchivedObject(ofClass: Credentials.self, from: data) else { return false }
guard let credentials = self.retrieveCredentials() else { return false }
return !self.hasExpired(credentials) && !self.willExpire(credentials, within: minTTL)
}

/// Checks that there are credentials stored, and that the credential has a valid refresh token
///
/// - Returns: If there are credentials stored containing an refresh token.
/// - See: ``Credentials/refreshToken``
public func hasValidRefreshToken() -> Bool {
guard let credentials = self.retrieveCredentials() else { return false }
return credentials.refreshToken != nil
}

#if WEB_AUTH_PLATFORM
/// Retrieves credentials from the Keychain and yields new credentials using the refresh token if the access token
/// is expired. Otherwise, the retrieved credentials will be returned via the success case as they are not expired.
Expand Down