chain-rdeveloper portal
build

Authentication Service (svc-auth)

Overview

svc-auth is a FastAPI service that handles OAuth2 authentication via Auth0 and manages server-side user sessions. It is the only service that communicates directly with Auth0 and the only place where access tokens are ever stored.

Internal port: 3000 (not exposed to host)

Responsibilities

  • Initiating and completing the Auth0 OAuth2 authorization code flow

  • Storing access and refresh tokens server-side in Redis (never in the browser)

  • Issuing and validating signed HTTP-only session cookies

  • Automatically refreshing tokens before expiry

  • Exposing a /verify endpoint used by nginx to gate protected routes

API Endpoints

See spec/api/svc-auth.md for the full auto-generated reference.

MethodPathDescription
GET/loginRedirect to Auth0 authorization endpoint
GET/callbackHandle Auth0 callback; create session
GET/verifyValidate session (used by nginx auth_request)
GET/logoutClear session and redirect to Auth0 logout
POST/logoutSame as GET logout (supports form/AJAX)
GET/meReturn current user info from session
GET/session/infoDebug session info (dev mode only)

OAuth2 Flow

Browser → GET /login
  ↓  generate state + nonce, store in Redis
Auth0 authorization endpoint (redirect)
  ↓  user authenticates
Auth0 → GET /callback?code=...&state=...
  ↓  validate state, exchange code for tokens
  ↓  store access_token + refresh_token in Redis keyed by session_id
  ↓  set signed session cookie (HTTP-only)
Browser → redirected to app

Session & Token Storage

Sessions are stored in Redis with the following structure:

  • Session cookie: signed with itsdangerous.URLSafeTimedSerializer using SESSION_SECRET; cookie name is SESSION_COOKIE_NAME (default: sid)

  • Redis key (session:{session_id}): contains user_id, access_token, refresh_token, expires_at

  • Token refresh: triggered automatically when the access token expires within TOKEN_REFRESH_THRESHOLD seconds (default: 300 s)

Tokens are never exposed to the browser — the cookie only identifies the session.

nginx Integration

nginx uses the auth_request directive to call /verify before serving any protected route on app.dev.datachainr.com. On a valid session, svc-auth responds 200 and sets X-User-Id and X-Session-Id headers that nginx passes downstream.

Configuration

VariableDescriptionDefault
AUTH0_CLIENT_IDAuth0 application client IDrequired
AUTH0_CLIENT_SECRETAuth0 application client secretrequired
AUTH0_AUDIENCEAuth0 API audiencerequired
AUTH_CALLBACK_URLFull URL of the /callback endpointrequired
REDIS_URLRedis connection URLredis://redis:6379
REDIS_PASSWORDRedis passwordoptional
SESSION_SECRETSecret for signing cookiesrequired
SESSION_COOKIE_NAMECookie namesid
SESSION_MAX_AGESession TTL in seconds900 (15 min)
TOKEN_REFRESH_THRESHOLDSeconds before expiry to refresh300
FRONTEND_URLWhere to redirect after loginrequired
ENVIRONMENTdevelopment / productiondevelopment
COOKIE_SECURESet Secure flag on cookiefalse

Runtime Dependencies

  • Redis — session and token storage (same bff-network)

  • Auth0 — external identity provider

No dedicated PostgreSQL database. svc-auth is stateless beyond Redis.

Networks

bff-network only — cannot reach backend-network or databases.