Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
import { rustCore } from '../rust/RustCore';
/**
* What the QR code of a mobile login request carries.
*/
export type MobileLoginQrPayload = {
requestId: string;
publicKeyHash: string;
}
/**
* Client-side rules of the mobile login (QR unlock) handshake, shared by the client that shows the QR code and the
* mobile app that scans it.
*/
export class MobileLoginProtocol {
public static readonly QR_PREFIX = 'aliasvault://open/mobile-unlock/';
/** Matches `VERIFICATION_CODE_DIGITS` in `core/rust/src/crypto/mobile_login.rs`. */
public static readonly VERIFICATION_CODE_LENGTH = 2;
private static readonly REQUEST_ID_PATTERN = /^[0-9a-f]{32}$/;
private static readonly PUBLIC_KEY_HASH_PATTERN = /^[A-Za-z0-9_-]{43}$/;
/**
* SHA-256 hash of the public key JWK string, base64url encoded without padding (43 characters).
*/
public static async computePublicKeyHash(publicKeyJwk: string): Promise<string> {
return rustCore().mobileLoginPublicKeyHash(publicKeyJwk);
}
/**
* The verification code for a public key: a fixed number of digits, zero padded.
*/
public static async computeVerificationCode(publicKeyJwk: string): Promise<string> {
return rustCore().mobileLoginVerificationCode(publicKeyJwk);
}
/**
* Whether the public key is the one the QR code was made for.
*/
public static async isExpectedPublicKey(publicKeyJwk: string, expectedPublicKeyHash: string): Promise<boolean> {
if (!MobileLoginProtocol.PUBLIC_KEY_HASH_PATTERN.test(expectedPublicKeyHash)) {
return false;
}
return await MobileLoginProtocol.computePublicKeyHash(publicKeyJwk) === expectedPublicKeyHash;
}
/**
* The text to put in the QR code.
*/
public static buildQrPayload(requestId: string, publicKeyHash: string): string {
return `${MobileLoginProtocol.QR_PREFIX}${requestId}?pk=${publicKeyHash}`;
}
/**
* Parse scanned QR code text. Returns null unless it is a complete mobile login payload: the public key hash is
* mandatory, a QR code without it is never accepted.
*/
public static parseQrPayload(data: string): MobileLoginQrPayload | null {
if (!data.startsWith(MobileLoginProtocol.QR_PREFIX)) {
return null;
}
const [requestId, query, ...rest] = data.substring(MobileLoginProtocol.QR_PREFIX.length).split('?');
if (rest.length > 0 || !query || !MobileLoginProtocol.REQUEST_ID_PATTERN.test(requestId)) {
return null;
}
const publicKeyHash = query.split('&').find(part => part.startsWith('pk='))?.substring(3) ?? '';
if (!MobileLoginProtocol.PUBLIC_KEY_HASH_PATTERN.test(publicKeyHash)) {
return null;
}
return { requestId, publicKeyHash };
}
}