Taskbase — Authentication¶
Overview¶
Taskbase uses Keycloak OIDC for authentication. The UI handles the browser login flow; the API validates Bearer JWTs on every request.
Authentication establishes who the caller is; authorization then decides what they can reach, granted per organization. See Authorization below.
Authentication is optional at runtime: when the relevant environment variables are not set the entire auth layer is skipped. This is the intended behaviour for local development, where running a full Keycloak instance would be unnecessary overhead.
How it works¶
Browser Keycloak platform-tasks-ui platform-tasks-api
| | | |
|-- OIDC login ------->| | |
|<- JWT --------------| | |
|-- load app ---------------------------------->| |
| |-- /api/* + Bearer JWT ->|
| | |-- verify JWT vs JWKS
| | |-- 401 if invalid
| |<- response -------------|
- The UI initialises
keycloak-jswithonLoad: 'login-required'. If the user has no active session, the browser is redirected to the Keycloak login page before the React app renders. - After login,
keycloak-jsholds the access token. The UI renews it before it runs out; see Session token renewal in the UI. - Every API call from the UI includes
Authorization: Bearer <token>. - The API middleware (
api/internal/auth) fetches Keycloak's JWKS via OIDC discovery and verifies the token signature and expiry. Requests without a valid token receive401. /api/healthis excluded from auth — it must remain reachable for Kubernetes liveness probes.
Session token renewal in the UI¶
The UI renews the access token before each request and when the page comes back into view. The
session ends only when Keycloak refuses a refresh. Source: ui/src/http.js,
ui/src/tokenSession.js, ui/src/auth.js and ui/src/SessionExpired.jsx in Skatzi/taskbase,
read 2026-10-26 at 6f1e124 (pull request #605, task #1702).
Every UI request goes through apiFetch (ui/src/http.js). With sign-in on, apiFetch hands the
request to the token session (ui/src/tokenSession.js), which ui/src/auth.js wires to
keycloak-js. Without sign-in, apiFetch is a plain fetch.
| When | What the token session does |
|---|---|
| Before each request | Renews the token when it has less than 30 seconds left (MIN_VALIDITY). All requests waiting at that moment share one refresh. |
| Every 10 seconds | The same check, from a timer in ui/src/auth.js. |
The page becomes visible (visibilitychange) or gets focus |
The same check, so the token is fresh before polling resumes. |
The API answers 401 |
Forces one refresh, unless another request already renewed the token since this one was sent, and retries the request once. |
| A refresh is refused | Ends the session (below). |
| A refresh fails for another reason, such as no network | Fails only the request that was waiting. The session goes on. |
Why the visibility and focus checks. Browsers throttle timers in hidden tabs and stop them while the machine sleeps, so the 10-second timer cannot be relied on. Before task #1702, a page left idle came back with an expired token and showed errors.
When the session ends. A refused refresh means the refresh token has expired: keycloak-js
clears its tokens, which the UI reads as refused. A network failure keeps them. Then, once:
- The page shows the notice "Your session has expired. Taking you to sign in again — you will come back to this page."
- Requests stop settling, so the page shows no error toasts.
- After 2 seconds (
SIGN_IN_DELAY_MS) the browser goes to Keycloak sign-in, with the current page as the return address.
keycloak-js does not redirect on its own when a refresh is refused (auth.js sets
kc.loginRequired = false), so the notice is shown first.
Environment variables¶
API (platform-tasks-api)¶
| Variable | Required in prod | Purpose |
|---|---|---|
KEYCLOAK_ISSUER |
Yes | Full issuer URL, e.g. https://keycloak.prod.skatzi.com/realms/skatzi. When unset, the auth middleware is a no-op. |
KEYCLOAK_URL |
Yes | Keycloak base URL, used for the Admin API (user search and group lookup). |
KEYCLOAK_REALM |
Yes | Realm name, e.g. skatzi. |
KEYCLOAK_SERVICE_CLIENT_ID |
Yes | Confidential client whose service account holds view-users (taskbase-service). |
KEYCLOAK_SERVICE_CLIENT_SECRET |
Yes | Secret for the above. |
TASKBASE_ADMIN_GROUPS |
Yes | Comma-separated Keycloak group names whose members are global admins, e.g. administrators. |
UI (platform-tasks-ui)¶
| Variable | Required in prod | Purpose |
|---|---|---|
VITE_KEYCLOAK_URL |
Yes | Keycloak base URL, e.g. https://keycloak.prod.skatzi.com |
VITE_KEYCLOAK_REALM |
No | Realm name — defaults to skatzi |
VITE_KEYCLOAK_CLIENT_ID |
Yes | The Keycloak client ID registered for this app, e.g. taskbase |
VITE_* variables are baked in at build time by Vite. They must be set in the build
environment (CI/CD), not injected at runtime.
When both VITE_KEYCLOAK_URL and VITE_KEYCLOAK_CLIENT_ID are absent, the UI skips the OIDC
flow entirely — no login page, no token, and the user menu has no Sign out item.
Local development¶
No Keycloak setup is needed. Leave all auth variables unset (they are commented out in
.env.example). The API will log:
and accept all requests without a token.
Authorization¶
Access is granted per organization. Holding a grant on an organization confers full access to it and everything beneath it — projects, tasks, epics, milestones, task groups and comments. There are no finer-grained permissions by design.
| Caller | Reaches |
|---|---|
X-API-Key — service key (no owner) |
Every organization, plus admin routes |
X-API-Key — personal key |
Exactly what its owner's login reaches — see API keys |
Member of a TASKBASE_ADMIN_GROUPS group |
Every organization, plus admin routes |
| Any other Keycloak user | Only organizations granted to their username or one of their groups |
A request carrying X-Taskbase-Run |
Only the organization of that run's task — see Run-scoped access |
A request carrying X-Taskbase-Turn |
Only GET routes of the organizations fixed at that turn's claim, plus its own session and draft — see Turn-scoped access |
Grants live in the organization_grants table and name either a Keycloak username
(principal_type: user) or a Keycloak group (principal_type: group). Global admins are held in
configuration rather than the database so an empty table can never lock everyone out.
Service API keys are deliberately unaffected: they already reached everything before grants existed, so restricting them would break agent automation without narrowing what a leaked key can do. Personal keys are bound by their owner's grants.
Where group membership comes from¶
The API reads the caller's groups from the Keycloak Admin API via the taskbase-service
account (which holds view-users), not from a groups token claim. This is deliberate: the API
accepts tokens minted by any client in the realm, and only some of those carry a groups mapper —
the taskbase client does not. Looking membership up server-side makes authorization independent
of which client the user logged in through. Results are cached for 60s, so a group change takes up
to a minute to take effect.
Enforcement¶
api/internal/authz/guard.go holds a route table mapping every API pattern to its rule, and wraps
the whole API mux. Any route absent from that table is denied, so a new endpoint is unreachable
until it is added there — a forgotten route fails closed rather than becoming world-readable. Two
tests hold the table and the handlers' registered routes to each other.
Resources resolve to their owning organization through the schema: projects, epics and
milestones carry organization_id directly; tasks and task_groups reach it via project_id;
task_comments and task_runs via task_id → tasks.
Runner routes are the exception to organization rules. Who may create a runner depends on its scope (instance runners need an administrator; organization runners need a grant on every organization they list), and the same rule applies to the new scope of a rescope. Changing or operating an existing runner — rescope, slots, pause / drain / resume, target version, caps, models, delete and its owners — is for the runner's owners and global administrators only (Runner owners). The runner's own endpoints check that the caller is the runner's user. The route table passes these through and the runner handler decides (ADR-0006).
Run-scoped access¶
An agent launched by agent-runner reaches Taskbase through the runner's local MCP proxy, which
uses the runner's Keycloak login — and an instance runner serves every organization. So the
proxy adds X-Taskbase-Run: <run id> to every request, taskbase-mcp forwards it, and the API
authorizes such a request as the run (api/internal/authz/runscope.go, between authentication
and the guard):
- The caller must be the user of the run's runner, and the run must be active. Otherwise
403, without saying which check failed. A service key with the header is refused too; a personal key counts as its owner, so only the runner user's own key passes. - The request reaches only the organization of the run's task, as if it held a single grant. The user's own grants and admin rights do not apply.
- It may use only routes the guard opens to agents: organization-scoped data routes, plus
GET /api/me,GET /api/organizations,GET /api/organizations/townandGET /api/users. The runner's own endpoints (register, heartbeat, claim, finish), runner management, cancelling runs, requeuing tasks and every admin route are refused. The table's default for a route is closed. - Exception for a triage run (Taskbase task #1669): a run of an
attention-triage task may also use
POST /api/tasks/{id}/requeueandPOST /api/tasks/{id}/comments, for the one task itsattention_triagerow names.RunInforeads that row (api/internal/authz/store_pg.go) intoRunScope.Triages, and the guard'striagerule opens the two routes when the path's id is that task (api/internal/authz/guard.go). On any other task, its own triage task included, the guard answers403withforbidden: a triage run may only requeue and comment on task N, the task it triages.POST /api/tasks/{id}/requeue-after,POST /api/attention/requeueandPOST /api/attention/{id}/choosestay people only. What the requeue and comment do is in What a triage run may do. The triage task may be the operator's or a manager role's (task #1767);RunInfoalso reads the run's role title intoRunScope.TriageTitle, which signs its comments. - Delegation rule (task #1766): when the run creates a task for an agent persona or changes a
task's agent assignee, the task handler checks the run's role in the organization's
team and answers
403outside it. The roster's write routes arehumansOnly, so a run can read the team but never change it. - Operator runs (tasks #2422, #3231): a run of the persona
operatoralso reaches the other organizations its runner serves, forGET /api/organizations(which lists them),GET /api/organizations/{orgId}/rolesand the organization pause, and may readGET /api/runners. See Team: Operator runs. - Repository reads (task #1771): on
GET /api/projects/{id}/repository/treeand/filethe guard'srunReporule checks the project against the run's rolerepo_readinstead of the task's organization, so aservedrun may read repositories of other organizations its runner serves. See Repository read scope.
Once the run finishes, its id grants nothing. A request without the header is authorized as usual.
Turn-scoped access¶
An agent answering a turn in an agent session gets X-Taskbase-Turn: <turn id> from the
proxy instead (api/internal/authz/turnscope.go, a sibling of the run scoper;
ADR-0013):
- The caller must be the Keycloak user of the runner holding the turn, and the turn must be
running. Otherwise403. A request may not carry bothX-Taskbase-RunandX-Taskbase-Turn(400). - It reaches only the organizations fixed at the turn's claim, and only GET routes the
guard opens to agents, plus
GET /api/sessions/{id}andPUT /api/sessions/{id}/draftfor the turn's own session. Every other route, including the session's/updates, messages, confirm and abandon, answers403. - The turn's organizations are a set, fixed at claim
(ADR-0016 §3). When
a runner claims the turn, Taskbase stores them in
session_turns.scope_org_ids: for a request or task session, the session's one organization; for a general chat, the organizations the owner reaches at that moment (their grants and groups through the cached Keycloak lookup; every organization for an administrator) intersected with the organizations the session's chosen runner serves. The principal carries the set asTurnScope.OrganizationIDs;CanAccessallows exactly those organizations andAllowedOrganizationsreturns them. - Fail-closed. A turn without a stored scope is refused, and an empty set reaches no organization, never "all". A general turn whose runner serves none of the owner's organizations is failed at claim, and one whose owner no longer exists in Keycloak too. Without the owner lookup no general turn is claimed at all.
- Never wider than the owner. The turn principal drops the owner's admin flag and groups, so admin
routes and
humansOnlyroutes (such asGET /api/attentionand the run lists) stay closed, even for an administrator's chat on a runner they do not own. A grant revoked during a turn takes effect at the next claim; a plan's confirm runs as the person and checks again. - A general session belongs to no organization: the guard lets its owner reach its routes by id, and the handler keeps them to the owner (and administrators reading). A turn reaches it only as its own session.
- Repository files: its own project only (ADR 0023, task #1755). The claim also stores the
turn's project in
session_turns.scope_project_id: a request session's project, or a task session's task's project; null for a general chat. The guard opensGET /api/projects/{id}/repository/treeand/fileto a turn only when{id}is that project, so a turn cannot read another project's repository in its organization, and a general chat none (403, an agent in a session may only read its own project's repository). People keep the organization rule; runs follow their role'srepo_read. - Platform docs: every turn.
GET /api/platform-docs/treeand/fileneed only a principal that reaches at least one organization (the guard'sanyOrganizationrule, task #1757), so every turn reads them, general chats included, with no project. Runs and every person with a grant or group in some organization, administrators included, read them too. A turn whose organization set is empty, and a person who reaches no organization, get403(forbidden: no access to any organization). The routes are GET only. The repository and path are server settings (PLATFORM_DOCS_REPO,PLATFORM_DOCS_PATH), never a parameter (Agent Sessions — Reading files;api/internal/authz/guard.goandturnproject_test.go, read 2026-10-28).
Once the turn finishes, its id grants nothing.
Managing grants¶
Admin-only, via the UI (Organization → Settings → Access) or the API. The UI populates its pickers from the realm directory:
| Endpoint | Scope | Purpose |
|---|---|---|
GET /api/users |
Authenticated | Search users by username/email. With no q, lists the realm (max 100) so pickers can browse. |
GET /api/groups |
Admin | Every group in the realm, flattened so nested groups are selectable. |
Principals are validated against Keycloak when a grant is created — an unknown
username or group is rejected with 400, so a typo cannot become a grant that
silently never matches anyone. Matching is case-sensitive: Keycloak's
exact=true user lookup still matches case-insensitively, so the API compares
the username itself before accepting it. If the directory cannot be reached the
API returns 502 rather than blaming the input.
Both lookups use the taskbase-service account, whose view-users role already
includes the query-users and query-groups permissions these need — no extra
Keycloak configuration is required.
Examples:
# List who can reach an organization
curl -H "X-API-Key: $TASKBASE_PROD_API_KEY" \
https://taskbase.skatzi.com/api/organizations/2/grants
# Grant a group, then a user
curl -X POST -H "X-API-Key: $TASKBASE_PROD_API_KEY" -H 'Content-Type: application/json' \
-d '{"principal_type":"group","principal":"developers"}' \
https://taskbase.skatzi.com/api/organizations/2/grants
# Revoke (id from the list call)
curl -X DELETE -H "X-API-Key: $TASKBASE_PROD_API_KEY" \
https://taskbase.skatzi.com/api/grants/5
GET /api/me returns the caller's identity and admin flag; the UI uses it to decide which
administrative controls to render.
API keys¶
An X-API-Key is either a personal key or a service key
(api/internal/apikey, api/internal/auth/middleware.go):
- A personal key has an owner and authenticates as that Keycloak user. On every request
the owner's current username and groups are looked up by their user id through the same cached
Keycloak lookup as a login. So the key reaches only the organizations the owner is granted, is
admin only while the owner is in a
TASKBASE_ADMIN_GROUPSgroup, and follows a renamed owner. If the owner has been deleted from Keycloak the key answers401. If the lookup fails, it answers500and grants nothing. - A service key has no owner and reaches every organization plus the admin routes, as every key did before personal keys existed. Every key created before migration 056 is a service key and keeps working unchanged.
Any signed-in person may manage their own keys. The routes are open to every authenticated caller except agents acting for a run, and the handler decides the rest:
| Endpoint | Who | Behaviour |
|---|---|---|
GET /api/api-keys |
Authenticated | The caller's own personal keys. A service key, or local development without auth, lists every key. |
GET /api/api-keys?all=true |
Admin | Every key, personal and service; personal keys carry owner (the username). |
POST /api/api-keys |
Signed-in user | {"name": "...", "expires_at": "<RFC 3339>"} (expiry optional) creates a personal key owned by the caller. The plaintext key is returned once, in this response only. |
POST /api/api-keys with "service": true |
Admin | Creates a service key. |
DELETE /api/api-keys/{id} |
Owner or admin | Revokes a key. An admin may revoke any key. Someone else's key answers 404, the same as a missing one. |
A personal key cannot create keys (403). Otherwise a leaked key could mint a replacement
that outlives its own revocation. It can still list and revoke its owner's keys. A caller with no
user behind it (a service key) must send "service": true, or the request answers 400.
In the UI, keys are managed on Settings (see User menu, Settings and theme).
Managing persona defaults¶
Personas may have an organization-specific default model, which is used when a task
assigned to that persona (or a session turn of a session persona) names no model. This overrides the runner's default.
A persona file's optional default_model fills it in at sync only while it is unset, so a value set
here is never overwritten; clearing it brings the file's value back on the next sync
(Gitea Integration):
| Endpoint | Scope | Purpose |
|---|---|---|
PUT /api/organizations/{orgId}/agent-types/{slug}/default-model |
Org grant or admin | Set or clear a persona's default model: {"default_model": "small" \| "medium" \| "large" \| <model-id> \| null}. Agents are refused. The value is validated with task.NormalizeModel — empty strings and whitespace are rejected. |
Keycloak client registration¶
A dedicated taskbase public client must exist in the skatzi realm. Add the following entry to
components/keycloak/base/skatzi-realm.yaml under clients::
- clientId: taskbase
name: "Taskbase"
description: "Task management application"
enabled: true
publicClient: true
standardFlowEnabled: true
directAccessGrantsEnabled: false
webOrigins:
- "https://taskbase.prod.skatzi.com"
redirectUris:
- "https://taskbase.prod.skatzi.com/*"
This follows the same pattern as the existing skatzi-web client. PKCE (S256) is enforced by
keycloak-js on the client side — no additional Keycloak configuration is required for it.
Testing auth locally (optional)¶
If you need to test the full OIDC flow locally, port-forward hetzner-mgmt's Keycloak and set the
variables. The forward runs in the foreground; stop it with Ctrl-C when you are done. It uses
28080, like the per-cluster ports of
Read Alertmanager and Prometheus on a cluster
(29090 and 29093 for hetzner-mgmt), so it does not share a port with forwards to other clusters.
If the port is taken, kubectl exits at once: stop the process that holds it (lsof -i :28080)
before you start the UI, or the UI talks to that process instead.
kubectl --context admin@mgmt-cluster -n keycloak port-forward svc/keycloak-service 28080:8080
# .env
KEYCLOAK_ISSUER=http://localhost:28080/realms/skatzi
# ui/.env.local (Vite picks this up automatically)
VITE_KEYCLOAK_URL=http://localhost:28080
VITE_KEYCLOAK_CLIENT_ID=taskbase
The taskbase client in Keycloak will also need http://localhost:5173/* added to its
redirectUris for the Vite dev server to work.
User menu, Settings and theme¶
The right of the UI header holds a theme toggle and a user menu, which is labelled with the
signed-in username (dev without auth). The menu has Needs attention (with the number of
open items, see Needs attention), Agent chat (/chat, with the number of
the person's open chats, see Sessions), Settings and Sign out. The former
/chats page redirects to /chat. Sign out is hidden when auth is
disabled. The menu works from the keyboard: arrow keys, Home/End and Esc.
Settings (/settings) is the signed-in user's page:
- Profile — username and email from the token.
- Appearance — Theme, the UI theme (see UI theme on the account), and Light or dark: System (follow the device setting), Light or Dark.
- API keys — the user's own personal keys: create one (name, optional expiry), copy the key shown once, and revoke. In local development without auth there is no user to own keys, so the section only says so.
- All API keys — administrators only: every key with its owner (or Service key), and creating service keys.
/api-keys, the former admin-only page, redirects to /settings. The header no longer has an API
Keys link.
The theme toggle switches between light and dark at once. The choice is stored in the browser's
localStorage under taskbase.theme. Choosing System on Settings removes it. With no stored
choice the page follows prefers-color-scheme and tracks changes to it. The resolved theme is
set as data-theme on <html>, and index.html applies it before first paint so the page never
flashes the wrong palette. Other open tabs follow the change. Light or dark stays per browser; it
is not kept on the account.
UI theme on the account¶
Settings → Appearance → Theme lists six UI themes, each with a swatch, name and
description. Choosing one applies it at once and saves it on the person's account, so it follows
them to every browser (tasks #1058, #1065). Source: api/internal/preference/, ui/src/themes.js,
the token blocks in ui/src/app.css and the "Themes" notes in CLAUDE.md in Skatzi/taskbase,
read 2026-10-17 at a180240 (pull request #427).
A theme changes colours, corners, fonts, shadows and icon strokes; Sap, Modern corporate, Hacker and Simplistic Scandinavian also change spacing density. A theme is only a block of design tokens, so it never changes what is on a page or the navigation. Every theme has a light and a dark palette. Light or dark (System, Light, Dark) stays a separate choice, kept per browser.
| Theme | Id | Description |
|---|---|---|
| Classic | classic |
Warm browns on cream: the original Taskbase look. |
| Futuristic | futuristic |
Neon accents on deep space tones. |
| Sap | sap |
Dense, grey enterprise screens like an old ERP. |
| Modern corporate | modern-corporate |
Clean blues and whites with crisp cards. |
| Hacker | hacker |
Green monospace text on black, terminal style. |
| Simplistic Scandinavian | scandinavian |
Calm, light and airy with soft natural colours. |
Classic is the default when no theme is chosen.
- Preview a theme without storing it with
?theme=<id>. The preview lasts until the page is reloaded without it; choosing a theme in Settings ends it. -
A theme added later with
hidden: trueinUI_THEMESis left out of Settings until it launches. It can still be previewed with?theme=<id>, and a stored hidden theme is applied as Classic. -
After sign-in the UI reads
GET /api/me/preferencesand applies the stored theme. - For first paint the applied theme is cached in
localStorageundertaskbase.ui-theme;index.htmlapplies it before the page renders. - Choosing a theme on Settings → Appearance applies it at once, caches it and saves it with
PUT /api/me/preferences. - When either call fails, the browser's cached choice stays. Without auth the UI does not call the API.
GET and PUT /api/me/preferences read and replace the caller's preferences:
theme is a theme id (classic, futuristic, sap, modern-corporate, hacker,
scandinavian) or null for none chosen; GET returns {"theme":null} before the first save.
PUT answers with the saved body.
| Caller | Result |
|---|---|
| A person, by token or personal API key | 200, their own preferences |
| A service key, an agent (run or turn scope), or no auth | 403 |
PUT with an unknown theme or an unknown field |
400 |
The rows live in user_preferences.