-
Notifications
You must be signed in to change notification settings - Fork 0
/
OutOfBandOperationHandlerImpl.swift
207 lines (180 loc) · 7.18 KB
/
OutOfBandOperationHandlerImpl.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//
// Nevis Mobile Authentication SDK Example App
//
// Copyright © 2022. Nevis Security AG. All rights reserved.
//
import NevisMobileAuthentication
import UIKit
/// Default implementation of ``OutOfBandOperationHandler`` protocol.
final class OutOfBandOperationHandlerImpl {
// MARK: - Properties
/// The client provider.
private let clientProvider: ClientProvider
/// The account selector.
private let accountSelector: AccountSelector
/// The authenticator selector used during registration.
private let registrationAuthenticatorSelector: AuthenticatorSelector
/// The authenticator selector used during authentication.
private let authenticationAuthenticatorSelector: AuthenticatorSelector
/// The PIN enroller.
private let pinEnroller: PinEnroller
/// The PIN user verifier.
private let pinUserVerifier: PinUserVerifier
/// The Password enroller.
private let passwordEnroller: PasswordEnroller
/// The Password user verifier.
private let passwordUserVerifier: PasswordUserVerifier
/// The biometric user verifier.
private let biometricUserVerifier: BiometricUserVerifier
/// The device passcode user verifier.
private let devicePasscodeUserVerifier: DevicePasscodeUserVerifier
/// The application coordinator.
private let appCoordinator: AppCoordinator
/// The error handler chain.
private let errorHandlerChain: ErrorHandlerChain
/// The logger.
private let logger: SDKLogger
/// The ``MobileAuthenticationClient`` instance.
private var mobileAuthenticationClient: MobileAuthenticationClient? {
clientProvider.get()
}
// MARK: - Initialization
/// Creates a new instance.
///
/// - Parameters:
/// - clientProvider: The client provider.
/// - accountSelector: The account selector.
/// - registrationAuthenticatorSelector: The authenticator selector used during registration.
/// - authenticationAuthenticatorSelector: The authenticator selector used during authentication.
/// - pinEnroller: The PIN enroller.
/// - pinUserVerifier: The PIN user verifier.
/// - passwordEnroller: The Password enroller.
/// - passwordUserVerifier: The Password user verifier.
/// - biometricUserVerifier: The biometric user verifier.
/// - devicePasscodeUserVerifier: The device passcode user verifier.
/// - appCoordinator: The application coordinator.
/// - errorHandlerChain: The error handler chain.
/// - logger: The logger.
init(clientProvider: ClientProvider,
accountSelector: AccountSelector,
registrationAuthenticatorSelector: AuthenticatorSelector,
authenticationAuthenticatorSelector: AuthenticatorSelector,
pinEnroller: PinEnroller,
pinUserVerifier: PinUserVerifier,
passwordEnroller: PasswordEnroller,
passwordUserVerifier: PasswordUserVerifier,
biometricUserVerifier: BiometricUserVerifier,
devicePasscodeUserVerifier: DevicePasscodeUserVerifier,
appCoordinator: AppCoordinator,
errorHandlerChain: ErrorHandlerChain,
logger: SDKLogger) {
self.clientProvider = clientProvider
self.accountSelector = accountSelector
self.registrationAuthenticatorSelector = registrationAuthenticatorSelector
self.authenticationAuthenticatorSelector = authenticationAuthenticatorSelector
self.pinEnroller = pinEnroller
self.pinUserVerifier = pinUserVerifier
self.passwordEnroller = passwordEnroller
self.passwordUserVerifier = passwordUserVerifier
self.biometricUserVerifier = biometricUserVerifier
self.devicePasscodeUserVerifier = devicePasscodeUserVerifier
self.appCoordinator = appCoordinator
self.errorHandlerChain = errorHandlerChain
self.logger = logger
}
/// :nodoc:
deinit {
os_log("OutOfBandOperationHandlerImpl", log: OSLog.deinit, type: .debug)
}
}
// MARK: - OutOfBandOperationHandler
extension OutOfBandOperationHandlerImpl: OutOfBandOperationHandler {
func handle(payload: String) {
decode(base64UrlEncoded: payload) {
self.startOutOfBandOperation(with: $0)
}
}
}
private extension OutOfBandOperationHandlerImpl {
/// Decodes a Base64 URL encoded string.
///
/// - Parameter handler: The code need to be executed on successful payload decoding.
func decode(base64UrlEncoded: String, completion handler: @escaping (OutOfBandPayload) -> ()) {
mobileAuthenticationClient?.operations.outOfBandPayloadDecode
.base64UrlEncoded(base64UrlEncoded)
.onSuccess {
self.logger.log("Decode payload succeeded.", color: .green)
handler($0)
}
.onError {
self.logger.log("Decode payload failed.", color: .red)
let operationError = OperationError(operation: .payloadDecode, underlyingError: $0)
self.errorHandlerChain.handle(error: operationError)
}
.execute()
}
/// Starts an Out-of-Band operation with the given payload.
///
/// - Parameter payload: The Out-of-Band payload.
func startOutOfBandOperation(with payload: OutOfBandPayload) {
mobileAuthenticationClient?.operations.outOfBandOperation
.payload(payload)
.onRegistration {
self.register(using: $0)
}
.onAuthentication {
self.authenticate(using: $0)
}
.onError {
self.logger.log("Out-of-Band operation failed.", color: .red)
let operationError = OperationError(operation: .outOfBand, underlyingError: $0)
self.errorHandlerChain.handle(error: operationError)
}
.execute()
}
/// Starts an Out-of-Band registration operation.
///
/// - Parameter registration: An ``OutOfBandRegistration`` object received from the SDK.
func register(using registration: OutOfBandRegistration) {
let deviceInformation = mobileAuthenticationClient?.localData.deviceInformation ?? DeviceInformation(name: UIDevice.extendedName)
registration
.deviceInformation(deviceInformation)
.authenticatorSelector(registrationAuthenticatorSelector)
.pinEnroller(pinEnroller)
.passwordEnroller(passwordEnroller)
.biometricUserVerifier(biometricUserVerifier)
.devicePasscodeUserVerifier(devicePasscodeUserVerifier)
.onSuccess {
self.logger.log("Out-of-Band registration succeeded.", color: .green)
self.appCoordinator.navigateToResult(with: .success(operation: .registration))
}
.onError {
self.logger.log("Out-of-Band registration failed.", color: .red)
let operationError = OperationError(operation: .registration, underlyingError: $0)
self.errorHandlerChain.handle(error: operationError)
}
.execute()
}
/// Starts an Out-of-Band authentication operation.
///
/// - Parameter authentication: An ``OutOfBandAuthentication`` object received from the SDK.
func authenticate(using authentication: OutOfBandAuthentication) {
authentication
.accountSelector(accountSelector)
.authenticatorSelector(authenticationAuthenticatorSelector)
.pinUserVerifier(pinUserVerifier)
.passwordUserVerifier(passwordUserVerifier)
.biometricUserVerifier(biometricUserVerifier)
.devicePasscodeUserVerifier(devicePasscodeUserVerifier)
.onSuccess { _ in
self.logger.log("Out-of-Band authentication succeeded.", color: .green)
self.appCoordinator.navigateToResult(with: .success(operation: .authentication))
}
.onError {
self.logger.log("Out-of-Band authentication failed.", color: .red)
let operationError = OperationError(operation: .authentication, underlyingError: $0)
self.errorHandlerChain.handle(error: operationError)
}
.execute()
}
}