-
-
Notifications
You must be signed in to change notification settings - Fork 865
Expand file tree
/
Copy pathpasswordManager.js
More file actions
129 lines (115 loc) · 4.12 KB
/
Copy pathpasswordManager.js
File metadata and controls
129 lines (115 loc) · 4.12 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
const { ipcRenderer } = require('electron')
const settings = require('util/settings/settings.js')
const webviews = require('webviews.js')
const keybindings = require('keybindings.js')
const statistics = require('js/statistics.js')
const Bitwarden = require('js/passwordManager/bitwarden.js')
const OnePassword = require('js/passwordManager/onePassword.js')
const Keychain = require('js/passwordManager/keychain.js')
const PasswordManagers = {
// List of supported password managers. Each password manager is expected to
// have getSuggestions(domain) method that returns a Promise with credentials
// suggestions matching given domain name.
managers: [
new Bitwarden(),
new OnePassword(),
new Keychain()
],
// Returns an active password manager, which is the one that is selected in app's
// settings.
getActivePasswordManager: function () {
if (PasswordManagers.managers.length === 0) {
return null
}
const managerSetting = settings.get('passwordManager')
if (managerSetting == null) {
return PasswordManagers.managers.find(mgr => mgr.name === 'Built-in password manager')
}
return PasswordManagers.managers.find(mgr => mgr.name === managerSetting.name)
},
getConfiguredPasswordManager: async function () {
const manager = PasswordManagers.getActivePasswordManager()
if (!manager) {
return null
}
const configured = await manager.checkIfConfigured()
if (!configured) {
return null
}
return manager
},
// Shows a prompt dialog for password store's master password.
promptForMasterPassword: async function (manager) {
return new Promise((resolve, reject) => {
const { password } = ipcRenderer.sendSync('prompt', {
text: l('passwordManagerUnlock').replace('%p', manager.name),
values: [{ placeholder: l('password'), id: 'password', type: 'password' }],
ok: l('dialogConfirmButton'),
cancel: l('dialogSkipButton'),
height: 175
})
if (password === null || password === '') {
reject(new Error('No password provided'))
} else {
resolve(password)
}
})
},
unlock: async function (manager) {
let success = false
while (!success) {
let password
try {
password = await PasswordManagers.promptForMasterPassword(manager)
} catch (e) {
// dialog was canceled
break
}
try {
success = await manager.unlockStore(password)
} catch (e) {
// incorrect password, prompt again
}
}
return success
},
// Binds IPC events.
initialize: function () {
// Called when page preload script detects a form with username and password.
webviews.bindIPC('password-autofill', function (tab, args, frameId, frameURL) {
// it's important to use frameURL here and not the tab URL, because the domain of the
// requesting iframe may not match the domain of the top-level page
const origin = new URL(frameURL).origin
PasswordManagers.getConfiguredPasswordManager().then(async (manager) => {
if (!manager) {
return
}
if (!manager.isUnlocked()) {
await PasswordManagers.unlock(manager)
}
manager.getSuggestions(origin).then(credentials => {
if (credentials != null) {
webviews.callAsync(tab, 'sendToFrame', [frameId, 'password-autofill-match', {
credentials,
origin
}])
}
}).catch(e => {
console.error('Failed to get password suggestions: ' + e.message)
})
})
})
webviews.bindIPC('password-autofill-check', function (tab, args, frameId) {
if (PasswordManagers.getActivePasswordManager()) {
webviews.callAsync(tab, 'sendToFrame', [frameId, 'password-autofill-enabled'])
}
})
keybindings.defineShortcut('fillPassword', function () {
webviews.callAsync(tabs.getSelected(), 'send', ['password-autofill-shortcut'])
})
statistics.registerGetter('passwordManager', function () {
return PasswordManagers.getActivePasswordManager().name
})
}
}
module.exports = PasswordManagers