Skip to content

JWT Authentication

JWT mode validates JSON Web Tokens from an external identity provider. It supports JWKS URL discovery and static public key verification.

JWT auth requires the PyJWT library:

Terminal window
pip install dockrion[jwt]

If PyJWT is not installed and mode: jwt is configured, the runtime falls back to NoAuthHandler with a warning.

auth:
mode: jwt
jwt:
jwks_url: https://your-idp.com/.well-known/jwks.json
issuer: https://your-idp.com/
audience: my-agent-api
algorithms: [RS256]
leeway_seconds: 30
claims:
user_id: sub
email: email
name: name
roles: roles
permissions: permissions
scopes: scope
tenant_id: custom.tenant_id
FieldTypeDefaultDescription
jwks_urlstringnullURL to JWKS endpoint for public key discovery
public_key_envstringnullEnvironment variable holding a static PEM public key (alternative to JWKS)
issuerstringnullExpected iss claim value. If set, tokens with a different issuer are rejected.
audiencestringnullExpected aud claim value. If set, tokens for a different audience are rejected.
algorithmslist[str]["RS256"]Allowed JWT signing algorithms
leeway_secondsint30Clock skew tolerance for exp/nbf claims (0–300)
claimsJWTClaimsConfignullMaps JWT claim names to Dockrion identity fields
FieldTypeDefaultDescription
user_idstring"sub"JWT claim for user ID
emailstring"email"JWT claim for email
namestring"name"JWT claim for display name
rolesstring"roles"JWT claim containing role list
permissionsstring"permissions"JWT claim containing permissions
scopesstring"scope"JWT claim for OAuth2 scopes
tenant_idstringnullJWT claim for tenant ID (supports dot paths like custom.tenant_id)

Claims support dot paths for nested JWT structures. For example, custom.tenant_id reads the tenant_id field inside a custom object in the JWT payload.

AlgorithmFamily
RS256, RS384, RS512RSA
ES256, ES384, ES512Elliptic Curve
HS256, HS384, HS512HMAC (symmetric)

The JWTAuthHandler processes each request:

  1. Extracts the token from Authorization: Bearer <token>
  2. If jwks_url is set, fetches public keys from the JWKS endpoint
  3. If public_key_env is set, reads the PEM key from the environment variable
  4. Decodes and validates the JWT (signature, expiration, issuer, audience)
  5. Maps claims to an AuthContext via AuthContext.from_jwt()
  6. On failure → raises AuthError (HTTP 401)
auth:
mode: jwt
jwt:
jwks_url: https://login.microsoftonline.com/{tenant}/discovery/v2.0/keys
algorithms: [RS256]

The runtime fetches keys from the JWKS endpoint. Key rotation is handled automatically by the identity provider.

auth:
mode: jwt
jwt:
public_key_env: JWT_PUBLIC_KEY
algorithms: [RS256]
Terminal window
export JWT_PUBLIC_KEY="-----BEGIN PUBLIC KEY-----\nMIIB..."
Terminal window
# Generate a test token (example using jwt.io or a script)
TOKEN="eyJhbGciOiJSUzI1NiIs..."
curl -X POST http://localhost:8080/invoke \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"query": "hello"}'

JWT claims can carry role information that maps to Dockrion roles:

auth:
mode: jwt
jwt:
jwks_url: https://your-idp.com/.well-known/jwks.json
claims:
roles: realm_access.roles # dot path into JWT payload
roles:
- name: admin
permissions: [deploy, invoke, view_metrics]
- name: user
permissions: [invoke]

See Roles & Rate Limits for more on RBAC.

Source: JWTConfig, JWTClaimsConfig in packages/schema/dockrion_schema/dockfile_v1.py; JWTAuthHandler in packages/runtime/dockrion_runtime/auth/jwt_handler.py


Previous: API Key | Next: OAuth2 →