Repository navigation
Expand file tree
/
Copy pathjwt-decoder.html
More file actions
96 lines (92 loc) · 3.54 KB
/
Copy pathjwt-decoder.html
File metadata and controls
96 lines (92 loc) · 3.54 KB
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JWT Decoder</title>
<script type="application/masscode-tool+json">
{
"schemaVersion": 1,
"name": "JWT Decoder",
"description": "Decode a signed JWT header and payload and read its timestamps.",
"category": "crypto",
"author": "massCode",
"authorUrl": "https://github.com/massCodeIO",
"license": "MIT",
"keywords": ["jwt", "claims", "token", "expiry"]
}
</script>
</head>
<body>
<h1>JWT Decoder</h1>
<p class="mc-muted">Decoded claims are unverified. This tool does not verify the signature.</p>
<div class="mc-card mc-grid">
<label>Token <textarea id="token" maxlength="16384" rows="5" spellcheck="false"></textarea></label>
<div class="mc-row">
<button type="button" id="decode" class="mc-btn-primary">Decode</button>
<button type="button" id="clear" class="mc-btn">Clear</button>
</div>
</div>
<p id="status" role="status"></p>
<section id="results" hidden>
<h2>Header</h2>
<pre id="header"></pre>
<h2>Payload</h2>
<pre id="payload"></pre>
<h2>UTC timestamps</h2>
<pre id="dates"></pre>
</section>
<script>
const get = id => document.getElementById(id)
function decodeSegment(value) {
if (!/^[A-Za-z0-9_-]+$/.test(value))
throw new Error('The token contains an invalid Base64URL segment.')
const binary = atob(value.replace(/-/g, '+').replace(/_/g, '/'))
const bytes = Uint8Array.from(binary, char => char.charCodeAt(0))
const result = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(bytes))
if (!result || typeof result !== 'object' || Array.isArray(result))
throw new Error('The header and payload must be JSON objects.')
return result
}
function clearResults() {
get('results').hidden = true
for (const id of ['header', 'payload', 'dates', 'status'])
get(id).textContent = ''
get('status').className = ''
}
get('clear').addEventListener('click', () => {
get('token').value = ''
clearResults()
})
get('decode').addEventListener('click', () => {
clearResults()
try {
const segments = get('token').value.trim().split('.')
if (segments.length !== 3 || !/^[A-Za-z0-9_-]+$/.test(segments[2]))
throw new Error('Enter a signed three-part JWT.')
const header = decodeSegment(segments[0])
const payload = decodeSegment(segments[1])
const now = Date.now()
const dates = Object.entries({ iat: 'Issued at', nbf: 'Not before', exp: 'Expires' })
.filter(([name]) => Object.hasOwn(payload, name))
.map(([name, label]) => {
const value = payload[name]
const date = new Date(typeof value === 'number' ? value * 1000 : NaN)
if (!Number.isFinite(date.getTime()))
return `${label}: invalid timestamp`
return `${label}: ${date.toISOString()}${name === 'exp' && date.getTime() <= now ? ' (expired)' : ''}`
})
get('header').textContent = JSON.stringify(header, null, 2)
get('payload').textContent = JSON.stringify(payload, null, 2)
get('dates').textContent = dates.length ? dates.join('\n') : 'No standard timestamps.'
get('results').hidden = false
}
catch (error) {
get('status').className = 'mc-error'
get('status').textContent = error instanceof SyntaxError
? 'The header or payload is not valid JSON.'
: error.message
}
})
</script>
</body>
</html>