Skip to content

Instantly share code, notes, and snippets.

@arianvp
Created January 19, 2023 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arianvp/4519d45b17388e811ee82a80f50667f3 to your computer and use it in GitHub Desktop.
Save arianvp/4519d45b17388e811ee82a80f50667f3 to your computer and use it in GitHub Desktop.
Simplified Webauthn
interface SimpleCredential {
id: string;
publicKey: ArrayBuffer;
}
async function create(name: string) : Promise<SimpleCredential> {
// NOTE: We don't care about the challenge because we don't check attestation
const challenge = crypto.getRandomValues(new Uint8Array(16));
let credential = await navigator.credentials.create({
publicKey: {
rp: { name: "Passkey", },
user: {
id: crypto.getRandomValues(new Uint8Array(16)),
name,
displayName: name,
},
challenge,
pubKeyCredParams: [{ type: "public-key", alg: -7 }],
},
});
if (!(credential instanceof PublicKeyCredential)) {
throw new Error("Unexpected credential type");
}
const response = credential.response
if (!(response instanceof AuthenticatorAttestationResponse)) {
throw new Error("Unexpected response type");
}
const publicKey = response.getPublicKey()
if (!publicKey) {
throw new Error("No public key");
}
return {
id: credential.id,
publicKey,
}
}
async function get(id: string, challenge: ArrayBuffer) : Promise<AuthenticatorAssertionResponse> {
let credential = await navigator.credentials.get({
publicKey: {
challenge,
allowCredentials: [{ type: "public-key", id }],
},
});
if (!(credential instanceof PublicKeyCredential)) {
throw new Error("Unexpected credential type");
}
const response = credential.response
if (!(response instanceof AuthenticatorAssertionResponse)) {
throw new Error("Unexpected response type");
}
return response
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment