1
+ <!DOCTYPE html>
1
2
< html lang ="en ">
2
- < head >
3
- < meta charset ="UTF-8 ">
4
- < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
5
- < title > JWT Validator</ title >
6
- < style type ="text/css ">
7
- .wrapper {
8
- max-width : 640px ;
9
- margin : 1rem auto;
10
- }
11
- fieldset {
12
- margin-bottom : 1rem ;
13
- }
14
- </ style >
15
- </ head >
16
- < body >
17
- < div class ="wrapper ">
18
- < form action ="# " id ="validator ">
19
- < fieldset >
20
- < legend for ="token "> Your token:</ legend >
21
- < textarea name ="token " id ="token " cols ="70 " rows ="10 "> </ textarea >
22
- </ fieldset >
23
- < button type ="submit "> Validate</ button >
24
- </ form >
3
+ < head >
4
+ < meta charset ="UTF-8 " />
5
+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 " />
6
+ < title > JWT Validator</ title >
7
+ < style type ="text/css ">
8
+ body {
9
+ font-family : Arial, sans-serif;
10
+ background-color : # f4f4f4 ;
11
+ color : # 333 ;
12
+ }
13
+ .wrapper {
14
+ max-width : 640px ;
15
+ margin : 2rem auto;
16
+ padding : 1rem ;
17
+ background : white;
18
+ border-radius : 8px ;
19
+ box-shadow : 0 2px 4px rgba (0 , 0 , 0 , 0.1 );
20
+ }
21
+ fieldset {
22
+ border : 1px solid # ddd ;
23
+ padding : 0.5rem ;
24
+ }
25
+ textarea {
26
+ width : calc (100% - 1rem ); /* Adjust width to prevent overflow */
27
+ padding : 0.25rem ;
28
+ border : 1px solid # ddd ;
29
+ border-radius : 4px ;
30
+ margin-top : 0.5rem ;
31
+ box-sizing : border-box; /* Include padding and border in the element's total width and height */
32
+ }
33
+ button {
34
+ padding : 0.5rem 1rem ;
35
+ color : white;
36
+ background-color : # 007bff ;
37
+ border : none;
38
+ border-radius : 4px ;
39
+ cursor : pointer;
40
+ margin-top : 1rem ;
41
+ }
42
+ button : hover {
43
+ background-color : # 0056b3 ;
44
+ }
45
+ # results {
46
+ margin-top : 1rem ;
47
+ padding : 1rem ;
48
+ background-color : # eee ;
49
+ border-radius : 4px ;
50
+ }
51
+ </ style >
52
+ </ head >
53
+ < body >
54
+ < div class ="wrapper ">
55
+ < h1 > JWT Validator</ h1 >
56
+ < form id ="validator ">
57
+ < fieldset >
58
+ < legend > Enter Your Token:</ legend >
59
+ < textarea
60
+ name ="token "
61
+ id ="token "
62
+ rows ="10 "
63
+ placeholder ="Paste your JWT here... "
64
+ >
65
+ eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJjb21tZW50IjoiRWRnZSBmdW5jdGlvbnMgYXJlIHN3ZWV0ISJ9.hc0nVeZWhE6MEf-CHJwljTY3uo6qqm8q_V_0zwm34tFALrjJDAa0CL3oUEehMRNdt3PQdcuWBgzMUqKVUWMRVQ</ textarea
66
+ >
67
+ </ fieldset >
68
+ < button type ="submit "> Validate</ button >
69
+ </ form >
25
70
26
- < div id ="results ">
27
- < p > Click "Validate" above.</ p >
71
+ < p >
72
+ You can generate a test token using the default signing key
73
+ < code > your-256-bit-secret</ code > at
74
+ < a href ="https://jwt.io " target ="_blank " rel ="noopener noreferrer "
75
+ > jwt.io</ a
76
+ > . Valid algorithms for this example include < code > HS256</ code > ,
77
+ < code > HS384</ code > , and < code > HS512</ code > .
78
+ </ p >
79
+
80
+ < div id ="results ">
81
+ < p > Click "Validate" to verify your token.</ p >
82
+ </ div >
28
83
</ div >
29
- </ div >
30
84
31
- < script type ="text/javascript ">
32
- const form = document . getElementById ( 'validator' )
33
- form . addEventListener ( 'submit' , async ( e ) => {
34
- e . preventDefault ( )
35
- const token = form . querySelector ( 'textarea[name="token"]' ) . value
36
- let resultString = ''
37
- try {
38
- const resp = await fetch ( '/jwt' , {
39
- method : 'POST' ,
40
- body : token
41
- } )
42
- const data = await resp . json ( )
43
- console . log ( data )
44
- if ( data . valid ) {
45
- resultString = `<p>Token successfully validated using ${ data . alg } . Refresh the page to try another token.</p><p>Payload:</p><pre>${ JSON . stringify ( data . payload ) } </pre>`
46
- } else {
47
- resultString = '<p>An error occurred validating the token.</p>'
48
- }
49
- } catch ( e ) {
50
- resultString = '<p>An error occurred validating the token.</p>'
51
- console . error ( e )
52
- } finally {
53
- document . getElementById ( 'results' ) . innerHTML = resultString
54
- }
55
- } )
56
- </ script >
57
- </ body >
58
- </ html >
85
+ < script type ="text/javascript ">
86
+ document
87
+ . getElementById ( 'validator' )
88
+ . addEventListener ( 'submit' , async ( e ) => {
89
+ e . preventDefault ( ) ;
90
+ const token = document . getElementById ( 'token' ) . value ;
91
+ let resultString = '' ;
92
+
93
+ try {
94
+ const response = await fetch ( '/jwt' , {
95
+ method : 'POST' ,
96
+ headers : { 'Content-Type' : 'application/json' } ,
97
+ body : JSON . stringify ( { token } ) ,
98
+ } ) ;
99
+
100
+ if ( ! response . ok ) {
101
+ throw new Error ( `Server error occurred. Response code: ${ response . status } , Response body: ${ JSON . stringify ( await response . json ( ) ) } ` ) ;
102
+ }
103
+
104
+ const data = await response . json ( ) ;
105
+
106
+ if ( data . valid ) {
107
+ resultString = `<p>Token successfully validated using ${
108
+ data . alg
109
+ } .</p><p>Payload:</p><pre>${ JSON . stringify (
110
+ data . payload ,
111
+ null ,
112
+ 2
113
+ ) } </pre>`;
114
+ } else {
115
+ resultString = '<p>An error occurred validating the token.</p>' ;
116
+ }
117
+ } catch ( error ) {
118
+ resultString = `<p>${ error . message } </p>` ;
119
+ console . error ( 'Error:' , error ) ;
120
+ } finally {
121
+ document . getElementById ( 'results' ) . innerHTML = resultString ;
122
+ }
123
+ } ) ;
124
+ </ script >
125
+ </ body >
126
+ </ html >
0 commit comments