Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Update PasskeyAuthenticator.ts (#520)
Leendert de Borst5fe2c3ab4c9b
diff --git a/apps/browser-extension/src/utils/passkey/PasskeyAuthenticator.ts b/apps/browser-extension/src/utils/passkey/PasskeyAuthenticator.tsindex d715c1f79..b2b05cbe3 100644--- a/apps/browser-extension/src/utils/passkey/PasskeyAuthenticator.ts+++ b/apps/browser-extension/src/utils/passkey/PasskeyAuthenticator.ts@@ -43,7 +43,6 @@ export class PasskeyAuthenticator {/*** Create a new passkey (registration).- * Returns credential data ready for the browser extension to return to the RP, plus storage data.*/public static async createPasskey(credentialIdBytes: Uint8Array,@@ -54,25 +53,15 @@ export class PasskeyAuthenticator {enablePrf?: boolean;prfInputs?: { first: string; second?: string };}- ): Promise<{- credential: {- id: string;- rawId: string;- response: {- clientDataJSON: string;- attestationObject: string;- };- type: 'public-key';- };- stored: StoredPasskeyRecord;- prfEnabled?: boolean;- prfResults?: { first: ArrayBuffer; second?: ArrayBuffer };- }> {+ ): Promise<PasskeyCreationResult> {+ // 1. Validate algorithm supportPasskeyAuthenticator.pickSupportedAlgorithm(req.publicKey.pubKeyCredParams);+ // 2. Compute RP ID hashconst rpId = req.publicKey.rp?.id || new URL(req.origin).hostname;const rpIdHash = new Uint8Array(await crypto.subtle.digest('SHA-256', PasskeyAuthenticator.te(rpId) as BufferSource));+ // 3. Generate ES256 key pairconst keyPair = await crypto.subtle.generateKey({ name: 'ECDSA', namedCurve: 'P-256' },true,@@ -81,9 +70,11 @@ export class PasskeyAuthenticator {const pubJwk = await crypto.subtle.exportKey('jwk', keyPair.publicKey);const prvJwk = await crypto.subtle.exportKey('jwk', keyPair.privateKey);+ // 4. Build credential ID and COSE keyconst credentialIdB64u = PasskeyAuthenticator.toB64u(credentialIdBytes);const coseKey = PasskeyAuthenticator.buildCoseEc2Es256(pubJwk);+ // 5. Build authenticator flagslet flags = 0x41; // UP (bit 0) + AT (bit 6)const uvReq = req.publicKey.authenticatorSelection?.userVerification;const uvPerformed = !!opts?.uvPerformed;@@ -93,13 +84,17 @@ export class PasskeyAuthenticator {flags |= 0x08; // BE (bit 3)flags |= 0x10; // BS (bit 4)+ // 6. Sign count (always 0 for syncable credentials)const signCount = new Uint8Array([0, 0, 0, 0]);+ // 7. Build attested credential dataconst credIdLenBytes = new Uint8Array([(credentialIdBytes.length >> 8) & 0xff, credentialIdBytes.length & 0xff]);const attestedCredData = PasskeyAuthenticator.concat(PasskeyAuthenticator.AAGUID, credIdLenBytes, credentialIdBytes, coseKey);+ // 8. Build authenticator dataconst authenticatorData = PasskeyAuthenticator.concat(rpIdHash, new Uint8Array([flags]), signCount, attestedCredData);+ // 9. Build client data JSONconst challengeB64u = PasskeyAuthenticator.challengeToB64u(req.publicKey.challenge);const clientDataObj = {type: 'webauthn.create',@@ -110,12 +105,14 @@ export class PasskeyAuthenticator {const clientDataJSONStr = JSON.stringify(clientDataObj);const clientDataJSONBytes = PasskeyAuthenticator.te(clientDataJSONStr);+ // 10. Build attestation objectconst attPref = req.publicKey.attestation || 'none';const attObjBytes =attPref === 'none' || attPref === 'indirect'? PasskeyAuthenticator.buildAttObjNone(authenticatorData): await PasskeyAuthenticator.buildAttObjPackedSelf(authenticatorData, clientDataJSONBytes, keyPair.privateKey);+ // 11. Process user IDlet userIdB64: string | null = null;if (req.publicKey.user?.id) {userIdB64 = typeof req.publicKey.user.id === 'string'@@ -123,6 +120,7 @@ export class PasskeyAuthenticator {: PasskeyAuthenticator.toB64(req.publicKey.user.id instanceof Uint8Array ? req.publicKey.user.id : new Uint8Array(req.publicKey.user.id));}+ // 12. Generate PRF secret if requestedlet prfSecret: string | undefined;let prfEnabled = false;let prfResults: { first: ArrayBuffer; second?: ArrayBuffer } | undefined;@@ -132,6 +130,7 @@ export class PasskeyAuthenticator {prfSecret = PasskeyAuthenticator.toB64u(prfSecretBytes);prfEnabled = true;+ // 13. Evaluate PRF values if requested during registrationif (opts?.prfInputs) {const firstSalt = PasskeyAuthenticator.fromB64u(opts.prfInputs.first);prfResults = {@@ -145,6 +144,7 @@ export class PasskeyAuthenticator {}}+ // 14. Build stored recordconst stored: StoredPasskeyRecord = {rpId,credentialId: credentialIdB64u,@@ -156,6 +156,7 @@ export class PasskeyAuthenticator {prfSecret};+ // 15. Build credential responseconst credential = {id: credentialIdB64u,rawId: credentialIdB64u,@@ -177,20 +178,14 @@ export class PasskeyAuthenticator {req: GetRequest,storedRecord: StoredPasskeyRecord,opts?: { uvPerformed?: boolean; includeBEBS?: boolean; prfInputs?: { first: ArrayBuffer | Uint8Array; second?: ArrayBuffer | Uint8Array } }- ): Promise<{- id: string;- rawId: string;- clientDataJSON: string;- authenticatorData: string;- signature: string;- userHandle: string | null;- prfResults?: { first: ArrayBuffer; second?: ArrayBuffer };- }> {+ ): Promise<PasskeyAssertionResult> {const rec = storedRecord;+ // 1. Compute RP ID hashconst rpId = req.publicKey.rpId || new URL(req.origin).hostname;const rpIdHash = new Uint8Array(await crypto.subtle.digest('SHA-256', PasskeyAuthenticator.te(rpId) as BufferSource));+ // 2. Build authenticator flagslet flags = 0x01; // UP (bit 0)const uvReq = req.publicKey.userVerification;const uvPerformed = !!opts?.uvPerformed;@@ -201,10 +196,14 @@ export class PasskeyAuthenticator {flags |= 0x08; // BE (bit 3)flags |= 0x10; // BS (bit 4)}++ // 3. Sign count (always 0 for syncable credentials)const signCount = new Uint8Array([0, 0, 0, 0]);+ // 4. Build authenticator dataconst authenticatorData = PasskeyAuthenticator.concat(rpIdHash, new Uint8Array([flags]), signCount);+ // 5. Build client data JSONconst challengeB64u = PasskeyAuthenticator.challengeToB64u(req.publicKey.challenge);const clientDataObj = {type: 'webauthn.get',@@ -215,9 +214,11 @@ export class PasskeyAuthenticator {const clientDataJSONStr = JSON.stringify(clientDataObj);const clientDataJSONBytes = PasskeyAuthenticator.te(clientDataJSONStr);+ // 6. Build data to sign: authenticatorData || clientDataHashconst clientDataHash = new Uint8Array(await crypto.subtle.digest('SHA-256', clientDataJSONBytes as BufferSource));const toSign = PasskeyAuthenticator.concat(authenticatorData, clientDataHash);+ // 7. Import private key and signconst privateKey = await crypto.subtle.importKey('jwk',rec.privateKey,@@ -229,13 +230,16 @@ export class PasskeyAuthenticator {await crypto.subtle.sign({ name: 'ECDSA', hash: 'SHA-256' }, privateKey, toSign as BufferSource));+ // 8. Convert raw signature to DER formatconst derSig = PasskeyAuthenticator.ecdsaRawToDer(rawSig);+ // 9. Process user handlelet userHandleB64u: string | null = null;if (rec.userId) {userHandleB64u = rec.userId.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');}+ // 10. Evaluate PRF if requestedlet prfResults: { first: ArrayBuffer; second?: ArrayBuffer } | undefined;if (opts?.prfInputs && rec.prfSecret) {const prfSecretBytes = PasskeyAuthenticator.fromB64u(rec.prfSecret);@@ -531,3 +535,45 @@ export class PasskeyAuthenticator {return out;}}++/**+ * Result of passkey creation containing credential and storage data.+ */+export type PasskeyCreationResult = {+ /** The credential object returned to the RP. */+ credential: {+ id: string;+ rawId: string;+ response: {+ clientDataJSON: string;+ attestationObject: string;+ };+ type: 'public-key';+ };+ /** The stored passkey record for vault storage. */+ stored: StoredPasskeyRecord;+ /** Whether PRF extension is enabled. */+ prfEnabled?: boolean;+ /** PRF evaluation results if requested. */+ prfResults?: { first: ArrayBuffer; second?: ArrayBuffer };+};++/**+ * Result of passkey assertion containing authentication data.+ */+export type PasskeyAssertionResult = {+ /** The credential identifier. */+ id: string;+ /** The raw credential identifier (base64url). */+ rawId: string;+ /** Client data JSON (base64url). */+ clientDataJSON: string;+ /** Authenticator data (base64url). */+ authenticatorData: string;+ /** Signature in DER format (base64url). */+ signature: string;+ /** User handle (base64url). */+ userHandle: string | null;+ /** PRF evaluation results if requested. */+ prfResults?: { first: ArrayBuffer; second?: ArrayBuffer };+};