Skip to content

Commit

Permalink
modify calc() to prepend date.toLocaleString() for exp, nbf, and iat …
Browse files Browse the repository at this point in the history
…claim values
  • Loading branch information
xta committed Jan 9, 2024
1 parent 07d446e commit 6dc3c82
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,35 @@ <h3>encoder</h3>
<textarea id="encoderInput" oninput="enc();">{"alg":"None"}</textarea>
<textarea id="encoderOutput"></textarea>
<script>
function pretty(jsonStr) {
return JSON.stringify(JSON.parse(jsonStr), null, 4);
function pretty(json) {
return JSON.stringify(json, null, 4);
}
function enc() {
var encoderInput = document.getElementById('encoderInput');
var encoderOutput = document.getElementById('encoderOutput');
encoderOutput.value = btoa(encoderInput.value);
}
function secondsToDateLabel(seconds) {
var date = new Date(seconds * 1000)
return `${date.toLocaleString()} (${seconds})`
}
function update(obj) {
var updated = {}
var keys = ['exp', 'iat', 'nbf']
for (var [key, value] of Object.entries(obj)) {
if (keys.includes(key)) {
try {
updated[key] = secondsToDateLabel(value)
} catch (e) {
console.error(e)
updated[key] = value
}
} else {
updated[key] = value
}
}
return updated
}
enc();
function calc() {
var input = document.getElementById('input');
Expand All @@ -45,13 +66,16 @@ <h3>encoder</h3>
if (input && input.value) {
var val = input.value.split('.');
try {
header.value = pretty(atob(val[0]));
header.value = pretty(JSON.parse(atob(val[0])));
} catch (e) {
header.value = "Invalid value: " + e;
console.error(e)
}
try {
payload.value = pretty(atob(val[1]));
var jsonStr = atob(val[1])
var obj = JSON.parse(jsonStr)
var updatedObj = update(obj)
payload.value = pretty(updatedObj);
} catch (e) {
payload.value = "Invalid value: " + e;
console.error(e);
Expand Down

0 comments on commit 6dc3c82

Please sign in to comment.