|
| 1 | +# accounts-passkey |
| 2 | + |
| 3 | +Passkey (WebAuthn) authentication library for Firefox Accounts. |
| 4 | + |
| 5 | +This library was generated with [Nx](https://nx.dev). |
| 6 | + |
| 7 | +## Building |
| 8 | + |
| 9 | +Run `nx run accounts-passkey:build` to build the library. |
| 10 | + |
| 11 | +## Running unit tests |
| 12 | + |
| 13 | +Run `nx run accounts-passkey:test-unit` to execute the unit tests via [Jest](https://jestjs.io). |
| 14 | + |
| 15 | +## Running integration tests |
| 16 | + |
| 17 | +Make sure local infrastructure (databases) are running. Check status with `yarn pm2 status` - you should see redis and mysql instances running. If not, run `yarn start infrastructure`. |
| 18 | + |
| 19 | +Run `nx run accounts-passkey:test-integration` to execute the integration tests via [Jest](https://jestjs.io). |
| 20 | + |
| 21 | +## Architecture |
| 22 | + |
| 23 | +This library follows the layered architecture pattern used across `libs/accounts/*`: |
| 24 | + |
| 25 | +### Layers |
| 26 | + |
| 27 | +1. **Service Layer** (`passkey.service.ts`) |
| 28 | + - High-level business logic and orchestration |
| 29 | + - Validates input, coordinates operations |
| 30 | + - Handles metrics and logging |
| 31 | + - Called by route handlers in auth-server/admin-server |
| 32 | + |
| 33 | +2. **Manager Layer** (`passkey.manager.ts`) |
| 34 | + - Injectable class that wraps database operations |
| 35 | + - Coordinates repository function calls |
| 36 | + - Handles transactions and business logic related to data |
| 37 | + - Injected into Service layer |
| 38 | + |
| 39 | +3. **Repository Layer** (`passkey.repository.ts`) |
| 40 | + - Pure functions that accept `AccountDatabase` as first parameter |
| 41 | + - Direct SQL queries using Kysely query builder |
| 42 | + - No business logic, just data access |
| 43 | + - Called by Manager layer |
| 44 | + |
| 45 | +4. **Error Layer** (`passkey.errors.ts`) |
| 46 | + - Domain-specific error classes |
| 47 | + - Structured error information for logging |
| 48 | + - HTTP status code mapping |
| 49 | + |
| 50 | +5. **Configuration** (`passkey.config.ts`) |
| 51 | + - Type-safe configuration interface |
| 52 | + - Validated with class-validator decorators |
| 53 | + - Loaded from Convict config in consuming applications |
| 54 | + |
| 55 | +### Pattern: No Module Export |
| 56 | + |
| 57 | +Unlike `libs/shared/nestjs/*`, this library **does not export a NestJS module**. This is intentional: |
| 58 | + |
| 59 | +- **auth-server** (Hapi + TypeDI): Uses `Container.get(PasskeyService)` |
| 60 | +- **admin-server** (NestJS): Manually wires providers in their modules |
| 61 | + |
| 62 | +This pattern gives consuming applications full control over DI setup. |
| 63 | + |
| 64 | +## WebAuthn / Passkey Background |
| 65 | + |
| 66 | +Passkeys are a WebAuthn-based authentication method that replaces passwords: |
| 67 | + |
| 68 | +- **Registration (Attestation)**: Create a new passkey credential |
| 69 | +- **Authentication (Assertion)**: Verify using existing passkey |
| 70 | +- **Challenge-Response**: Server generates challenge, client signs it |
| 71 | +- **Public Key Cryptography**: Private key stays on device, public key stored in DB |
| 72 | + |
| 73 | +Key WebAuthn concepts: |
| 74 | + |
| 75 | +- **Relying Party (RP)**: Our service (accounts.firefox.com) |
| 76 | +- **Authenticator**: User's device (phone, laptop, security key) |
| 77 | +- **Credential ID**: Unique identifier for each passkey |
| 78 | +- **Counter**: Signature counter for replay attack prevention |
| 79 | +- **User Verification (UV)**: Biometric or PIN on the device |
| 80 | + |
| 81 | +### Resources |
| 82 | + |
| 83 | +- [WebAuthn Spec](https://www.w3.org/TR/webauthn-3/) |
| 84 | +- [Passkey Developer Guide](https://passkeys.dev/) |
0 commit comments