-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeregistrationUseCaseImpl.swift
111 lines (96 loc) · 3.67 KB
/
DeregistrationUseCaseImpl.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
//
// Nevis Mobile Authentication SDK Example App
//
// Copyright © 2022. Nevis Security AG. All rights reserved.
//
import NevisMobileAuthentication
import RxSwift
/// Default implementation of ``DeregistrationUseCase`` protocol.
class DeregistrationUseCaseImpl {
// MARK: - Properties
/// The client provider.
private let clientProvider: ClientProvider
/// The logger.
private let logger: SDKLogger
// MARK: - Initialization
/// Creates a new instance.
///
/// - Parameters:
/// - clientProvider: The client provider.
/// - logger: The logger.
init(clientProvider: ClientProvider,
logger: SDKLogger) {
self.clientProvider = clientProvider
self.logger = logger
}
}
// MARK: - DeregistrationUseCase
extension DeregistrationUseCaseImpl: DeregistrationUseCase {
func execute(username: String?, authorizationProvider: AuthorizationProvider?) -> Observable<OperationResponse> {
guard let authenticators = clientProvider.get()?.localData.authenticators else {
return .just(CompletedResponse(operation: .deregistration))
}
var responses: [Observable<OperationResponse>] = []
authenticators.forEach { authenticator in
if let username {
// deregister only an account
if authenticator.registration.isRegistered(username) {
responses.append(deregister(username: username,
aaid: authenticator.aaid,
authorizationProvider: authorizationProvider))
}
}
else {
// username not provided, deregister all account
authenticator.registration.registeredAccounts.forEach { account in
responses.append(deregister(username: account.username,
aaid: authenticator.aaid,
authorizationProvider: authorizationProvider))
}
}
}
return Observable.create { [weak self] observer in
Observable.concat(responses)
.observe(on: MainScheduler.instance)
.subscribe(on: SerialDispatchQueueScheduler(qos: .background))
.subscribe(onError: {
self?.logger.log("Deregistration failed.", color: .red)
observer.onError(OperationError(operation: .deregistration,
underlyingError: $0))
},
onCompleted: {
observer.onNext(CompletedResponse(operation: .deregistration))
observer.onCompleted()
})
}
}
}
private extension DeregistrationUseCaseImpl {
/// Deregisters an authenticator for the given user.
///
/// - Parameters:
/// - username: The username that must be deregistered.
/// - aaid: The AAID of the ``Authenticator`` that must be deregistered.
/// - authorizationProvider: The authorization provider that must be used to deregister the authenticator.
/// - Returns: The observable sequence that will emit an ``OperationResponse`` object.
func deregister(username: String, aaid: String, authorizationProvider: AuthorizationProvider?) -> Observable<OperationResponse> {
Observable.create { [weak self] observer in
self?.logger.log("Deregistering authenticator with aaid \(aaid) for user \(username)")
let client = self?.clientProvider.get()
let operation = client?.operations.deregistration
.username(username)
.aaid(aaid)
.onSuccess {
self?.logger.log("Deregistration succeeded for authenticator with aaid \(aaid) for user \(username)", color: .green)
observer.onNext(CompletedResponse(operation: .deregistration))
observer.onCompleted()
}
.onError(observer.onError)
if let authorizationProvider {
operation?.authorizationProvider(authorizationProvider)
}
operation?.execute()
return Disposables.create()
}
}
}