Skip to content

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

Cleanup PasskeyAuthenticator.ts (#520)

Leendert de Borst99a24c23e4b3
diff --git a/apps/browser-extension/src/utils/passkey/PasskeyAuthenticator.ts b/apps/browser-extension/src/utils/passkey/PasskeyAuthenticator.ts
index d30e083a9..d715c1f79 100644
--- a/apps/browser-extension/src/utils/passkey/PasskeyAuthenticator.ts
+++ b/apps/browser-extension/src/utils/passkey/PasskeyAuthenticator.ts
@@ -1,25 +1,25 @@
/**
* PasskeyAuthenticator
* -------------------------
- * A small self-contained WebAuthn "virtual authenticator" for a browser extension.
- * It can create ("register") and use ("authenticate") passkeys. The class focuses on:
- * - Correct WebAuthn data assembly (authenticatorData, attestationObject, clientDataJSON)
- * - Proper CBOR/COSE encoding for ES256 keys
- * - Dynamic flags for UV/UP/AT/BE/BS
- * - Consistent base64url/base64 handling
+ * A WebAuthn "virtual authenticator" for browser extensions.
+ * Implements passkey creation (registration) and authentication (assertion) following
+ * the WebAuthn Level 2 specification.
*
- * This is the reference implementation. Platform-specific ports of this class:
+ * This is the reference TypeScript implementation:
* - iOS: apps/mobile-app/ios/VaultStoreKit/Passkeys/PasskeyAuthenticator.swift
* - Android: apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/passkey/PasskeyAuthenticator.kt
*
* IMPORTANT: Keep all implementations synchronized. Changes to the public interface must be
* reflected in all ports. Method names, parameters, and behavior should remain consistent.
*
- * NOTE:
- * - 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).
- * - For authentication ("get"), we also set BE/BS bits to indicate backup-eligible & backed-up.
+ * Key features:
+ * - ES256 (ECDSA P-256) key pair generation
+ * - CBOR/COSE encoding for attestation objects
+ * - Proper authenticator data with WebAuthn flags
+ * - Self-attestation (packed format) or none attestation
+ * - Consistent base64url handling
+ * - Sign count always 0 for syncable passkeys
+ * - BE/BS flags for backup-eligible and backed-up status
*/
import type { CreateRequest, GetRequest, StoredPasskeyRecord } from './types';
@@ -30,21 +30,20 @@ import type { CreateRequest, GetRequest, StoredPasskeyRecord } from './types';
export class PasskeyAuthenticator {
/**
* Private constructor to prevent instantiation.
- * This class only contains static methods.
*/
private constructor() {}
- /*
- * ------------------------------------------------------------------------------------
- * Public API
- * ------------------------------------------------------------------------------------
- */
+ /** AliasVault AAGUID: a11a5vau-9f32-4b8c-8c5d-2f7d13e8c942 */
+ private static readonly AAGUID = new Uint8Array([
+ 0xa1, 0x1a, 0x5f, 0xaa, 0x9f, 0x32, 0x4b, 0x8c,
+ 0x8c, 0x5d, 0x2f, 0x7d, 0x13, 0xe8, 0xc9, 0x42
+ ]);
+
+ // MARK: - Public API
/**
- * Create a new passkey (registration). Returns a credential-like object similar to
- * navigator.credentials.create() output, ready to be posted to the RP.
- *
- * Also returns the passkey data that should be stored for later use.
+ * 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,
@@ -53,30 +52,27 @@ export class PasskeyAuthenticator {
uvPerformed?: boolean;
credentialIdBytes?: number;
enablePrf?: boolean;
- prfInputs?: { first: string; second?: string }; // Base64url encoded salts for PRF evaluation during registration
+ prfInputs?: { first: string; second?: string };
}
): Promise<{
credential: {
id: string;
- rawId: string; // base64url for transport (pages usually expect ArrayBuffers; adapt as needed)
+ rawId: string;
response: {
- clientDataJSON: string; // base64 (UTF-8 JSON)
- attestationObject: string; // base64 (CBOR)
+ clientDataJSON: string;
+ attestationObject: string;
};
type: 'public-key';
};
stored: StoredPasskeyRecord;
- prfEnabled?: boolean; // Indicates if PRF was enabled for this credential
- prfResults?: { first: ArrayBuffer; second?: ArrayBuffer }; // PRF evaluation results if requested during registration
+ prfEnabled?: boolean;
+ prfResults?: { first: ArrayBuffer; second?: ArrayBuffer };
}> {
- // 1) Validate and resolve algorithm (-7 = ES256)
PasskeyAuthenticator.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', PasskeyAuthenticator.te(rpId) as BufferSource));
- // 3) Key pair generation (ES256 / P-256)
const keyPair = await crypto.subtle.generateKey(
{ name: 'ECDSA', namedCurve: 'P-256' },
true,
@@ -85,45 +81,25 @@ export class PasskeyAuthenticator {
const pubJwk = await crypto.subtle.exportKey('jwk', keyPair.publicKey);
const prvJwk = await crypto.subtle.exportKey('jwk', keyPair.privateKey);
- // 4) Use the provided credentialIdBytes param
const credentialIdB64u = PasskeyAuthenticator.toB64u(credentialIdBytes);
-
- // 5) COSE public key (CBOR) from JWK (ES256 / P-256)
const coseKey = PasskeyAuthenticator.buildCoseEc2Es256(pubJwk);
- /*
- * 6) Flags (creation): UP (bit0)=1, UV (bit2) depends on policy, AT (bit6)=1, BE/BS optional
- * - UV rules: set to 1 only if req requires it OR we actually performed UV (opts.uvPerformed)
- */
- let flags = 0x41; // UP + AT
+ let flags = 0x41; // UP (bit 0) + AT (bit 6)
const uvReq = req.publicKey.authenticatorSelection?.userVerification;
const uvPerformed = !!opts?.uvPerformed;
if (uvReq === 'required' || (uvReq === 'preferred' && uvPerformed)) {
- flags |= 0x04; // UV
- }
- // For passkeys (syncable), we may set BE (0x08) and BS (0x10) to 1 to indicate backup-eligible & backed-up
- flags |= 0x08; // BE
- flags |= 0x10; // BS
-
- const signCount = new Uint8Array([0, 0, 0, 0]); // 0 for syncable credentials
-
- /*
- * 7) AttestedCredentialData = AAGUID + credIdLen(2) + credId + COSEKey
- * AliasVault AAGUID: a11a5vau-9f32-4b8c-8c5d-2f7d13e8c942
- */
- const aaguidStr = 'a11a5vau-9f32-4b8c-8c5d-2f7d13e8c942';
- const aaguidHex = aaguidStr.replace(/-/g, '').replace(/v/g, 'f').replace(/u/g, 'a');
- const aaguid = new Uint8Array(16);
- for (let i = 0; i < 16; i++) {
- aaguid[i] = parseInt(aaguidHex.substr(i * 2, 2), 16);
+ flags |= 0x04; // UV (bit 2)
}
+ flags |= 0x08; // BE (bit 3)
+ flags |= 0x10; // BS (bit 4)
+
+ const signCount = new Uint8Array([0, 0, 0, 0]);
+
const credIdLenBytes = new Uint8Array([(credentialIdBytes.length >> 8) & 0xff, credentialIdBytes.length & 0xff]);
- const attestedCredData = PasskeyAuthenticator.concat(aaguid, credIdLenBytes, credentialIdBytes, coseKey);
+ const attestedCredData = PasskeyAuthenticator.concat(PasskeyAuthenticator.AAGUID, credIdLenBytes, credentialIdBytes, coseKey);
- // 8) authenticatorData = rpIdHash (32) + flags (1) + signCount (4) + attestedCredData
const authenticatorData = PasskeyAuthenticator.concat(rpIdHash, new Uint8Array([flags]), signCount, attestedCredData);
- // 9) clientDataJSON (stringify with challenge as base64url)
const challengeB64u = PasskeyAuthenticator.challengeToB64u(req.publicKey.challenge);
const clientDataObj = {
type: 'webauthn.create',
@@ -134,45 +110,34 @@ export class PasskeyAuthenticator {
const clientDataJSONStr = JSON.stringify(clientDataObj);
const clientDataJSONBytes = PasskeyAuthenticator.te(clientDataJSONStr);
- // 10) Build attestationObject (CBOR map with "fmt","attStmt","authData")
const attPref = req.publicKey.attestation || 'none';
const attObjBytes =
attPref === 'none' || attPref === 'indirect'
? PasskeyAuthenticator.buildAttObjNone(authenticatorData)
: await PasskeyAuthenticator.buildAttObjPackedSelf(authenticatorData, clientDataJSONBytes, keyPair.privateKey);
- /*
- * 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;
if (req.publicKey.user?.id) {
- // The injection script already converted ArrayBuffer to standard base64, store as-is
userIdB64 = typeof req.publicKey.user.id === 'string'
? req.publicKey.user.id
: PasskeyAuthenticator.toB64(req.publicKey.user.id instanceof Uint8Array ? req.publicKey.user.id : new Uint8Array(req.publicKey.user.id));
}
- // 11.5) PRF support: Generate PRF secret if requested and optionally evaluate during registration
let prfSecret: string | undefined;
let prfEnabled = false;
let prfResults: { first: ArrayBuffer; second?: ArrayBuffer } | undefined;
if (opts?.enablePrf) {
- // Generate a 32-byte random secret for PRF (hmac-secret extension)
const prfSecretBytes = new Uint8Array(32);
crypto.getRandomValues(prfSecretBytes);
prfSecret = PasskeyAuthenticator.toB64u(prfSecretBytes);
prfEnabled = true;
- // If the caller requested PRF evaluation during registration, evaluate it now
if (opts?.prfInputs) {
- // Decode base64url salts to bytes
const firstSalt = PasskeyAuthenticator.fromB64u(opts.prfInputs.first);
prfResults = {
first: await PasskeyAuthenticator.evaluatePrf(prfSecretBytes, firstSalt)
};
- // Evaluate second salt if provided
if (opts.prfInputs.second) {
const secondSalt = PasskeyAuthenticator.fromB64u(opts.prfInputs.second);
prfResults.second = await PasskeyAuthenticator.evaluatePrf(prfSecretBytes, secondSalt);
@@ -191,7 +156,6 @@ export class PasskeyAuthenticator {
prfSecret
};
- // 12) Return a credential-like object (base64url-encoded fields for transport per RFC 4648 §5)
const credential = {
id: credentialIdB64u,
rawId: credentialIdB64u,
@@ -206,16 +170,8 @@ export class PasskeyAuthenticator {
}
/**
- * Create an assertion (authentication) with a stored passkey.
- *
- * Returns the flat object shape your client example expects:
- * {
- * id, rawId, clientDataJSON, authenticatorData, signature, userHandle
- * }
- *
- * NOTE:
- * - 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.
+ * Create an assertion (authentication).
+ * Returns assertion data ready for the browser extension to return to the RP.
*/
public static async getAssertion(
req: GetRequest,
@@ -224,36 +180,31 @@ export class PasskeyAuthenticator {
): Promise<{
id: string;
rawId: string;
- clientDataJSON: string; // base64
- authenticatorData: string; // base64
- signature: string; // base64 (DER)
- userHandle: string | null; // base64 (if you choose to return it)
- prfResults?: { first: ArrayBuffer; second?: ArrayBuffer }; // PRF outputs if requested
+ clientDataJSON: string;
+ authenticatorData: string;
+ signature: string;
+ userHandle: string | null;
+ prfResults?: { first: ArrayBuffer; second?: ArrayBuffer };
}> {
- // 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', PasskeyAuthenticator.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
+ let flags = 0x01; // UP (bit 0)
const uvReq = req.publicKey.userVerification;
const uvPerformed = !!opts?.uvPerformed;
if (uvReq === 'required' || (uvReq === 'preferred' && uvPerformed)) {
- flags |= 0x04;
- } // UV
+ flags |= 0x04; // UV (bit 2)
+ }
if (opts?.includeBEBS ?? true) {
- flags |= 0x08; // BE
- flags |= 0x10; // BS
+ flags |= 0x08; // BE (bit 3)
+ flags |= 0x10; // BS (bit 4)
}
- const signCount = new Uint8Array([0, 0, 0, 0]); // always 0 (sync-friendly)
+ const signCount = new Uint8Array([0, 0, 0, 0]);
- // 4) authenticatorData = rpIdHash + flags + signCount
const authenticatorData = PasskeyAuthenticator.concat(rpIdHash, new Uint8Array([flags]), signCount);
- // 5) clientDataJSON
const challengeB64u = PasskeyAuthenticator.challengeToB64u(req.publicKey.challenge);
const clientDataObj = {
type: 'webauthn.get',
@@ -264,7 +215,6 @@ export class PasskeyAuthenticator {
const clientDataJSONStr = JSON.stringify(clientDataObj);
const clientDataJSONBytes = PasskeyAuthenticator.te(clientDataJSONStr);
- // 6) Signature over authenticatorData || SHA256(clientDataJSON)
const clientDataHash = new Uint8Array(await crypto.subtle.digest('SHA-256', clientDataJSONBytes as BufferSource));
const toSign = PasskeyAuthenticator.concat(authenticatorData, clientDataHash);
@@ -279,39 +229,26 @@ export class PasskeyAuthenticator {
await crypto.subtle.sign({ name: 'ECDSA', hash: 'SHA-256' }, privateKey, toSign as BufferSource)
);
- // 7) Convert raw (r|s) to DER sequence
const derSig = PasskeyAuthenticator.ecdsaRawToDer(rawSig);
- /*
- * 8) Return userHandle (userId) - convert to base64url if present
- * This is required for discoverable credentials (resident keys) where the RP doesn't ask for a username first
- * userId is stored as standard base64, convert to base64url for RFC 4648 §5 compliance
- */
let userHandleB64u: string | null = null;
if (rec.userId) {
- // Convert standard base64 to base64url (remove padding, replace chars)
userHandleB64u = rec.userId.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
- } else {
- console.warn('PasskeyAuthenticator.getAssertion: No userId found in stored passkey record');
}
- // 9) PRF evaluation if requested and supported
let prfResults: { first: ArrayBuffer; second?: ArrayBuffer } | undefined;
if (opts?.prfInputs && rec.prfSecret) {
const prfSecretBytes = PasskeyAuthenticator.fromB64u(rec.prfSecret);
- // Evaluate first salt
const firstResult = await PasskeyAuthenticator.evaluatePrf(prfSecretBytes, opts.prfInputs.first);
prfResults = { first: firstResult };
- // Evaluate second salt if provided
if (opts.prfInputs.second) {
const secondResult = await PasskeyAuthenticator.evaluatePrf(prfSecretBytes, opts.prfInputs.second);
prfResults.second = secondResult;
}
}
- // 10) Return object in the flat shape (base64url strings per RFC 4648 §5)
return {
id: rec.credentialId,
rawId: rec.credentialId,
@@ -323,28 +260,19 @@ export class PasskeyAuthenticator {
};
}
- /*
- * ------------------------------------------------------------------------------------
- * Internal helpers (encoding, CBOR/COSE, DER, utils)
- * ------------------------------------------------------------------------------------
- */
+ // MARK: - PRF Extension
/**
- * Evaluate PRF (hmac-secret extension) for a given salt input.
- * Implements the WebAuthn PRF extension algorithm:
- * 1. Hash the salt with domain separation: SHA-256("WebAuthn PRF\x00" || salt)
- * 2. Compute HMAC-SHA256(prfSecret, hashedSalt)
- * 3. Return the 32-byte output
+ * Evaluate PRF (hmac-secret extension).
+ * Implements: HMAC-SHA256(prfSecret, SHA-256("WebAuthn PRF\x00" || salt)).
*/
private static async evaluatePrf(prfSecretBytes: Uint8Array, salt: ArrayBuffer | Uint8Array): Promise<ArrayBuffer> {
const saltBytes = salt instanceof Uint8Array ? salt : new Uint8Array(salt);
- // Step 1: Domain separation - hash salt with "WebAuthn PRF\x00" prefix
const prefix = PasskeyAuthenticator.te('WebAuthn PRF\x00');
const domainSeparatedSalt = PasskeyAuthenticator.concat(prefix, saltBytes);
const hashedSalt = await crypto.subtle.digest('SHA-256', domainSeparatedSalt as BufferSource);
- // Step 2: Import PRF secret as HMAC key
const hmacKey = await crypto.subtle.importKey(
'raw',
prfSecretBytes as BufferSource,
@@ -353,17 +281,22 @@ export class PasskeyAuthenticator {
['sign']
);
- // Step 3: Compute HMAC-SHA256(prfSecret, hashedSalt)
const prfOutput = await crypto.subtle.sign('HMAC', hmacKey, hashedSalt);
return prfOutput;
}
- /** Ensure ES256 (-7) is available; else throw (or extend to support others). */
+ // MARK: - CBOR Encoding
+
+ /**
+ * Ensure ES256 (-7) is available.
+ * @param params - Public key credential parameters
+ * @returns Algorithm identifier (-7 for ES256)
+ */
private static pickSupportedAlgorithm(params?: Array<{ type: 'public-key'; alg: number }>): number {
if (!params || params.length === 0) {
return -7;
- } // assume ES256 default
+ }
const hasEs256 = params.some(p => p.type === 'public-key' && p.alg === -7);
if (!hasEs256) {
throw new Error('No supported algorithm (ES256) in pubKeyCredParams');
@@ -371,38 +304,36 @@ export class PasskeyAuthenticator {
return -7;
}
- /** Build COSE EC2 public key for ES256: {1:2, 3:-7, -1:1, -2:x, -3:y} in CBOR. */
+ /**
+ * Build COSE EC2 public key for ES256.
+ * CBOR map: {1: 2, 3: -7, -1: 1, -2: x, -3: y}.
+ */
private static buildCoseEc2Es256(jwk: JsonWebKey): Uint8Array {
const x = PasskeyAuthenticator.pad32(PasskeyAuthenticator.fromB64u(jwk.x!));
const y = PasskeyAuthenticator.pad32(PasskeyAuthenticator.fromB64u(jwk.y!));
- /*
- * Map(5): 0xa5
- * 1:2 (kty: EC2)
- * 3:-7 (alg: ES256)
- * -1:1 (crv: P-256)
- * -2:x(32) (x)
- * -3:y(32) (y)
- */
+
return new Uint8Array([
0xa5,
- 0x01, 0x02, // 1: 2
- 0x03, 0x26, // 3: -7
- 0x20, 0x01, // -1: 1
- 0x21, 0x58, 0x20, ...x, // -2: bstr(32) x
- 0x22, 0x58, 0x20, ...y // -3: bstr(32) y
+ 0x01, 0x02, // 1: 2 (kty: EC2)
+ 0x03, 0x26, // 3: -7 (alg: ES256)
+ 0x20, 0x01, // -1: 1 (crv: P-256)
+ 0x21, 0x58, 0x20, ...x, // -2: bytes(32) for x
+ 0x22, 0x58, 0x20, ...y // -3: bytes(32) for y
]);
}
- /** Build "none" attestation object: { fmt: "none", attStmt: {}, authData: <bytes> } (CBOR). */
+ /**
+ * Build attestation object with "none" format.
+ * CBOR map: {fmt: "none", attStmt: {}, authData: <bytes>}.
+ */
private static buildAttObjNone(authenticatorData: Uint8Array): Uint8Array {
- const fmtKey = PasskeyAuthenticator.cborText('fmt'); // "fmt"
- const fmtVal = PasskeyAuthenticator.cborText('none'); // "none"
- const attStmtKey = PasskeyAuthenticator.cborText('attStmt'); // "attStmt"
- const attStmtVal = new Uint8Array([0xa0]); // map(0) {}
- const authDataKey = PasskeyAuthenticator.cborText('authData'); // "authData"
+ const fmtKey = PasskeyAuthenticator.cborText('fmt');
+ const fmtVal = PasskeyAuthenticator.cborText('none');
+ const attStmtKey = PasskeyAuthenticator.cborText('attStmt');
+ const attStmtVal = new Uint8Array([0xa0]);
+ const authDataKey = PasskeyAuthenticator.cborText('authData');
const authDataVal = PasskeyAuthenticator.cborBstr(authenticatorData);
- // map(3)
return PasskeyAuthenticator.concat(
new Uint8Array([0xa3]),
fmtKey, fmtVal,
@@ -411,13 +342,14 @@ export class PasskeyAuthenticator {
);
}
- /** Build "packed" self-attestation object (no x5c). */
+ /**
+ * Build "packed" self-attestation object (no x5c).
+ */
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 as BufferSource));
const toSign = PasskeyAuthenticator.concat(authenticatorData, clientDataHash);
const rawSig = new Uint8Array(
@@ -425,15 +357,12 @@ export class PasskeyAuthenticator {
);
const derSig = PasskeyAuthenticator.ecdsaRawToDer(rawSig);
- // attStmt = { alg: -7, sig: <derSig> }
const attStmtMap = PasskeyAuthenticator.concat(
- PasskeyAuthenticator.cborText('alg'), new Uint8Array([0x26]), // -7
+ PasskeyAuthenticator.cborText('alg'), new Uint8Array([0x26]),
PasskeyAuthenticator.cborText('sig'), PasskeyAuthenticator.cborBstr(derSig)
);
- // prepend map(2)
const attStmt = PasskeyAuthenticator.concat(new Uint8Array([0xa2]), attStmtMap);
- // final: { fmt:"packed", attStmt:{...}, authData:<bytes> }
const fmtKey = PasskeyAuthenticator.cborText('fmt');
const fmtVal = PasskeyAuthenticator.cborText('packed');
const attStmtKey = PasskeyAuthenticator.cborText('attStmt');
@@ -441,41 +370,45 @@ export class PasskeyAuthenticator {
const authDataVal = PasskeyAuthenticator.cborBstr(authenticatorData);
return PasskeyAuthenticator.concat(
- new Uint8Array([0xa3]), // map(3)
+ new Uint8Array([0xa3]),
fmtKey, fmtVal,
attStmtKey, attStmt,
authDataKey, authDataVal
);
}
- // ----- CBOR small helpers -----
-
- /** Encode a UTF-8 string as CBOR text. */
+ /**
+ * Encode a string as CBOR text.
+ */
private static cborText(s: string): Uint8Array {
const bytes = PasskeyAuthenticator.te(s);
if (bytes.length <= 23) {
return new Uint8Array([0x60 | bytes.length, ...bytes]);
- } // major type 3
+ }
if (bytes.length <= 0xff) {
return new Uint8Array([0x78, bytes.length, ...bytes]);
}
return new Uint8Array([0x79, (bytes.length >> 8) & 0xff, bytes.length & 0xff, ...bytes]);
}
- /** Encode a byte string as CBOR bstr. */
+ /**
+ * Encode bytes as CBOR byte string.
+ */
private static cborBstr(b: Uint8Array): Uint8Array {
if (b.length <= 23) {
return new Uint8Array([0x40 | b.length, ...b]);
- } // major type 2
+ }
if (b.length <= 0xff) {
return new Uint8Array([0x58, b.length, ...b]);
}
return new Uint8Array([0x59, (b.length >> 8) & 0xff, b.length & 0xff, ...b]);
}
- // ----- DER helpers -----
+ // MARK: - Signature Conversion
- /** Convert raw ECDSA signature (r|s, 64 bytes) to DER SEQUENCE. */
+ /**
+ * Convert raw ECDSA signature (r|s, 64 bytes) to DER SEQUENCE.
+ */
private static ecdsaRawToDer(raw: Uint8Array): Uint8Array {
if (raw.length !== 64) {
throw new Error('Unexpected ECDSA signature length');
@@ -487,15 +420,15 @@ export class PasskeyAuthenticator {
return new Uint8Array([0x30, rDer.length + sDer.length, ...rDer, ...sDer]);
}
- /** Encode a positive big integer to DER INTEGER. */
+ /**
+ * Encode a positive big integer as DER INTEGER.
+ */
private static derInt(src: Uint8Array): Uint8Array {
- // Trim leading zeros
let i = 0;
while (i < src.length - 1 && src[i] === 0x00) {
i++;
}
let v = src.slice(i);
- // If MSB set, prepend 0 to keep it positive
if ((v[0] & 0x80) !== 0) {
const padded = new Uint8Array(v.length + 1);
padded[0] = 0x00;
@@ -505,15 +438,23 @@ export class PasskeyAuthenticator {
return new Uint8Array([0x02, v.length, ...v]);
}
- // ----- Base64 / Base64url helpers -----
+ // MARK: - Base64 Encoding
- /** UTF-8 encode string to bytes. */
+ /**
+ * UTF-8 encode string to bytes.
+ * @param s - String to encode
+ * @returns Encoded bytes
+ */
private static te(s: string): Uint8Array {
const encoder = new TextEncoder();
return encoder.encode(s);
}
- /** Base64 encode bytes (Uint8Array) to ASCII string. */
+ /**
+ * Base64 encode bytes.
+ * @param bytes - Bytes to encode
+ * @returns Base64 string
+ */
private static toB64(bytes: Uint8Array): string {
let bin = '';
for (let i = 0; i < bytes.length; i++) {
@@ -522,12 +463,20 @@ export class PasskeyAuthenticator {
return btoa(bin);
}
- /** Base64url encode bytes. */
+ /**
+ * Base64url encode bytes.
+ * @param bytes - Bytes to encode
+ * @returns Base64url string
+ */
private static toB64u(bytes: Uint8Array): string {
return PasskeyAuthenticator.toB64(bytes).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/g, '');
}
- /** Base64url decode to bytes. */
+ /**
+ * Base64url decode to bytes.
+ * @param b64u - Base64url string
+ * @returns Decoded bytes
+ */
private static fromB64u(b64u: string): Uint8Array {
const b64 = b64u.replace(/-/g, '+').replace(/_/g, '/');
const pad = b64.length % 4 === 2 ? '==' : b64.length % 4 === 3 ? '=' : '';
@@ -541,14 +490,11 @@ export class PasskeyAuthenticator {
/**
* Normalize challenge to base64url string.
- * The injection script sends challenges as standard base64, so we need to convert to base64url.
+ * @param challenge - Challenge from WebAuthn request
+ * @returns Base64url encoded challenge
*/
private static challengeToB64u(challenge: ArrayBuffer | Uint8Array | string): string {
if (typeof challenge === 'string') {
- /*
- * String from injection script - it's standard base64, convert to base64url
- * Remove padding and replace + with - and / with _
- */
return challenge.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
}
const bytes = challenge instanceof Uint8Array ? challenge : new Uint8Array(challenge);
@@ -556,28 +502,10 @@ export class PasskeyAuthenticator {
}
/**
- * Normalize userId to base64url string.
- * userId can be:
- * - ArrayBuffer/Uint8Array from WebAuthn API (needs encoding)
- * - Plain UTF-8 string from test/simple cases (needs encoding)
- * - Already base64url encoded string (use as-is)
+ * Left-pad to 32 bytes for P-256 coordinates.
+ * @param b - Bytes to pad
+ * @returns Padded bytes
*/
- 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) {
- // Looks like base64url already (and not an invalid length)
- return userId;
- } else {
- // Plain UTF-8 string, encode it
- return PasskeyAuthenticator.toB64u(PasskeyAuthenticator.te(userId));
- }
- }
- const bytes = userId instanceof Uint8Array ? userId : new Uint8Array(userId);
- return PasskeyAuthenticator.toB64u(bytes);
- }
-
- /** Left-pad to 32 bytes (P-256 coord). */
private static pad32(b: Uint8Array): Uint8Array {
if (b.length === 32) {
return b;
@@ -587,7 +515,11 @@ export class PasskeyAuthenticator {
return out;
}
- /** Concatenate typed arrays. */
+ /**
+ * Concatenate typed arrays.
+ * @param chunks - Arrays to concatenate
+ * @returns Concatenated array
+ */
private static concat(...chunks: Uint8Array[]): Uint8Array {
const total = chunks.reduce((s, c) => s + c.length, 0);
const out = new Uint8Array(total);