Skip to content

Commit d235692

Browse files
committed
Fixes #39: implements claim description tooltips.
1 parent 321f47b commit d235692

File tree

8 files changed

+82
-5
lines changed

8 files changed

+82
-5
lines changed

src/dom-elements.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ export const decodedTabLink =
44
document.querySelector('.tab-link a[href="#decoded-jwt"]');
55
export const encodedTabElement = document.getElementById('encoded-jwt');
66
export const decodedTabElement = document.getElementById('decoded-jwt');
7+
78
export const editorElement = document.querySelector('#encoded-jwt .input');
89
export const decodedElement = document.querySelector('#decoded-jwt .output');
10+
export const claimsTooltipElement =
11+
document.getElementById('js-claims-tooltip');
912
export const headerElement = document.querySelector('.js-header');
1013
export const payloadElement = document.querySelector('.js-payload');
1114
export const payloadTooltipElement =

src/editor/claims-tooltip.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { claimsTooltipElement } from '../dom-elements.js';
2+
import { timeClaims } from './time-tooltip.js';
3+
import { stringifyIndentSpaces } from './utils.js';
4+
import strings from '../strings.js';
5+
6+
function hideTooltip() {
7+
claimsTooltipElement.style.display = 'none';
8+
}
9+
10+
function showTooltip(x, y, text) {
11+
claimsTooltipElement.firstChild.textContent = text;
12+
claimsTooltipElement.style.left = x + 'px';
13+
claimsTooltipElement.style.top = y + 'px';
14+
claimsTooltipElement.style.display = 'block';
15+
}
16+
17+
export function claimsTooltipHandler(event) {
18+
const editor = event.currentTarget.querySelector('.CodeMirror').CodeMirror;
19+
//const editor = event.currentTarget.firstChild.CodeMirror;
20+
21+
if(!editor) {
22+
return;
23+
}
24+
25+
const result = editor.coordsChar({
26+
left: event.pageX,
27+
top: event.pageY
28+
}, 'page');
29+
30+
const line = editor.getLine(result.line);
31+
const matches = /"(.*)":\s*"?(.*)"?/.exec(line);
32+
33+
if(!matches) {
34+
hideTooltip();
35+
return;
36+
}
37+
38+
const claim = matches[1];
39+
40+
// If this is a time claim and the mouse cursor is on top of the time,
41+
// let the time tooltip handle this, do nothing.
42+
// TODO: merge both tooltip handlers?
43+
const claimEnd = line.indexOf(':');
44+
if(result.ch >= claimEnd && timeClaims.includes(claim)) {
45+
hideTooltip();
46+
return;
47+
}
48+
49+
const claimText = strings.common.claims[claim];
50+
if(!claimText) {
51+
hideTooltip();
52+
return;
53+
}
54+
55+
showTooltip(event.pageX, event.pageY, claimText);
56+
}

src/editor/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
} from '../utils.js';
66
import { downloadPublicKeyIfPossible } from './public-key-download.js';
77
import { timeTooltipHandler } from './time-tooltip.js';
8+
import { claimsTooltipHandler } from './claims-tooltip.js';
89
import { tokenEditor, headerEditor, payloadEditor } from './instances.js';
910
import {
1011
getTrimmedValue,
@@ -371,6 +372,8 @@ function setupEvents() {
371372

372373
// Human readable timestamp tooltips
373374
payloadElement.addEventListener('mousemove', timeTooltipHandler);
375+
payloadElement.addEventListener('mousemove', claimsTooltipHandler);
376+
headerElement.addEventListener('mousemove', claimsTooltipHandler);
374377

375378
setupTabEvents();
376379
}

src/editor/time-tooltip.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { payloadEditor } from './instances.js';
22
import { payloadTooltipElement } from '../dom-elements.js';
33

4+
export const timeClaims = ['exp', 'nbf', 'iat', 'auth_time', 'updated_at'];
5+
46
export function timeTooltipHandler(event) {
57
const result = payloadEditor.coordsChar({
68
left: event.pageX,
@@ -9,8 +11,6 @@ export function timeTooltipHandler(event) {
911

1012
const line = payloadEditor.getLine(result.line);
1113

12-
const timeClaims = ['exp', 'nbf', 'iat', 'auth_time', 'updated_at'];
13-
1414
const matches = /"(.*)":\s*"?(\d*)"?/.exec(line);
1515
if (matches && timeClaims.indexOf(matches[1]) !== -1) {
1616
const dateStr = (new Date(parseInt(matches[2], 10) * 1000)).toString();

src/editor/utils.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ export function fixEditorHeight() {
2222
}
2323
}
2424

25+
export const stringifyIndentSpaces = 2;
26+
2527
export function stringify(object) {
26-
return JSON.stringify(object, null, 2);
28+
return JSON.stringify(object, null, stringifyIndentSpaces);
2729
}
2830

2931
export function getSelectedAlgorithm() {

src/strings.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ export default {
22
common: {
33
shareThisJwt: 'Share JWT',
44
jwtIoUrlCopied: 'Copied',
5+
claims: {
6+
exp: 'Expiration time (seconds since Unix epoch)',
7+
nbf: 'Not valid before (seconds since Unix epoch)',
8+
iat: 'Issued at (seconds since Unix epoch)',
9+
iss: 'Issuer: who created and signed this token',
10+
sub: 'Subject: who the token refers to',
11+
aud: 'Audience: who or what the token is intended for',
12+
jti: 'JWT ID: unique identifier for this token',
13+
typ: 'Type of token',
14+
azp: 'Authorized party: the party to which this token was issued',
15+
alg: 'Signature or encryption algorithm'
16+
}
517
},
618
extension: {
719
alreadyInstalled: 'Already installed',

stylus/website/codemirror.styl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
.text-line a, .CodeMirror *
4141
color #D63AFF
4242

43-
#js-payload-tooltip
43+
#js-payload-tooltip, #js-claims-tooltip
4444
position absolute
4545
background-color #00b9f1
4646
display none
@@ -51,4 +51,4 @@
5151

5252
.jwt-playground .output .jwt-signature
5353
.text-line a, pre
54-
color #00B9F1
54+
color #00B9F1

views/token-editor-common.pug

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
.input.js-input
1313
#decoded-jwt.box-content
1414
.output
15+
#js-claims-tooltip placeholder
1516
.jwt-explained.jwt-header
1617
p.text-line HEADER:
1718
span ALGORITHM & TOKEN TYPE

0 commit comments

Comments
 (0)