Skip to content

Commit eff007f

Browse files
addressed comment
1 parent 7826e86 commit eff007f

File tree

3 files changed

+36
-21
lines changed

3 files changed

+36
-21
lines changed

Auth0/BioAuthentication.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ struct BioAuthentication {
2424
return self.authContext.canEvaluatePolicy(evaluationPolicy, error: nil)
2525
}
2626

27-
init(authContext: LAContext, evaluationPolicy: LAPolicy, title: String, cancelTitle: String? = nil, fallbackTitle: String? = nil, policy: BiometricPolicy = .always) {
27+
init(authContext: LAContext,
28+
evaluationPolicy: LAPolicy,
29+
title: String,
30+
cancelTitle: String? = nil,
31+
fallbackTitle: String? = nil,
32+
policy: BiometricPolicy = .always
33+
) {
2834
self.authContext = authContext
2935
self.evaluationPolicy = evaluationPolicy
3036
self.title = title

Auth0/CredentialsManager.swift

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,17 @@ public struct CredentialsManager {
3232
private let dispatchQueue = DispatchQueue(label: "com.auth0.credentialsmanager.serial")
3333
#if WEB_AUTH_PLATFORM
3434
var bioAuth: BioAuthentication?
35-
// Biometric session management
36-
private static let noSession: TimeInterval = -1
37-
private static var lastBiometricAuthTime: TimeInterval = noSession
38-
private static let sessionLock = NSLock()
35+
// Biometric session management - using a class to allow mutation in non-mutating methods
36+
private final class BiometricSession {
37+
let noSession: TimeInterval = -1
38+
var lastBiometricAuthTime: TimeInterval = -1
39+
let lock = NSLock()
40+
41+
init() {
42+
lastBiometricAuthTime = noSession
43+
}
44+
}
45+
private let biometricSession = BiometricSession()
3946
#endif
4047

4148
/// Creates a new `CredentialsManager` instance.
@@ -140,7 +147,9 @@ public struct CredentialsManager {
140147
/// - Returns: If the credentials were removed.
141148
public func clear() -> Bool {
142149
#if WEB_AUTH_PLATFORM
143-
Self.clearBiometricSession()
150+
self.biometricSession.lock.lock()
151+
self.biometricSession.lastBiometricAuthTime = self.biometricSession.noSession
152+
self.biometricSession.lock.unlock()
144153
#endif
145154
return self.storage.deleteEntry(forKey: self.storeKey)
146155
}
@@ -172,11 +181,11 @@ public struct CredentialsManager {
172181
public func isBiometricSessionValid() -> Bool {
173182
guard let bioAuth = self.bioAuth else { return false }
174183

175-
Self.sessionLock.lock()
176-
defer { Self.sessionLock.unlock() }
184+
self.biometricSession.lock.lock()
185+
defer { self.biometricSession.lock.unlock() }
177186

178-
let lastAuth = Self.lastBiometricAuthTime
179-
if lastAuth == Self.noSession { return false }
187+
let lastAuth = self.biometricSession.lastBiometricAuthTime
188+
if lastAuth == self.biometricSession.noSession { return false }
180189

181190
switch bioAuth.policy {
182191
case .session(let timeoutInSeconds), .appLifecycle(let timeoutInSeconds):
@@ -193,12 +202,12 @@ public struct CredentialsManager {
193202
/// ## Usage
194203
///
195204
/// ```swift
196-
/// CredentialsManager.clearBiometricSession()
205+
/// credentialsManager.clearBiometricSession()
197206
/// ```
198-
public static func clearBiometricSession() {
199-
sessionLock.lock()
200-
defer { sessionLock.unlock() }
201-
lastBiometricAuthTime = noSession
207+
public func clearBiometricSession() {
208+
self.biometricSession.lock.lock()
209+
defer { self.biometricSession.lock.unlock() }
210+
self.biometricSession.lastBiometricAuthTime = self.biometricSession.noSession
202211
}
203212
#endif
204213

@@ -392,7 +401,7 @@ public struct CredentialsManager {
392401
}
393402

394403
// Update biometric session after successful authentication (only for session-based policies)
395-
Self.updateBiometricSession(for: bioAuth.policy)
404+
self.updateBiometricSession(for: bioAuth.policy)
396405

397406
self.retrieveCredentials(scope: scope,
398407
minTTL: minTTL,
@@ -1582,15 +1591,15 @@ public extension CredentialsManager {
15821591
#if WEB_AUTH_PLATFORM
15831592
/// Updates the biometric session timestamp to the current time.
15841593
/// Only updates for session-based policies (Session and AppLifecycle).
1585-
private static func updateBiometricSession(for policy: BiometricPolicy) {
1594+
private func updateBiometricSession(for policy: BiometricPolicy) {
15861595
// Don't update session for "Always" policy
15871596
switch policy {
15881597
case .always:
15891598
return
15901599
case .session, .appLifecycle:
1591-
sessionLock.lock()
1592-
defer { sessionLock.unlock() }
1593-
lastBiometricAuthTime = Date().timeIntervalSince1970
1600+
self.biometricSession.lock.lock()
1601+
defer { self.biometricSession.lock.unlock() }
1602+
self.biometricSession.lastBiometricAuthTime = Date().timeIntervalSince1970
15941603
}
15951604
}
15961605
#endif

EXAMPLES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ You can manually clear the biometric session to force re-authentication on the n
578578

579579
```swift
580580
// Clear the biometric session
581-
CredentialsManager.clearBiometricSession()
581+
credentialsManager.clearBiometricSession()
582582

583583
// Check if the current session is valid
584584
let isValid = credentialsManager.isBiometricSessionValid()

0 commit comments

Comments
 (0)