-
-
Notifications
You must be signed in to change notification settings - Fork 865
Expand file tree
/
Copy pathbitwarden.js
More file actions
203 lines (167 loc) · 5.51 KB
/
Copy pathbitwarden.js
File metadata and controls
203 lines (167 loc) · 5.51 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
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
195
196
197
198
199
200
201
202
203
const ProcessSpawner = require('util/process.js')
const path = require('path')
const fs = require('fs')
var { ipcRenderer } = require('electron')
// Bitwarden password manager. Requires session key to unlock the vault.
class Bitwarden {
constructor () {
this.sessionKey = null
this.lastCallList = {}
this.name = 'Bitwarden'
}
getDownloadLink () {
switch (window.platformType) {
case 'mac':
return 'https://vault.bitwarden.com/download/?app=cli&platform=macos'
case 'windows':
return 'https://vault.bitwarden.com/download/?app=cli&platform=windows'
case 'linux':
return 'https://vault.bitwarden.com/download/?app=cli&platform=linux'
}
}
getLocalPath () {
return path.join(window.globalArgs['user-data-path'], 'tools', (platformType === 'windows' ? 'bw.exe' : 'bw'))
}
getSetupMode () {
return 'dragdrop'
}
// Returns a Bitwarden-CLI tool path by checking possible locations.
// First it checks if the tool was installed for Min specifically
// by checking the settings value. If that is not set or doesn't point
// to a valid executable, it checks if 'bw' is available globally.
async _getToolPath () {
const localPath = this.getLocalPath()
if (localPath) {
let local = false
try {
await fs.promises.access(localPath, fs.constants.X_OK)
local = true
} catch (e) { }
if (local) {
return localPath
}
}
const global = await new ProcessSpawner('bw').checkCommandExists()
if (global) {
return 'bw'
}
return null
}
// Checks if Bitwarden integration is configured properly by trying to
// obtain a valid Bitwarden-CLI tool path.
async checkIfConfigured () {
this.path = await this._getToolPath()
return this.path != null
}
// Returns current Bitwarden-CLI status. If we have a session key, then
// password store is considered unlocked.
isUnlocked () {
return this.sessionKey != null
}
// Tries to get a list of credential suggestions for a given domain name.
async getSuggestions (domain) {
if (this.lastCallList[domain] != null) {
return this.lastCallList[domain]
}
const command = this.path
if (!command) {
return Promise.resolve([])
}
if (!this.isUnlocked()) {
throw new Error()
}
this.lastCallList[domain] = this.loadSuggestions(command, domain).then(suggestions => {
this.lastCallList[domain] = null
return suggestions
}).catch(ex => {
this.lastCallList[domain] = null
})
return this.lastCallList[domain]
}
// Loads credential suggestions for given domain name.
async loadSuggestions (command, domain) {
try {
const urlObj = new URL(domain)
const process = new ProcessSpawner(command, ['list', 'items', '--url', `${urlObj.protocol}//${this.sanitize(urlObj.hostname)}`, '--session', this.sessionKey])
const data = await process.execute()
const matches = JSON.parse(data)
const credentials = matches.map(match => {
const { login: { username, password } } = match
return { username, password, manager: 'Bitwarden' }
})
return credentials
} catch (ex) {
const { error, data } = ex
console.error('Error accessing Bitwarden CLI. STDOUT: ' + data + '. STDERR: ' + error)
return []
}
}
async forceSync (command) {
try {
const process = new ProcessSpawner(command, ['sync', '--session', this.sessionKey])
await process.execute()
} catch (ex) {
const { error, data } = ex
console.error('Error accessing Bitwarden CLI. STDOUT: ' + data + '. STDERR: ' + error)
}
}
// Tries to unlock the password store with given master password.
async unlockStore (password) {
try {
const process = new ProcessSpawner(this.path, ['unlock', '--raw', password])
const result = await process.execute()
if (!result) {
throw new Error()
}
this.sessionKey = result
await this.forceSync(this.path)
return true
} catch (ex) {
const { error, data } = ex
console.error('Error accessing Bitwarden CLI. STDOUT: ' + data + '. STDERR: ' + error)
if (error.includes('not logged in')) {
await this.signInAndSave()
return await this.unlockStore(password)
}
throw ex
}
}
async signInAndSave (path = this.path) {
// It's possible to be already logged in
const logoutProcess = new ProcessSpawner(path, ['logout'])
try {
await logoutProcess.execute()
} catch (e) {
console.warn(e)
}
// show credentials dialog
var signInFields = [
{ placeholder: 'Client ID', id: 'clientID', type: 'password' },
{ placeholder: 'Client Secret', id: 'clientSecret', type: 'password' }
]
const credentials = ipcRenderer.sendSync('prompt', {
text: l('passwordManagerBitwardenSignIn'),
values: signInFields,
ok: l('dialogConfirmButton'),
cancel: l('dialogSkipButton'),
width: 500,
height: 260
})
for (const key in credentials) {
if (credentials[key] === '') {
throw new Error('no credentials entered')
}
}
const process = new ProcessSpawner(path, ['login', '--apikey'], {
BW_CLIENTID: credentials.clientID.trim(),
BW_CLIENTSECRET: credentials.clientSecret.trim()
})
await process.execute()
return true
}
// Basic domain name cleanup. Removes any non-ASCII symbols.
sanitize (domain) {
return domain.replace(/[^a-zA-Z0-9.-]/g, '')
}
}
module.exports = Bitwarden