-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.html
More file actions
190 lines (161 loc) · 5.38 KB
/
basic.html
File metadata and controls
190 lines (161 loc) · 5.38 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login Page</title>
</head>
<body>
<h1>Login Form</h1>
<!-- Form to collect username and password -->
<div>
<form id="loginForm">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required /><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required /><br><br>
<button type="submit">Submit</button>
</form>
</div>
</br>
<div>
<button onclick="login()">Fido2 Login</button>
</div>
</br></br>
<a href="/register">
Register
</a>
<script>
function base64urlToUint8Array(base64url) {
// Replace - with + and _ with / and pad with =
let base64 = base64url.replace(/-/g, '+').replace(/_/g, '/');
while (base64.length % 4 !== 0) {
base64 += '=';
}
const binary = atob(base64);
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) {
bytes[i] = binary.charCodeAt(i);
}
return bytes;
}
function bufferToBase64url(buffer) {
return btoa(String.fromCharCode(...new Uint8Array(buffer)))
.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
}
async function login() {
const username = document.getElementById('username').value;
if (!username) {
alert('Please enter a username');
return;
}
const payload = {
username: username,
};
try {
const optionsResponse = await fetch('/Fido2-Begin', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
});
const options = await optionsResponse.json();
if (options.error) {
throw options.error;
}
console.log('Authentication options received:', options);
if (options.allowCredentials) {
options.allowCredentials = options.allowCredentials.map((cred) => ({
...cred,
id: base64urlToUint8Array(cred.id)
}));
}
const credential = await navigator.credentials.get({
publicKey: {
...options,
challenge: base64urlToUint8Array(options.challenge)
}
});
const credentialWithUsername = {
id: credential.id,
rawId: bufferToBase64url(credential.rawId),
type: credential.type,
username: username,
authenticatorAttachment: credential.authenticatorAttachment,
clientExtensionResults: credential.clientExtensionResults,
response: {
clientDataJSON: bufferToBase64url(credential.response.clientDataJSON),
authenticatorData: bufferToBase64url(credential.response.authenticatorData),
signature: bufferToBase64url(credential.response.signature),
userHandle: bufferToBase64url(credential.response.userHandle)
}
};
console.log('Credential info to send:',credentialWithUsername);
const verifyResponse = await fetch("/Fido2-End", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(credentialWithUsername)
});
const result = await verifyResponse.json();
console.log('Result from server:',result);
if (result.message) {
alert(`Login successful! ${result.message}`);
if (verifyResponse.ok) {
try {
window.location.href = "/Box/Service.png";
} catch (error) {
console.log(error);
}
}
} else {
alert(`Error: ${result.error}`);
}
} catch (error) {
console.error('Error: ', error);
alert('Error: ' + error);
}
}
// Event listener for form submission (non-Fido2 login)
document.getElementById('loginForm').addEventListener('submit', async function(event) {
event.preventDefault(); // Prevent form submission from reloading the page
// Get values from input fields
const username = document.getElementById('username').value;
const password = document.getElementById('password').value;
// Create the payload to send in the POST request
const payload = {
username: username,
password: password
};
try {
// Send POST request to /submit with the payload
const response = await fetch('/submit', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
});
// Parse and handle the JSON response from the server
const data = await response.json();
if (data.message) {
// Alert the success message
alert(data.message)
if (response.ok) {
try {
window.location.href = "/Box/Service.png";
} catch (error) {
console.log(error);
}
}
} else {
alert(data.error);
}
} catch (error) {
console.error('Error during POST request:', error);
alert('An error occurred while sending the data.');
}
});
</script>
</body>
</html>