Demos & Scenarios
Runnable demos that exercise key OneAuth scenarios end-to-end. Each section describes what the demo proves, how it simplifies things for clarity,...
Source: docs/DEMOS.md
Demos & Scenarios ¶
Runnable demos that exercise key OneAuth scenarios end-to-end. Each section describes what the demo proves, how it simplifies things for clarity, and what changes in a real deployment.
Demo 1: Federated Resource Server Auth ¶
Location: demo/ (Docker Compose) + cmd/demo-hostapp/ + cmd/demo-resource-server/
What It Demonstrates ¶
Multiple independent applications (“Apps”) register with a central auth server, authenticate their own users independently, mint resource-scoped JWTs, and have those tokens validated by resource servers that share no direct connection to the apps.
This answers the core question: How does a resource server trust a JWT issued by an application it has never seen before?
Answer: a shared PostgreSQL signing_keys table acts as the trust anchor. OneAuth server writes signing keys at app registration time. Resource servers read from it at validation time.
Service Topology ¶
PostgreSQL (shared signing_keys table)
|
+-- OneAuth Server :9999 (user auth, app registration, key management)
+-- Resource-Server-A :4001 (validates JWTs via shared KeyStore)
+-- Resource-Server-B :4002 (validates JWTs via shared KeyStore)
|
+-- DrawApp :3001 (app #1 — registers, owns users, mints resource tokens)
+-- ChatApp :3002 (app #2 — same binary, separate user database)
Scenarios Exercised ¶
| Scenario | What It Proves |
|---|---|
| App auto-registration on startup | App calls POST /apps/dcr (RFC 7591), gets client_id + client_secret, persists to disk |
| Independent user databases | DrawApp and ChatApp each have their own FS-backed user store — users don’t overlap |
| Resource token minting | Authenticated app user clicks “Get Resource Token” → MintResourceToken() signs JWT with client_secret, includes kid header (RFC 7638 thumbprint) |
| Token validation by resource server | JWT POSTed to resource server’s /validate → server reads kid from header, looks up signing key, verifies HMAC. Falls back to client_id claim for legacy tokens |
| Cross-resource-server validation | Token from DrawApp validates on both Resource-Server-A and Resource-Server-B (they share the same KeyStore) |
| Cross-app isolation | Token signed with DrawApp’s secret but claiming ChatApp’s client_id is rejected (kid owner doesn’t match client_id claim) |
| Token introspection | Resource server returns user_id, client_id, scopes, max_rooms, max_msg_rate from JWT custom claims |
| WebSocket-style auth | GET /ws?token=... validates token via query param (simulates WebSocket upgrade). Uses APIMiddleware.LegacyQueryParamBearer — the OAuth 2.0 escape hatch; see WebSocket auth alternatives below for the three OAuth 2.1-clean replacements |
Running the Demo ¶
Option A: Live-reload development (recommended for iterating)
cd demo
make dev # PG in Docker, all Go services native with auto-rebuild
Option B: Full Docker Compose
cd demo
make up # builds and starts all 6 services in Docker
make status # shows URLs and container status
WebSocket auth alternatives ¶
The demo’s GET /ws?token=… pattern is the OAuth 2.0 legacy carry, enabled via APIMiddleware.LegacyQueryParamBearer. OAuth 2.1 §5.4 retired query-string bearer tokens because they land in browser history, server access logs, Referer headers, caches, and CDNs.
A real WebSocket upgrade can’t always set the Authorization header on the initial GET (browser WebSocket constructors don’t accept arbitrary headers). The three header-clean replacements are below. New deployments should pick one; the legacy field is retained for existing OAuth 2.0 fleets that cannot migrate.
1. WebSocket subprotocol header (recommended) ¶
The browser WebSocket constructor accepts a subprotocol argument. Encode the bearer token as one of the negotiated subprotocols; the server reads it on upgrade.
// client
const ws = new WebSocket("wss://relay.example/ws", [`bearer.${jwt}`]);
// server (on Upgrade)
proto := r.Header.Get("Sec-WebSocket-Protocol")
for _, part := range strings.Split(proto, ",") {
if t, ok := strings.CutPrefix(strings.TrimSpace(part), "bearer."); ok {
// validate t with mw.ValidateBearer, then accept upgrade
}
}
Pros: standard mechanism (Discord, Slack, others use this); browser-supported. Cons: server must parse the subprotocol on upgrade; token visible in DevTools but not in URL bar / logs.
2. Initial-frame auth ¶
Accept the upgrade unauthenticated; require the first WebSocket frame to be {"type": "auth", "token": "…"}. Defer auth-required handlers until that frame lands; disconnect if the timer expires first.
Pros: works with any WebSocket client library (no constructor arg). Cons: one extra round-trip; server holds a connection in a half-open state until auth lands.
3. Short-lived per-connection ticket ¶
The client POSTs to a regular HTTP endpoint (POST /ws/ticket) with Authorization: Bearer <token> and receives a single-use, ~30-second ticket. WebSocket URL carries ?ticket=…. Server consumes the ticket on upgrade and binds it to the connection.
Pros: simplest for embedded clients that can’t do subprotocol negotiation. Cons: the ticket is a URL query carry — but it’s connection-scoped, single-use, and short-lived, mitigating the leak surface that motivated OAuth 2.1 §5.4.
Pick the one that fits your client constraints. The OneAuth library exposes the validation primitives (APIMiddleware.ValidateToken, IntrospectionValidator) any of these patterns can call.
A token that arrives inside the connection (option 2, or a fresh token for re-auth on a long-lived socket) has no HTTP request around it. Validate it with APIMiddleware.ValidateBearer. It runs the same checks as ValidateToken and returns an *apiauth.AuthError carrying the RFC 6750 code. TokenInfo.ExpiresAt gives the moment to close the connection, so there’s no need to re-parse the token.
A DPoP-bound token is refused there unless the request supplies ConfirmBinding, since a frame carries no proof. When the upgrade itself was a DPoP request, the connection has already proven a key. ConfirmBindingMatches accepts a bound token only for that key, and refuses every bound token on a connection that opened with a plain bearer token:
// on upgrade, behind mw.ValidateToken
opened := apiauth.GetTokenInfoFromAPIContext(r.Context())
// on a re-auth frame
resp, err := mw.ValidateBearer(ctx, &apiauth.ValidateBearerRequest{
Token: frame.Token,
ConfirmBinding: apiauth.ConfirmBindingMatches(opened.Confirmation),
})
Manual Walkthrough ¶
- http://localhost:9999 — OneAuth landing page. Sign up for an account. Dashboard shows registered apps.
- http://localhost:3001 — DrawApp. Sign up (separate user DB). After login, click “Get Resource Token”. JWT claims display inline.
- Click “Validate” — JWT is POSTed to Resource-Server-A. Response shows
valid: truewith user ID and claims. - http://localhost:4001 — Resource-Server-A status page. See the validation in the log table.
- http://localhost:4001/test — Paste any JWT to test manually.
- http://localhost:3002 — ChatApp. Repeat flow. Try validating against Resource-Server-B at
:4002.
Automated Tests ¶
# From repo root, with demo stack running:
DEMO_SERVER_URL=http://localhost:9999 \
DEMO_RESOURCE_SERVER_A_URL=http://localhost:4001 \
DEMO_RESOURCE_SERVER_B_URL=http://localhost:4002 \
DEMO_ADMIN_KEY=demo-admin-key-12345 \
uv run pytest tests/integration/test_05_browser_auth.py \
tests/integration/test_06_federated_flow.py \
tests/integration/test_07_multi_host.py \
tests/integration/test_08_token_refresh.py -v
What the Demo Simplifies (vs. Production) ¶
| Demo Simplification | Production Reality |
|---|---|
In-memory app metadata — AppRegistrar.apps map is lost on restart. Signing keys survive (in PostgreSQL) but metadata like domain, quotas, created_at is gone. |
Persist app registrations in a database (AppStore interface — not yet implemented). |
FS-backed user stores — each app stores users on disk under /data/{name}/. |
Use GORM or GAE stores behind a real database. FS stores don’t work in clusters. |
| HS256 only — demo uses symmetric HMAC signing for simplicity. The resource server needs the same secret as the app. | Asymmetric signing (RS256/ES256) is now supported — apps can register with a public key and keep their private key secret. Resource servers only need the public key. See FEDERATED_AUTH.md. |
| No JWKS — resource server reads keys from PostgreSQL directly. | JWKS endpoint (issue #7) lets resource servers auto-discover public keys via HTTP without shared database access. |
| Single PostgreSQL — all services share one database instance. | Separate read replicas, or JWKS-based discovery, or each resource server has its own KeyStore sync. |
Static quotas — max_rooms and max_msg_rate are set at registration time and hardcoded in token minting. |
Dynamic quotas from a billing/entitlement system. |
| No token refresh — resource tokens expire in 15 minutes, app mints a fresh one each time. | Client-side refresh loop or server-side refresh grant for long-lived sessions. |
| No TLS — all traffic is plaintext HTTP on localhost. | TLS everywhere. Secrets encrypted at rest. |
Hardcoded admin key — demo-admin-key-12345 in docker-compose env. |
Admin key from secrets manager (e.g., GCP Secret Manager). Rotated regularly. |
| Cookie-based sessions — apps use HttpOnly cookie JWTs for browser sessions. | Works for browsers but blocks SPA/mobile clients (see Phase 3 OAuth API Mode below). |
Key Architecture Points ¶
Trust anchor: The shared signing_keys table. This is the only thing that connects apps and resource servers. Written by oneauth-server at registration, read by resource servers at validation.
Secret shown once: POST /apps/dcr returns client_secret exactly once. It’s stored in the KeyStore as bytes but never returned again via any API. Apps must persist it (demo-hostapp saves to app_credentials.json).
Resource server independence: Once keys are in the shared KeyStore, resource servers validate tokens with zero runtime dependency on the auth server or apps. The resource server only needs database access.
Credential recovery: On startup, demo apps verify their cached credentials against the auth server (GET /apps/{client_id}). If the auth server doesn’t recognize them (e.g., database was reset), the app deletes its stale credentials and re-registers automatically.
Scenario: OAuth API Mode (Not Yet Implemented) ¶
Status: Superseded by headless OAuth (#54) — LoginWithBrowser + loopback redirect now covers CLI/SPA/mobile via standard auth-code + PKCE. The narrative below is retained for context.
The Problem ¶
The current OAuth flow (Google/GitHub login) is browser-only:
- Redirect to provider (stores state in cookie)
- Provider redirects back to callback (reads state from cookie)
- Callback sets
oa_tokenHttpOnly cookie and redirects to/dashboard
Every step requires browser cookies and HTTP redirects. SPAs doing fetch() and mobile apps using deep links cannot participate.
APIAuth already returns proper {access_token, refresh_token} JSON — but only for password-based login (grant_type=password). There is no path from “OAuth succeeded” to “here are your API tokens.”
What Would Be Demonstrated ¶
| Scenario | Flow |
|---|---|
| SPA OAuth login | React app calls /auth/google/callback?mode=api → receives {access_token, refresh_token} JSON |
| Mobile OAuth | iOS opens Google OAuth in Safari → deep link myapp://auth?code=xyz → app exchanges code for tokens via POST /api/token |
| PKCE | Public client generates code_verifier + code_challenge, server validates on exchange |
Prerequisites ¶
- Phase 3 implementation (API mode OAuth callbacks, token exchange endpoint, PKCE)
- A demo SPA or mobile client that drives the flow
Future Demos ¶
| Demo | Scenario | Depends On |
|---|---|---|
| Asymmetric resource server auth | App uses RS256 private key, resource server validates with public key only | ✅ Core support done (issue #4), demo extension needed |
| JWKS discovery | Resource server auto-fetches app public keys via .well-known/jwks.json — use JWKS_URL env var on demo-resource-server |
✅ Core support done (issue #7), demo extension needed |
| Client SDK | CLI tool authenticates, gets tokens, makes authenticated API calls | Client SDK migration (lilbattle) |
| Multi-factor auth | Login with password + TOTP code from authenticator app | MFA implementation |