This repository was archived by the owner on Feb 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathAutofillCredentialsDebugViewModel.swift
150 lines (122 loc) · 4.88 KB
/
AutofillCredentialsDebugViewModel.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
//
// AutofillCredentialsDebugViewModel.swift
//
// Copyright © 2024 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import BrowserServicesKit
import Common
import os.log
final class AutofillCredentialsDebugViewModel: ObservableObject {
struct DisplayCredentials: Identifiable {
let tld: TLD
let autofillDomainNameUrlMatcher: AutofillDomainNameUrlMatcher
var credential: SecureVaultModels.WebsiteCredentials
var id = UUID()
var accountId: String {
credential.account.id ?? ""
}
var accountTitle: String {
credential.account.title ?? ""
}
var displayTitle: String {
credential.account.name(tld: tld, autofillDomainNameUrlMatcher: autofillDomainNameUrlMatcher)
}
var websiteUrl: String {
credential.account.domain ?? ""
}
var domain: String {
guard let url = credential.account.domain,
let urlComponents = autofillDomainNameUrlMatcher.normalizeSchemeForAutofill(url),
let domain = urlComponents.eTLDplus1(tld: tld) ?? urlComponents.host else {
return ""
}
return domain
}
var username: String {
credential.account.username ?? ""
}
var displayPassword: String {
return credential.password.flatMap { String(data: $0, encoding: .utf8) } ?? "FAILED TO DECODE PW"
}
var notes: String {
credential.account.notes ?? ""
}
var created: String {
"\(credential.account.created)"
}
var lastUpdated: String {
"\(credential.account.lastUpdated)"
}
var lastUsed: String {
credential.account.lastUsed != nil ? "\(credential.account.lastUsed!)" : ""
}
var signature: String {
credential.account.signature ?? ""
}
}
private let tld: TLD = ContentBlocking.shared.tld
private let autofillDomainNameUrlMatcher: AutofillDomainNameUrlMatcher = AutofillDomainNameUrlMatcher()
private var userAuthenticator: UserAuthenticating
@Published var credentials: [DisplayCredentials] = []
init(userAuthenticator: UserAuthenticating = DeviceAuthenticator.shared) {
self.userAuthenticator = userAuthenticator
beginAuthentication()
}
private func beginAuthentication() {
userAuthenticator.authenticateUser(reason: .viewAllCredentials) { [weak self] authenticationResult in
guard let self = self else {
return
}
if authenticationResult.authenticated {
self.credentials = self.loadCredentials()
}
}
}
private func loadCredentials() -> [DisplayCredentials] {
do {
let secureVault = try AutofillSecureVaultFactory.makeVault(reporter: SecureVaultReporter.shared)
let accounts = try secureVault.accounts()
var credentials: [DisplayCredentials] = []
var accountsFailedToLoad: [String?] = []
for account in accounts {
guard let accountId = account.id,
let accountIdInt = Int64(accountId),
let credential = try secureVault.websiteCredentialsFor(accountId: accountIdInt) else {
accountsFailedToLoad.append(account.id)
continue
}
let displayCredential = DisplayCredentials(tld: tld, autofillDomainNameUrlMatcher: autofillDomainNameUrlMatcher, credential: credential)
credentials.append(displayCredential)
}
if !accountsFailedToLoad.isEmpty {
os_log("Failed to load credentials for accounts: %@", accountsFailedToLoad)
showErrorAlertFor(accountsFailedToLoad)
}
return credentials
} catch {
os_log("Failed to fetch accounts")
return []
}
}
private func showErrorAlertFor(_ accountIds: [String?]) {
let alert = NSAlert()
alert.messageText = "Failed to load credentials for accounts:"
alert.informativeText = accountIds.compactMap { $0 }.joined(separator: ", ")
alert.alertStyle = .warning
alert.addButton(withTitle: UserText.ok)
alert.runModal()
}
}