-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
144 lines (128 loc) · 4.1 KB
/
index.html
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<!doctype html>
<html>
<head>
<meta charset='utf-8'>
<title>JWT datetime (all local)</title>
<style>
body {
padding: 20px;
margin-bottom: 20px;
}
textarea {
width: 80%;
height: 200px;
padding: 4px;
margin-left: 1rem;
}
.container {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 20px;
}
.leading {
grid-column: 1;
}
.trailing {
grid-column: 2;
}
.both {
grid-column: 1 / 3;
grid-row: 2;
background-color: rgb(231 229 228);
padding: 1rem;
}
footer {
padding: 20px;
}
</style>
</head>
<body>
<h1>JWT datetime (all local)</h1>
<p>We do not record tokens. All validation and debugging is done on the client side.</p>
<div class="container">
<div class="leading">
<h2>JWT encoded value (edit me)</h2>
<textarea id="input"
oninput="calc();">eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c</textarea><br />
</div>
<div class="trailing">
<h2>jwt header</h2>
<textarea id="header"></textarea><br />
<h2>jwt payload</h2>
<textarea id="payload"></textarea><br />
</div>
<div class="both">
<div class="container">
<h3>encoder</h3>
<div class="leading">
<textarea id="encoderInput" oninput="enc();">{"alg":"None"}</textarea>
</div>
<div class="trailing">
<textarea id="encoderOutput"></textarea>
</div>
</div>
</div>
</div>
<footer>
2024 <a href="https://github.com/xta/jwt-datetime">view on github</a>
</footer>
<script>
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
}
calc();
enc();
function calc() {
var input = document.getElementById('input');
var header = document.getElementById('header');
var payload = document.getElementById('payload');
if (input && input.value) {
var val = input.value.split('.');
try {
header.value = pretty(JSON.parse(atob(val[0])));
} catch (e) {
header.value = "Invalid value: " + e;
console.error(e)
}
try {
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);
}
} else {
console.log('no data');
}
}
</script>
</body>
</html>