mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 00:20:07 +00:00
refactor(auth): passkeys and registration invites through repositories
This commit is contained in:
@@ -17,6 +17,17 @@ export interface InviteTokenRow {
|
||||
|
||||
const _inviteTokenRowKeys: AssertRowKeys<InviteTokenRow, InviteTokens> = true;
|
||||
|
||||
/**
|
||||
* RI1/RI5's joined projection: `invite_tokens` plus the creator's username
|
||||
* and the bound trip's title (if any). `created_by_name` mirrors an admin
|
||||
* invite always having a creator (INNER `JOIN users`); `trip_title` is
|
||||
* `null` when the invite isn't bound to a trip (`LEFT JOIN trips`).
|
||||
*/
|
||||
export interface InviteWithCreatorAndTripRow extends InviteTokenRow {
|
||||
created_by_name: string;
|
||||
trip_title: string | null;
|
||||
}
|
||||
|
||||
/** The column set `RegistrationInvitesService.createInvite` (Task 3) writes. */
|
||||
export interface NewInviteTokenRow {
|
||||
token: string;
|
||||
@@ -45,13 +56,17 @@ export class InviteTokensRepository extends EntityRepository<InviteTokens> {
|
||||
* and `OidcService.findOrCreateUser` (O11). A name promising "valid" would
|
||||
* describe a filter this statement doesn't have.
|
||||
*
|
||||
* Not a primary-key filter — `token` is a unique column, not `id` — so this
|
||||
* always re-queries; no `{ refresh: true }` needed (Task 3a review's
|
||||
* precision ruling: the identity-map short-circuit only fires for a
|
||||
* PK-only filter).
|
||||
* Not a primary-key filter — `token` is a unique column, not `id`.
|
||||
* `disableIdentityMap: true`, not `{ refresh: true }`: a plain `refresh`
|
||||
* re-snapshots only the selected fields on an already identity-mapped
|
||||
* entity, which risks a stale-write-back on the request's closing
|
||||
* `flush()` if this same row is (or later becomes) managed under a
|
||||
* different projection in the same transaction — reproduced live on
|
||||
* `users` (see `WebauthnCredentialsRepository.hasAny`'s docstring). An
|
||||
* isolated, unmanaged read can never be part of that flush.
|
||||
*/
|
||||
async findByToken(token: string): Promise<InviteTokenRow | null> {
|
||||
const invite = await this.findOne({ token });
|
||||
const invite = await this.findOne({ token }, { disableIdentityMap: true });
|
||||
return invite ? (toRow(invite) as InviteTokenRow) : null;
|
||||
}
|
||||
|
||||
@@ -63,18 +78,25 @@ export class InviteTokensRepository extends EntityRepository<InviteTokens> {
|
||||
* `Categories.user`/`user_id`); the repository writes them through the
|
||||
* `Ref`-typed relation properties, which MikroORM accepts a raw primary
|
||||
* key for.
|
||||
*
|
||||
* `EntityRepository.insert()`, not `create()` + `persist().flush()` +
|
||||
* `refresh()`: a native insert with no identity-map/UnitOfWork side
|
||||
* effects — `persist().flush()` flushes the ENTIRE request's UnitOfWork,
|
||||
* which can write back a stale identity-mapped entity from an unrelated
|
||||
* earlier read in the same request (see `WebauthnCredentialsRepository
|
||||
* .hasAny`'s docstring). The re-select after `insert()` uses
|
||||
* `disableIdentityMap: true` for the same reason `findByToken` does.
|
||||
*/
|
||||
async insertInvite(row: NewInviteTokenRow): Promise<InviteTokenRow> {
|
||||
const invite = this.create({
|
||||
const id = await this.insert({
|
||||
token: row.token,
|
||||
max_uses: row.max_uses,
|
||||
expires_at: row.expires_at,
|
||||
createdByRef: row.created_by,
|
||||
trip: row.trip_id ?? null,
|
||||
});
|
||||
await this.getEntityManager().persist(invite).flush();
|
||||
await this.getEntityManager().refresh(invite);
|
||||
return toRow(invite) as InviteTokenRow;
|
||||
const invite = await this.findOne({ id }, { disableIdentityMap: true });
|
||||
return toRow(invite!) as InviteTokenRow;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -116,4 +138,76 @@ export class InviteTokensRepository extends EntityRepository<InviteTokens> {
|
||||
.execute('run');
|
||||
return result.row ? (result.row as InviteTokenRow) : null;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// RI1 — admin invite list (Task 3, RegistrationInvitesService.listInvites)
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* ```sql
|
||||
* SELECT i.*, u.username as created_by_name, t.title as trip_title
|
||||
* FROM invite_tokens i
|
||||
* JOIN users u ON i.created_by = u.id
|
||||
* LEFT JOIN trips t ON i.trip_id = t.id
|
||||
* ORDER BY i.created_at DESC
|
||||
* ```
|
||||
*
|
||||
* A QueryBuilder join (D4's T5 tier), projection root `InviteTokens` —
|
||||
* `i.createdByRef`/`i.trip` are the entity's own relation properties for
|
||||
* exactly these two joins, so no hand-spelled join condition is needed.
|
||||
* `mapResults: false` (the same choice `TripsRepository.findAccessible`
|
||||
* makes for its own aliased join) leaves the driver's row alone: the keys
|
||||
* are the column names the `AS` aliases spell, which is why the result
|
||||
* type is hand-written here rather than inferred from the entity.
|
||||
*/
|
||||
async listWithCreatorAndTrip(): Promise<InviteWithCreatorAndTripRow[]> {
|
||||
return this.qb('i')
|
||||
.join('i.createdByRef', 'u')
|
||||
.leftJoin('i.trip', 't')
|
||||
.select(['i.*', 'u.username as created_by_name', 't.title as trip_title'])
|
||||
.orderBy({ 'i.created_at': 'desc' })
|
||||
.execute<InviteWithCreatorAndTripRow[]>('all', false);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// RI5 — createInvite's re-select (same JOIN shape, filtered to one row)
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/** RI1's exact JOIN shape, `WHERE i.id = ?` — the response of `createInvite`. */
|
||||
async findWithCreatorAndTrip(id: number): Promise<InviteWithCreatorAndTripRow | null> {
|
||||
const row = await this.qb('i')
|
||||
.join('i.createdByRef', 'u')
|
||||
.leftJoin('i.trip', 't')
|
||||
.select(['i.*', 'u.username as created_by_name', 't.title as trip_title'])
|
||||
.where({ 'i.id': id })
|
||||
.execute<InviteWithCreatorAndTripRow | undefined>('get', false);
|
||||
return row ?? null;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// RI6 — deleteInvite's 404 check
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* `SELECT id FROM invite_tokens WHERE id = ?`. Named `findIdById`, not
|
||||
* `findById` — `findById` would read like it belonged next to
|
||||
* `UsersRepository.findById`'s full-row shape; this is a narrow
|
||||
* existence probe for one column, and neither name shadows an
|
||||
* `EntityRepository` method (there is no base `findById`/`deleteById`
|
||||
* to collide with — the distinct names are for a human reading two
|
||||
* unrelated repositories side by side, not a compiler conflict).
|
||||
*/
|
||||
async findIdById(id: number): Promise<number | null> {
|
||||
const row = await this.findOne({ id }, { fields: ['id'], disableIdentityMap: true });
|
||||
return row ? row.id : null;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// RI7 — deleteInvite's delete
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/** `DELETE FROM invite_tokens WHERE id = ?` */
|
||||
async deleteById(id: number): Promise<number> {
|
||||
return this.nativeDelete({ id });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,98 @@
|
||||
import type { WebauthnChallenges } from '../entities/WebauthnChallenges.entity';
|
||||
import { EntityRepository } from '@mikro-orm/sql';
|
||||
|
||||
export class WebauthnChallengesRepository extends EntityRepository<WebauthnChallenges> {}
|
||||
/** `PasskeyService.storeChallenge` (PK2). */
|
||||
export interface NewChallengeRow {
|
||||
challenge: string;
|
||||
user_id: number | null;
|
||||
type: 'registration' | 'authentication';
|
||||
expires_at: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* The `webauthn_challenges` table shape as `claimChallenge`'s
|
||||
* `em.getKysely()` call needs it — an explicit type argument rather than
|
||||
* relying on `getKysely()`'s automatic entity-metadata inference (kept
|
||||
* narrow to the one column this repository ever reads back).
|
||||
*/
|
||||
interface WebauthnChallengesKyselyDB {
|
||||
webauthn_challenges: {
|
||||
challenge: string;
|
||||
user_id: number | null;
|
||||
type: string;
|
||||
expires_at: number;
|
||||
};
|
||||
}
|
||||
|
||||
/** The single-use, TTL'd WebAuthn ceremony challenge store — PK1–PK3. */
|
||||
export class WebauthnChallengesRepository extends EntityRepository<WebauthnChallenges> {
|
||||
/**
|
||||
* `DELETE FROM webauthn_challenges WHERE expires_at < ?` — best-effort
|
||||
* sweep run before minting a new challenge (no cron for this table; see
|
||||
* the inventory's surprise #9).
|
||||
*/
|
||||
async purgeExpired(now: number): Promise<void> {
|
||||
await this.nativeDelete({ expires_at: { $lt: now } });
|
||||
}
|
||||
|
||||
/**
|
||||
* `INSERT INTO webauthn_challenges (challenge, user_id, type, expires_at)
|
||||
* VALUES (?, ?, ?, ?)`. `EntityRepository.insert()`, not `create()` +
|
||||
* `persist().flush()` — a native insert with no identity-map/UnitOfWork
|
||||
* side effects (see `WebauthnCredentialsRepository.insertCredential`'s
|
||||
* docstring for why `persist().flush()` is the hazard here: it flushes
|
||||
* the entire request's UnitOfWork, which can write back a stale
|
||||
* identity-mapped entity from an unrelated earlier read in the same
|
||||
* request).
|
||||
*/
|
||||
async insertChallenge(row: NewChallengeRow): Promise<void> {
|
||||
await this.insert({
|
||||
challenge: row.challenge,
|
||||
user: row.user_id,
|
||||
type: row.type,
|
||||
expires_at: row.expires_at,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* `DELETE FROM webauthn_challenges WHERE challenge = ? AND type = ? AND
|
||||
* expires_at > ? RETURNING user_id` — the anti-replay primitive: it runs
|
||||
* BEFORE any async verification, so a concurrent double-submit of the
|
||||
* same assertion can spend the row exactly once.
|
||||
*
|
||||
* Built on `em.getKysely()` (D4's T6 tier), not the QueryBuilder's own
|
||||
* `.returning()` (the InviteTokensRepository.incrementUsedCount shape):
|
||||
* verified empirically against the installed MikroORM 7.2.1 that the
|
||||
* QueryBuilder's `processReturningStatement` (`@mikro-orm/sql`'s
|
||||
* `QueryBuilder.js`) only ever honours an explicit `.returning()` hint
|
||||
* for `INSERT`/`UPDATE` — a bare `DELETE` has no `data` in its internal
|
||||
* state, and the very first branch of that method (`if (!data &&
|
||||
* !this.#state.insertSubQuery) return;`) exits before the "always
|
||||
* respect explicit returning hint" branch below it is ever reached. A
|
||||
* `delete().returning([...]).execute('run')` therefore silently drops
|
||||
* the RETURNING clause: it deletes the row (`affectedRows: 1`) but
|
||||
* `result.row`/`result.rows` come back empty every time — this is NOT
|
||||
* the `update().returning()` shape Task 0 proved; it's a different,
|
||||
* narrower gap the docs don't call out. `em.getKysely()` is bound to the
|
||||
* ambient transaction the same way the QueryBuilder is (see its own
|
||||
* doc comment — inside `em.transactional`/`uow.transactional` it reuses
|
||||
* the open transaction's connection), so D6's single-connection-
|
||||
* serialises-writers guarantee still holds: no SELECT→await→DELETE
|
||||
* window for either caller to lose, proven by `WEBAUTHN-CHAL-REPO`'s
|
||||
* concurrent-claim test asserting exactly one non-null winner.
|
||||
*
|
||||
* `.returning(['user_id'])` only (not `.selectAll()`/`returningAll()`),
|
||||
* matching the legacy statement's own narrower `RETURNING user_id`.
|
||||
*/
|
||||
async claimChallenge(challenge: string, type: 'registration' | 'authentication', now: number): Promise<{ user_id: number | null } | null> {
|
||||
const row = await this.getEntityManager()
|
||||
.getKysely<WebauthnChallengesKyselyDB>()
|
||||
.deleteFrom('webauthn_challenges')
|
||||
.where('challenge', '=', challenge)
|
||||
.where('type', '=', type)
|
||||
.where('expires_at', '>', now)
|
||||
.returning(['user_id'])
|
||||
.executeTakeFirst();
|
||||
return row ?? null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,28 +1,260 @@
|
||||
import type { WebauthnCredentials } from '../entities/WebauthnCredentials.entity';
|
||||
import { toRow, type AssertRowKeys } from './_shared/rows';
|
||||
import { currentTimestamp } from '../dialect/sql-functions';
|
||||
import { EntityRepository } from '@mikro-orm/sql';
|
||||
|
||||
/**
|
||||
* Plan 3b Task 1 reserves this class (empty stub since Plan 3a) and implements
|
||||
* exactly the one method the AUTH cluster's Task 1 needs NOW: `mfa-policy.guard.ts`
|
||||
* (MP3) and, cross-service, `AuthService.updateAppSettings` (AU24, Task 5) both
|
||||
* read "does this user own at least one passkey" — a passkey satisfies the
|
||||
* `require_mfa` policy the same way TOTP does. Task 3 (`PasskeyService`)
|
||||
* extends this class with the rest of the WebAuthn ceremony state
|
||||
* (`listByUser`, `create`, `findByCredentialId`, `updateCounterAndLastUsed`,
|
||||
* `renameOwned`, `deleteOwned`, `deleteAllForUser`) — this method's name and
|
||||
* shape are chosen so nothing here needs to change when that lands.
|
||||
* A full `webauthn_credentials` row (`SELECT * ...`), as PK9 (`passkeyLoginVerify`)
|
||||
* needs it — `public_key` stays a `Buffer` end to end: `BlobType` (the entity's
|
||||
* `p.blob()` column type) has no `toJSON` override, so `EntityTransformer`'s
|
||||
* default (return the value unchanged) applies and `toRow`'s `wrap().toObject()`
|
||||
* never touches the bytes.
|
||||
*/
|
||||
export interface WebauthnCredentialRow {
|
||||
id: number;
|
||||
user_id: number;
|
||||
credential_id: string;
|
||||
public_key: Buffer;
|
||||
counter: number;
|
||||
transports: string | null;
|
||||
device_type: string | null;
|
||||
backed_up: number;
|
||||
name: string | null;
|
||||
aaguid: string | null;
|
||||
created_at: string | null;
|
||||
last_used_at: string | null;
|
||||
}
|
||||
|
||||
const _webauthnCredentialRowKeys: AssertRowKeys<WebauthnCredentialRow, WebauthnCredentials> = true;
|
||||
|
||||
/** `PasskeyService.passkeyRegisterVerify` (PK7) — only verifier-vouched fields. */
|
||||
export interface NewCredentialRow {
|
||||
user_id: number;
|
||||
credential_id: string;
|
||||
public_key: Buffer;
|
||||
counter: number;
|
||||
transports: string | null;
|
||||
device_type: string | null;
|
||||
backed_up: number;
|
||||
name: string | null;
|
||||
aaguid: string | null;
|
||||
}
|
||||
|
||||
/** PK5's `excludeCredentials` shape and PK8/PK13's shared management-panel shape. */
|
||||
export interface CredentialTransportsRow {
|
||||
credential_id: string;
|
||||
transports: string | null;
|
||||
}
|
||||
|
||||
/** The column set PK8 (post-registration re-select) and PK13 (`listPasskeys`) both project. */
|
||||
export interface PasskeyPanelRow {
|
||||
id: number;
|
||||
name: string | null;
|
||||
device_type: string | null;
|
||||
backed_up: number;
|
||||
created_at: string | null;
|
||||
last_used_at: string | null;
|
||||
}
|
||||
|
||||
const PANEL_FIELDS = ['id', 'name', 'device_type', 'backed_up', 'created_at', 'last_used_at'] as const;
|
||||
|
||||
function toPanelRow(row: { id: number; name?: string | null; device_type?: string | null; backed_up: number; created_at?: string | null; last_used_at?: string | null }): PasskeyPanelRow {
|
||||
return {
|
||||
id: row.id,
|
||||
name: row.name ?? null,
|
||||
device_type: row.device_type ?? null,
|
||||
backed_up: row.backed_up,
|
||||
created_at: row.created_at ?? null,
|
||||
last_used_at: row.last_used_at ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Plan 3b Task 1 reserved this class and implemented `hasAny` (MP3/AU24 —
|
||||
* shared with `mfa-policy.guard.ts` and `AuthService.updateAppSettings`).
|
||||
* Task 3 (`PasskeyService`) adds everything else: the full WebAuthn
|
||||
* credential-store surface (PK5–PK18, minus PK6's dup check which the
|
||||
* service still gates the same insert with, race-proofed by the surrounding
|
||||
* `uow.transactional`). `hasAny` is untouched — nothing here changes its
|
||||
* name or shape.
|
||||
*/
|
||||
export class WebauthnCredentialsRepository extends EntityRepository<WebauthnCredentials> {
|
||||
/**
|
||||
* `SELECT 1 FROM webauthn_credentials WHERE user_id = ? LIMIT 1` (MP3/AU24
|
||||
* — identical statement, two callers). `count` with an implicit `LIMIT 1`
|
||||
* via `findOne` mirrors the legacy existence probe without pulling a row;
|
||||
* MikroORM's `count()` has no built-in short-circuit, so `findOne` with a
|
||||
* narrow `fields` selection is the closer match to "does one exist" than
|
||||
* `count() > 0`, which would still scan/aggregate every matching row.
|
||||
* — identical statement, two callers). `findOne` with a narrow `fields`
|
||||
* selection mirrors the legacy existence probe without pulling a full
|
||||
* row; MikroORM's `count()` has no built-in short-circuit, so this is the
|
||||
* closer match to "does one exist" than `count() > 0`, which would still
|
||||
* scan/aggregate every matching row.
|
||||
*
|
||||
* `disableIdentityMap: true` (every row-out read in this file): this
|
||||
* method's caller (`mfa-policy.guard.ts`, `AuthService.updateAppSettings`)
|
||||
* can run in the same request as other `webauthn_credentials`/`users`
|
||||
* reads and writes; a `findOne` that joins the identity map (even for a
|
||||
* `fields: ['id']` existence probe) registers a managed entity whose
|
||||
* unselected columns hold whatever the query returned as undefined/stale
|
||||
* — if that entity is later merged with a broader projection AND a
|
||||
* sibling `nativeUpdate` changes a column out of band in the same
|
||||
* transaction, the closing `flush()` can write the identity-mapped
|
||||
* entity's stale in-memory snapshot back over the `nativeUpdate`'s write
|
||||
* (reproduced live: `PUT /api/auth/me/settings` returning 200 while
|
||||
* discarding the change). `disableIdentityMap: true` returns an isolated,
|
||||
* unmanaged entity instead — nothing this method reads is ever merged
|
||||
* into the request's identity map, so it can never be flushed.
|
||||
*/
|
||||
async hasAny(userId: number): Promise<boolean> {
|
||||
const row = await this.findOne({ user: userId }, { fields: ['id'] });
|
||||
const row = await this.findOne({ user: userId }, { fields: ['id'], disableIdentityMap: true });
|
||||
return row !== null;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// PK5 — passkeyRegisterOptions' excludeCredentials
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/** `SELECT credential_id, transports FROM webauthn_credentials WHERE user_id = ?` */
|
||||
async listExcludeCredentials(userId: number): Promise<CredentialTransportsRow[]> {
|
||||
const rows = await this.find({ user: userId }, { fields: ['credential_id', 'transports'], disableIdentityMap: true });
|
||||
return rows.map((row) => ({ credential_id: row.credential_id, transports: row.transports ?? null }));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// PK6 — passkeyRegisterVerify's in-transaction duplicate check
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* `SELECT id FROM webauthn_credentials WHERE credential_id = ?` — called
|
||||
* from inside `uow.transactional` alongside `insertCredential` so the
|
||||
* check-then-insert can never race a UNIQUE violation past the service's
|
||||
* `DUPLICATE_CREDENTIAL` sentinel (identity-compared, not string-matched).
|
||||
* `disableIdentityMap: true` per `hasAny`'s docstring above — this read
|
||||
* runs inside the same transaction as PK7's write, so keeping it out of
|
||||
* the identity map matters even more here.
|
||||
*/
|
||||
async existsByCredentialId(credentialId: string): Promise<boolean> {
|
||||
const row = await this.findOne({ credential_id: credentialId }, { fields: ['id'], disableIdentityMap: true });
|
||||
return row !== null;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// PK7 — credential enrollment (same transaction as PK6)
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* `INSERT INTO webauthn_credentials (user_id, credential_id, public_key,
|
||||
* counter, transports, device_type, backed_up, name, aaguid, last_used_at)
|
||||
* VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NULL)` — `last_used_at` is always
|
||||
* `NULL` on insert (a fresh credential has never been used yet), same as
|
||||
* the legacy statement's literal.
|
||||
*
|
||||
* `EntityRepository.insert()`, not `create()` + `persist().flush()`: a
|
||||
* native insert that commits immediately and has no identity-map/UnitOfWork
|
||||
* side effects, so it can never be part of a stale-snapshot flush the way
|
||||
* a persisted-and-tracked entity could; `persist().flush()` would also
|
||||
* flush the ENTIRE request's UnitOfWork, not just this row, which is
|
||||
* exactly the write-discarding hazard `hasAny`'s docstring describes.
|
||||
*/
|
||||
async insertCredential(row: NewCredentialRow): Promise<void> {
|
||||
await this.insert({
|
||||
user: row.user_id,
|
||||
credential_id: row.credential_id,
|
||||
public_key: row.public_key,
|
||||
counter: row.counter,
|
||||
transports: row.transports,
|
||||
device_type: row.device_type,
|
||||
backed_up: row.backed_up,
|
||||
name: row.name,
|
||||
aaguid: row.aaguid,
|
||||
last_used_at: null,
|
||||
});
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// PK8 — post-registration re-select (after the transaction resolves)
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* `SELECT id, name, device_type, backed_up, created_at, last_used_at
|
||||
* FROM webauthn_credentials WHERE credential_id = ?` — a DIFFERENT
|
||||
* column set from PK9's full-row read below, so a separate method (D4:
|
||||
* mirror each statement's own projection, never one full-row read the
|
||||
* caller trims down).
|
||||
*/
|
||||
async findCreatedCredential(credentialId: string): Promise<PasskeyPanelRow | null> {
|
||||
const row = await this.findOne({ credential_id: credentialId }, { fields: [...PANEL_FIELDS], disableIdentityMap: true });
|
||||
return row ? toPanelRow(row) : null;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// PK9 — login-verify ceremony input (full row, Buffer public_key)
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* `SELECT * FROM webauthn_credentials WHERE credential_id = ?`.
|
||||
* `disableIdentityMap: true`, not `{ refresh: true }` — this is not a
|
||||
* primary-key filter (`credential_id` is a unique column, not `id`), and
|
||||
* per `hasAny`'s docstring, a plain `refresh: true` re-snapshot risks a
|
||||
* stale-write-back on flush if this same entity is already (or later)
|
||||
* identity-mapped under a different projection within the same request —
|
||||
* PK11's `updateCounterAndLastUsed` runs a `nativeUpdate` on this exact
|
||||
* row moments later, in the same request.
|
||||
*/
|
||||
async findByCredentialId(credentialId: string): Promise<WebauthnCredentialRow | null> {
|
||||
const credential = await this.findOne({ credential_id: credentialId }, { disableIdentityMap: true });
|
||||
return credential ? (toRow(credential) as WebauthnCredentialRow) : null;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// PK11 — counter/last-used bookkeeping (same transaction as UsersRepository.touchLastLogin)
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/** `UPDATE webauthn_credentials SET counter = ?, last_used_at = CURRENT_TIMESTAMP WHERE id = ?` */
|
||||
async updateCounterAndLastUsed(id: number, counter: number): Promise<void> {
|
||||
const platform = this.getEntityManager().getPlatform();
|
||||
await this.nativeUpdate({ id }, { counter, last_used_at: currentTimestamp(platform) });
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// PK13 — settings-panel list
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* `SELECT id, name, device_type, backed_up, created_at, last_used_at
|
||||
* FROM webauthn_credentials WHERE user_id = ? ORDER BY created_at DESC`
|
||||
*/
|
||||
async listForPanel(userId: number): Promise<PasskeyPanelRow[]> {
|
||||
const rows = await this.find({ user: userId }, { fields: [...PANEL_FIELDS], orderBy: { created_at: 'desc' }, disableIdentityMap: true });
|
||||
return rows.map((row) => toPanelRow(row));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// PK14 — rename (ownership in the WHERE)
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* `UPDATE webauthn_credentials SET name = ? WHERE id = ? AND user_id = ?`
|
||||
* — ownership enforced in the WHERE, not a separate check; returns the
|
||||
* affected-row count so `renamePasskey` keeps its 404-never-403 behaviour
|
||||
* (a foreign or unknown id both yield 0, indistinguishable by design).
|
||||
*/
|
||||
async renameOwned(id: number, userId: number, name: string): Promise<number> {
|
||||
return this.nativeUpdate({ id, user: userId }, { name });
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// PK16 — delete (ownership in the WHERE)
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/** `DELETE FROM webauthn_credentials WHERE id = ? AND user_id = ?` */
|
||||
async deleteOwned(id: number, userId: number): Promise<number> {
|
||||
return this.nativeDelete({ id, user: userId });
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// PK18 — admin bulk-clear
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/** `DELETE FROM webauthn_credentials WHERE user_id = ?` — returns the deleted count for `adminResetPasskeys`' response. */
|
||||
async deleteAllForUser(userId: number): Promise<number> {
|
||||
return this.nativeDelete({ user: userId });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,9 @@ import { Module } from '@nestjs/common';
|
||||
import { MikroOrmModule } from '@mikro-orm/nestjs';
|
||||
import { AppSettings } from '../../db/entities/AppSettings.entity';
|
||||
import { Users } from '../../db/entities/Users.entity';
|
||||
import { WebauthnCredentials } from '../../db/entities/WebauthnCredentials.entity';
|
||||
import { WebauthnChallenges } from '../../db/entities/WebauthnChallenges.entity';
|
||||
import { InviteTokens } from '../../db/entities/InviteTokens.entity';
|
||||
import { TokensModule } from '../tokens/tokens.module';
|
||||
import { AuthPublicController } from './auth-public.controller';
|
||||
import { AuthController } from './auth.controller';
|
||||
@@ -69,9 +72,12 @@ import { AllowedFileTypesModule } from '../files/allowed-file-types.module';
|
||||
// AppSettings/Users: AuthService/UserProfileService each pass their own
|
||||
// AppSettingsRepository/UsersRepository to instance-api-keys.ts's
|
||||
// resolveApiKey/readInstanceApiKey/writeInstanceApiKey now (Plan 3a Task
|
||||
// 5) — everything else either service still reads/writes is raw SQL
|
||||
// through DatabaseService (auth's own conversion is a later plan).
|
||||
MikroOrmModule.forFeature([AppSettings, Users])],
|
||||
// 5). WebauthnCredentials/WebauthnChallenges: PasskeyService (Plan 3b
|
||||
// Task 3). InviteTokens: RegistrationInvitesService (Plan 3b Tasks 0/3)
|
||||
// — everything else either service still reads/writes is raw SQL
|
||||
// through DatabaseService (AuthService's own conversion is a later
|
||||
// task).
|
||||
MikroOrmModule.forFeature([AppSettings, Users, WebauthnCredentials, WebauthnChallenges, InviteTokens])],
|
||||
controllers: [AuthPublicController, AuthController, PasskeyController],
|
||||
providers: [AuthService, UserProfileService, RegistrationInvitesService, PasskeyService, UserCleanupService, WebauthnConfigService, AuthMcp],
|
||||
exports: [AuthService, RegistrationInvitesService, PasskeyService, UserCleanupService],
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { InjectRepository } from '@mikro-orm/nestjs';
|
||||
import bcrypt from 'bcryptjs';
|
||||
import {
|
||||
generateRegistrationOptions,
|
||||
@@ -11,8 +12,13 @@ import { WebauthnConfigService, originWithinRpScope, type WebauthnConfig } from
|
||||
import { avatarUrl } from '../common/avatarUrl';
|
||||
import { stripUserForClient } from './auth.helpers';
|
||||
import { AuthService } from './auth.service';
|
||||
import { DatabaseService } from '../database/database.service';
|
||||
import { UnitOfWork } from '../database/unit-of-work';
|
||||
import { WebauthnCredentials } from '../../db/entities/WebauthnCredentials.entity';
|
||||
import type { WebauthnCredentialsRepository } from '../../db/repositories/WebauthnCredentials.repository';
|
||||
import { WebauthnChallenges } from '../../db/entities/WebauthnChallenges.entity';
|
||||
import type { WebauthnChallengesRepository } from '../../db/repositories/WebauthnChallenges.repository';
|
||||
import { Users } from '../../db/entities/Users.entity';
|
||||
import type { UsersRepository } from '../../db/repositories/Users.repository';
|
||||
import type { User } from '../../types';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -38,21 +44,6 @@ const AUTH_FAILED = { error: 'Authentication failed', status: 401 } as const;
|
||||
// insert-failure 400 without string-matching SQLite errors.
|
||||
const DUPLICATE_CREDENTIAL = new Error('duplicate credential');
|
||||
|
||||
interface CredentialRow {
|
||||
id: number;
|
||||
user_id: number;
|
||||
credential_id: string;
|
||||
public_key: Buffer;
|
||||
counter: number;
|
||||
transports: string | null;
|
||||
device_type: string | null;
|
||||
backed_up: number;
|
||||
name: string | null;
|
||||
aaguid: string | null;
|
||||
created_at: string;
|
||||
last_used_at: string | null;
|
||||
}
|
||||
|
||||
function clientDataFromResponse(resp: unknown): { challenge?: unknown; origin?: unknown } | null {
|
||||
try {
|
||||
const cdj = (resp as { response?: { clientDataJSON?: unknown } })?.response?.clientDataJSON;
|
||||
@@ -105,10 +96,12 @@ export class PasskeyService {
|
||||
private readonly logger = new Logger(PasskeyService.name);
|
||||
|
||||
constructor(
|
||||
private readonly db: DatabaseService,
|
||||
private readonly auth: AuthService,
|
||||
private readonly webauthn: WebauthnConfigService,
|
||||
private readonly uow: UnitOfWork,
|
||||
@InjectRepository(WebauthnCredentials) private readonly webauthnCredentials: WebauthnCredentialsRepository,
|
||||
@InjectRepository(WebauthnChallenges) private readonly webauthnChallenges: WebauthnChallengesRepository,
|
||||
@InjectRepository(Users) private readonly users: UsersRepository,
|
||||
) {}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
@@ -116,14 +109,11 @@ export class PasskeyService {
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
private async purgeExpiredChallenges(now: number): Promise<void> {
|
||||
this.db.run('DELETE FROM webauthn_challenges WHERE expires_at < ?', now);
|
||||
await this.webauthnChallenges.purgeExpired(now);
|
||||
}
|
||||
|
||||
private async storeChallenge(challenge: string, userId: number | null, type: 'registration' | 'authentication', now: number): Promise<void> {
|
||||
this.db.run(
|
||||
'INSERT INTO webauthn_challenges (challenge, user_id, type, expires_at) VALUES (?, ?, ?, ?)',
|
||||
challenge, userId, type, now + CHALLENGE_TTL_MS,
|
||||
);
|
||||
await this.webauthnChallenges.insertChallenge({ challenge, user_id: userId, type, expires_at: now + CHALLENGE_TTL_MS });
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -133,11 +123,7 @@ export class PasskeyService {
|
||||
* twice (the replay window a SELECT→await→DELETE ordering would open).
|
||||
*/
|
||||
private async claimChallenge(challenge: string, type: 'registration' | 'authentication', now: number): Promise<{ user_id: number | null } | null> {
|
||||
const row = this.db.get<{ user_id: number | null }>(
|
||||
'DELETE FROM webauthn_challenges WHERE challenge = ? AND type = ? AND expires_at > ? RETURNING user_id',
|
||||
challenge, type, now,
|
||||
);
|
||||
return row ?? null;
|
||||
return this.webauthnChallenges.claimChallenge(challenge, type, now);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
@@ -209,7 +195,7 @@ export class PasskeyService {
|
||||
if (!cfg) return { ...NOT_CONFIGURED };
|
||||
if (this.originCannotVerify(cfg, requestOrigin)) return { ...NOT_CONFIGURED };
|
||||
|
||||
const user = this.db.get<User>('SELECT * FROM users WHERE id = ?', userId);
|
||||
const user = await this.users.findById(userId);
|
||||
if (!user) return { error: 'User not found', status: 404 };
|
||||
|
||||
// Re-authentication: a hijacked session must not be able to silently plant an
|
||||
@@ -219,9 +205,7 @@ export class PasskeyService {
|
||||
return { error: 'Incorrect password', status: 401 };
|
||||
}
|
||||
|
||||
const existing = this.db.all<{ credential_id: string; transports: string | null }>(
|
||||
'SELECT credential_id, transports FROM webauthn_credentials WHERE user_id = ?', userId,
|
||||
);
|
||||
const existing = await this.webauthnCredentials.listExcludeCredentials(userId);
|
||||
|
||||
const now = Date.now();
|
||||
await this.purgeExpiredChallenges(now);
|
||||
@@ -294,23 +278,20 @@ export class PasskeyService {
|
||||
// between them; the sentinel keeps the legacy 409-vs-400 split intact.
|
||||
try {
|
||||
await this.uow.transactional(async () => {
|
||||
if (this.db.get('SELECT id FROM webauthn_credentials WHERE credential_id = ?', credential.id)) {
|
||||
if (await this.webauthnCredentials.existsByCredentialId(credential.id)) {
|
||||
throw DUPLICATE_CREDENTIAL;
|
||||
}
|
||||
this.db.run(
|
||||
`INSERT INTO webauthn_credentials
|
||||
(user_id, credential_id, public_key, counter, transports, device_type, backed_up, name, aaguid, last_used_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NULL)`,
|
||||
userId,
|
||||
credential.id,
|
||||
Buffer.from(credential.publicKey),
|
||||
credential.counter ?? 0,
|
||||
credential.transports ? JSON.stringify(credential.transports) : null,
|
||||
credentialDeviceType ?? null,
|
||||
credentialBackedUp ? 1 : 0,
|
||||
await this.webauthnCredentials.insertCredential({
|
||||
user_id: userId,
|
||||
credential_id: credential.id,
|
||||
public_key: Buffer.from(credential.publicKey),
|
||||
counter: credential.counter ?? 0,
|
||||
transports: credential.transports ? JSON.stringify(credential.transports) : null,
|
||||
device_type: credentialDeviceType ?? null,
|
||||
backed_up: credentialBackedUp ? 1 : 0,
|
||||
name,
|
||||
aaguid ?? null,
|
||||
);
|
||||
aaguid: aaguid ?? null,
|
||||
});
|
||||
});
|
||||
} catch (err) {
|
||||
if (err === DUPLICATE_CREDENTIAL) {
|
||||
@@ -319,10 +300,7 @@ export class PasskeyService {
|
||||
return { error: 'Could not register this passkey.', status: 400 };
|
||||
}
|
||||
|
||||
const created = this.db.get<{ backed_up: number } & Record<string, unknown>>(
|
||||
'SELECT id, name, device_type, backed_up, created_at, last_used_at FROM webauthn_credentials WHERE credential_id = ?',
|
||||
credential.id,
|
||||
) as { backed_up: number } & Record<string, unknown>;
|
||||
const created = (await this.webauthnCredentials.findCreatedCredential(credential.id))!;
|
||||
return { success: true, credential: { ...created, backed_up: created.backed_up === 1 } };
|
||||
}
|
||||
|
||||
@@ -377,7 +355,7 @@ export class PasskeyService {
|
||||
const credId = (resp as { id?: unknown; rawId?: unknown }).id ?? (resp as { rawId?: unknown }).rawId;
|
||||
if (typeof credId !== 'string') return { ...AUTH_FAILED };
|
||||
|
||||
const cred = this.db.get<CredentialRow>('SELECT * FROM webauthn_credentials WHERE credential_id = ?', credId);
|
||||
const cred = await this.webauthnCredentials.findByCredentialId(credId);
|
||||
if (!cred) return { ...AUTH_FAILED };
|
||||
|
||||
const expectedOrigin = this.expectedOrigins(cfg, resp);
|
||||
@@ -414,20 +392,20 @@ export class PasskeyService {
|
||||
return { ...AUTH_FAILED, auditUserId: cred.user_id, auditAction: 'user.passkey_clone_suspected' };
|
||||
}
|
||||
|
||||
const user = this.db.get<User>('SELECT * FROM users WHERE id = ?', cred.user_id);
|
||||
const user = await this.users.findById(cred.user_id);
|
||||
if (!user) return { ...AUTH_FAILED };
|
||||
|
||||
// Persist the new counter + last-used and bump login bookkeeping atomically.
|
||||
await this.uow.transactional(async () => {
|
||||
this.db.run('UPDATE webauthn_credentials SET counter = ?, last_used_at = CURRENT_TIMESTAMP WHERE id = ?', newCounter, cred.id);
|
||||
this.db.run('UPDATE users SET last_login = CURRENT_TIMESTAMP, login_count = login_count + 1 WHERE id = ?', user.id);
|
||||
await this.webauthnCredentials.updateCounterAndLastUsed(cred.id, newCounter);
|
||||
await this.users.touchLastLogin(user.id);
|
||||
});
|
||||
|
||||
// A user-verified passkey is phishing-resistant and inherently two-factor
|
||||
// (device possession + biometric/PIN), so it mints the real session directly
|
||||
// — the SAME path as password and OIDC login (no new token shape).
|
||||
const token = await this.auth.generateToken(user);
|
||||
const userSafe = stripUserForClient(user) as Record<string, unknown>;
|
||||
const userSafe = stripUserForClient(user as unknown as User) as Record<string, unknown>;
|
||||
return { token, user: { ...userSafe, avatar_url: avatarUrl(user) }, auditUserId: Number(user.id) };
|
||||
}
|
||||
|
||||
@@ -436,10 +414,7 @@ export class PasskeyService {
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
async listPasskeys(userId: number): Promise<Array<Record<string, unknown>>> {
|
||||
const rows = this.db.all<{ backed_up: number } & Record<string, unknown>>(
|
||||
'SELECT id, name, device_type, backed_up, created_at, last_used_at FROM webauthn_credentials WHERE user_id = ? ORDER BY created_at DESC',
|
||||
userId,
|
||||
);
|
||||
const rows = await this.webauthnCredentials.listForPanel(userId);
|
||||
return rows.map((r) => ({ ...r, backed_up: r.backed_up === 1 }));
|
||||
}
|
||||
|
||||
@@ -447,8 +422,8 @@ export class PasskeyService {
|
||||
const cleanName = sanitizeName(name);
|
||||
if (!cleanName) return { error: 'Name is required', status: 400 };
|
||||
// Ownership enforced in SQL (404 on miss, never a 403 that leaks existence).
|
||||
const result = this.db.run('UPDATE webauthn_credentials SET name = ? WHERE id = ? AND user_id = ?', cleanName, Number(id), userId);
|
||||
if (result.changes === 0) return { error: 'Passkey not found', status: 404 };
|
||||
const changes = await this.webauthnCredentials.renameOwned(Number(id), userId, cleanName);
|
||||
if (changes === 0) return { error: 'Passkey not found', status: 404 };
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
@@ -461,20 +436,20 @@ export class PasskeyService {
|
||||
// strip the victim's passkeys). Deleting is always allowed because every
|
||||
// account keeps a usable password as recovery fallback — losing all passkeys
|
||||
// can never lock anyone out.
|
||||
const user = this.db.get<{ password_hash: string }>('SELECT password_hash FROM users WHERE id = ?', userId);
|
||||
if (!user || !user.password_hash || !password || !bcrypt.compareSync(password, user.password_hash)) {
|
||||
const passwordHash = await this.users.getPasswordHash(userId);
|
||||
if (!passwordHash || !password || !bcrypt.compareSync(password, passwordHash)) {
|
||||
return { error: 'Incorrect password', status: 401 };
|
||||
}
|
||||
const result = this.db.run('DELETE FROM webauthn_credentials WHERE id = ? AND user_id = ?', Number(id), userId);
|
||||
if (result.changes === 0) return { error: 'Passkey not found', status: 404 };
|
||||
const changes = await this.webauthnCredentials.deleteOwned(Number(id), userId);
|
||||
if (changes === 0) return { error: 'Passkey not found', status: 404 };
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
/** Admin: clear all of a user's passkeys (e.g. on suspected compromise). */
|
||||
async adminResetPasskeys(targetUserId: number): Promise<{ error?: string; status?: number; success?: boolean; deleted?: number; email?: string }> {
|
||||
const target = this.db.get<{ id: number; email: string }>('SELECT id, email FROM users WHERE id = ?', targetUserId);
|
||||
const target = await this.users.findIdAndEmail(targetUserId);
|
||||
if (!target) return { error: 'User not found', status: 404 };
|
||||
const result = this.db.run('DELETE FROM webauthn_credentials WHERE user_id = ?', targetUserId);
|
||||
return { success: true, deleted: result.changes, email: target.email };
|
||||
const deleted = await this.webauthnCredentials.deleteAllForUser(targetUserId);
|
||||
return { success: true, deleted, email: target.email };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@mikro-orm/nestjs';
|
||||
import crypto from 'crypto';
|
||||
import { DatabaseService } from '../database/database.service';
|
||||
import { InviteTokens } from '../../db/entities/InviteTokens.entity';
|
||||
import type { InviteTokensRepository } from '../../db/repositories/InviteTokens.repository';
|
||||
|
||||
/**
|
||||
* Registration invites: the tokens an admin hands out so someone can create an
|
||||
@@ -16,22 +19,32 @@ import { DatabaseService } from '../database/database.service';
|
||||
* because the management routes are under /api/admin. Those routes keep their
|
||||
* paths and their guards; AdminController now injects this instead of carrying
|
||||
* another domain's SQL.
|
||||
*
|
||||
* Plan 3b Task 3: `invite_tokens` reads/writes go through
|
||||
* `InviteTokensRepository` (RI1, RI4–RI7). RI2/RI3 (`trips`) stay on
|
||||
* `DatabaseService` — `trips` is `nest/trips`' table, not this domain's, and
|
||||
* Plan 3c is the one that builds a `TripsRepository`; building a shim one
|
||||
* here would be exactly the "manual synchronization" this migration exists
|
||||
* to remove (same carve-out shape as Plan 3a's `addons.service.ts`
|
||||
* `listTripsForInvite`/`createInvite`'s trip-binding validation, documented
|
||||
* at the inventory's §6).
|
||||
*/
|
||||
@Injectable()
|
||||
export class RegistrationInvitesService {
|
||||
constructor(private readonly db: DatabaseService) {}
|
||||
constructor(
|
||||
private readonly db: DatabaseService,
|
||||
@InjectRepository(InviteTokens) private readonly inviteTokens: InviteTokensRepository,
|
||||
) {}
|
||||
|
||||
/** RI1 — `InviteTokensRepository.listWithCreatorAndTrip()`'s joined projection. */
|
||||
async listInvites() {
|
||||
return this.db.all(`
|
||||
SELECT i.*, u.username as created_by_name, t.title as trip_title
|
||||
FROM invite_tokens i
|
||||
JOIN users u ON i.created_by = u.id
|
||||
LEFT JOIN trips t ON i.trip_id = t.id
|
||||
ORDER BY i.created_at DESC
|
||||
`);
|
||||
return this.inviteTokens.listWithCreatorAndTrip();
|
||||
}
|
||||
|
||||
/** Trips an admin can bind an invite to — id + title only, for the picker (#1402). */
|
||||
/**
|
||||
* Trips an admin can bind an invite to — id + title only, for the picker
|
||||
* (#1402). RI2 — stays raw on `DatabaseService` (see the class docstring).
|
||||
*/
|
||||
async listTripsForInvite() {
|
||||
return this.db.all('SELECT id, title FROM trips ORDER BY title COLLATE NOCASE ASC');
|
||||
}
|
||||
@@ -49,6 +62,7 @@ export class RegistrationInvitesService {
|
||||
|
||||
// Optional trip binding: only persist a trip that actually exists, so a stale
|
||||
// or forged id can never bind (and never auto-adds anyone on registration).
|
||||
// RI3 — stays raw on `DatabaseService` (see the class docstring).
|
||||
let tripId: number | null = null;
|
||||
if (data.trip_id != null && String(data.trip_id).trim() !== '') {
|
||||
const parsed = Number.parseInt(String(data.trip_id));
|
||||
@@ -60,27 +74,29 @@ export class RegistrationInvitesService {
|
||||
tripId = parsed;
|
||||
}
|
||||
|
||||
const ins = this.db.run(
|
||||
'INSERT INTO invite_tokens (token, max_uses, expires_at, created_by, trip_id) VALUES (?, ?, ?, ?, ?)',
|
||||
token, uses, expiresAt, createdBy, tripId,
|
||||
);
|
||||
// RI4: the write. RI5: the same joined re-select RI1 projects, filtered
|
||||
// to the new row — `insertInvite`'s column set already matches this
|
||||
// INSERT exactly (Task 0).
|
||||
const created = await this.inviteTokens.insertInvite({ token, max_uses: uses, expires_at: expiresAt, created_by: createdBy, trip_id: tripId });
|
||||
const invite = await this.inviteTokens.findWithCreatorAndTrip(created.id);
|
||||
|
||||
const inviteId = Number(ins.lastInsertRowid);
|
||||
const invite = this.db.get(`
|
||||
SELECT i.*, u.username as created_by_name, t.title as trip_title
|
||||
FROM invite_tokens i
|
||||
JOIN users u ON i.created_by = u.id
|
||||
LEFT JOIN trips t ON i.trip_id = t.id
|
||||
WHERE i.id = ?
|
||||
`, inviteId);
|
||||
|
||||
return { invite, inviteId, uses, expiresInDays: data.expires_in_days ?? null, tripId };
|
||||
return { invite, inviteId: created.id, uses, expiresInDays: data.expires_in_days ?? null, tripId };
|
||||
}
|
||||
|
||||
async deleteInvite(id: string) {
|
||||
const invite = this.db.get('SELECT id FROM invite_tokens WHERE id = ?', id);
|
||||
if (!invite) return { error: 'Invite not found', status: 404 };
|
||||
this.db.run('DELETE FROM invite_tokens WHERE id = ?', id);
|
||||
// A non-numeric id can never match an `invite_tokens.id` row — resolved
|
||||
// here rather than handed to the repository as `NaN` (SQLite's driver
|
||||
// has no representation for it as a bind parameter; the legacy raw
|
||||
// statement tolerated a non-numeric string bind and simply matched no
|
||||
// row, so the 404 below reproduces that same observable outcome without
|
||||
// routing an invalid value into the query layer).
|
||||
const numericId = Number(id);
|
||||
// RI6 — the 404 check.
|
||||
if (!Number.isInteger(numericId) || (await this.inviteTokens.findIdById(numericId)) === null) {
|
||||
return { error: 'Invite not found', status: 404 };
|
||||
}
|
||||
// RI7.
|
||||
await this.inviteTokens.deleteById(numericId);
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user