-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
200 lines (164 loc) · 7.52 KB
/
index.php
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JWT Pure PHP</title>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body class="m-0 p-0 border-box p-5">
<div class="alert alert-info mb-5" role="alert">
<strong class="expire-in">Token expire in 60 seconds</strong>
</div>
<div class="mb-5 d-flex align-center justify-content-between">
<div class="w-100 p-2 border rounded-lg mr-3" id="created-token" name="created-token">
</div>
<div class="d-flex align-center">
<button class="btn copy btn-outline-secondary mr-3" type="button">
Copy
</button>
<button type="button" class="btn btn-primary" id="generate-token">
Generate
</button>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<label for="secret-key">Secret Key</label>
<input type="text" class="form-control" id="secret-key" placeholder="Secret Key" value="my-secret-key">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-8">
<label for="validate-token">Validate Token</label>
<input type="text" class="form-control" id="validate-token" placeholder="Validate Token">
</div>
<div class="form-group col-md-4">
<label for="secret-key">Test Secret Key</label>
<input type="text" class="form-control" id="test-secret-key" placeholder="Secret Key" value="my-secret-key">
</div>
</div>
<div id="token-status"></div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script src="plugins/clipboard.min.js"></script>
<script>
new ClipboardJS('.btn');
</script>
<script>
let interval;
const createdToken = document.querySelector('#created-token');
const tokenStatus = document.querySelector('#token-status');
handleInitializeGenerateTokenBtn();
handleInitializeCopyBtn();
handleTokenOrTestSecretKeyChange();
handleGenerateNewToken();
async function handleGenerateNewToken() {
const validateToken = document.querySelector('#validate-token');
const secret = document.querySelector('#secret-key').value;
const fetchResponse = await fetch('getToken.php', {
method: 'POST',
headers: {
'accept': '*',
'Content-Type': 'application/json'
},
body: JSON.stringify({
secret
})
});
const {
response
} = await fetchResponse.json();
if (response.statusCode > 299) {
alert(`${response.statusCode}: ${response.message ? response.message : response.statusText}`);
return;
}
createdToken.innerText = response.data.token;
validateToken.value = response.data.token;
handleCountdownTokenExpiration();
await handleTokenValidation();
}
async function handleTokenOrTestSecretKeyChange() {
const validateToken = document.querySelector('#validate-token');
const secret = document.querySelector('#test-secret-key');
const tokenOrTestSecretKeyChangeEventFunc = async () => {
await handleTokenValidation();
}
removeEventListener('change', tokenOrTestSecretKeyChangeEventFunc);
validateToken.addEventListener('change', tokenOrTestSecretKeyChangeEventFunc);
secret.addEventListener('change', tokenOrTestSecretKeyChangeEventFunc);
}
async function handleTokenValidation() {
const validateToken = document.querySelector('#validate-token').value;
const secret = document.querySelector('#test-secret-key').value;
const fetchResponse = await fetch('validToken.php', {
method: 'POST',
headers: {
'accept': '*',
'Content-Type': 'application/json',
'Authorization': `Bearer ${validateToken}`
},
body: JSON.stringify({
token: validateToken,
secret
})
});
const {
response
} = await fetchResponse.json();
if (response.statusCode > 299) {
alert(`${response.statusCode}: ${response.message ? response.message : response.statusText}`);
}
const {
status,
message
} = response.data;
tokenStatus.innerHTML = `
<div class="alert alert-${status === 1 ? 'success' : 'danger'} mb-5" role="alert">
<strong>${message}</strong>
</div>
`;
}
function handleInitializeGenerateTokenBtn() {
const btnGenerateToken = document.querySelector('#generate-token');
const btnGenerateTokenEventFunc = async () => {
await handleGenerateNewToken();
}
removeEventListener('click', btnGenerateTokenEventFunc);
btnGenerateToken.addEventListener('click', btnGenerateTokenEventFunc);
}
function handleInitializeCopyBtn() {
const btnCopyJWT = document.querySelector('.btn.copy');
const btnCopyEventFunc = () => {
const createdJwt = document.querySelector('#created-token');
btnCopyJWT.setAttribute('data-clipboard-text', createdJwt.innerText);
};
removeEventListener('click', btnCopyEventFunc);
btnCopyJWT.addEventListener('click', btnCopyEventFunc);
}
function handleCountdownTokenExpiration() {
let time = 59;
const infoTokenExpireIn = document.querySelector('.expire-in');
infoTokenExpireIn.innerText = `Token expire in 60 seconds`;
if (interval) clearInterval(interval);
interval = setInterval(async () => {
if (time <= 0) {
tokenStatus.innerHTML = `
<div class="alert alert-danger mb-5" role="alert">
<strong>Token expired</strong>
</div>
`;
clearInterval(interval);
}
infoTokenExpireIn.innerText = `Token expire in ${time} seconds`;
--time;
}, 1000);
}
</script>
</body>
</html>