-
-
Notifications
You must be signed in to change notification settings - Fork 865
Expand file tree
/
Copy pathkeychain.js
More file actions
103 lines (86 loc) · 2.66 KB
/
Copy pathkeychain.js
File metadata and controls
103 lines (86 loc) · 2.66 KB
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
const { ipcRenderer } = require('electron')
const papaparse = require('papaparse')
class Keychain {
constructor () {
this.name = 'Built-in password manager'
}
getDownloadLink () {
return null
}
getLocalPath () {
return null
}
getSetupMode () {
return null
}
async checkIfConfigured () {
return true
}
isUnlocked () {
return true
}
async getSuggestions (domain) {
return ipcRenderer.invoke('credentialStoreGetCredentials').then(function (results) {
return results
.filter(function (result) {
return result.domain === domain
})
.map(function (result) {
return {
...result,
manager: 'Keychain'
}
})
})
}
saveCredential (domain, username, password) {
ipcRenderer.invoke('credentialStoreSetPassword', { domain, username, password })
}
deleteCredential (domain, username) {
ipcRenderer.invoke('credentialStoreDeletePassword', { domain, username })
}
async importCredentials (fileContents) {
try {
const csvData = papaparse.parse(fileContents, {
header: true,
skipEmptyLines: true,
transformHeader (header) {
return header.toLowerCase().trim().replace(/["']/g, '')
}
})
const credentialsToImport = csvData.data.map((credential) => {
try {
const includesProtocol = credential.url.match(/^https?:\/\//g)
const domainWithProtocol = includesProtocol ? credential.url : `https://${credential.url}`
return {
domain: new URL(domainWithProtocol).origin,
username: credential.username,
password: credential.password
}
} catch {
return null
}
}).filter(credential => credential !== null)
if (credentialsToImport.length === 0) return []
const currentCredentials = await this.getAllCredentials()
const credentialsWithoutDuplicates = currentCredentials.filter(account => !credentialsToImport.some(a => a.domain === account.domain && a.username === account.username))
const mergedCredentials = credentialsWithoutDuplicates.concat(credentialsToImport)
await ipcRenderer.invoke('credentialStoreSetPasswordBulk', mergedCredentials)
return mergedCredentials
} catch (error) {
console.error('Error importing credentials:', error)
return []
}
}
getAllCredentials () {
return ipcRenderer.invoke('credentialStoreGetCredentials').then(function (results) {
return results.map(function (result) {
return {
...result,
manager: 'Keychain'
}
})
})
}
}
module.exports = Keychain