auth/api_token_queries.ts view source
ApiTokenQueryDeps Extended deps for query_validate_api_token which needs a logger.
inheritance
log
LoggerAPI token query functions for token CRUD and validation.
7 declarations
auth/api_token_queries.ts view source
ApiTokenQueryDeps Extended deps for query_validate_api_token which needs a logger.
logLoggerauth/api_token_queries.ts view source
(deps: QueryDeps, account_id: string, max_tokens: number): Promise<number> Enforce a per-account token limit by evicting the oldest tokens.
Race safety: this function must run inside a transaction alongside the
INSERT that created the new token. The caller (POST /tokens/create)
uses the default transaction: true (framework-managed transaction
wrapping in apply_route_specs), ensuring the INSERT + enforce_limit
pair is atomic — concurrent token creation cannot interleave.
depsquery dependencies (must be transaction-scoped)
account_idthe account to enforce the limit for
stringmax_tokensmaximum number of tokens to keep
numberPromise<number> the number of tokens evicted
auth/api_token_queries.ts view source
(deps: QueryDeps, account_id: string): Promise<Omit<ApiToken, "token_hash">[]> List all tokens for an account (does not include hashes).
Columns are enumerated explicitly to exclude token_hash.
Must be updated if the api_token table gains new columns.
depsaccount_idstringPromise<Omit<ApiToken, "token_hash">[]> auth/api_token_queries.ts view source
(deps: QueryDeps, id: string, account_id: string, name: string, token_hash: string, expires_at?: Date | null | undefined): Promise<ApiToken> Store a new API token (the hash, not the raw token).
depsquery dependencies
idthe public token id (e.g. tok_abc123)
stringaccount_idthe owning account
stringnamehuman-readable name
stringtoken_hashblake3 hash of the raw token
stringexpires_at?optional expiration
Date | null | undefinedPromise<ApiToken> the stored token record
auth/api_token_queries.ts view source
(deps: QueryDeps, account_id: string): Promise<number> Revoke all tokens for an account.
depsquery dependencies
account_idthe account whose tokens to revoke
stringPromise<number> the number of tokens revoked
auth/api_token_queries.ts view source
(deps: QueryDeps, id: string, account_id: string): Promise<boolean> Revoke a token only if it belongs to the specified account.
Prevents cross-account token revocation.
depsquery dependencies
idthe public token id
stringaccount_idthe account that must own the token
stringPromise<boolean> true if a token was revoked, false if not found or wrong account
auth/api_token_queries.ts view source
(deps: ApiTokenQueryDeps, raw_token: string, ip: string | undefined, pending_effects: Promise<void>[] | undefined): Promise<ApiToken | undefined> Validate a raw API token and return the token record.
Hashes the token with blake3, looks up the hash, and checks
expiration. Updates last_used_at and last_used_ip on success
(fire-and-forget — errors logged, never thrown).
depsquery dependencies with logger
raw_tokenthe raw API token from the Authorization header
stringipthe client IP address (for audit)
string | undefinedpending_effectsoptional array to register the usage-tracking effect for later awaiting
Promise<void>[] | undefinedPromise<ApiToken | undefined> the token record if valid, or undefined