31 lines
934 B
JavaScript
31 lines
934 B
JavaScript
const start_webauthn = async () => {
|
|
const init_data = {
|
|
uuid: self.crypto.randomUUID(),
|
|
user_name: "ninjatrappeur",
|
|
display_name: "ninjatrappeur"
|
|
};
|
|
const challenge_resp = await fetch("/account/register-init", {
|
|
method: 'POST',
|
|
body: JSON.stringify(init_data),
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
}
|
|
});
|
|
return await challenge_resp.json();
|
|
}
|
|
|
|
const solve_challenge = async (publicKey) => {
|
|
const encoder = new TextEncoder();
|
|
publicKey.publicKey.challenge = encoder.encode(publicKey.publicKey.challenge);
|
|
publicKey.publicKey.user.id = encoder.encode(publicKey.publicKey.user.id);
|
|
return await navigator.credentials.create(publicKey);
|
|
}
|
|
|
|
// Main
|
|
(async () => {
|
|
const publicKey = await start_webauthn()
|
|
let solved = await solve_challenge(publicKey);
|
|
// TODO: send back to server
|
|
console.log(solved);
|
|
}) ();
|