-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.html
100 lines (94 loc) · 2.82 KB
/
client.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>qrcode_speakeasy</title>
<script
type="text/javascript"
src="https://unpkg.com/[email protected]/lib/qr-code-styling.js"
></script>
</head>
<body>
<div>
<button id="generate-qrcode">Generate QRCODE</button>
<div style="margin-bottom: 4px"></div>
<div id="qrcode"></div>
</div>
<div>
<button id="verify-totp">Verify TOTP</button>
<div style="margin-bottom: 4px"></div>
<input
id="totp-code"
type="number"
placeholder="Enter TOTP Code"
size="6"
/>
<input
id="for-test-totp-window"
type="number"
placeholder="[TEST] Enter Window"
/>
</div>
<script type="text/javascript">
const userId = 1;
const generateQrcode = () => {
fetch("http://localhost:3000/generate", {
method: "POST",
body: JSON.stringify({ userId }),
headers: {
"Content-Type": "application/json",
},
})
.then((response) => {
return response.json();
})
.then(({ qrCodeData }) => {
const qrCodeImage = new QRCodeStyling({
width: 300,
height: 300,
type: "svg",
data: qrCodeData,
image:
"https://upload.wikimedia.org/wikipedia/commons/5/51/Facebook_f_logo_%282019%29.svg",
dotsOptions: {
color: "#4267b2",
type: "rounded",
},
backgroundOptions: {
color: "#e9ebee",
},
imageOptions: {
crossOrigin: "anonymous",
margin: 20,
},
});
qrCodeImage.append(document.getElementById("qrcode"));
});
};
const verifyTotp = () => {
const totpCode = document.getElementById("totp-code").value || null;
const maybeWindow =
+document.getElementById("for-test-totp-window").value ?? null;
fetch("http://localhost:3000/verify", {
method: "POST",
body: JSON.stringify({ userId, totpCode, window: maybeWindow }),
headers: {
"Content-Type": "application/json",
},
})
.then((response) => {
if (response.status === 401) {
throw new Error("Not Verified");
}
alert("Verified");
})
.catch(() => {
alert("Not Verified");
});
};
document.getElementById("generate-qrcode").onclick = generateQrcode;
document.getElementById("verify-totp").onclick = verifyTotp;
</script>
</body>
</html>