generated from bitwarden/template
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathVaultItemMoreOptionsHelper.swift
194 lines (176 loc) · 6.98 KB
/
VaultItemMoreOptionsHelper.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
import Foundation
// MARK: - VaultItemMoreOptionsHelper
/// A protocol for a helper object to handle displaying the more options menu for a vault item and
/// responding to the user's selection.
///
protocol VaultItemMoreOptionsHelper {
/// Show the more options alert for the selected item.
///
/// - Parameters
/// - item: The selected item to show the options for.
/// - handleDisplayToast: A closure called to handle displaying a toast.
/// - handleOpenURL: A closure called to open a URL.
///
func showMoreOptionsAlert(
for item: VaultListItem,
handleDisplayToast: @escaping (Toast) -> Void,
handleOpenURL: @escaping (URL) -> Void
) async
}
// MARK: - DefaultVaultItemMoreOptionsHelper
/// A default implementation of `VaultItemMoreOptionsHelper`.
///
@MainActor
class DefaultVaultItemMoreOptionsHelper: VaultItemMoreOptionsHelper {
// MARK: Types
typealias Services = HasAuthRepository
& HasErrorReporter
& HasEventService
& HasPasteboardService
& HasStateService
& HasVaultRepository
// MARK: Private Properties
/// The `Coordinator` that handles navigation.
private var coordinator: AnyCoordinator<VaultRoute, AuthAction>
/// The services used by this helper.
private var services: Services
// MARK: Initialization
/// Initialize a `VaultItemMoreOptionsHelper`.
///
/// - Parameters:
/// - coordinator: The coordinator that handles navigation.
/// - services: The services used by this helper.
///
init(
coordinator: AnyCoordinator<VaultRoute, AuthAction>,
services: Services
) {
self.coordinator = coordinator
self.services = services
}
// MARK: Methods
func showMoreOptionsAlert(
for item: VaultListItem,
handleDisplayToast: @escaping (Toast) -> Void,
handleOpenURL: @escaping (URL) -> Void
) async {
do {
// Only ciphers have more options.
guard case let .cipher(cipherView, _) = item.itemType else { return }
let hasPremium = try await services.vaultRepository.doesActiveAccountHavePremium()
let hasMasterPassword = try await services.stateService.getUserHasMasterPassword()
coordinator.showAlert(.moreOptions(
canCopyTotp: hasPremium || cipherView.organizationUseTotp,
cipherView: cipherView,
hasMasterPassword: hasMasterPassword,
id: item.id,
showEdit: true
) { action in
await self.handleMoreOptionsAction(
action,
handleDisplayToast: handleDisplayToast,
handleOpenURL: handleOpenURL
)
})
} catch {
services.errorReporter.log(error: error)
coordinator.showAlert(.defaultAlert(title: Localizations.anErrorHasOccurred))
}
}
// MARK: Private Methods
/// Generates and copies a TOTP code for the cipher's TOTP key.
///
/// - Parameter totpKey: The TOTP key used to generate a TOTP code.
///
private func generateAndCopyTotpCode(
totpKey: TOTPKeyModel,
handleDisplayToast: @escaping (Toast) -> Void
) async {
do {
let response = try await services.vaultRepository.refreshTOTPCode(for: totpKey)
guard let code = response.codeModel?.code else {
throw TOTPServiceError.unableToGenerateCode(nil)
}
services.pasteboardService.copy(code)
handleDisplayToast(
Toast(title: Localizations.valueHasBeenCopied(Localizations.verificationCodeTotp))
)
} catch {
coordinator.showAlert(.defaultAlert(title: Localizations.anErrorHasOccurred))
services.errorReporter.log(error: error)
}
}
/// Handle the result of the selected option on the More Options alert..
///
/// - Parameter action: The selected action.
///
private func handleMoreOptionsAction(
_ action: MoreOptionsAction,
handleDisplayToast: @escaping (Toast) -> Void,
handleOpenURL: (URL) -> Void
) async {
switch action {
case let .copy(toast, value, requiresMasterPasswordReprompt, event, cipherId):
let copyBlock = {
self.services.pasteboardService.copy(value)
handleDisplayToast(Toast(title: Localizations.valueHasBeenCopied(toast)))
if let event {
Task {
await self.services.eventService.collect(
eventType: event,
cipherId: cipherId
)
}
}
}
if requiresMasterPasswordReprompt {
presentMasterPasswordRepromptAlert(completion: copyBlock)
} else {
copyBlock()
}
case let .copyTotp(totpKey, requiresMasterPasswordReprompt):
if requiresMasterPasswordReprompt {
presentMasterPasswordRepromptAlert {
await self.generateAndCopyTotpCode(totpKey: totpKey, handleDisplayToast: handleDisplayToast)
}
} else {
await generateAndCopyTotpCode(totpKey: totpKey, handleDisplayToast: handleDisplayToast)
}
case let .edit(cipherView, requiresMasterPasswordReprompt):
guard let id = cipherView.id else { return }
if requiresMasterPasswordReprompt {
presentMasterPasswordRepromptAlert {
self.coordinator.navigate(to: .editItemFrom(id: id), context: self)
}
} else {
coordinator.navigate(to: .editItemFrom(id: id), context: self)
}
case let .launch(url):
handleOpenURL(url.sanitized)
case let .view(id):
coordinator.navigate(to: .viewItem(id: id))
}
}
/// Presents the master password reprompt alert and calls the completion handler when the user's
/// master password has been confirmed.
///
/// - Parameter completion: A completion handler that is called when the user's master password
/// has been confirmed.
///
private func presentMasterPasswordRepromptAlert(completion: @escaping () async -> Void) {
let alert = Alert.masterPasswordPrompt { password in
do {
let isValid = try await self.services.authRepository.validatePassword(password)
guard isValid else {
self.coordinator.showAlert(.defaultAlert(title: Localizations.invalidMasterPassword))
return
}
await completion()
} catch {
self.coordinator.showAlert(.defaultAlert(title: Localizations.anErrorHasOccurred))
self.services.errorReporter.log(error: error)
}
}
coordinator.showAlert(alert)
}
}