Initial Commit
This commit is contained in:
73
openspec/archive/2026-05-25-api-auth/proposal.md
Normal file
73
openspec/archive/2026-05-25-api-auth/proposal.md
Normal file
@@ -0,0 +1,73 @@
|
||||
# `@suaveplan/api-auth` — abstract session-resolver contract
|
||||
|
||||
**Package**: `@suaveplan/api-auth`
|
||||
**Status**: PROPOSED
|
||||
**Type**: new-package
|
||||
**Stage**: 1 (Contracts) — see umbrella `openspec/changes/api-plugin-foundation/`
|
||||
|
||||
## Why
|
||||
|
||||
Conflating session resolution and account lifecycle is what makes most plugin systems painful to integrate with third-party auth (design §2.3). `api-auth` is the session-resolver half: `Request → Principal | null`, used on EVERY request, called at step 3 of the per-request pipeline (§6). Concrete adapters (`api-auth-jwt`, `api-auth-anonymous`, `api-auth-bearer-static`, `api-auth-better-auth`, `api-auth-rolled`) plug behind this contract. Multiple resolvers can be chained — host config takes an ordered chain, first match wins (e.g. JWT for users + bearer-static for service callers).
|
||||
|
||||
The contract also covers `SecuritySchemeContribution` — every resolver declares what OpenAPI `securitySchemes` entry it contributes (`api-openapi` reads these — design §7 step 3).
|
||||
|
||||
## What changes
|
||||
|
||||
Net-new package. Contract types + the chain runner + the conformance harness.
|
||||
|
||||
- ADDED: `./resolver` — abstract `SessionResolver`: `resolve(req: Request): Promise<Principal | null>`; `dispose(): Promise<void>`.
|
||||
- ADDED: `./chain` — `SessionResolverChain`: ordered list of resolvers; runs each in order, first non-null wins; returns `null` if all return null.
|
||||
- ADDED: `./security-scheme` — `SecuritySchemeContribution`: `{ name: string, scheme: OpenApiSecurityScheme }`; every resolver MUST contribute one so `api-openapi` can emit the right `securitySchemes` block.
|
||||
- ADDED: `./csrf-policy` — `CsrfPolicy`: `requiresCsrf(scheme): boolean`; cookie-based schemes default ON (per §6), JWT/Bearer default OFF.
|
||||
- ADDED: `./session-adapter` — composite `AuthAdapter` interface: `{ resolver, securityScheme, csrf }`; what concrete adapters export.
|
||||
- ADDED: `./error-codes` — frozen `AUTH_CODES`: `RESOLVER_THREW`, `CHAIN_EMPTY`, `PRINCIPAL_INVALID_SHAPE`, `SECURITY_SCHEME_MISSING`.
|
||||
- ADDED: `./capability-id` — adds `"auth.session"` to the `CapabilityId` union via type-level augmentation.
|
||||
- ADDED: `./testing` — `runAuthSessionConformance(factory)` reusable harness.
|
||||
|
||||
## CapabilityId additions
|
||||
|
||||
This package contributes `"auth.session"` to the `CapabilityId` union. (The provider-side `"auth.provider"` is contributed by `api-auth-provider` — they're intentionally separate capabilities so a plugin can require session resolution without forcing the host to ship account-lifecycle routes.)
|
||||
|
||||
## Out of scope
|
||||
|
||||
- The `Principal` type itself — that's in `@suaveplan/api-types`.
|
||||
- Account lifecycle (login/register/oauth) — that's `api-auth-provider`.
|
||||
- Concrete adapters (`api-auth-jwt`, `api-auth-anonymous`, `api-auth-bearer-static`, `api-auth-better-auth`, `api-auth-rolled`) — Stages 4/6/8.
|
||||
|
||||
## Source mapping
|
||||
|
||||
Genesis-original. Source of design intent: `docs/superpowers/specs/2026-05-13-api-plugin-foundation-design.md`.
|
||||
|
||||
| Requirement | Design doc section |
|
||||
|---|---|
|
||||
| Session-resolver shape | §2.3 (top half) |
|
||||
| Chained resolvers (first match wins) | §2.3 ("ordered chain, first match wins") |
|
||||
| `SecuritySchemeContribution` for OpenAPI | §7 step 3 |
|
||||
| CSRF default ON for cookie auth | §6 ("Cookie-based auth adapters trigger CSRF verification") |
|
||||
| 401 only if non-public AND null | §6 step 3 |
|
||||
|
||||
### Requirements with no source line
|
||||
|
||||
| Requirement | Justification |
|
||||
|---|---|
|
||||
| `runAuthSessionConformance` harness | Stage-5 deliverable (§13.4). |
|
||||
|
||||
## Verification approach
|
||||
|
||||
- `src/resolver/resolver.spec-conformance.test.ts` — `SessionResolver` shape; `dispose` lifecycle.
|
||||
- `src/chain/chain.test.ts` — ordering: A returns null → B runs; A returns Principal → B skipped. Empty chain → `AUTH.CHAIN_EMPTY`.
|
||||
- `src/chain/chain.test.ts` — A throws → `AUTH.RESOLVER_THREW` surfaces; chain does NOT continue to B (fail closed).
|
||||
- `src/security-scheme/security-scheme.test.ts` — resolver MUST contribute a scheme; missing → `AUTH.SECURITY_SCHEME_MISSING`.
|
||||
- `src/csrf-policy/csrf-policy.test.ts` — cookie scheme default ON; JWT/Bearer default OFF.
|
||||
- `src/capability-id/capability-id.spec-conformance.test.ts` — `"auth.session"` is in `CapabilityId`.
|
||||
- `tests/dep-graph.test.ts` — deps: `@suaveplan/api-types`, `@suaveplan/error`, `@suaveplan/types`, `zod`.
|
||||
|
||||
## Migration impact
|
||||
|
||||
None — additive only.
|
||||
|
||||
## Impact
|
||||
|
||||
### Affected dependents
|
||||
|
||||
- (none) — [x] no action needed (reason: net-new; concrete adapters land Stages 4/6/8)
|
||||
132
openspec/archive/2026-05-25-api-auth/specs/api-auth/spec.md
Normal file
132
openspec/archive/2026-05-25-api-auth/specs/api-auth/spec.md
Normal file
@@ -0,0 +1,132 @@
|
||||
<!-- markdownlint-disable MD024 -->
|
||||
|
||||
# Capability Spec Delta: api-auth (api-auth)
|
||||
|
||||
**Package**: `@suaveplan/api-auth`
|
||||
**Status**: PROPOSED
|
||||
|
||||
> Every requirement carries a `**Verified by:**` line.
|
||||
|
||||
## ADDED Requirements
|
||||
|
||||
### Requirement: SessionResolver contract
|
||||
|
||||
The package SHALL export `SessionResolver` interface: `resolve(req: Request): Promise<Principal | null>`, `dispose(): Promise<void>`. A resolver returning `null` means "I don't recognise this request" — NOT "auth failed." Auth failure (401) is the host's decision based on chain outcome + route's `publicRoutes` membership.
|
||||
|
||||
#### Scenario: every method present
|
||||
|
||||
- **WHEN** `expectTypeOf<SessionResolver>()` is checked
|
||||
- **THEN** `resolve` and `dispose` are declared with the documented signatures
|
||||
|
||||
**Verified by:** `src/resolver/resolver.spec-conformance.test.ts::"SessionResolver declares resolve and dispose"`
|
||||
|
||||
---
|
||||
|
||||
### Requirement: `import z from "zod"` everywhere
|
||||
|
||||
Every Zod import MUST use the default form.
|
||||
|
||||
#### Scenario: no named import of z
|
||||
|
||||
- **WHEN** the test greps `import z` from `"zod"`
|
||||
- **THEN** zero matches
|
||||
|
||||
**Verified by:** `tests/zod-import-form.test.ts::"every src file uses default-import z"`
|
||||
|
||||
---
|
||||
|
||||
### Requirement: Resolver chain — first-match-wins
|
||||
|
||||
The package SHALL export `SessionResolverChain(resolvers: readonly SessionResolver[]).resolve(req): Promise<Principal | null>` running each resolver in order; the first non-null result wins; if all return null, the chain returns null. An empty chain MUST throw `AUTH.CHAIN_EMPTY` at construction. By default, a resolver throwing MUST surface `AUTH.RESOLVER_THREW` AND short-circuit the chain (fail-closed). An opt-in `continueOnThrow: true` flag lets the chain try the next resolver; this is documented as escape-hatch only.
|
||||
|
||||
#### Scenario: first non-null wins
|
||||
|
||||
- **WHEN** chain `[null, principalA, principalB]` is run
|
||||
- **THEN** the chain returns `principalA` AND resolver B is NOT called
|
||||
|
||||
#### Scenario: all null returns null
|
||||
|
||||
- **WHEN** every resolver returns null
|
||||
- **THEN** the chain returns null
|
||||
|
||||
#### Scenario: throw is fail-closed by default
|
||||
|
||||
- **WHEN** resolver A throws AND `continueOnThrow` is false (default)
|
||||
- **THEN** the chain throws `AUTH.RESOLVER_THREW` AND resolver B is NOT called
|
||||
|
||||
#### Scenario: empty chain rejected at construction
|
||||
|
||||
- **WHEN** `new SessionResolverChain([])` is called
|
||||
- **THEN** it throws `AUTH.CHAIN_EMPTY`
|
||||
|
||||
**Verified by:** `src/chain/chain.test.ts::"first non-null wins, later resolvers skipped"` AND `src/chain/chain.test.ts::"all null returns null"` AND `src/chain/chain.test.ts::"throw is fail-closed by default"` AND `src/chain/chain.test.ts::"empty chain rejected at construction"`
|
||||
|
||||
---
|
||||
|
||||
### Requirement: SecuritySchemeContribution
|
||||
|
||||
The package SHALL export `SecuritySchemeContribution` with `{ name: string, scheme: OpenApiSecurityScheme }`. Every concrete `SessionResolver` MUST contribute exactly one scheme so `api-openapi` can emit the right `securitySchemes` block. An `AuthAdapter` whose `securityScheme` is absent at registration MUST surface `AUTH.SECURITY_SCHEME_MISSING`.
|
||||
|
||||
#### Scenario: missing scheme rejected
|
||||
|
||||
- **WHEN** an `AuthAdapter` without `securityScheme` is registered
|
||||
- **THEN** registration throws `AUTH.SECURITY_SCHEME_MISSING`
|
||||
|
||||
**Verified by:** `src/security-scheme/security-scheme.test.ts::"registration rejects adapter without securityScheme"`
|
||||
|
||||
---
|
||||
|
||||
### Requirement: CSRF default policy
|
||||
|
||||
The package SHALL export `CsrfPolicy.requiresCsrf(scheme: OpenApiSecurityScheme): boolean`. Cookie-based schemes (`type: "apiKey"` AND `in: "cookie"`) default ON; JWT/Bearer schemes (`type: "http"` AND `scheme: "bearer"`) default OFF. Per-route `csrf: false` opt-out is honoured by the host (this package exposes the default; the host applies the override).
|
||||
|
||||
#### Scenario: cookie scheme defaults to CSRF ON
|
||||
|
||||
- **WHEN** `requiresCsrf({ type: "apiKey", in: "cookie", ... })` is called
|
||||
- **THEN** the result is `true`
|
||||
|
||||
#### Scenario: bearer scheme defaults to CSRF OFF
|
||||
|
||||
- **WHEN** `requiresCsrf({ type: "http", scheme: "bearer" })` is called
|
||||
- **THEN** the result is `false`
|
||||
|
||||
**Verified by:** `src/csrf-policy/csrf-policy.test.ts::"cookie schemes default to CSRF ON"` AND `src/csrf-policy/csrf-policy.test.ts::"bearer schemes default to CSRF OFF"`
|
||||
|
||||
---
|
||||
|
||||
### Requirement: CapabilityId augmentation contributes "auth.session"
|
||||
|
||||
The package SHALL augment the `CapabilityId` union to include `"auth.session"`.
|
||||
|
||||
#### Scenario: auth.session assignable to CapabilityId
|
||||
|
||||
- **WHEN** `expectTypeOf<"auth.session">().toMatchTypeOf<CapabilityId>()` is checked
|
||||
- **THEN** the assertion passes
|
||||
|
||||
**Verified by:** `src/capability-id/capability-id.spec-conformance.test.ts::"auth.session is a valid CapabilityId after augmentation"`
|
||||
|
||||
---
|
||||
|
||||
### Requirement: Error codes registry frozen
|
||||
|
||||
The package SHALL export `AUTH_CODES` containing exactly: `RESOLVER_THREW`, `CHAIN_EMPTY`, `PRINCIPAL_INVALID_SHAPE`, `SECURITY_SCHEME_MISSING`.
|
||||
|
||||
#### Scenario: every code reachable
|
||||
|
||||
- **WHEN** the test enumerates `AUTH_CODES`
|
||||
- **THEN** every code has a production throw site
|
||||
|
||||
**Verified by:** `src/error-codes/error-codes.spec-conformance.test.ts::"every AUTH_CODES entry thrown by production path"`
|
||||
|
||||
---
|
||||
|
||||
### Requirement: Conformance harness for resolvers
|
||||
|
||||
The package SHALL export `runAuthSessionConformance({ factory, label })`. Every concrete resolver in Stages 4/6/8 MUST pass it.
|
||||
|
||||
#### Scenario: harness covers resolve null, resolve principal, dispose
|
||||
|
||||
- **WHEN** the harness is run against a stub resolver
|
||||
- **THEN** test categories exist for each documented behavior
|
||||
|
||||
**Verified by:** `src/testing/conformance.test.ts::"harness covers resolve null, resolve principal, dispose lifecycle"`
|
||||
71
openspec/archive/2026-05-25-api-auth/tasks.md
Normal file
71
openspec/archive/2026-05-25-api-auth/tasks.md
Normal file
@@ -0,0 +1,71 @@
|
||||
# Tasks — `@suaveplan/api-auth`
|
||||
|
||||
> Every checkbox MUST reflect work actually done in this change.
|
||||
|
||||
## 0. Pre-flight (non-negotiable)
|
||||
|
||||
- [x] 0.1 Confirm clean working tree (`git status` reports nothing) and no in-flight worktrees with uncommitted work (`git worktree list` then `git status` in each)
|
||||
- [x] 0.2 If any worktrees exist under `.claude/worktrees/`, account for each one: still-active agent, work-pending-merge, or stale-and-prune. Stale worktrees MUST be removed (`git worktree remove`) before opening a new one — code that has been lost twice on main is the failure mode this guards against.
|
||||
- [x] 0.3 Confirm the package set in `MANIFEST.md` matches `find packages -maxdepth 3 -name package.json` count. Drift here means a prior change archived without regenerating the manifest.
|
||||
|
||||
## 1. Spec authoring
|
||||
|
||||
- [x] 1.1 Read design doc §2.3 (auth two contracts), §6 step 3 (auth in pipeline + CSRF), §7 step 3 (security schemes for OpenAPI)
|
||||
- [x] 1.2 Read `openspec/specs/api-auth/spec.md` (does not exist yet)
|
||||
- [x] 1.3 Read `openspec/AGENTS.md` Sections 13, 14
|
||||
- [x] 1.4 Draft `proposal.md`
|
||||
- [x] 1.5 Source-mapping table
|
||||
- [x] 1.6 Draft `specs/api-auth/spec.md`
|
||||
- [x] 1.7 `**Verified by:**` line on every requirement
|
||||
|
||||
## 2. Implementation
|
||||
|
||||
- [x] 2.1 Scaffold `packages/api-stack/api/api-auth/` (`"version": "0.1.0"`, `"sideEffects": false`, deps: `@suaveplan/api-types`, `@suaveplan/error`, `@suaveplan/types`, `tslib`, `zod`)
|
||||
- [x] 2.2 `src/resolver/` — `SessionResolver` interface
|
||||
- [x] 2.3 `src/chain/` — `SessionResolverChain` runtime
|
||||
- [x] 2.4 `src/security-scheme/` — `SecuritySchemeContribution` + Zod schema (`import z from "zod"`)
|
||||
- [x] 2.5 `src/csrf-policy/` — `CsrfPolicy` helper
|
||||
- [x] 2.6 `src/session-adapter/` — composite `AuthAdapter`
|
||||
- [x] 2.7 `src/capability-id/` — `AUTH_SESSION_CAPABILITY_ID` + assignability assertion
|
||||
- [x] 2.8 `src/error-codes/` — `AUTH_CODES`
|
||||
- [x] 2.9 `src/testing/` — `runAuthSessionConformance(factory)`
|
||||
- [x] 2.10 `src/index.ts` — explicit named re-exports
|
||||
- [x] 2.11 `package.json` `exports` per subpath
|
||||
|
||||
## 3. Tests
|
||||
|
||||
- [x] 3.1 Unit tests per feature
|
||||
- [x] 3.2 Spec-conformance type tests
|
||||
- [x] 3.3 Property test on chain ordering (Fisher-Yates of null-returning resolvers + one non-null wins)
|
||||
- [x] 3.4 Every `it(...)` carries `{ timeout: 5000 }`
|
||||
|
||||
## 4. Documentation
|
||||
|
||||
- [x] 4.1 Sibling `.md` for every implementation `.ts`
|
||||
- [x] 4.2 `README.md` linking every `.md`
|
||||
- [x] 4.3 No CLAUDE.md update
|
||||
|
||||
## 5. Pre-archive verification
|
||||
|
||||
- [x] 5.1 `bun run check` — 0/0/0
|
||||
- [x] 5.2 `bunx turbo lint typecheck test --filter=@suaveplan/api-auth` — green (55/55 tests)
|
||||
- [x] 5.3 `bunx turbo test:coverage --filter=@suaveplan/api-auth` — 100/100/100/100
|
||||
- [x] 5.4 `bun scripts/openspec-conformance.ts packages/api-stack/api/api-auth` — exit 0
|
||||
- [x] 5.5 `bunx turbo build --filter=@suaveplan/api-auth` — green
|
||||
|
||||
## 6. Pre-archive 7-row checklist
|
||||
|
||||
- [x] 6.1 Generics on `SessionResolver<P extends Principal>`; `expectTypeOf` assertions
|
||||
- [x] 6.2 Zod wired at security-scheme boundary
|
||||
- [x] 6.3 Every `AUTH_CODES` entry thrown
|
||||
- [x] 6.4 Chain fail-closed-on-throw strict-mode tested (default fail-closed; opt-in continue-on-throw)
|
||||
- [x] 6.5 OTel — N/A (adapters emit)
|
||||
- [x] 6.6 Lifecycle dedup — `dispose()` idempotent; tested
|
||||
- [x] 6.7 No retroactive tick
|
||||
|
||||
## 7. Archive
|
||||
|
||||
- [x] 7.1 Fold spec delta
|
||||
- [x] 7.2 Move to `openspec/archive/<yyyy-mm-dd>-api-auth/`
|
||||
- [x] 7.3 Tick umbrella `1.7`
|
||||
- [x] 7.4 Refresh `MANIFEST.md`
|
||||
Reference in New Issue
Block a user