22
22
23
23
import Foundation
24
24
import SimpleKeychain
25
+ #if os(iOS)
25
26
import LocalAuthentication
27
+ #endif
26
28
27
29
/// Credentials management utility
28
30
public struct CredentialsManager {
29
31
30
32
private let storage = A0SimpleKeychain ( )
31
33
private let storeKey = " credentials "
32
34
private let authentication : Authentication
35
+ #if os(iOS)
33
36
private var bioAuth : BioAuthentication ?
37
+ #endif
34
38
35
39
/// Creates a new CredentialsManager instance
36
40
///
@@ -46,20 +50,24 @@ public struct CredentialsManager {
46
50
/// - title: main message to display in TouchID prompt
47
51
/// - cancelTitle: cancel message to display in TouchID prompt (iOS 10+)
48
52
/// - fallbackTitle: fallback message to display in TouchID prompt after a failed match
53
+ #if os(iOS)
49
54
@available ( * , deprecated, message: " see enableBiometrics(withTitle title:, cancelTitle:, fallbackTitle:) " )
50
55
public mutating func enableTouchAuth( withTitle title: String , cancelTitle: String ? = nil , fallbackTitle: String ? = nil ) {
51
56
self . enableBiometrics ( withTitle: title, cancelTitle: cancelTitle, fallbackTitle: fallbackTitle)
52
57
}
58
+ #endif
53
59
54
60
/// Enable Biometric Authentication for additional securtity during credentials retrieval.
55
61
///
56
62
/// - Parameters:
57
63
/// - title: main message to display when Touch ID is used
58
64
/// - cancelTitle: cancel message to display when Touch ID is used (iOS 10+)
59
65
/// - fallbackTitle: fallback message to display when Touch ID is used after a failed match
66
+ #if os(iOS)
60
67
public mutating func enableBiometrics( withTitle title: String , cancelTitle: String ? = nil , fallbackTitle: String ? = nil ) {
61
68
self . bioAuth = BioAuthentication ( authContext: LAContext ( ) , title: title, cancelTitle: cancelTitle, fallbackTitle: fallbackTitle)
62
69
}
70
+ #endif
63
71
64
72
/// Store credentials instance in keychain
65
73
///
@@ -106,6 +114,7 @@ public struct CredentialsManager {
106
114
/// - callback: callback with the user's credentials or the cause of the error.
107
115
/// - Important: This method only works for a refresh token obtained after auth with OAuth 2.0 API Authorization.
108
116
/// - Note: [Auth0 Refresh Tokens Docs](https://auth0.com/docs/tokens/refresh-token)
117
+ #if os(iOS)
109
118
public func credentials( withScope scope: String ? = nil , callback: @escaping ( CredentialsManagerError ? , Credentials ? ) -> Void ) {
110
119
guard self . hasValid ( ) else { return callback ( . noCredentials, nil ) }
111
120
if let bioAuth = self . bioAuth {
@@ -120,6 +129,12 @@ public struct CredentialsManager {
120
129
self . retrieveCredentials ( withScope: scope, callback: callback)
121
130
}
122
131
}
132
+ #else
133
+ public func credentials( withScope scope: String ? = nil , callback: @escaping ( CredentialsManagerError ? , Credentials ? ) -> Void ) {
134
+ guard self . hasValid ( ) else { return callback ( . noCredentials, nil ) }
135
+ self . retrieveCredentials ( withScope: scope, callback: callback)
136
+ }
137
+ #endif
123
138
124
139
private func retrieveCredentials( withScope scope: String ? = nil , callback: @escaping ( CredentialsManagerError ? , Credentials ? ) -> Void ) {
125
140
guard
@@ -134,11 +149,11 @@ public struct CredentialsManager {
134
149
switch $0 {
135
150
case . success( let credentials) :
136
151
let newCredentials = Credentials ( accessToken: credentials. accessToken,
137
- tokenType: credentials. tokenType,
138
- idToken: credentials. idToken,
139
- refreshToken: refreshToken,
140
- expiresIn: credentials. expiresIn,
141
- scope: credentials. scope)
152
+ tokenType: credentials. tokenType,
153
+ idToken: credentials. idToken,
154
+ refreshToken: refreshToken,
155
+ expiresIn: credentials. expiresIn,
156
+ scope: credentials. scope)
142
157
_ = self . store ( credentials: newCredentials)
143
158
callback ( nil , newCredentials)
144
159
case . failure( let error) :
0 commit comments