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

Added support for custom Keychain key in Credentials Manager #208

Merged
merged 2 commits into from
Jul 26, 2018
Merged
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions Auth0/CredentialsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import LocalAuthentication
public struct CredentialsManager {

private let storage = A0SimpleKeychain()
private let storeKey = "credentials"
private let storeKey: String
private let authentication: Authentication
#if os(iOS)
private var bioAuth: BioAuthentication?
Expand All @@ -40,7 +40,9 @@ public struct CredentialsManager {
///
/// - Parameters:
/// - authentication: Auth0 authentication instance
public init(authentication: Authentication) {
/// - storeKey: Key used to store user credentials in the keychain, defaults to "credentials"
public init(authentication: Authentication, storeKey: String = "credentials") {
self.storeKey = storeKey
self.authentication = authentication
}

Expand Down
76 changes: 76 additions & 0 deletions Auth0Tests/CredentialsManagerSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,83 @@ class CredentialsManagerSpec: QuickSpec {
it("should fail to clear credentials") {
expect(credentialsManager.clear()).to(beFalse())
}

}

describe("multi instances of credentials manager") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not as bothered about tests as long as they work. However, they would look cleaner to use closure shorthand like https://github.com/danielphillips/Auth0.swift/blob/a78a4018722c9202ee5d1a75c2608f5c00f855ac/Auth0Tests/CredentialsManagerSpec.swift#L263

credentialsManager.credentials { 
   expect($1?.accessToken) == AccessToken
   expect($1?.idToken) == IdToken
  done()
}

Cut down on the extra params.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point thank you. Done that now.


var secondaryCredentialsManager: CredentialsManager!
var sedondaryCredentials: Credentials!

beforeEach {
secondaryCredentialsManager = CredentialsManager(authentication: authentication, storeKey: "secondary_store")
sedondaryCredentials = Credentials(accessToken: "SecondaryAccessToken", tokenType: TokenType, idToken: "SecondaryIdToken", refreshToken: "SecondaryRefreshToken", expiresIn: Date(timeIntervalSinceNow: 10))
}

it("should store credentials into distinct locations") {
expect(credentialsManager.store(credentials: credentials)).to(beTrue())
expect(secondaryCredentialsManager.store(credentials: sedondaryCredentials)).to(beTrue())

waitUntil(timeout: 2) { done in
credentialsManager.credentials {
expect($0).to(beNil())
expect($1!.accessToken) == AccessToken
expect($1!.refreshToken) == RefreshToken
expect($1!.idToken) == IdToken
done()
}
}

waitUntil(timeout: 2) { done in
secondaryCredentialsManager.credentials {
expect($0).to(beNil())
expect($1!.accessToken) == "SecondaryAccessToken"
expect($1!.refreshToken) == "SecondaryRefreshToken"
expect($1!.idToken) == "SecondaryIdToken"
done()
}
}

}

it("should store new credentials") {
credentials = Credentials(accessToken: AccessToken, tokenType: TokenType, idToken: IdToken, refreshToken: RefreshToken, expiresIn: Date(timeIntervalSinceNow: -3600))
_ = credentialsManager.store(credentials: credentials)
}

it("should share a space in the keychain if using the same store key") {

credentialsManager = CredentialsManager(authentication: authentication, storeKey: "secondary_store")

expect(credentialsManager.store(credentials: credentials)).to(beTrue())

waitUntil(timeout: 2) { done in
credentialsManager.credentials {
expect($0).to(beNil())
expect($1!.accessToken) == AccessToken
expect($1!.refreshToken) == RefreshToken
expect($1!.idToken) == IdToken
done()
}
}

waitUntil(timeout: 2) { done in
secondaryCredentialsManager.credentials {
expect($0).to(beNil())
expect($1!.accessToken) == AccessToken
expect($1!.refreshToken) == RefreshToken
expect($1!.idToken) == IdToken
done()
}
}

}

afterEach {
_ = credentialsManager.clear()
_ = secondaryCredentialsManager.clear()
}

}

describe("valididity") {
Expand Down