-
Notifications
You must be signed in to change notification settings - Fork 0
/
OutOfBandOperationUseCaseImpl.swift
181 lines (156 loc) · 6.37 KB
/
OutOfBandOperationUseCaseImpl.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
//
// Nevis Mobile Authentication SDK Example App
//
// Copyright © 2022. Nevis Security AG. All rights reserved.
//
import NevisMobileAuthentication
import RxSwift
/// Default implementation of ``OutOfBandOperationUseCase`` protocol.
class OutOfBandOperationUseCaseImpl {
// MARK: - Properties
/// The client provider.
private let clientProvider: ClientProvider
/// Use case for creating device information.
private let createDeviceInformationUseCase: CreateDeviceInformationUseCase
/// 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 logger.
private let logger: SDKLogger
// MARK: - Initialization
/// Creates a new instance.
///
/// - Parameters:
/// - clientProvider: The client provider.
/// - createDeviceInformationUseCase: Use case for creating device information.
/// - 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.
/// - logger: The logger.
init(clientProvider: ClientProvider,
createDeviceInformationUseCase: CreateDeviceInformationUseCase,
accountSelector: AccountSelector,
registrationAuthenticatorSelector: AuthenticatorSelector,
authenticationAuthenticatorSelector: AuthenticatorSelector,
pinEnroller: PinEnroller,
pinUserVerifier: PinUserVerifier,
passwordEnroller: PasswordEnroller,
passwordUserVerifier: PasswordUserVerifier,
biometricUserVerifier: BiometricUserVerifier,
devicePasscodeUserVerifier: DevicePasscodeUserVerifier,
logger: SDKLogger) {
self.clientProvider = clientProvider
self.createDeviceInformationUseCase = createDeviceInformationUseCase
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.logger = logger
}
}
// MARK: - OutOfBandOperationUseCase
extension OutOfBandOperationUseCaseImpl: OutOfBandOperationUseCase {
func execute(payload: OutOfBandPayload) -> Observable<OperationResponse> {
Observable.create { [weak self] observer in
guard let self else {
return Disposables.create()
}
let client = clientProvider.get()
client?.operations.outOfBandOperation
.payload(payload)
.onRegistration {
self.register(using: $0, observer: observer)
}
.onAuthentication {
self.authenticate(using: $0, observer: observer)
}
.onError {
self.logger.log("Out-of-Band operation failed.", color: .red)
observer.onError(OperationError(operation: .outOfBand,
underlyingError: $0))
}
.execute()
return Disposables.create()
}
}
}
// MARK: - Private Interface
private extension OutOfBandOperationUseCaseImpl {
/// Starts an Out-of-Band registration operation.
///
/// - Parameters:
/// - registration: An ``OutOfBandRegistration`` object received from the SDK.
/// - observer: Observable sequence used to emit the result of the operation.
func register(using registration: OutOfBandRegistration, observer: AnyObserver<OperationResponse>) {
registration
.deviceInformation(createDeviceInformationUseCase.execute())
.authenticatorSelector(registrationAuthenticatorSelector)
.pinEnroller(pinEnroller)
.passwordEnroller(passwordEnroller)
.biometricUserVerifier(biometricUserVerifier)
.devicePasscodeUserVerifier(devicePasscodeUserVerifier)
.onSuccess {
self.logger.log("Out-of-Band registration succeeded.", color: .green)
observer.onNext(CompletedResponse(operation: .registration))
observer.onCompleted()
}
.onError {
self.logger.log("Out-of-Band registration failed.", color: .red)
observer.onError(OperationError(operation: .registration,
underlyingError: $0))
}
.execute()
}
/// Starts an Out-of-Band authentication operation.
///
/// - Parameters:
/// - registration: An ``OutOfBandAuthentication`` object received from the SDK.
/// - observer: Observable sequence used to emit the result of the operation.
func authenticate(using authentication: OutOfBandAuthentication, observer: AnyObserver<OperationResponse>) {
authentication
.accountSelector(accountSelector)
.authenticatorSelector(authenticationAuthenticatorSelector)
.pinUserVerifier(pinUserVerifier)
.passwordUserVerifier(passwordUserVerifier)
.biometricUserVerifier(biometricUserVerifier)
.devicePasscodeUserVerifier(devicePasscodeUserVerifier)
.onSuccess {
self.logger.log("Out-of-Band authentication succeeded.", color: .green)
observer.onNext(CompletedResponse(operation: .authentication,
authorizationProvider: $0))
observer.onCompleted()
}
.onError {
self.logger.log("Out-of-Band authentication failed.", color: .red)
observer.onError(OperationError(operation: .authentication,
underlyingError: $0))
}
.execute()
}
}