Skip to content

Phase B3: OIDC authorization and consent #2017

Closed

Description

@neuromechanist

Goal

After this phase a person signed in at app.nemar.org can be sent to /oauth/authorize by a registered client, see what the client may do, consent, and be redirected back with a single-use authorization code and the iss parameter.
The backend decides everything; the website page renders its verdict and carries the person's click.

Routing

Scope

In: migration 0122, two session-bound routes, code minting, consent, the website page with its copy and headers.
Out: the token endpoint (#2018), revocation (#2019), a person revoking their own grants.

Design references

Design section 3 (Flow, Request handling, Who may sign in, Clients); ADR 0079 and 0056; OpenID Connect Core 3.1.2, RFC 6749 4.1.2.1, RFC 9207.

Changes

  • backend/src/db/migrations/0122_oidc_grants_and_codes.sql. oidc_grants: id, user_id, client_id (the oidc_clients row id), sorted JSON scopes, consent_kind (first_party | user), created_at, revoked_at, and a partial unique index for one live grant per user and client. oidc_codes: code_hash (primary key), client_id, user_id, grant_id, redirect_uri, scopes, nonce, code_challenge, auth_time, created_at, expires_at, used_at; used rows stay until an hour past expiry (pruned in the check route) so Phase B4: OIDC token, userinfo, revocation and introspection #2018 can detect replay.
  • backend/src/routes/auth-oidc.ts, mounted at /auth in index.ts: POST /auth/oidc/authorize (check) and POST /auth/oidc/consent (decide). Each runs webSessionMiddleware, isAllowedOrigin, no-store and a per-account budget checkOidcAuthorizeBudget (30 a minute, modelled on checkPrivateGrantBudget); check also answers a visitor with no session.
  • Validation order, in backend/src/services/oidc-authorize.ts. Unknown or disabled client, or a redirect_uri that is not an exact registered string: answer {kind: "error_page", code} and never redirect. Then errors that redirect with error, state and iss: invalid_request (not response_type=code, a method other than S256, no challenge, a response mode other than query), request_not_supported, request_uri_not_supported, invalid_scope (openid absent, or a catalogue scope the client may not have; scopes outside the catalogue are dropped), login_required and consent_required for prompt=none. Accept display, ui_locales, login_hint; a valid id_token_hint for another subject forces re-authentication. prompt=login or a max_age older than the session's created_at answers {kind: "reauth"}; the page sends the person to /login?next= with prompt=login replaced by nemar_auth_after=<unix seconds>, which check honours by requiring a newer session.
  • Sign-in rule from the client row: active_account needs status active and no deletion; person_proven_orcid also needs kind person and the proven-ORCID predicate, exported once as ORCID_PROVEN_SQL in services/oidc-identity.ts (used again by Phase B4: OIDC token, userinfo, revocation and introspection #2018 and Phase B5: OIDC revocation cascades and back-channel logout #2019). A failure answers {kind: "rule_refused", reason} (orcid_not_proven, account_not_person, account_inactive) with a prebuilt access_denied return URL.
  • Consent: none for first_party; ask_once asks when the scopes are not a subset of the live grant; always always asks. A decision needs a consent token, an HMAC (domain tag oidc-consent-v1, key OIDC_SECRET) over session id, client, redirect, scope set, state, challenge and a 10 minute expiry, returned by check and verified by consent. Deny returns the access_denied redirect.
  • Minting: a code is 256 random bits via generateGrantCode, stored with hashGrantCode, expiry 60 seconds SQL-side; the insert re-reads client status, account status and session liveness in its own WHERE clause (shape of DOCS_MINT_INSERT_SQL). Response: {kind: "redirect", location} with code, state and iss.
  • Website PR: src/pages/oauth/authorize.astro (GET and POST accepted, server-rendered, shape of src/pages/auth/private/authorize.astro), src/lib/oidc-authorize.ts and src/lib/oidc-authorize-api.ts (shape of private-authorize.ts and private-auth-api.ts), /oauth added to APP_ROUTE_PREFIXES in src/lib/host.ts, frame-ancestors 'none' and X-Frame-Options: DENY for this path in src/middleware.ts, no-store, no-referrer. The site-wide form-action 'self' blocks the redirect to the client after the consent post (Chrome applies it to the redirect chain), so the page sets an internal header with the one validated redirect origin and applySecurityHeaders adds it to form-action for this path only, then deletes it.
  • Copy: oidc.consent.*, oidc.rule.* and oidc.error.* keys in shared/contract/account-copy.ts, literal strings only, transcribed to the website's src/lib/account-copy.ts. A non-first-party client's consent text says it can act as the person; with compute.reserve or compute.submit it says the client can reserve and spend the person's compute time. Wire shapes in shared/contract/oidc-authorize.ts with a website drift test.

Tests (write these first)

  1. Every refusal path above through app.fetch with a real session (issueSession): error page versus redirect, iss and state present. Mutation: accept a redirect differing by a trailing slash.
  2. Sign-in rule: an ADR 0106 attested iD is refused, an oauth_identities iD passes. Mutation: test users.orcid_verified instead.
  3. Consent: first-party skips, ask_once asks again on a wider set, a forged, expired or other-session consent token is refused.
  4. The code: 60 seconds, hashed at rest, bound to the session account, minted only while the client and account are live (revoke the account between check and consent).
  5. prompt=none without a session answers login_required; stale max_age and prompt=login answer reauth.
  6. Website vitest: each kind renders, the path's security headers, form-action widened only for the validated origin, copy drift.

Acceptance criteria

  • Only exact registered redirect URIs ever receive a code or a redirect.
  • A person can allow or deny; the page cannot be framed.
  • Codes are hashed and expire in 60 seconds.

Gates

bun run typecheck, bun run lint, bunx biome check backend/src shared test, the new tests, test/account-copy-parity.test.ts, bun run migrations:d1-check, full bun test; website bun run lint, bun run typecheck, bun run test.

Rules that bite here

  • Origin is checked before the session is acted on, and a missing Origin is refused (the website pins one).
  • All timestamps SQL-side (datetime('now', ...)), never from JavaScript.
  • Never echo request values into pages unescaped; never log state or a code.
  • The page must never enter the edge cache; check the bypass.

Escalate to the owner if

  • The compute spend sentence needs the owner's wording.

When merged

Add the route names to design section 3; screens are documented in #2020.

Activity

  1. added
    feature
    on Nov 4, 2026
  2. neuromechanist commented on Nov 4, 2026

    @neuromechanist
    ContributorAuthor

    Driver amendment, 2026-11-04, from the phase B1 Opus checkpoint (PR #2031). This comment is part of the phase brief; where it differs from the body above, this comment wins.

    • The authorize and consent routes and the website /oauth/authorize page treat a session read under the LEGACY cookie name (nemar_session) as no session: sign-in is required again, and only a __Host-nemar_session session can approve a grant. Phase B1 makes a __Host- cookie something only a sign-in mints, so this costs a returning person at most one extra sign-in.
    • Consent state and any cookie this phase sets that can mint or link a session follow the same rule as B1's: host-only and __Host- prefixed, no Domain.
  3. neuromechanist commented on Nov 4, 2026

    @neuromechanist
    ContributorAuthor

    Backend merged into the epic branch by #2036 (squash 891fc97). Round 1 and an Opus delta review converged (one real hole found and fixed in round 2: with the flag off the ORCID link flow could be armed by a plain GET start; link now starts only from the Origin-checked POST). Targeted tests pass; every guard mutated; the real-browser Allow and Deny run passed 29 of 29 checks. Named red on the PR's CI: the website parity test, which reads website staging and clears when website #501 lands. The website page is nemarOrg/website#501: it merges only after the epic reaches dev and CI is re-run; its form must be live no later than this API. Owner decisions are listed in the PR body and on the private tracker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurehpc

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions