Skip to content

Commit edb584e

Browse files
committed
Implement token grabber
When looking at the vaut web UI, you can now grab its token This should help with SSO Vaults like in #30 and #21
1 parent 37981ec commit edb584e

File tree

6 files changed

+78
-10
lines changed

6 files changed

+78
-10
lines changed

.eslintrc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ env:
33
es6: true
44
extends: 'eslint:recommended'
55
parserOptions:
6-
ecmaVersion: 2018
6+
ecmaVersion: 2019
77
rules:
88
indent:
99
- error

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v1.4.0 # Use the ref you want to point at
3+
rev: v4.3.0 # Use the ref you want to point at
44
hooks:
55
- id: trailing-whitespace
66
- id: check-case-conflict
@@ -10,7 +10,7 @@ repos:
1010
- id: end-of-file-fixer
1111
- id: check-symlinks
1212
- repo: https://github.com/pre-commit/mirrors-eslint
13-
rev: v5.11.1 # Use the sha / tag you want to point at
13+
rev: v8.27.0 # Use the sha / tag you want to point at
1414
hooks:
1515
- id: eslint
1616
exclude: >

content.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-console */
2-
/* global browser, chrome */
2+
/* global browser */
33
// We can only access the TABs DOM with this script.
44
// It will get the credentials via message passing from the popup
55
// It is also responsible to copy strings to the clipboard
@@ -12,6 +12,9 @@ browser.runtime.onMessage.addListener((request) => {
1212
case 'fill_creds':
1313
handleFillCredits(request);
1414
break;
15+
case 'fetch_token':
16+
handleFetchToken();
17+
break;
1518
}
1619
});
1720

@@ -93,8 +96,38 @@ function handleFillCredits(request) {
9396
fillIn(passwordNode, request.password);
9497
}
9598

99+
function handleFetchToken() {
100+
let element = '';
101+
for (const [, value] of Object.entries(window.localStorage)) {
102+
try {
103+
element = JSON.parse(value);
104+
} catch {
105+
continue;
106+
}
107+
if (
108+
Object.prototype.hasOwnProperty.call(element,'token') &&
109+
Object.prototype.hasOwnProperty.call(element,'ttl') &&
110+
Object.prototype.hasOwnProperty.call(element,'policies')
111+
) {
112+
browser.runtime.sendMessage({
113+
type: 'fetch_token',
114+
token: element.token,
115+
policies: element.policies,
116+
address: window.location.origin,
117+
});
118+
return;
119+
}
120+
}
121+
browser.runtime.sendMessage({
122+
type: 'token_missing',
123+
token: element.token,
124+
policies: element.policies,
125+
address: window.location.origin,
126+
});
127+
}
128+
96129
function fillForm() {
97-
chrome.runtime.sendMessage({
130+
browser.runtime.sendMessage({
98131
type: 'auto_fill_secrets',
99132
});
100133
}

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifest_version": 3,
33
"name": "VaultPass",
44
"description": "A Chrome extension to leverage Hashicorp Vault as Credential Storage for teams",
5-
"version": "2.3",
5+
"version": "2.3.2",
66
"action": {
77
"default_icon": "icons/logo128.png",
88
"default_popup": "popup.html",

options.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ <h1 class="h1 title">VaultPass</h1>
7373
value="Login to Vault"
7474
id="authButton"
7575
/>
76+
<input
77+
type="submit"
78+
class="button button--primary"
79+
value="Get Token from Vault"
80+
id="tokenGrabber"
81+
/>
7682
</div>
7783

7884
<div>

options.js

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-console */
2-
/* global authButtonClick browser Notify */
2+
/* global browser Notify */
33

44
const notify = new Notify(document.querySelector('#notify'));
55
async function mainLoaded() {
@@ -11,6 +11,9 @@ async function mainLoaded() {
1111
document
1212
.getElementById('authButton')
1313
.addEventListener('click', authButtonClick, false);
14+
document
15+
.getElementById('tokenGrabber')
16+
.addEventListener('click', tokenGrabberClick, false);
1417
document
1518
.getElementById('logoutButton')
1619
.addEventListener('click', logout, false);
@@ -59,9 +62,7 @@ async function querySecrets(vaultServerAdress, vaultToken, policies) {
5962
);
6063
if (!fetchListOfSecretDirs.ok) {
6164
const returnText = await fetchListOfSecretDirs.text();
62-
notify.error(
63-
`Fetching list of secret directories failed: ${returnText}`
64-
);
65+
notify.error(`Fetching list of secret directories failed: ${returnText}`);
6566
throw new Error(
6667
`Fetching list of secret directories failed: ${returnText}`
6768
);
@@ -223,4 +224,32 @@ async function authButtonClick() {
223224
}
224225
}
225226

227+
async function tokenGrabberClick() {
228+
var tabs = await browser.tabs.query({ active: true, currentWindow: true });
229+
for (let tabIndex = 0; tabIndex < tabs.length; tabIndex++) {
230+
var tab = tabs[tabIndex];
231+
if (tab.url) {
232+
browser.tabs.sendMessage(tab.id, {
233+
message: 'fetch_token',
234+
});
235+
break;
236+
}
237+
}
238+
}
239+
226240
document.addEventListener('DOMContentLoaded', mainLoaded, false);
241+
242+
browser.runtime.onMessage.addListener( async function (message) {
243+
switch (message.type) {
244+
case 'fetch_token':
245+
await browser.storage.local.set({ vaultToken: message.token });
246+
await browser.storage.sync.set({ vaultAddress: message.address });
247+
await querySecrets(message.address, message.token, message.policies);
248+
break;
249+
case 'token_missing':
250+
notify.error('Failed to find Vault info from current tab');
251+
break;
252+
default:
253+
break;
254+
}
255+
});

0 commit comments

Comments
 (0)