Authentication
Overview: BFF Pattern
SOCSuite uses the Backend-for-Frontend (BFF) pattern for authentication. The frontend never holds tokens at any point — they remain exclusively server-side.
Auth Flow Diagram
sequenceDiagram
participant B as Browser
participant A as API (Go/BFF)
participant K as Keycloak
participant R as Redis
B->>A: GET /auth/login
A->>K: Redirect (PKCE)
K->>B: Login form
B->>K: Credentials
K->>B: Redirect + Authorization Code
B->>A: GET /auth/callback?code=...
A->>K: Code exchange (Token Request)
K-->>A: Access Token + Refresh Token
A->>R: Store session (session:{id})
A-->>B: Set-Cookie: soc_session (httpOnly, Secure, Lax)
Note over B,R: All subsequent requests
B->>A: GET /api/... (Cookie: soc_session)
A->>R: Validate session
R-->>A: Session data + tokens
A-->>B: ResponseAuth Constants
Source: pkg/auth/constants.go
| Constant | Value | Note |
|---|---|---|
SessionCookieName | soc_session | |
SessionKeyPrefix | session: | Redis key prefix |
SessionTTL | 1h | Session lifetime |
CookieSameSite | Lax | Not Strict — would break OAuth redirect |
CookieHTTPOnly | true | No JS access to cookie |
CookieSecure | true | HTTPS only |
MaxFailedAttempts | 5 | Before account lockout |
LockoutDuration | 30min | Lockout duration |
JIT User Provisioning
:::warn [Critical Pattern]
After every successful Keycloak login, the user must exist in PostgreSQL before any
further database operations are possible. 14 tables reference users(id) via
foreign key constraints.
:::
After the successful callback from Keycloak, the API executes the following upsert:
INSERT INTO users (id, username, email, source, is_active, last_login_at, last_seen)
VALUES ($1, $2, $3, 'oidc', TRUE, NOW(), NOW())
ON CONFLICT (email) DO UPDATE SET
username = EXCLUDED.username,
source = 'oidc',
last_login_at = NOW(),
last_seen = NOW(),
is_active = TRUE;The UUID (id) is immutable — it is never changed after initial provisioning.
Keycloak Configuration
Realm: socsuite
Backend client: socsuite-backend (confidential)
Token:
Access token: 900s (15 minutes, maximum)
Refresh token: 30d (single-use, rotated)
Validation: RS256 + JWKS
Issuer + Audience MUST match
Roles (hierarchy):
superadmin → admin → soc_manager → soc_teamlead → soc_analyst → viewer
(soc_teamlead: Dev_o-357, approval workflow — analyst rights + ticketing.tickets.approve)
Horizontal roles (outside the hierarchy): ciso, it_admin, automation_engineer (DEV-224: playbook management)
Permissions: Backend RBAC map (apps/api/internal/auth/rbac/permissions.go) — single source of truth:::info [Details]
Full Keycloak setup documentation (Protocol Mapper, MFA) → infrastructure/CLAUDE.md
:::
Anti-Patterns
:::error [Using SameSite=Strict]
SameSite=Strict breaks the OAuth redirect. The browser does not send the cookie
when redirected back from Keycloak to the API. Always use Lax.
:::
:::error [Storing tokens in localStorage]
Tokens must never be stored in localStorage or sessionStorage.
JavaScript access enables token theft via XSS. The BFF pattern with
httpOnly cookies is designed exactly for this reason.
:::
:::error [InsecureSkipVerify=true in production]
InsecureSkipVerify=true in the TLS configuration is forbidden in production and
causes a panic. TLS certificates must be correctly configured.
:::
:::error [Frontend OIDC libraries (e.g. oidc-client-ts)] Using OIDC libraries in the frontend is deprecated. The BFF pattern handles all auth logic server-side. :::
:::error [Wildcards in redirect_uris (production)]
Wildcard entries in Keycloak redirect_uris are forbidden in production.
Only explicit, fully-qualified redirect URIs are allowed.
:::
Further Reading
- Architecture — System architecture and Clean Architecture
- API Reference — REST endpoints including auth details
- Operations — Security checklist and rate limiting