Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Refactor AliasVaultPasskeyProvider.ts (#520)

Leendert de Borst5569202b9a1d
diff --git a/apps/browser-extension/src/entrypoints/popup/pages/passkeys/PasskeyAuthenticate.tsx b/apps/browser-extension/src/entrypoints/popup/pages/passkeys/PasskeyAuthenticate.tsx
index 02ff5ea54..747bfc602 100644
--- a/apps/browser-extension/src/entrypoints/popup/pages/passkeys/PasskeyAuthenticate.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/pages/passkeys/PasskeyAuthenticate.tsx
@@ -58,17 +58,11 @@ const PasskeyAuthenticate: React.FC = () => {
setLoading(true);
try {
- // Create the provider with storage callbacks
- const provider = new AliasVaultPasskeyProvider(
- async (record: StoredPasskeyRecord) => {
- // Not used during authentication
- await sendMessage('STORE_PASSKEY', record, 'background');
- },
- async (credentialId: string) : Promise<StoredPasskeyRecord | null> => {
- const result = await sendMessage('GET_PASSKEY_BY_ID', { credentialId }, 'background') as StoredPasskeyRecord | null;
- return result || null;
- }
- );
+ // Get the stored passkey record
+ const storedRecord = await sendMessage('GET_PASSKEY_BY_ID', { credentialId }, 'background') as StoredPasskeyRecord | null;
+ if (!storedRecord) {
+ throw new Error('Passkey not found');
+ }
// Build the GetRequest
const getRequest: GetRequest = {
@@ -82,8 +76,8 @@ const PasskeyAuthenticate: React.FC = () => {
}
};
- // Get the assertion using the provider
- const credential: PasskeyGetCredentialResponse = await provider.getAssertion(getRequest, credentialId, {
+ // Get the assertion using the static method
+ const credential: PasskeyGetCredentialResponse = await AliasVaultPasskeyProvider.getAssertion(getRequest, storedRecord, {
uvPerformed: true, // Set to true if you implement actual user verification
includeBEBS: true // Include backup-eligible and backup-state flags
});
diff --git a/apps/browser-extension/src/entrypoints/popup/pages/passkeys/PasskeyCreate.tsx b/apps/browser-extension/src/entrypoints/popup/pages/passkeys/PasskeyCreate.tsx
index e3ccc51fa..1ebd2dab2 100644
--- a/apps/browser-extension/src/entrypoints/popup/pages/passkeys/PasskeyCreate.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/pages/passkeys/PasskeyCreate.tsx
@@ -8,7 +8,7 @@ import LoadingSpinner from '@/entrypoints/popup/components/LoadingSpinner';
import { useLoading } from '@/entrypoints/popup/context/LoadingContext';
import { AliasVaultPasskeyProvider } from '@/utils/passkey/AliasVaultPasskeyProvider';
-import type { CreateRequest, StoredPasskeyRecord, PasskeyCreateCredentialResponse, PendingPasskeyCreateRequest } from '@/utils/passkey/types';
+import type { CreateRequest, PasskeyCreateCredentialResponse, PendingPasskeyCreateRequest, StorePasskeyRequest, WebAuthnCreationPayload } from '@/utils/passkey/types';
/**
* PasskeyCreate
@@ -62,29 +62,6 @@ const PasskeyCreate: React.FC = () => {
setLoading(true);
try {
- /*
- * Create the provider with storage callbacks
- * TODO: refactor this flow to be simpler.
- */
- const provider = new AliasVaultPasskeyProvider(
- async (record: StoredPasskeyRecord) => {
- await sendMessage('STORE_PASSKEY', {
- rpId: record.rpId,
- credentialId: record.credentialId,
- displayName,
- publicKey: record.publicKey,
- privateKey: record.privateKey,
- userId: record.userId,
- userName: record.userName,
- userDisplayName: record.userDisplayName
- } as any, 'background');
- },
- async (credentialId: string) : Promise<StoredPasskeyRecord | null> => {
- const result = await sendMessage('GET_PASSKEY_BY_ID', { credentialId }, 'background') as StoredPasskeyRecord | null;
- return result || null;
- }
- );
-
// Build the CreateRequest
const createRequest: CreateRequest = {
origin: request.origin,
@@ -99,11 +76,28 @@ const PasskeyCreate: React.FC = () => {
}
};
- // Create the passkey using the provider
- const { credential } = await provider.createPasskey(createRequest, {
- uvPerformed: false // Set to true if you implement actual user verification
+ // Create passkey using static method
+ const result = await AliasVaultPasskeyProvider.createPasskey(createRequest, {
+ uvPerformed: true, // Set to true if you implement actual user verification
+ credentialIdBytes: 16
});
+ // Store the passkey data
+ const data: StorePasskeyRequest = {
+ rpId: result.stored.rpId,
+ credentialId: result.stored.credentialId,
+ displayName: displayName,
+ publicKey: result.stored.publicKey as WebAuthnCreationPayload,
+ privateKey: result.stored.privateKey,
+ userId: result.stored.userId,
+ userName: result.stored.userName,
+ userDisplayName: result.stored.userDisplayName
+ };
+
+ await sendMessage('STORE_PASSKEY', data as unknown, 'background');
+
+ const { credential } = result;
+
console.info('PasskeyCreate: Created credential successfully', credential);
/*
diff --git a/apps/browser-extension/src/utils/passkey/AliasVaultPasskeyProvider.ts b/apps/browser-extension/src/utils/passkey/AliasVaultPasskeyProvider.ts
index 32c5df42e..beb2ce665 100644
--- a/apps/browser-extension/src/utils/passkey/AliasVaultPasskeyProvider.ts
+++ b/apps/browser-extension/src/utils/passkey/AliasVaultPasskeyProvider.ts
@@ -9,8 +9,6 @@
* - Consistent base64url/base64 handling
*
* NOTE:
- * - This class does not handle UI concerns or message passing. Inject storage callbacks so you
- * can persist and retrieve passkeys (keys + metadata) in your extension background.
* - By design, signCount is always 0 (clone detection disabled) for syncable passkeys.
* - Attestation defaults to "none" (privacy-preserving). If an RP requests "direct", we do a
* "packed" *self* attestation using the generated credential private key (no x5c chain).
@@ -20,18 +18,14 @@
import type { CreateRequest, GetRequest, StoredPasskeyRecord } from './types';
/**
- * AliasVaultPasskeyProvider
+ * AliasVaultPasskeyProvider - Static utility class for WebAuthn operations
*/
export class AliasVaultPasskeyProvider {
/**
- * Inject your storage layer via callbacks.
- * - store: persist a newly created passkey
- * - getById: fetch a stored passkey (by credentialId)
+ * Private constructor to prevent instantiation.
+ * This class only contains static methods.
*/
- public constructor(
- private readonly store: (record: StoredPasskeyRecord) => Promise<void>,
- private readonly getById: (credentialId: string) => Promise<StoredPasskeyRecord | null>
- ) {}
+ private constructor() {}
/*
* ------------------------------------------------------------------------------------
@@ -43,9 +37,9 @@ export class AliasVaultPasskeyProvider {
* Create a new passkey (registration). Returns a credential-like object similar to
* navigator.credentials.create() output, ready to be posted to the RP.
*
- * It also calls `this.store(...)` to persist the passkey (private/public keys, ids, etc).
+ * Also returns the passkey data that should be stored for later use.
*/
- public async createPasskey(
+ public static async createPasskey(
req: CreateRequest,
opts?: { uvPerformed?: boolean; credentialIdBytes?: number } // uvPerformed: only set to true if your app did real UV
): Promise<{
@@ -61,11 +55,11 @@ export class AliasVaultPasskeyProvider {
stored: StoredPasskeyRecord;
}> {
// 1) Validate and resolve algorithm (-7 = ES256)
- this.pickSupportedAlgorithm(req.publicKey.pubKeyCredParams);
+ AliasVaultPasskeyProvider.pickSupportedAlgorithm(req.publicKey.pubKeyCredParams);
// 2) Determine RP ID (domain) and hash it
const rpId = req.publicKey.rp?.id || new URL(req.origin).hostname;
- const rpIdHash = new Uint8Array(await crypto.subtle.digest('SHA-256', this.te(rpId)));
+ const rpIdHash = new Uint8Array(await crypto.subtle.digest('SHA-256', AliasVaultPasskeyProvider.te(rpId) as BufferSource));
// 3) Key pair generation (ES256 / P-256)
const keyPair = await crypto.subtle.generateKey(
@@ -79,10 +73,10 @@ export class AliasVaultPasskeyProvider {
// 4) Generate a credentialId (random bytes; default 16)
const credLen = Math.max(8, Math.min(opts?.credentialIdBytes ?? 16, 64));
const credIdBytes = crypto.getRandomValues(new Uint8Array(credLen));
- const credentialIdB64u = this.toB64u(credIdBytes);
+ const credentialIdB64u = AliasVaultPasskeyProvider.toB64u(credIdBytes);
// 5) COSE public key (CBOR) from JWK (ES256 / P-256)
- const coseKey = this.buildCoseEc2Es256(pubJwk);
+ const coseKey = AliasVaultPasskeyProvider.buildCoseEc2Es256(pubJwk);
/*
* 6) Flags (creation): UP (bit0)=1, UV (bit2) depends on policy, AT (bit6)=1, BE/BS optional
@@ -103,13 +97,13 @@ export class AliasVaultPasskeyProvider {
// 7) AttestedCredentialData = AAGUID(16 zeros) + credIdLen(2) + credId + COSEKey
const aaguid = new Uint8Array(16); // all zeros in "none" attestation
const credIdLenBytes = new Uint8Array([(credIdBytes.length >> 8) & 0xff, credIdBytes.length & 0xff]);
- const attestedCredData = this.concat(aaguid, credIdLenBytes, credIdBytes, coseKey);
+ const attestedCredData = AliasVaultPasskeyProvider.concat(aaguid, credIdLenBytes, credIdBytes, coseKey);
// 8) authenticatorData = rpIdHash (32) + flags (1) + signCount (4) + attestedCredData
- const authenticatorData = this.concat(rpIdHash, new Uint8Array([flags]), signCount, attestedCredData);
+ const authenticatorData = AliasVaultPasskeyProvider.concat(rpIdHash, new Uint8Array([flags]), signCount, attestedCredData);
// 9) clientDataJSON (stringify with challenge as base64url)
- const challengeB64u = this.challengeToB64u(req.publicKey.challenge);
+ const challengeB64u = AliasVaultPasskeyProvider.challengeToB64u(req.publicKey.challenge);
const clientDataObj = {
type: 'webauthn.create',
challenge: challengeB64u,
@@ -117,17 +111,17 @@ export class AliasVaultPasskeyProvider {
crossOrigin: false
};
const clientDataJSONStr = JSON.stringify(clientDataObj);
- const clientDataJSONBytes = this.te(clientDataJSONStr);
+ const clientDataJSONBytes = AliasVaultPasskeyProvider.te(clientDataJSONStr);
// 10) Build attestationObject (CBOR map with "fmt","attStmt","authData")
const attPref = req.publicKey.attestation || 'none';
const attObjBytes =
attPref === 'none' || attPref === 'indirect'
- ? this.buildAttObjNone(authenticatorData)
- : await this.buildAttObjPackedSelf(authenticatorData, clientDataJSONBytes, keyPair.privateKey);
+ ? AliasVaultPasskeyProvider.buildAttObjNone(authenticatorData)
+ : await AliasVaultPasskeyProvider.buildAttObjPackedSelf(authenticatorData, clientDataJSONBytes, keyPair.privateKey);
/*
- * 11) Store the passkey in your vault (so it can be used later for authentication)
+ * 11) Prepare the passkey data for storage (caller will handle actual storage)
* Store userId as-is (injection script sends it as standard base64 string)
*/
let userIdB64: string | null = null;
@@ -135,7 +129,7 @@ export class AliasVaultPasskeyProvider {
// The injection script already converted ArrayBuffer to standard base64, store as-is
userIdB64 = typeof req.publicKey.user.id === 'string'
? req.publicKey.user.id
- : this.toB64(req.publicKey.user.id instanceof Uint8Array ? req.publicKey.user.id : new Uint8Array(req.publicKey.user.id));
+ : AliasVaultPasskeyProvider.toB64(req.publicKey.user.id instanceof Uint8Array ? req.publicKey.user.id : new Uint8Array(req.publicKey.user.id));
}
const stored: StoredPasskeyRecord = {
@@ -148,15 +142,14 @@ export class AliasVaultPasskeyProvider {
userDisplayName: req.publicKey.user?.displayName,
lastUsedAt: Date.now()
};
- await this.store(stored);
// 12) Return a credential-like object (base64-encoded fields for transport)
const credential = {
id: credentialIdB64u,
rawId: credentialIdB64u,
response: {
- clientDataJSON: this.toB64(clientDataJSONBytes),
- attestationObject: this.toB64(attObjBytes)
+ clientDataJSON: AliasVaultPasskeyProvider.toB64(clientDataJSONBytes),
+ attestationObject: AliasVaultPasskeyProvider.toB64(attObjBytes)
},
type: 'public-key' as const
};
@@ -176,9 +169,9 @@ export class AliasVaultPasskeyProvider {
* - The "standard" WebAuthn shape would nest these in `response` and use ArrayBuffers.
* You can adapt this output to that shape if the page expects the standard structure.
*/
- public async getAssertion(
+ public static async getAssertion(
req: GetRequest,
- credentialIdB64u: string,
+ storedRecord: StoredPasskeyRecord,
opts?: { uvPerformed?: boolean; includeBEBS?: boolean }
): Promise<{
id: string;
@@ -188,15 +181,12 @@ export class AliasVaultPasskeyProvider {
signature: string; // base64 (DER)
userHandle: string | null; // base64 (if you choose to return it)
}> {
- // 1) Load passkey
- const rec = await this.getById(credentialIdB64u);
- if (!rec) {
- throw new Error('Passkey not found');
- }
+ // Use the provided stored record
+ const rec = storedRecord;
// 2) rpId & hash
const rpId = req.publicKey.rpId || new URL(req.origin).hostname;
- const rpIdHash = new Uint8Array(await crypto.subtle.digest('SHA-256', this.te(rpId)));
+ const rpIdHash = new Uint8Array(await crypto.subtle.digest('SHA-256', AliasVaultPasskeyProvider.te(rpId) as BufferSource));
// 3) Flags (assertion): UP=1, UV depends on request & policy, BE/BS for syncable, AT=0 for auth
let flags = 0x01; // UP
@@ -212,10 +202,10 @@ export class AliasVaultPasskeyProvider {
const signCount = new Uint8Array([0, 0, 0, 0]); // always 0 (sync-friendly)
// 4) authenticatorData = rpIdHash + flags + signCount
- const authenticatorData = this.concat(rpIdHash, new Uint8Array([flags]), signCount);
+ const authenticatorData = AliasVaultPasskeyProvider.concat(rpIdHash, new Uint8Array([flags]), signCount);
// 5) clientDataJSON
- const challengeB64u = this.challengeToB64u(req.publicKey.challenge);
+ const challengeB64u = AliasVaultPasskeyProvider.challengeToB64u(req.publicKey.challenge);
const clientDataObj = {
type: 'webauthn.get',
challenge: challengeB64u,
@@ -223,11 +213,11 @@ export class AliasVaultPasskeyProvider {
crossOrigin: false
};
const clientDataJSONStr = JSON.stringify(clientDataObj);
- const clientDataJSONBytes = this.te(clientDataJSONStr);
+ const clientDataJSONBytes = AliasVaultPasskeyProvider.te(clientDataJSONStr);
// 6) Signature over authenticatorData || SHA256(clientDataJSON)
- const clientDataHash = new Uint8Array(await crypto.subtle.digest('SHA-256', clientDataJSONBytes));
- const toSign = this.concat(authenticatorData, clientDataHash);
+ const clientDataHash = new Uint8Array(await crypto.subtle.digest('SHA-256', clientDataJSONBytes as BufferSource));
+ const toSign = AliasVaultPasskeyProvider.concat(authenticatorData, clientDataHash);
const privateKey = await crypto.subtle.importKey(
'jwk',
@@ -237,11 +227,11 @@ export class AliasVaultPasskeyProvider {
['sign']
);
const rawSig = new Uint8Array(
- await crypto.subtle.sign({ name: 'ECDSA', hash: 'SHA-256' }, privateKey, toSign)
+ await crypto.subtle.sign({ name: 'ECDSA', hash: 'SHA-256' }, privateKey, toSign as BufferSource)
);
// 7) Convert raw (r|s) to DER sequence
- const derSig = this.ecdsaRawToDer(rawSig);
+ const derSig = AliasVaultPasskeyProvider.ecdsaRawToDer(rawSig);
/*
* 8) Return userHandle (userId) as-is
@@ -258,11 +248,11 @@ export class AliasVaultPasskeyProvider {
// 9) Return object in the flat shape (base64 strings), as your client example expects
return {
- id: credentialIdB64u,
- rawId: credentialIdB64u,
- clientDataJSON: this.toB64(clientDataJSONBytes),
- authenticatorData: this.toB64(authenticatorData),
- signature: this.toB64(derSig),
+ id: rec.credentialId,
+ rawId: rec.credentialId,
+ clientDataJSON: AliasVaultPasskeyProvider.toB64(clientDataJSONBytes),
+ authenticatorData: AliasVaultPasskeyProvider.toB64(authenticatorData),
+ signature: AliasVaultPasskeyProvider.toB64(derSig),
userHandle: userHandleB64
};
}
@@ -274,7 +264,7 @@ export class AliasVaultPasskeyProvider {
*/
/** Ensure ES256 (-7) is available; else throw (or extend to support others). */
- private pickSupportedAlgorithm(params?: Array<{ type: 'public-key'; alg: number }>): number {
+ private static pickSupportedAlgorithm(params?: Array<{ type: 'public-key'; alg: number }>): number {
if (!params || params.length === 0) {
return -7;
} // assume ES256 default
@@ -286,9 +276,9 @@ export class AliasVaultPasskeyProvider {
}
/** Build COSE EC2 public key for ES256: {1:2, 3:-7, -1:1, -2:x, -3:y} in CBOR. */
- private buildCoseEc2Es256(jwk: JsonWebKey): Uint8Array {
- const x = this.pad32(this.fromB64u(jwk.x!));
- const y = this.pad32(this.fromB64u(jwk.y!));
+ private static buildCoseEc2Es256(jwk: JsonWebKey): Uint8Array {
+ const x = AliasVaultPasskeyProvider.pad32(AliasVaultPasskeyProvider.fromB64u(jwk.x!));
+ const y = AliasVaultPasskeyProvider.pad32(AliasVaultPasskeyProvider.fromB64u(jwk.y!));
/*
* Map(5): 0xa5
* 1:2 (kty: EC2)
@@ -308,16 +298,16 @@ export class AliasVaultPasskeyProvider {
}
/** Build "none" attestation object: { fmt: "none", attStmt: {}, authData: <bytes> } (CBOR). */
- private buildAttObjNone(authenticatorData: Uint8Array): Uint8Array {
- const fmtKey = this.cborText('fmt'); // "fmt"
- const fmtVal = this.cborText('none'); // "none"
- const attStmtKey = this.cborText('attStmt'); // "attStmt"
+ private static buildAttObjNone(authenticatorData: Uint8Array): Uint8Array {
+ const fmtKey = AliasVaultPasskeyProvider.cborText('fmt'); // "fmt"
+ const fmtVal = AliasVaultPasskeyProvider.cborText('none'); // "none"
+ const attStmtKey = AliasVaultPasskeyProvider.cborText('attStmt'); // "attStmt"
const attStmtVal = new Uint8Array([0xa0]); // map(0) {}
- const authDataKey = this.cborText('authData'); // "authData"
- const authDataVal = this.cborBstr(authenticatorData);
+ const authDataKey = AliasVaultPasskeyProvider.cborText('authData'); // "authData"
+ const authDataVal = AliasVaultPasskeyProvider.cborBstr(authenticatorData);
// map(3)
- return this.concat(
+ return AliasVaultPasskeyProvider.concat(
new Uint8Array([0xa3]),
fmtKey, fmtVal,
attStmtKey, attStmtVal,
@@ -326,35 +316,35 @@ export class AliasVaultPasskeyProvider {
}
/** Build "packed" self-attestation object (no x5c). */
- private async buildAttObjPackedSelf(
+ private static async buildAttObjPackedSelf(
authenticatorData: Uint8Array,
clientDataJSON: Uint8Array,
privateKey: CryptoKey
): Promise<Uint8Array> {
// Signature over authenticatorData || SHA256(clientDataJSON)
- const clientDataHash = new Uint8Array(await crypto.subtle.digest('SHA-256', clientDataJSON));
- const toSign = this.concat(authenticatorData, clientDataHash);
+ const clientDataHash = new Uint8Array(await crypto.subtle.digest('SHA-256', clientDataJSON as BufferSource));
+ const toSign = AliasVaultPasskeyProvider.concat(authenticatorData, clientDataHash);
const rawSig = new Uint8Array(
- await crypto.subtle.sign({ name: 'ECDSA', hash: 'SHA-256' }, privateKey, toSign)
+ await crypto.subtle.sign({ name: 'ECDSA', hash: 'SHA-256' }, privateKey, toSign as BufferSource)
);
- const derSig = this.ecdsaRawToDer(rawSig);
+ const derSig = AliasVaultPasskeyProvider.ecdsaRawToDer(rawSig);
// attStmt = { alg: -7, sig: <derSig> }
- const attStmtMap = this.concat(
- this.cborText('alg'), new Uint8Array([0x26]), // -7
- this.cborText('sig'), this.cborBstr(derSig)
+ const attStmtMap = AliasVaultPasskeyProvider.concat(
+ AliasVaultPasskeyProvider.cborText('alg'), new Uint8Array([0x26]), // -7
+ AliasVaultPasskeyProvider.cborText('sig'), AliasVaultPasskeyProvider.cborBstr(derSig)
);
// prepend map(2)
- const attStmt = this.concat(new Uint8Array([0xa2]), attStmtMap);
+ const attStmt = AliasVaultPasskeyProvider.concat(new Uint8Array([0xa2]), attStmtMap);
// final: { fmt:"packed", attStmt:{...}, authData:<bytes> }
- const fmtKey = this.cborText('fmt');
- const fmtVal = this.cborText('packed');
- const attStmtKey = this.cborText('attStmt');
- const authDataKey = this.cborText('authData');
- const authDataVal = this.cborBstr(authenticatorData);
+ const fmtKey = AliasVaultPasskeyProvider.cborText('fmt');
+ const fmtVal = AliasVaultPasskeyProvider.cborText('packed');
+ const attStmtKey = AliasVaultPasskeyProvider.cborText('attStmt');
+ const authDataKey = AliasVaultPasskeyProvider.cborText('authData');
+ const authDataVal = AliasVaultPasskeyProvider.cborBstr(authenticatorData);
- return this.concat(
+ return AliasVaultPasskeyProvider.concat(
new Uint8Array([0xa3]), // map(3)
fmtKey, fmtVal,
attStmtKey, attStmt,
@@ -365,8 +355,8 @@ export class AliasVaultPasskeyProvider {
// ----- CBOR small helpers -----
/** Encode a UTF-8 string as CBOR text. */
- private cborText(s: string): Uint8Array {
- const bytes = this.te(s);
+ private static cborText(s: string): Uint8Array {
+ const bytes = AliasVaultPasskeyProvider.te(s);
if (bytes.length <= 23) {
return new Uint8Array([0x60 | bytes.length, ...bytes]);
} // major type 3
@@ -377,7 +367,7 @@ export class AliasVaultPasskeyProvider {
}
/** Encode a byte string as CBOR bstr. */
- private cborBstr(b: Uint8Array): Uint8Array {
+ private static cborBstr(b: Uint8Array): Uint8Array {
if (b.length <= 23) {
return new Uint8Array([0x40 | b.length, ...b]);
} // major type 2
@@ -390,19 +380,19 @@ export class AliasVaultPasskeyProvider {
// ----- DER helpers -----
/** Convert raw ECDSA signature (r|s, 64 bytes) to DER SEQUENCE. */
- private ecdsaRawToDer(raw: Uint8Array): Uint8Array {
+ private static ecdsaRawToDer(raw: Uint8Array): Uint8Array {
if (raw.length !== 64) {
throw new Error('Unexpected ECDSA signature length');
}
const r = raw.slice(0, 32);
const s = raw.slice(32, 64);
- const rDer = this.derInt(r);
- const sDer = this.derInt(s);
+ const rDer = AliasVaultPasskeyProvider.derInt(r);
+ const sDer = AliasVaultPasskeyProvider.derInt(s);
return new Uint8Array([0x30, rDer.length + sDer.length, ...rDer, ...sDer]);
}
/** Encode a positive big integer to DER INTEGER. */
- private derInt(src: Uint8Array): Uint8Array {
+ private static derInt(src: Uint8Array): Uint8Array {
// Trim leading zeros
let i = 0;
while (i < src.length - 1 && src[i] === 0x00) {
@@ -411,7 +401,10 @@ export class AliasVaultPasskeyProvider {
let v = src.slice(i);
// If MSB set, prepend 0 to keep it positive
if ((v[0] & 0x80) !== 0) {
- v = this.concat(new Uint8Array([0x00]), v);
+ const padded = new Uint8Array(v.length + 1);
+ padded[0] = 0x00;
+ padded.set(v, 1);
+ v = padded;
}
return new Uint8Array([0x02, v.length, ...v]);
}
@@ -419,12 +412,13 @@ export class AliasVaultPasskeyProvider {
// ----- Base64 / Base64url helpers -----
/** UTF-8 encode string to bytes. */
- private te(s: string): Uint8Array {
- return new TextEncoder().encode(s);
+ private static te(s: string): Uint8Array {
+ const encoder = new TextEncoder();
+ return encoder.encode(s);
}
/** Base64 encode bytes (Uint8Array) to ASCII string. */
- private toB64(bytes: Uint8Array): string {
+ private static toB64(bytes: Uint8Array): string {
let bin = '';
for (let i = 0; i < bytes.length; i++) {
bin += String.fromCharCode(bytes[i]);
@@ -433,12 +427,12 @@ export class AliasVaultPasskeyProvider {
}
/** Base64url encode bytes. */
- private toB64u(bytes: Uint8Array): string {
- return this.toB64(bytes).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/g, '');
+ private static toB64u(bytes: Uint8Array): string {
+ return AliasVaultPasskeyProvider.toB64(bytes).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/g, '');
}
/** Base64url decode to bytes. */
- private fromB64u(b64u: string): Uint8Array {
+ private static fromB64u(b64u: string): Uint8Array {
const b64 = b64u.replace(/-/g, '+').replace(/_/g, '/');
const pad = b64.length % 4 === 2 ? '==' : b64.length % 4 === 3 ? '=' : '';
const s = atob(b64 + pad);
@@ -453,7 +447,7 @@ export class AliasVaultPasskeyProvider {
* Normalize challenge to base64url string.
* The injection script sends challenges as standard base64, so we need to convert to base64url.
*/
- private challengeToB64u(challenge: ArrayBuffer | Uint8Array | string): string {
+ private static challengeToB64u(challenge: ArrayBuffer | Uint8Array | string): string {
if (typeof challenge === 'string') {
/*
* String from injection script - it's standard base64, convert to base64url
@@ -462,7 +456,7 @@ export class AliasVaultPasskeyProvider {
return challenge.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
}
const bytes = challenge instanceof Uint8Array ? challenge : new Uint8Array(challenge);
- return this.toB64u(bytes);
+ return AliasVaultPasskeyProvider.toB64u(bytes);
}
/**
@@ -472,7 +466,7 @@ export class AliasVaultPasskeyProvider {
* - Plain UTF-8 string from test/simple cases (needs encoding)
* - Already base64url encoded string (use as-is)
*/
- private userIdToB64u(userId: ArrayBuffer | Uint8Array | string): string {
+ private static userIdToB64u(userId: ArrayBuffer | Uint8Array | string): string {
if (typeof userId === 'string') {
// Check if it's already base64url (contains only valid base64url chars)
if (/^[A-Za-z0-9_-]+$/.test(userId) && userId.length % 4 !== 1) {
@@ -480,15 +474,15 @@ export class AliasVaultPasskeyProvider {
return userId;
} else {
// Plain UTF-8 string, encode it
- return this.toB64u(this.te(userId));
+ return AliasVaultPasskeyProvider.toB64u(AliasVaultPasskeyProvider.te(userId));
}
}
const bytes = userId instanceof Uint8Array ? userId : new Uint8Array(userId);
- return this.toB64u(bytes);
+ return AliasVaultPasskeyProvider.toB64u(bytes);
}
/** Left-pad to 32 bytes (P-256 coord). */
- private pad32(b: Uint8Array): Uint8Array {
+ private static pad32(b: Uint8Array): Uint8Array {
if (b.length === 32) {
return b;
}
@@ -498,7 +492,7 @@ export class AliasVaultPasskeyProvider {
}
/** Concatenate typed arrays. */
- private concat(...chunks: Uint8Array[]): Uint8Array {
+ private static concat(...chunks: Uint8Array[]): Uint8Array {
const total = chunks.reduce((s, c) => s + c.length, 0);
const out = new Uint8Array(total);
let o = 0;
diff --git a/apps/browser-extension/src/utils/passkey/__tests__/AliasVaultPasskeyProvider.test.ts b/apps/browser-extension/src/utils/passkey/__tests__/AliasVaultPasskeyProvider.test.ts
index 5afb8bc86..3aa5792b4 100644
--- a/apps/browser-extension/src/utils/passkey/__tests__/AliasVaultPasskeyProvider.test.ts
+++ b/apps/browser-extension/src/utils/passkey/__tests__/AliasVaultPasskeyProvider.test.ts
@@ -1,4 +1,4 @@
-import { describe, it, expect, beforeEach, vi } from 'vitest';
+import { describe, it, expect, beforeEach } from 'vitest';
import { AliasVaultPasskeyProvider } from '../AliasVaultPasskeyProvider';
@@ -6,20 +6,9 @@ import type { CreateRequest, GetRequest, StoredPasskeyRecord } from '../types';
describe('AliasVaultPasskeyProvider', () => {
let storedPasskeys: Map<string, StoredPasskeyRecord>;
- let provider: AliasVaultPasskeyProvider;
beforeEach(() => {
storedPasskeys = new Map();
-
- // Create provider with mock storage callbacks
- provider = new AliasVaultPasskeyProvider(
- async (record: StoredPasskeyRecord) => {
- storedPasskeys.set(record.credentialId, record);
- },
- async (credentialId: string) => {
- return storedPasskeys.get(credentialId) || null;
- }
- );
});
describe('createPasskey', () => {
@@ -46,7 +35,10 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const result = await provider.createPasskey(createRequest);
+ const result = await AliasVaultPasskeyProvider.createPasskey(createRequest);
+
+ // Store the passkey manually for testing
+ storedPasskeys.set(result.stored.credentialId, result.stored);
// Verify credential structure
expect(result.credential).toBeDefined();
@@ -88,7 +80,7 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const result = await provider.createPasskey(createRequest);
+ const result = await AliasVaultPasskeyProvider.createPasskey(createRequest);
// Decode and verify clientDataJSON
const clientDataJSON = atob(result.credential.response.clientDataJSON);
@@ -125,7 +117,7 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const result = await provider.createPasskey(createRequest);
+ const result = await AliasVaultPasskeyProvider.createPasskey(createRequest);
// Decode clientDataJSON and verify challenge matches
const clientDataJSON = atob(result.credential.response.clientDataJSON);
@@ -161,7 +153,7 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const result = await provider.createPasskey(createRequest);
+ const result = await AliasVaultPasskeyProvider.createPasskey(createRequest);
// Decode attestation object (base64)
const attObjBytes = Uint8Array.from(
@@ -188,7 +180,7 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const result = await provider.createPasskey(createRequest);
+ const result = await AliasVaultPasskeyProvider.createPasskey(createRequest);
expect(result.stored.rpId).toBe('subdomain.example.com');
});
@@ -202,7 +194,7 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const result = await provider.createPasskey(createRequest);
+ const result = await AliasVaultPasskeyProvider.createPasskey(createRequest);
// Should still create successfully with ES256
expect(result.credential).toBeDefined();
@@ -221,7 +213,7 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- await expect(provider.createPasskey(createRequest)).rejects.toThrow(
+ await expect(AliasVaultPasskeyProvider.createPasskey(createRequest)).rejects.toThrow(
'No supported algorithm (ES256) in pubKeyCredParams'
);
});
@@ -238,7 +230,7 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const result = await provider.createPasskey(createRequest, { uvPerformed: false });
+ const result = await AliasVaultPasskeyProvider.createPasskey(createRequest, { uvPerformed: false });
/*
* Find authenticatorData in the CBOR structure
@@ -265,7 +257,8 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const createResult = await provider.createPasskey(createRequest);
+ const createResult = await AliasVaultPasskeyProvider.createPasskey(createRequest);
+ storedPasskeys.set(createResult.credential.id, createResult.stored);
const credentialId = createResult.credential.id;
// Now, authenticate with the passkey
@@ -279,7 +272,8 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const assertion = await provider.getAssertion(getRequest, credentialId);
+ const storedRecord = storedPasskeys.get(credentialId)!;
+ const assertion = await AliasVaultPasskeyProvider.getAssertion(getRequest, storedRecord);
// Verify assertion structure
expect(assertion.id).toBe(credentialId);
@@ -302,7 +296,8 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const createResult = await provider.createPasskey(createRequest);
+ const createResult = await AliasVaultPasskeyProvider.createPasskey(createRequest);
+ storedPasskeys.set(createResult.credential.id, createResult.stored);
// Authenticate
const getRequest: GetRequest = {
@@ -313,7 +308,8 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const assertion = await provider.getAssertion(getRequest, createResult.credential.id);
+ const storedRecord = storedPasskeys.get(createResult.credential.id)!;
+ const assertion = await AliasVaultPasskeyProvider.getAssertion(getRequest, storedRecord);
// Decode and validate clientDataJSON
const clientDataJSON = atob(assertion.clientDataJSON);
@@ -336,7 +332,8 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const createResult = await provider.createPasskey(createRequest);
+ const createResult = await AliasVaultPasskeyProvider.createPasskey(createRequest);
+ storedPasskeys.set(createResult.credential.id, createResult.stored);
// Authenticate
const getRequest: GetRequest = {
@@ -347,7 +344,8 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const assertion = await provider.getAssertion(getRequest, createResult.credential.id);
+ const storedRecord = storedPasskeys.get(createResult.credential.id)!;
+ const assertion = await AliasVaultPasskeyProvider.getAssertion(getRequest, storedRecord);
// Decode authenticatorData
const authDataBytes = Uint8Array.from(atob(assertion.authenticatorData), c => c.charCodeAt(0));
@@ -385,7 +383,8 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const createResult = await provider.createPasskey(createRequest);
+ const createResult = await AliasVaultPasskeyProvider.createPasskey(createRequest);
+ storedPasskeys.set(createResult.credential.id, createResult.stored);
// Authenticate
const getRequest: GetRequest = {
@@ -395,7 +394,8 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const assertion = await provider.getAssertion(getRequest, createResult.credential.id);
+ const storedRecord = storedPasskeys.get(createResult.credential.id)!;
+ const assertion = await AliasVaultPasskeyProvider.getAssertion(getRequest, storedRecord);
// Decode signature
const sigBytes = Uint8Array.from(atob(assertion.signature), c => c.charCodeAt(0));
@@ -423,9 +423,17 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
+ // Test with a non-existent stored record
+ const invalidRecord = {
+ rpId: 'example.com',
+ credentialId: 'non-existent-id',
+ publicKey: {} as JsonWebKey,
+ privateKey: {} as JsonWebKey
+ } satisfies StoredPasskeyRecord;
+
await expect(
- provider.getAssertion(getRequest, 'non-existent-id')
- ).rejects.toThrow('Passkey not found');
+ AliasVaultPasskeyProvider.getAssertion(getRequest, invalidRecord)
+ ).rejects.toThrow();
});
it('should set UV flag when userVerification is required', async () => {
@@ -438,7 +446,8 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const createResult = await provider.createPasskey(createRequest);
+ const createResult = await AliasVaultPasskeyProvider.createPasskey(createRequest);
+ storedPasskeys.set(createResult.credential.id, createResult.stored);
// Authenticate with UV required
const getRequest: GetRequest = {
@@ -449,7 +458,8 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const assertion = await provider.getAssertion(getRequest, createResult.credential.id, {
+ const storedRecord = storedPasskeys.get(createResult.credential.id)!;
+ const assertion = await AliasVaultPasskeyProvider.getAssertion(getRequest, storedRecord, {
uvPerformed: false // Even without actual UV, if required, flag should be set
});
@@ -478,7 +488,8 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const createResult = await provider.createPasskey(createRequest);
+ const createResult = await AliasVaultPasskeyProvider.createPasskey(createRequest);
+ storedPasskeys.set(createResult.credential.id, createResult.stored);
// Authenticate
const getRequest: GetRequest = {
@@ -488,7 +499,8 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const assertion = await provider.getAssertion(getRequest, createResult.credential.id);
+ const storedRecord = storedPasskeys.get(createResult.credential.id)!;
+ const assertion = await AliasVaultPasskeyProvider.getAssertion(getRequest, storedRecord);
// userHandle should be present and be base64 encoded
expect(assertion.userHandle).toBeDefined();
@@ -515,7 +527,8 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const createResult = await provider.createPasskey(createRequest);
+ const createResult = await AliasVaultPasskeyProvider.createPasskey(createRequest);
+ storedPasskeys.set(createResult.credential.id, createResult.stored);
// Authenticate
const getRequest: GetRequest = {
@@ -525,7 +538,8 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const assertion = await provider.getAssertion(getRequest, createResult.credential.id);
+ const storedRecord = storedPasskeys.get(createResult.credential.id)!;
+ const assertion = await AliasVaultPasskeyProvider.getAssertion(getRequest, storedRecord);
// userHandle should be null when no user.id was provided
expect(assertion.userHandle).toBeNull();
@@ -541,7 +555,8 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const createResult = await provider.createPasskey(createRequest);
+ const createResult = await AliasVaultPasskeyProvider.createPasskey(createRequest);
+ storedPasskeys.set(createResult.credential.id, createResult.stored);
// Authenticate without BE/BS flags
const getRequest: GetRequest = {
@@ -551,7 +566,8 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const assertion = await provider.getAssertion(getRequest, createResult.credential.id, {
+ const storedRecord = storedPasskeys.get(createResult.credential.id)!;
+ const assertion = await AliasVaultPasskeyProvider.getAssertion(getRequest, storedRecord, {
includeBEBS: false
});
@@ -585,18 +601,15 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const storeSpy = vi.fn();
- const getSpy = vi.fn();
+ const result = await AliasVaultPasskeyProvider.createPasskey(createRequest);
- const testProvider = new AliasVaultPasskeyProvider(storeSpy, getSpy);
- await testProvider.createPasskey(createRequest);
+ // Manually store the passkey
+ storedPasskeys.set(result.stored.credentialId, result.stored);
- // Verify store was called with userId
- expect(storeSpy).toHaveBeenCalledTimes(1);
- const storedRecord = storeSpy.mock.calls[0][0] as StoredPasskeyRecord;
- expect(storedRecord.userId).toBeDefined();
- expect(storedRecord.userId).not.toBeNull();
- expect(typeof storedRecord.userId).toBe('string');
+ // Verify the result contains userId
+ expect(result.stored.userId).toBeDefined();
+ expect(result.stored.userId).not.toBeNull();
+ expect(typeof result.stored.userId).toBe('string');
});
it('should retrieve userId from getById callback during authentication', async () => {
@@ -617,7 +630,8 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const createResult = await provider.createPasskey(createRequest);
+ const createResult = await AliasVaultPasskeyProvider.createPasskey(createRequest);
+ storedPasskeys.set(createResult.credential.id, createResult.stored);
// Now authenticate - verify that getById is called and userId is preserved
const getRequest: GetRequest = {
@@ -628,21 +642,8 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const getBySpy = vi.fn(async (credentialId: string) => {
- // Return the stored passkey with userId
- return storedPasskeys.get(credentialId) || null;
- });
-
- const testProvider = new AliasVaultPasskeyProvider(
- async () => {},
- getBySpy
- );
-
- const assertion = await testProvider.getAssertion(getRequest, createResult.credential.id);
-
- // Verify getById was called
- expect(getBySpy).toHaveBeenCalledTimes(1);
- expect(getBySpy).toHaveBeenCalledWith(createResult.credential.id);
+ const storedRecord = storedPasskeys.get(createResult.credential.id)!;
+ const assertion = await AliasVaultPasskeyProvider.getAssertion(getRequest, storedRecord);
// Verify userHandle was populated from the retrieved userId
expect(assertion.userHandle).not.toBeNull();
@@ -654,17 +655,7 @@ describe('AliasVaultPasskeyProvider', () => {
const realStorage = new Map<string, StoredPasskeyRecord>();
const userIdBytes = new Uint8Array([100, 101, 102, 103]);
- const roundTripProvider = new AliasVaultPasskeyProvider(
- async (record: StoredPasskeyRecord) => {
- // Simulate real storage that might serialize/deserialize
- const serialized = JSON.stringify(record);
- const deserialized = JSON.parse(serialized);
- realStorage.set(record.credentialId, deserialized);
- },
- async (credentialId: string) => {
- return realStorage.get(credentialId) || null;
- }
- );
+ // We'll create the passkey and then simulate storage
// Create passkey
const createRequest: CreateRequest = {
@@ -680,7 +671,12 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const createResult = await roundTripProvider.createPasskey(createRequest);
+ const createResult = await AliasVaultPasskeyProvider.createPasskey(createRequest);
+
+ // Simulate real storage that might serialize/deserialize
+ const serialized = JSON.stringify(createResult.stored);
+ const deserialized = JSON.parse(serialized) as StoredPasskeyRecord;
+ realStorage.set(createResult.stored.credentialId, deserialized);
// Verify it was stored
const storedRecord = realStorage.get(createResult.credential.id);
@@ -695,7 +691,7 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const assertion = await roundTripProvider.getAssertion(getRequest, createResult.credential.id);
+ const assertion = await AliasVaultPasskeyProvider.getAssertion(getRequest, deserialized);
// Verify userHandle was preserved through the round-trip
expect(assertion.userHandle).not.toBeNull();
@@ -724,7 +720,8 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const createResult = await provider.createPasskey(createRequest);
+ const createResult = await AliasVaultPasskeyProvider.createPasskey(createRequest);
+ storedPasskeys.set(createResult.credential.id, createResult.stored);
// Authenticate
const getRequest: GetRequest = {
@@ -735,7 +732,8 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const assertion = await provider.getAssertion(getRequest, createResult.credential.id);
+ const storedRecord = storedPasskeys.get(createResult.credential.id)!;
+ const assertion = await AliasVaultPasskeyProvider.getAssertion(getRequest, storedRecord);
// Import public key for verification
const publicKey = await crypto.subtle.importKey(
@@ -812,7 +810,7 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const result = await provider.createPasskey(createRequest);
+ const result = await AliasVaultPasskeyProvider.createPasskey(createRequest);
expect(result.credential).toBeDefined();
});
@@ -827,7 +825,7 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const result = await provider.createPasskey(createRequest);
+ const result = await AliasVaultPasskeyProvider.createPasskey(createRequest);
expect(result.credential).toBeDefined();
});
@@ -840,7 +838,7 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const result = await provider.createPasskey(createRequest);
+ const result = await AliasVaultPasskeyProvider.createPasskey(createRequest);
expect(result.stored.rpId).toBe('subdomain.test.com');
});
@@ -855,7 +853,8 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const createResult = await provider.createPasskey(createRequest);
+ const createResult = await AliasVaultPasskeyProvider.createPasskey(createRequest);
+ storedPasskeys.set(createResult.credential.id, createResult.stored);
// Authenticate without explicit rpId
const getRequest: GetRequest = {
@@ -865,7 +864,8 @@ describe('AliasVaultPasskeyProvider', () => {
}
};
- const assertion = await provider.getAssertion(getRequest, createResult.credential.id);
+ const storedRecord = storedPasskeys.get(createResult.credential.id)!;
+ const assertion = await AliasVaultPasskeyProvider.getAssertion(getRequest, storedRecord);
expect(assertion).toBeDefined();
});
});
diff --git a/apps/browser-extension/src/utils/passkey/types.ts b/apps/browser-extension/src/utils/passkey/types.ts
index bd4b87f0e..4c0ac7e0d 100644
--- a/apps/browser-extension/src/utils/passkey/types.ts
+++ b/apps/browser-extension/src/utils/passkey/types.ts
@@ -145,13 +145,13 @@ export type PendingPasskeyGetRequest = {
export type WebAuthnPublicKeyGetPayload = {
challenge: string; // Base64URL-encoded challenge
timeout?: number;
- rpId: string;
+ rpId?: string;
allowCredentials?: {
id: string; // Base64URL-encoded credential ID
type: "public-key";
transports?: string[];
}[];
- userVerification: "required" | "preferred" | "discouraged";
+ userVerification?: "required" | "preferred" | "discouraged";
hints?: string[];
};