forked from mulbc/vaultPass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
181 lines (162 loc) · 5.7 KB
/
popup.js
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
/* global browser Notify */
const notify = new Notify(document.querySelector('#notify'));
async function mainLoaded() {
var resultList = document.getElementById('resultList');
var currentUrl;
var tabs = await browser.tabs.query({ active: true, currentWindow: true });
for (let tabIndex = 0; tabIndex < tabs.length; tabIndex++) {
var tab = tabs[tabIndex];
if (tab.url) {
currentUrl = tab.url;
break;
}
}
var vaultToken = (await browser.storage.local.get('vaultToken')).vaultToken;
if (!vaultToken || vaultToken.length === 0) {
return notify.clear().info(
`No Vault-Token information available.<br>
Please use the <a href="/options.html" class="link">options page</a> to login.`,
{ removeOption: false }
);
}
var vaultServerAdress = (await browser.storage.sync.get('vaultAddress'))
.vaultAddress;
var secretList = (await browser.storage.sync.get('secrets')).secrets;
if (!secretList) {
secretList = [];
}
resultList.textContent = '';
var promises = [];
for (const secret of secretList) {
promises.push((async function () {
var secretsInPath = await fetch(`${vaultServerAdress}/v1/secret/metadata/vaultPass/${secret}`, {
method: 'LIST',
headers: {
'X-Vault-Token': vaultToken,
'Content-Type': 'application/json'
},
});
if (!secretsInPath.ok) {
notify.error(
`Token is not able to read ${secret}... Try re-login`,
{ removeOption: true }
);
return;
}
let anyMatch = false;
for (const element of (await secretsInPath.json()).data.keys) {
var pattern = new RegExp(element);
var patternMatches = pattern.test(currentUrl);
if (patternMatches) {
const urlPath = `${vaultServerAdress}/v1/secret/data/vaultPass/${secret}${element}`;
const credentials = await getCredentials(urlPath);
addCredentials(credentials.data.data, element, resultList);
anyMatch = true;
notify.clear();
}
}
if (!anyMatch) {
notify.info(
'No matching key found for this page.',
{ removeOption: false }
);
}
})());
}
await Promise.all(promises);
}
function addCredentials(credentials, credentialName, list) {
const item = document.createElement('li');
item.classList.add('list__item', 'list__item--three-line');
const primaryContent = document.createElement('button');
primaryContent.title = 'insert credentials';
primaryContent.classList.add(
'list__item-primary-content',
'list__item-button',
'nobutton',
'js-button',
'js-ripple-effect'
);
primaryContent.addEventListener('click', function () {
fillCredentialsInBrowser(credentials.username, credentials.password);
});
item.appendChild(primaryContent);
const titleContent = document.createElement('span');
titleContent.classList.add('list__item-text-title', 'link');
titleContent.textContent = credentials.title || credentialName;
primaryContent.appendChild(titleContent);
const detailContent = document.createElement('span');
detailContent.classList.add('list__item-text-body');
detailContent.textContent = `User: ${credentials.username}`;
primaryContent.appendChild(detailContent);
const actions = document.createElement('div');
actions.classList.add('list__item-actions');
item.appendChild(actions);
const copyUsernameButton = document.createElement('button');
copyUsernameButton.classList.add('button');
copyUsernameButton.title = 'copy username to clipboard';
copyUsernameButton.innerHTML = `
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon--inline">
<use href="icons/copy-user.svg#copy-user"/>
</svg>
`;
copyUsernameButton.addEventListener('click', function () {
copyStringToClipboard(credentials.username);
});
actions.appendChild(copyUsernameButton);
const copyPasswordButton = document.createElement('button');
copyPasswordButton.classList.add('button');
copyPasswordButton.title = 'copy password to clipboard';
copyPasswordButton.innerHTML = `
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon--inline">
<use href="icons/copy-key.svg#copy-key"/>
</svg>
`;
copyPasswordButton.addEventListener('click', function () {
copyStringToClipboard(credentials.password);
});
actions.appendChild(copyPasswordButton);
list.appendChild(item);
}
async function getCredentials(urlPath) {
const vaultToken = (await browser.storage.local.get('vaultToken')).vaultToken;
const result = await fetch(urlPath, {
headers: {
'X-Vault-Token': vaultToken,
'Content-Type': 'application/json'
}
});
if (!result.ok) {
throw new Error(`getCredentials: ${await result.text}`);
}
return await result.json();
}
async function fillCredentialsInBrowser(username, password) {
var tabs = await browser.tabs.query({ active: true, currentWindow: true });
for (let tabIndex = 0; tabIndex < tabs.length; tabIndex++) {
var tab = tabs[tabIndex];
if (tab.url) {
// tabs.sendMessage(integer tabId, any message, optional object options, optional function responseCallback)
browser.tabs.sendMessage(tab.id, {
message: 'fill_creds',
username: username,
password: password
});
break;
}
}
}
async function copyStringToClipboard(string) {
var tabs = await browser.tabs.query({ active: true, currentWindow: true });
for (let tabIndex = 0; tabIndex < tabs.length; tabIndex++) {
var tab = tabs[tabIndex];
if (tab.url) {
browser.tabs.sendMessage(tab.id, {
message: 'copy_to_clipboard',
string: string
});
break;
}
}
}
document.addEventListener('DOMContentLoaded', mainLoaded, false);