-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathindex.html
194 lines (170 loc) · 7.56 KB
/
index.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
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
<!-
python3 -m http.server 3000
open http://localhost:3000/index.html
->
<!DOCTYPE html>
<html>
<head>
<title>WebAuthn Signin</title>
</head>
<body>
<h1>WebAuthn Signin</h1>
<button onclick="startSignin()">Start Signin</button>
<div id="emailForm" style="display: none; margin-top: 20px;">
<p>No credentials found. Please enter your email to register:</p>
<input type="email" id="emailInput" placeholder="Enter your email">
<button onclick="startSignup()">Register</button>
</div>
<script>
async function startSignin() {
try {
// First POST request to /signin/webauthn
const initialResponse = await fetch('http://localhost:4000/signin/webauthn', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: '{}'
});
if (!initialResponse.ok) {
throw new Error('Initial request failed');
}
// Get the options from the response
let options = await initialResponse.json();
// Convert base64 strings to ArrayBuffer where needed
if (options.challenge) {
options.challenge = base64URLToArrayBuffer(options.challenge);
}
if (options.allowCredentials) {
options.allowCredentials = options.allowCredentials.map(credential => ({
...credential,
id: base64URLToArrayBuffer(credential.id)
}));
}
// Call navigator.credentials.get with the options
const credential = await navigator.credentials.get({
publicKey: options
});
console.log(arrayBufferToBase64URL(credential.response.userHandle))
// Prepare the credential data for sending to server
const verifyData = {
id: credential.id,
rawId: arrayBufferToBase64URL(credential.rawId),
response: {
authenticatorData: arrayBufferToBase64URL(credential.response.authenticatorData),
clientDataJSON: arrayBufferToBase64URL(credential.response.clientDataJSON),
signature: arrayBufferToBase64URL(credential.response.signature),
userHandle: credential.response.userHandle ? arrayBufferToBase64URL(credential.response.userHandle) : null
},
type: credential.type
};
// Second POST request to /signin/webauthn/verify
const verifyResponse = await fetch('http://localhost:4000/signin/webauthn/verify', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ credential: verifyData })
});
if (!verifyResponse.ok) {
throw new Error('Verification failed');
}
const result = await verifyResponse.json();
console.log('Signin successful:', result);
document.getElementById('emailForm').style.display = 'none';
} catch (error) {
console.error('Error during signin:', error);
// Show email form for registration
document.getElementById('emailForm').style.display = 'block';
}
}
async function startSignup() {
const email = document.getElementById('emailInput').value;
if (!email) {
alert('Please enter an email address');
return;
}
try {
// First POST request to /signup/webauthn
const initialResponse = await fetch('http://localhost:4000/signup/webauthn', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ email })
});
if (!initialResponse.ok) {
throw new Error('Initial signup request failed');
}
// Get the options from the response
let options = await initialResponse.json();
// Convert base64 strings to ArrayBuffer where needed
if (options.challenge) {
options.challenge = base64URLToArrayBuffer(options.challenge);
}
if (options.user && options.user.id) {
options.user.id = base64URLToArrayBuffer(options.user.id);
}
// Call navigator.credentials.create with the options
const credential = await navigator.credentials.create({
publicKey: options
});
// Prepare the credential data for sending to server
const verifyData = {
id: credential.id,
rawId: arrayBufferToBase64URL(credential.rawId),
response: {
attestationObject: arrayBufferToBase64URL(credential.response.attestationObject),
clientDataJSON: arrayBufferToBase64URL(credential.response.clientDataJSON)
},
type: credential.type
};
// Second POST request to /signup/webauthn/verify
const verifyResponse = await fetch('http://localhost:4000/signup/webauthn/verify', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ credential: verifyData })
});
if (!verifyResponse.ok) {
throw new Error('Signup verification failed');
}
const result = await verifyResponse.json();
console.log('Signup successful:', result);
document.getElementById('emailForm').style.display = 'none';
} catch (error) {
console.error('Error during signup:', error);
alert('Registration failed. Please try again.');
}
}
// Helper function to convert base64URL to ArrayBuffer
function base64URLToArrayBuffer(base64URL) {
const padding = '='.repeat((4 - base64URL.length % 4) % 4);
const base64 = base64URL
.replace(/-/g, '+')
.replace(/_/g, '/')
+ padding;
const binaryString = window.atob(base64);
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes.buffer;
}
// Helper function to convert ArrayBuffer to base64URL
function arrayBufferToBase64URL(buffer) {
const bytes = new Uint8Array(buffer);
let binary = '';
for (let i = 0; i < bytes.byteLength; i++) {
binary += String.fromCharCode(bytes[i]);
}
const base64 = window.btoa(binary);
return base64
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=/g, '');
}
</script>
</body>
</html>