Skip to content

Commit a058353

Browse files
committed
Merge branch 'stage-6-extras' of https://github.com/panva/jsonwebtoken.github.io into stage-6-pr-305
2 parents fbb63a5 + 91cfa3f commit a058353

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

src/utils.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { KEYUTIL } from 'jsrsasign';
22
import log from 'loglevel';
33
import clipboard from 'clipboard-polyfill';
4+
import { isToken } from './editor/jwt.js';
45

56
export function httpGet(url, cache = true) {
67
return new Promise((resolve, reject) => {
@@ -117,6 +118,25 @@ export function copyTokenLink(token, publicKeyOptional) {
117118
return url;
118119
}
119120

121+
function regexp(body, flag) {
122+
return new RegExp("[?&#]" + body + "(?:=([^&#]*)|&|#|$)", flag);
123+
}
124+
125+
const tokenRegexp = regexp('((?:id_|access_)?token|value)', 'g');
126+
127+
export function getTokensFromLocation() {
128+
const { href } = window.location;
129+
let name, value;
130+
const val = {};
131+
132+
try {
133+
while ([, name, value] = tokenRegexp.exec(href)) {
134+
if(isToken(value)) val[name] = value;
135+
}
136+
} catch (err) {}
137+
return val;
138+
}
139+
120140
// https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
121141
export function getParameterByName(name, url) {
122142
if(!url) {
@@ -125,16 +145,16 @@ export function getParameterByName(name, url) {
125145

126146
name = name.replace(/[\[\]]/g, "\\$&");
127147

128-
const regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)");
148+
const regex = regexp(name);
129149
const results = regex.exec(url);
130150
if(!results) {
131151
return null;
132152
}
133-
if(!results[2]) {
153+
if(!results[1]) {
134154
return '';
135155
}
136156

137-
return decodeURIComponent(results[2].replace(/\+/g, " "));
157+
return decodeURIComponent(results[1].replace(/\+/g, " "));
138158
}
139159

140160
export function isWideScreen() {

src/website/index.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { setupTokenEditor, setTokenEditorValue } from '../editor';
55
import { setupJwtCounter } from './counter.js';
66
import { setupSmoothScrolling } from './smooth-scrolling.js';
77
import { setupHighlighting } from './highlighting.js';
8-
import { getParameterByName } from '../utils.js';
8+
import { getParameterByName, getTokensFromLocation } from '../utils.js';
99
import { isChrome, isFirefox } from './utils.js';
1010
import { setupShareJwtButton } from '../share-button.js';
1111
import {
@@ -22,20 +22,17 @@ import {
2222

2323
function parseLocationQuery() {
2424
const publicKey = getParameterByName('publicKey');
25-
const value = getParameterByName('value');
26-
const token = getParameterByName('token');
25+
const { id_token, access_token, value, token } = getTokensFromLocation();
2726

2827
let scroll = false;
2928
if(publicKey) {
3029
publicKeyTextArea.value = publicKey;
3130
scroll = true;
3231
}
33-
if(value) {
34-
setTokenEditorValue(value);
35-
scroll = true;
36-
}
37-
if(token) {
38-
setTokenEditorValue(token);
32+
33+
const val = value || token || id_token || access_token;
34+
if(val) {
35+
setTokenEditorValue(val);
3936
scroll = true;
4037
}
4138

test/functional/editor.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,4 +1151,23 @@ describe('Editor', function() {
11511151
}
11521152
);
11531153
});
1154+
1155+
describe('parsing tokens from window.location.href', () => {
1156+
const token = 'eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.';
1157+
['token', 'value', 'id_token', 'access_token'].forEach((key) => {
1158+
[
1159+
`/?${key}=${token}`,
1160+
`/#${key}=${token}`,
1161+
`/?foo=bar&${key}=${token}`,
1162+
`/#foo=bar&${key}=${token}`,
1163+
].forEach((structure, i) => {
1164+
it(`Should parse ${key} from window.location.href [${i}]`, async function () {
1165+
await this.page.goto(`http://localhost:8000${structure}${key}${i}`);
1166+
expect(await this.page.evaluate(() => {
1167+
return window.test.tokenEditor.getValue();
1168+
})).to.equal(`${token}${key}${i}`);
1169+
});
1170+
})
1171+
});
1172+
});
11541173
});

0 commit comments

Comments
 (0)