From ff350eacea0df36cee49befd40ea81c55bed001c Mon Sep 17 00:00:00 2001 From: Daniel Phillips Date: Tue, 24 Jul 2018 16:13:59 +0100 Subject: [PATCH 1/2] Move storeKey to init, allowing usage to point to anywhere in keychain. --- Auth0/CredentialsManager.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Auth0/CredentialsManager.swift b/Auth0/CredentialsManager.swift index e13bcdfc..188965f2 100644 --- a/Auth0/CredentialsManager.swift +++ b/Auth0/CredentialsManager.swift @@ -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? @@ -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 } From 2735db4bf4683ecf2f9137b75c41e718a3e56101 Mon Sep 17 00:00:00 2001 From: Daniel Phillips Date: Tue, 24 Jul 2018 18:05:46 +0100 Subject: [PATCH 2/2] Added two tests for multi instance usage of credentials manager --- Auth0Tests/CredentialsManagerSpec.swift | 76 +++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/Auth0Tests/CredentialsManagerSpec.swift b/Auth0Tests/CredentialsManagerSpec.swift index d5c2ad96..703d6b1e 100644 --- a/Auth0Tests/CredentialsManagerSpec.swift +++ b/Auth0Tests/CredentialsManagerSpec.swift @@ -71,7 +71,83 @@ class CredentialsManagerSpec: QuickSpec { it("should fail to clear credentials") { expect(credentialsManager.clear()).to(beFalse()) } + + } + + describe("multi instances of credentials manager") { + + 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") {