-
Notifications
You must be signed in to change notification settings - Fork 41
/
popup.js
104 lines (93 loc) · 3.23 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
/* global chrome jQuery */
/* eslint "space-before-function-paren": ["error", {
"anonymous": "always",
"named": "never",
"asyncArrow": "always"
}], */
var vaultServerAdress, vaultToken, secretList, currentUrl
function mainLoaded() {
var resultList = document.getElementById('resultList')
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
for (let tabIndex = 0; tabIndex < tabs.length; tabIndex++) {
var tab = tabs[tabIndex]
if (tab.url) {
currentUrl = tab.url
}
}
})
chrome.storage.local.get(['vaultToken'], function (result) {
if (result.vaultToken) {
vaultToken = result.vaultToken
} else {
console.error('No Vault-Token information available\nPlease use the options page to login')
}
})
chrome.storage.sync.get(['vaultAddress'], function (result) {
vaultServerAdress = result.vaultAddress
})
chrome.storage.sync.get(['secrets'], function (result) {
secretList = result.secrets
if (!secretList) {
secretList = []
}
resultList.textContent = ''
secretList.forEach(secret => {
jQuery.ajax({
type: 'LIST',
url: vaultServerAdress + '/v1/secret/metadata/vaultPass/' + secret,
headers: { 'X-Vault-Token': vaultToken },
contentType: 'application/json',
dataType: 'json',
success: function (data) {
data.data.keys.forEach(element => {
var pattern = new RegExp(element)
var patternMatches = pattern.test(currentUrl)
if (patternMatches) {
getCredentials(vaultServerAdress + '/v1/secret/data/vaultPass/' + secret + element).then((credentials) =>
addCredentials(credentials.data.data, element, resultList))
}
})
},
error: function (data) {
console.error('ERROR accessing ' + secret + ': ' + JSON.stringify(data))
}
})
})
})
}
function addCredentials(credentials, credentialName, list) {
var item = document.createElement('li')
item.addEventListener('click', function () {
fillCredentialsInBrowser(credentials.username, credentials.password)
})
item.appendChild(document.createTextNode(credentialName + ' - ' + JSON.stringify(credentials)))
list.appendChild(item)
}
async function getCredentials(urlPath) {
console.debug('Looking for credentials in ' + urlPath)
let result
try {
result = await jQuery.ajax({
type: 'GET',
url: urlPath,
headers: { 'X-Vault-Token': vaultToken },
contentType: 'application/json',
dataType: 'json'
})
return result
} catch (error) {
console.error(error)
}
}
function fillCredentialsInBrowser(username, password) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
for (let tabIndex = 0; tabIndex < tabs.length; tabIndex++) {
var tab = tabs[tabIndex]
if (tab.url) {
currentUrl = tab.url
chrome.tabs.sendMessage(tab.id, { message: 'fill_creds', username: username, password: password }, function () { })
}
}
})
}
document.addEventListener('DOMContentLoaded', mainLoaded, false)