-
Notifications
You must be signed in to change notification settings - Fork 0
/
AccountSelectorImpl.swift
87 lines (76 loc) · 3.12 KB
/
AccountSelectorImpl.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
//
// Nevis Mobile Authentication SDK Example App
//
// Copyright © 2022. Nevis Security AG. All rights reserved.
//
import NevisMobileAuthentication
/// Default implementation of ``AccountSelector`` protocol.
///
/// First validates the accounts based on policy compliance. Then based on the number of accounts:
/// - if no account found, the SDK will raise an error.
/// - if one account found and the transaction confirmation data is present navigates to the ``TransactionConfirmationScreen``.
/// - if one account found and the transaction confirmation data is not present performs automatic account selection.
/// - if multiple account found navigates to the ``SelectAccountScreen``.
class AccountSelectorImpl {
// MARK: - Properties
/// The application coordinator.
private let appCoordinator: AppCoordinator
/// The logger.
private let logger: SDKLogger
// MARK: - Initialization
/// Creates a new instance.
///
/// - Parameters:
/// - appCoordinator: The application coordinator.
/// - logger: The logger.
init(appCoordinator: AppCoordinator,
logger: SDKLogger) {
self.appCoordinator = appCoordinator
self.logger = logger
}
}
// MARK: - AccountSelector
extension AccountSelectorImpl: AccountSelector {
func selectAccount(context: AccountSelectionContext, handler: AccountSelectionHandler) {
logger.log("Please select one of the received available accounts!")
let validator = AccountValidator()
let result = validator.validate(context: context)
switch result {
case let .success(validAccounts):
switch validAccounts.count {
case 0:
// No username is compliant with the policy.
// Provide a random username that will generate an error in the SDK.
logger.log("No valid account found!", color: .red)
handler.username("")
case 1:
if let transactionConfirmationData = context.transactionConfirmationData,
let message = String(data: transactionConfirmationData, encoding: .utf8) {
let parameter: TransactionConfirmationParameter = .confirm(message: message,
account: validAccounts.first!,
handler: handler)
appCoordinator.navigateToTransactionConfirmation(with: parameter)
}
else {
// Typical case: authentication with username provided, just use it.
logger.log("One account found, performing automatic selection!")
handler.username(validAccounts.first!.username)
}
default:
var transactionConfirmationDataString: String? {
if let transactionConfirmationData = context.transactionConfirmationData {
return String(data: transactionConfirmationData, encoding: .utf8)
}
return nil
}
let parameter: SelectAccountParameter = .select(accounts: validAccounts,
operation: .unknown,
handler: handler,
message: transactionConfirmationDataString)
appCoordinator.navigateToAccountSelection(with: parameter)
}
case .failure:
handler.cancel()
}
}
}