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

CredentialsManager function to clear and revoke the refresh token #312

Merged
merged 14 commits into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions App/Assets.xcassets/AppIcon.appiconset/Contents.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
"idiom" : "iphone",
"size" : "60x60",
"scale" : "3x"
},
{
"idiom" : "ios-marketing",
stevehobbsdev marked this conversation as resolved.
Show resolved Hide resolved
"size" : "1024x1024",
"scale" : "1x"
}
],
"info" : {
Expand Down
10 changes: 5 additions & 5 deletions App/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11542" systemVersion="16B2555" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14868" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11524"/>
<deployment identifier="iOS"/>
stevehobbsdev marked this conversation as resolved.
Show resolved Hide resolved
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14824"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand Down Expand Up @@ -99,6 +98,7 @@
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="138" y="132"/>
</scene>
</scenes>
</document>
27 changes: 27 additions & 0 deletions Auth0/CredentialsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,37 @@ public struct CredentialsManager {
/// Clear credentials stored in keychain
///
/// - Returns: if credentials were removed
@available(*, deprecated, message: "use clearAndRevokeToken(callback) instead")
stevehobbsdev marked this conversation as resolved.
Show resolved Hide resolved
public func clear() -> Bool {
return self.storage.deleteEntry(forKey: storeKey)
}

/// Revokes the refresh token and, if successful, the credentials are cleared. Otherwise,
/// the credentials are not cleared and an error is raised through the callback.
lbalmaceda marked this conversation as resolved.
Show resolved Hide resolved
///
/// - Parameter callback: callback with an error if the refresh token could not be revoked
public func clearAndRevokeToken(_ callback: @escaping (CredentialsManagerError?) -> Void) {
stevehobbsdev marked this conversation as resolved.
Show resolved Hide resolved
guard
let data = self.storage.data(forKey: self.storeKey),
let credentials = NSKeyedUnarchiver.unarchiveObject(with: data) as? Credentials,
let refreshToken = credentials.refreshToken else {
self.storage.deleteEntry(forKey: self.storeKey)
return callback(nil)
}

self.authentication
.revoke(refreshToken: refreshToken)
.start { result in
switch result {
case .failure(let error):
callback(CredentialsManagerError.revokeFailed(error))
default:
stevehobbsdev marked this conversation as resolved.
Show resolved Hide resolved
self.storage.deleteEntry(forKey: self.storeKey)
callback(nil)
}
}
}

/// Checks if a non-expired set of credentials are stored
///
/// - Returns: if there are valid and non-expired credentials stored
Expand Down
1 change: 1 addition & 0 deletions Auth0/CredentialsManagerError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ public enum CredentialsManagerError: Error {
case noRefreshToken
case failedRefresh(Error)
case touchFailed(Error)
case revokeFailed(Error)
}
58 changes: 58 additions & 0 deletions Auth0Tests/CredentialsManagerSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,64 @@ class CredentialsManagerSpec: QuickSpec {
}
}

describe("clearing and revoking refresh token") {

beforeEach {
_ = credentialsManager.store(credentials: credentials)

stub(condition: isRevokeToken(Domain) && hasAtLeast(["refresh_token": RefreshToken])) { _ in return revokeTokenResponse() }.name = "revoke success"
}

afterEach {
_ = credentialsManager.clear()
}

it("should clear credentials and revoke the refresh token") {
credentialsManager.clearAndRevokeToken {
expect($0).to(beNil())
expect(credentialsManager.hasValid()).to(beFalse())
}
}

it("should not return an error if there were no credentials stored") {
_ = credentialsManager.clear()

credentialsManager.clearAndRevokeToken {
expect($0).to(beNil())
expect(credentialsManager.hasValid()).to(beFalse())
}
}

it("should not return an error if there is no refresh token") {
stevehobbsdev marked this conversation as resolved.
Show resolved Hide resolved
let credentials = Credentials(
accessToken: AccessToken,
idToken: IdToken,
expiresIn: Date(timeIntervalSinceNow: ExpiresIn)
)

_ = credentialsManager.store(credentials: credentials)

credentialsManager.clearAndRevokeToken {
expect($0).to(beNil())
expect(credentialsManager.hasValid()).to(beFalse())
}
}

it("should return the failure if the token could not be revoked, and not clear credentials") {
stub(condition: isRevokeToken(Domain) && hasAtLeast(["token": RefreshToken])) { _ in
return authFailure(code: "400", description: "Revoke failed")
}

credentialsManager.clearAndRevokeToken {
expect($0).to(matchError(
CredentialsManagerError.revokeFailed(AuthenticationError(string: "Revoke failed", statusCode: 400))
))

expect(credentialsManager.hasValid()).to(beTrue())
}
}
}

describe("multi instances of credentials manager") {

var secondaryCredentialsManager: CredentialsManager!
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,20 @@ credentialsManager.credentials { error, credentials in
}
```

#### Clearing credentials

Credentials can be cleared by using the `clearAndRevokeToken` function. This function will attempt to revoke any associated refresh token that has been stored, before clearing the credentials from memory:

```swift
credentialsManager.clearAndRevokeToken { error in
guard error == nil else {
return print("Failed to clear credentials")
stevehobbsdev marked this conversation as resolved.
Show resolved Hide resolved
}

print("Credentials cleared")
stevehobbsdev marked this conversation as resolved.
Show resolved Hide resolved
}
```

#### Biometric authentication

You can enable an additional level of user authentication before retrieving credentials using the biometric authentication supported by your device e.g. Face ID or Touch ID.
Expand Down