-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.js
More file actions
257 lines (224 loc) · 7.33 KB
/
client.js
File metadata and controls
257 lines (224 loc) · 7.33 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
const messageBox = document.getElementById("message");
const registerButton = document.getElementById("register");
const loginButton = document.getElementById("login");
const logoutButton = document.getElementById("logout");
const output = document.getElementById("output");
registerButton.addEventListener("click", async () => {
try {
// Simulate getting registration options from the server
const registrationOptions = getRegistrationOptions();
// Convert base64 to ArrayBuffer
registrationOptions.challenge = Uint8Array.from(
atob(registrationOptions.challenge),
(c) => c.charCodeAt(0)
);
registrationOptions.user.id = Uint8Array.from(
atob(registrationOptions.user.id),
(c) => c.charCodeAt(0)
);
const credential = await navigator.credentials.create({
publicKey: registrationOptions,
});
// Convert ArrayBuffer to base64
const attestationResponse = {
id: credential.id,
rawId: btoa(
String.fromCharCode.apply(null, new Uint8Array(credential.rawId))
),
type: credential.type,
response: {
attestationObject: btoa(
String.fromCharCode.apply(
null,
new Uint8Array(credential.response.attestationObject)
)
),
clientDataJSON: btoa(
String.fromCharCode.apply(
null,
new Uint8Array(credential.response.clientDataJSON)
)
),
},
};
// Simulate sending the attestation response to the server
const result = register(attestationResponse);
output.textContent = JSON.stringify(result, null, 2);
registerButton.style.display = "none";
messageBox.innerHTML =
"Passkey is now successfully registered on the server";
} catch (error) {
output.textContent = `Error: ${error.message}`;
}
});
loginButton.addEventListener("click", async () => {
const output = document.getElementById("output");
try {
// Simulate getting authentication options from the server
const authOptions = getAuthenticationOptions();
// Convert base64 to ArrayBuffer
authOptions.challenge = Uint8Array.from(atob(authOptions.challenge), (c) =>
c.charCodeAt(0)
);
authOptions.allowCredentials = authOptions.allowCredentials.map((cred) => ({
...cred,
id: Uint8Array.from(
atob(cred.id.replace(/_/g, "/").replace(/-/g, "+")),
(c) => c.charCodeAt(0)
), // Replacing URL-safe characters
}));
const assertion = await navigator.credentials.get({
publicKey: authOptions,
});
// Convert ArrayBuffer to base64
const assertionResponse = {
id: assertion.id,
rawId: btoa(
String.fromCharCode.apply(null, new Uint8Array(assertion.rawId))
),
type: assertion.type,
response: {
authenticatorData: btoa(
String.fromCharCode.apply(
null,
new Uint8Array(assertion.response.authenticatorData)
)
),
clientDataJSON: btoa(
String.fromCharCode.apply(
null,
new Uint8Array(assertion.response.clientDataJSON)
)
),
signature: btoa(
String.fromCharCode.apply(
null,
new Uint8Array(assertion.response.signature)
)
),
userHandle: assertion.response.userHandle
? btoa(
String.fromCharCode.apply(
null,
new Uint8Array(assertion.response.userHandle)
)
)
: null,
},
};
// Simulate sending the assertion response to the server
const result = login(assertionResponse);
output.textContent = JSON.stringify(result, null, 2);
loginButton.style.display = "none";
logoutButton.style.display = "block";
messageBox.innerHTML = "Passkey login successful!";
} catch (error) {
output.textContent = `Error: ${error.message}`;
}
});
// Simulated server-side logic
let users = {}; // Simulated user database
let challenges = {}; // Simulated challenge storage
const getRegistrationOptions = () => {
// must be a cryptographically random number sent from a server
const challenge = btoa(
String.fromCharCode(...crypto.getRandomValues(new Uint8Array(32)))
);
const userId = btoa("user123");
challenges[userId] = challenge;
return {
challenge,
rp: {
name: "example",
// id: "example.com"
},
user: {
id: userId,
name: "user@example.com",
displayName: "User Example",
},
pubKeyCredParams: [
{ alg: -7, type: "public-key" },
{ alg: -257, type: "public-key" },
],
// excludeCredentials: [{
// id: *****,
// type: 'public-key',
// transports: ['internal'],
// }],
authenticatorSelection: {
userVerification: "preferred",
authenticatorAttachment: "platform",
requireResidentKey: true,
},
timeout: 60000,
attestation: "none",
};
};
const register = (attestationResponse) => {
const userId = btoa("user123");
const challenge = challenges[userId];
// Here, you'd normally verify the attestationResponse with the challenge and store the credential.
users[userId] = {
id: attestationResponse.id,
publicKey: attestationResponse.response.attestationObject,
};
delete challenges[userId];
return { status: "ok", userId };
};
const getAuthenticationOptions = () => {
const userId = btoa("user123");
if (!users[userId]) {
throw new Error("User not registered");
}
const challenge = btoa(
String.fromCharCode(...crypto.getRandomValues(new Uint8Array(32)))
);
challenges[userId] = challenge;
return {
challenge,
allowCredentials: [{ type: "public-key", id: users[userId].id }],
userVerification: "preferred",
timeout: 60000,
};
};
const login = (assertionResponse) => {
const userId = btoa("user123");
const challenge = challenges[userId];
// Here, you'd normally verify the assertionResponse with the stored public key and challenge.
const isValid = true; // Simulated validation result
delete challenges[userId];
return { status: isValid ? "ok" : "error", userId: isValid ? userId : null };
};
logoutButton.addEventListener("click", async () => {
loginButton.style.display = "block";
logoutButton.style.display = "none";
messageBox.innerHTML = "";
output.innerHTML = "";
});
// Extras
// Want to Sign in with a passkey through form autofill?
const autoFillCode = async () => {
// To abort a WebAuthn call, instantiate an `AbortController`.
const abortController = new AbortController();
// Availability of `window.PublicKeyCredential` means WebAuthn is usable.
if (window.PublicKeyCredential && window.isConditionalMediationAvailable) {
// Check if conditional mediation is available.
const isCMA = await PublicKeyCredential.isConditionalMediationAvailable();
if (isCMA) {
// Call WebAuthn authentication
const publicKeyCredentialRequestOptions = {
// Server generated challenge
challenge: "****",
// The same RP ID as used during registration
rpId: "example.com",
};
const credential = await navigator.credentials.get({
publicKey: publicKeyCredentialRequestOptions,
signal: abortController.signal,
// Specify 'conditional' to activate conditional UI
mediation: "conditional",
});
}
}
};