Authorization and tenancy
Hatchet protects service access by first identifying the caller and then checking what that caller may do in a tenant. HTTP routes can accept browser sessions, tenant API tokens, delegated exchange tokens, or a custom auth provider. The same tenant identity also reaches internal gRPC calls, so workflows and platform resources stay within the tenant selected by the request. This page complements the API and access layer, public API contracts, and operational command interfaces pages.
Access model
Hatchet treats authentication and authorization as separate steps. Authentication establishes a user or token identity. Authorization then checks tenant membership, role permissions, account state, and any operation-specific limits before the request reaches a service handler.
When a request enters the HTTP service, it follows this high-level path:
flowchart LR
R[Route and security metadata] --> N[Authentication]
N --> I[User or token identity]
I --> T[Tenant scope]
T --> Z[Authorization]
Z --> H[Service operation]
Z --> F[Access denied]
The route contract selects the allowed authentication modes. The authorization layer uses the route operation name and the identity placed in the request context. Resource loading also checks that nested resources belong to the selected parent.
Authentication modes
The HTTP API supports four route-level modes:
| Mode | Identity | Main use |
|---|---|---|
| Cookie session | A signed-in user | Web console and user actions |
| Bearer token | A tenant-scoped API token | SDK and service access |
| Exchange token | A user plus a tenant | Delegated control-plane access |
| Custom auth | An identity defined by the host | Product-specific auth integration |
Routes can also be public or mark security as optional. A public route does not require an identity. An optional route can continue without one and can use identity data when a valid credential is present. The gRPC service uses bearer tokens and resolves the tenant before it serves the call.
References: api/…/middleware.go• L17-85 api/…/middleware.go• L53-120 internal/…/auth.go• L31-71
Authentication order
Routes that allow more than one credential type try bearer auth first, then custom auth, then the browser cookie. This order avoids creating a new browser session when a request already has a service credential. A failed credential is not accepted as proof of identity; the request continues only when another route-approved mode succeeds.
References: api/…/middleware.go• L63-120
Disabled-auth mode
An authdisabled build is a local or test mode, not a production security mode. It resolves the seeded admin user, uses a NOAUTH role for route checks, and disables several account-management paths such as signup, invites, and tenant creation. The loader warns that this mode must not run in production.
Its embedded tenant-token manager uses a separate JWT keyset from the main manager. An embedded token is accepted only by the auth-disabled manager, while the main manager rejects it. This keeps local credentials isolated from production credentials even when both managers use the same tenant-token rules.
References: api/…/authdisabled.go• L11-25 api/…/authdisabled.go• L11-17 pkg/…/loader.go• L675-684 pkg/…/loader.go• L1191-1196 pkg/…/token_authdisabled_test.go• L67-105 pkg/…/token_embedded_authdisabled_test.go• L20-46
Identity and tenant scope
Tenant scope is the boundary for Hatchet resources. A request gets a tenant from its route or from a validated token. Services then use that tenant when they read or change workflows, runs, workers, events, and other tenant resources.
Browser identity
A browser session identifies a user rather than a tenant. The session store keeps the user session in a signed, protected cookie and a database record. Authentication loads the user from the session, then authorization checks the user’s membership in the tenant named by the route. The membership becomes the source for the tenant role and member-specific settings. The tenant repository persists this membership with a role and a payload-visibility setting, so the access decision can use tenant-specific data.
The session store can send an expired browser cookie when it cannot load the session. A deleted session ID is not revived: a missing row causes a new session ID to be issued, while a deletion that races with an update causes the save to fail without issuing a replacement cookie. During logout it clears the browser cookie even if removal of the database record fails, and it records that persistence failure for diagnosis. Session rotation also replaces the session ID while keeping its values, so an old ID cannot follow a privilege change.
References: api/…/middleware.go• L148-215 pkg/…/sessionstore.go• L102-159 pkg/…/sessionstore.go• L168-219 pkg/…/sessionstore.go• L228-269 pkg/…/sessionstore.go• L288-310 pkg/…/sessionstore_test.go• L45-249 api/…/middleware.go• L159-197 pkg/…/tenant.go• L55-64 pkg/…/tenant.go• L419-568
Tenant identity
A tenant API token carries the tenant in its JWT subject. The server validates the signature and token record, then compares that tenant with the tenant selected by the request. A mismatch stops the request before the handler can access a resource. Internal gRPC authentication follows the same pattern and places the resolved tenant in the call context.
References: pkg/…/token.go• L105-200 api/…/middleware.go• L309-346 internal/…/auth.go• L40-71
Resource ownership
Route resource metadata describes parent-child relationships such as a tenant and one of its workflows. The resource populator loads those objects before authorization and rejects a child whose parent does not match the route. This prevents a valid resource ID from being used through another tenant’s URL.
References: api/…/populator.go• L52-124 api/…/populator.go• L135-191
Delegated identity
An exchange token represents a user acting within a tenant. Its tenant claim must match the route tenant. After validation, the request goes through email and tenant membership checks like cookie auth. This keeps delegated access under user RBAC instead of treating it as an unrestricted tenant API token.
References: pkg/…/token.go• L48-102 api/…/middleware.go• L249-306 api/…/middleware.go• L98-118
Permission enforcement
Hatchet maps each API operation to a tenant role. The role map is checked after authentication and tenant resolution, so a credential proves who is calling but does not by itself grant every operation.
Tenant roles
The role model is cumulative. OWNER inherits ADMIN, ADMIN inherits MEMBER, and MEMBER inherits VIEWER. The VIEWER role provides read access. The higher roles add changes to tenant resources and administration of members, invites, and API tokens. NOAUTH inherits MEMBER for the local auth-disabled mode. Tenant membership accepts these four production roles and stores the optional CanViewPayloads capability with the member record.
References: api/…/rbac.yaml• L1-19 api/…/rbac.yaml• L74-170 pkg/…/tenant.go• L59-68 pkg/…/tenant.go• L419-568
Operation permissions
The authorizer loads the role map and checks that its operation names match the OpenAPI operation set. A route operation is allowed when the role owns that permission or inherits it from another role. Extension operations can use separate allow lists, but write extensions still deny the VIEWER role.
The RBAC checks cover every operation in the API specification. They keep member access separate from admin-only actions such as tenant member management and API token management. They also verify that viewer access remains read-focused by checking both denied state-changing operations and allowed read and account actions.
References: pkg/…/rbac.go• L22-43 pkg/…/rbac.go• L45-78 pkg/…/rbac.go• L80-149 pkg/…/server.go• L702-713 api/…/middleware.go• L202-220 api/…/rbac_test.go• L13-164
Account and payload limits
Authorization includes checks beyond the role map. Most user operations require a verified email. The current-user and logout operations remain available to an unverified user so that the user can inspect or end the session. Payload visibility is also a member setting; owners and admins can view payloads, while other members need the tenant member capability.
References: api/…/middleware.go• L74-88 api/…/middleware.go• L136-156 api/…/payloads.go• L9-23
Bearer token limits
Regular, non-read-only tenant API tokens are treated as tenant-admin credentials after the tenant match succeeds. They cannot list, create, update, or revoke other API tokens. A read-only bearer token is limited to the VIEWER permission set, so it can inspect tenant resources but cannot perform member-level changes. Exchange tokens are different because they represent a user and therefore receive the normal email and tenant RBAC checks.
Read-only status is stored with the API-token record and is applied after bearer authentication. It limits the operations available to the token without changing the bearer token’s tenant binding or payload-view behavior.
References: api/…/middleware.go• L95-139 api/…/bearer_test.go• L19-59
Tenant-level gates
Tenant scope also controls access to plan-based features and shared resource capacity. These gates apply after the system knows the tenant, so a caller cannot use a feature or capacity assigned to another tenant.
Feature entitlements
Feature entitlements are tenant-level switches supplied by an upstream control plane and stored locally for engine and API checks. They cover features such as audit logs, Prometheus metrics, strict metadata filters, the DAG operator, and durable streams. A tenant with no entitlement record is treated as not entitled. Reads use a short-lived cache, while a full update replaces the tenant’s entitlement set and clears its cached value.
The same repository supports single-tenant checks and a query that asks whether any selected tenant has audit logging. Services use these results to gate feature paths without crossing database boundaries for each request.
References: pkg/…/tenant_entitlement.go• L16-79 pkg/…/tenant_entitlement.go• L106-169 pkg/…/tenant_entitlement_test.go• L16-50
Resource limits
Resource limits provide a second tenant boundary for capacity and retention. They can deny new task runs, events, workers, webhooks, stream topics, or stream messages when a tenant reaches its configured limit. Stream retention is also resolved per tenant and falls back to the configured default.
When a tenant has no limit row, the service creates default rows without replacing paid or custom values. It then checks the stored value before allowing the request. This keeps first use from bypassing a limit or lowering a limit that was already assigned to the tenant. Stream topic limits follow the tenant’s live topic count, so deletion frees capacity. The tenant retention value is stored with the tenant limits and used for stream retention.
References: pkg/…/tenant_limit.go• L201-279 pkg/…/tenant_limit.go• L482-564 pkg/…/tenant_limit_test.go• L121-202 pkg/…/tenant_limit_test.go• L215-272
API token lifecycle
API tokens give SDKs and services a durable bearer credential for one tenant. The token is a signed JWT, but the database record remains part of validation so an operator can revoke a token before its expiry.
Token creation and validation
Token creation makes a tenant-bound JWT and records its ID, tenant, name, expiry, and internal and read-only flags. If no expiry is supplied, the token lifetime defaults to 90 days. Validation checks the issuer, audience, signature, token ID, server URL when present, database record, revocation state, expiry, and tenant subject. The stored token tenant must match the JWT subject, so changing the signed subject does not move a token to another tenant.
References: pkg/…/token.go• L19-29 pkg/…/token.go• L53-107 pkg/…/token.go• L110-201 pkg/…/token.go• L204-236 pkg/…/token_authdisabled_test.go• L107-129 pkg/…/token_readonly_test.go• L17-39
Token storage and revocation
The token repository stores token metadata, including whether a token is read-only, and supports tenant-scoped listing, revocation, and deletion. Requests validate the signed token and its database record, but repository caching can delay recognition of a revocation. A revoked token is rejected after the cached record expires or after a cache policy that avoids stale records is used.
References: pkg/…/api_token.go• L14-37 pkg/…/api_token.go• L55-110 pkg/…/token_test.go• L64-182
Client credential use
Clients send a token as a bearer credential and bind the client to a tenant ID. For example, the TypeScript client stores the tenant ID as part of its client state, while the Python helper produces the standard Authorization: Bearer header. The server remains responsible for checking that the token and route tenant agree.
References: sdks/…/client.ts• L120-159 sdks/…/api_auth.py• L1-2 api/…/middleware.go• L236-346
Security helpers and service boundaries
Shared helpers keep identity checks consistent across the web service, internal calls, and sign-in flows. They also attach user, tenant, token, and source data to request context so downstream logs and traces can be tied to the access decision.
Session helpers
Session helpers create authenticated sessions, clear stale browser cookies, clear old session values on logout, read user and OAuth state, and remove single-use OAuth state after validation. The session store protects the browser cookie and persists the session data so a stale cookie cannot silently become another user’s session. A revoked session cannot be updated or reissued under its old ID. Logout still clears the browser cookie when database deletion reports a failure, while the store logs that failure for diagnosis. Rotation gives a session a new ID after a sensitive change while preserving its values.
References: api/…/session_helpers.go• L27-58 api/…/session_helpers.go• L135-213 pkg/…/sessionstore.go• L161-176 pkg/…/sessionstore.go• L228-310 pkg/…/sessionstore_test.go• L133-249
Context and telemetry identity
Authentication places the selected user, tenant, token ID, auth strategy, and request source into context when those values exist. Resource loading adds the tenant to traces. This gives internal services a clear access boundary and makes tenant-level activity traceable without changing the business operation itself.
References: api/…/middleware.go• L278-345 api/…/populator.go• L111-122 internal/…/auth.go• L48-71
Custom authentication hooks
Deployments can provide a custom authenticator for routes marked with the custom security scheme. The hook has separate authentication and authorization steps, plus a hook for cookie-based authorization. This allows a host integration to add its identity rules without bypassing the route security contract.
References: pkg/…/server.go• L676-685 api/…/middleware.go• L349-355 api/…/middleware.go• L128-133
Change checklist
When these files or sections are changed, remember to consider these:
- Keep OpenAPI operation IDs and api/…/rbac.yaml aligned. The authorizer rejects a permission map that names an operation missing from either side.
- When adding a tenant-scoped route, define its tenant resource metadata and ensure every credential mode checks the same tenant identity.
- When changing API token claims or validation, update both HTTP bearer auth and internal gRPC auth. They share the tenant token validator.
- When adding an exchange-token claim, update the control-plane signer and the service-side validator together. The token must still map to both a user and a tenant.
- When changing a tenant role or member capability, check both operation RBAC and resource-specific checks such as payload visibility. Keep the RBAC expectations in api/…/rbac_test.go aligned with the role map and the tenant member validation in pkg/…/tenant.go.
- When changing a tenant role, review the
NOAUTHinheritance path separately. Local auth-disabled behavior must not change production access rules. - When changing auth-disabled token keys or pinned claims, keep the embedded token, its stored token record, and the auth-disabled manager options aligned. Preserve rejection by the main manager and by the auth-disabled manager for tokens from the other keyset.
- When changing token revocation or repository cache duration, verify both immediate rejection with a short cache and the expected stale-token window with a longer cache.
- When changing read-only token behavior, keep token creation metadata, bearer authorization, and the viewer operation boundary aligned. Confirm that exchange tokens still follow user RBAC rather than read-only bearer rules.
- When adding or renaming a tenant entitlement, update the upstream entitlement sync, the local entitlement record, and every service gate that uses the entitlement. Keep the cached read path and full-set update path consistent.
- When changing a tenant limit or retention default, verify that first-use provisioning does not overwrite paid values and that the stored value is checked before the operation is allowed.
- When changing stream topic limits or retention, verify that live topic deletion frees capacity and that the stored tenant retention value remains the source for stream retention.
- When changing session cookie settings, session revocation, rotation, or logout handling, keep the browser cookie and database session lifecycle aligned. A missing or revoked row must not revive its old ID, rotation must replace IDs after sensitive changes, and a failed database delete must still clear the browser cookie and remain visible in diagnostics.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Token tenant does not match the route tenant | The request is rejected before the handler runs | Compare the token subject or exchange claim with the resolved tenant |
| Token is revoked, expired, has the wrong issuer or audience, or has the wrong server URL | Bearer authentication fails, but a revoked token may remain accepted until its cached record expires | Validate signed claims and the current API-token record; set cache behavior to match the revocation response needed |
| Token subject is changed without changing the stored token tenant | Token validation fails | Compare the signed subject with the tenant on the stored token record |
| Token is signed by the other JWT keyset | The token is rejected by the selected manager | Keep the main and auth-disabled keysets separate and test both directions |
| Read-only token is treated as a normal bearer token | A token intended for inspection can perform member-level changes | Apply the stored read-only flag through the viewer permission set |
| User has no membership in the tenant | The request is unauthorized | Load the tenant membership before applying the role |
| Child resource belongs to another parent | The request is forbidden | Check parent-child ownership during resource population |
| OpenAPI operation is missing from the role map | Authorization setup fails at startup | Validate the permission map against the OpenAPI spec |
| Role expectations drift from the RBAC map | A member or viewer can gain or lose an operation unexpectedly | Run the role-wide and representative operation checks in rbac_test.go |
authdisabled build runs without the seeded admin | Requests fail during auth preflight | Seed the configured admin and never use this build in production |
| Tenant entitlement is absent or stale | A tenant can lose a feature until the entitlement is synced or the cache expires | Treat missing rows as disabled and clear the cache after a full entitlement update |
| A new tenant limit is inserted but not re-read | A request can bypass the default limit on first use | Insert missing defaults without overwriting existing rows, then enforce the stored value |
| A stream topic limit is checked without counting the live topic set or rolling back a rejected create | A tenant can exceed its topic limit or lose capacity after a failed create | Enforce the limit against live topics and commit a new topic only when capacity is available |
| A session row is deleted before an update saves it | The old session ID is rejected and no replacement cookie is issued | Return ErrSessionRevoked when the update finds no row |
| A session is rotated without deleting its old ID | An ID known before a privilege change can remain valid | Delete the old row and save the values under a new session ID |
| Session deletion fails during logout | The browser is signed out but the server-side session record can remain | Clear the browser cookie anyway and log the deletion failure |
Key files
| File | Why to read it |
|---|---|
| api/…/middleware.go | Main HTTP authentication flow and tenant-token checks |
| api/…/middleware.go | User membership, role, email, bearer-token, and extension checks |
| api/…/middleware.go | Route security metadata and operation context |
| api/…/populator.go | Tenant and nested-resource ownership boundary |
| api/…/rbac.yaml | Role inheritance and operation permissions |
| api/…/rbac_test.go | Role-wide checks against the API operation set and viewer access boundaries |
| api/…/bearer_test.go | Bearer-token restrictions and read-only operation boundaries |
| pkg/…/rbac.go | Permission-map validation, role inheritance, and OpenAPI consistency checks |
| pkg/…/token.go | API-token signing and database-backed validation |
| pkg/…/token_authdisabled_test.go | Token tenant binding and isolation between the main and auth-disabled keysets |
| pkg/…/token_readonly_test.go | Persistence of the read-only token flag during token creation |
| pkg/…/token_test.go | Integration behavior for token revocation and repository cache timing |
| pkg/…/token_embedded_authdisabled_test.go | Validation of the committed auth-disabled token against its pinned identity and tenant |
| pkg/…/token.go | Delegated user-and-tenant token validation |
| pkg/…/sessionstore.go | Protected browser session storage, stale-cookie cleanup, revocation handling, rotation, and logout behavior |
| pkg/…/sessionstore_test.go | Integration guarantees for session recovery, revocation, rotation, and cookie clearing |
| pkg/…/tenant.go | Tenant and member records that supply roles and member access settings |
| pkg/…/tenant_entitlement.go | Tenant feature gates and their local update boundary |
| pkg/…/tenant_limit.go | Tenant capacity checks and retention defaults |
| internal/…/auth.go | Tenant authentication for internal gRPC calls |
| pkg/…/loader.go | Auth mode, JWT, exchange-token, and local auth-disabled setup |