|
| 1 | +--- |
| 2 | +id: stateless-jwt |
| 3 | +title: Stateless JWT access tokens |
| 4 | +sidebar_label: Stateless JWT tokens |
| 5 | +--- |
| 6 | + |
| 7 | +# Stateless JWT access tokens |
| 8 | + |
| 9 | +This document explains how to configure stateless JWT access tokens in Ory Hydra. When enabled, JWT access tokens are issued as |
| 10 | +self-contained tokens without persisting them to the database, significantly improving performance for high-throughput workloads. |
| 11 | + |
| 12 | +Stateless JWT access tokens are available only to customers on an Ory Enterprise plan (Ory Enterprise License / Ory Network |
| 13 | +Enterprise). If you are interested in this feature, please [contact us](https://www.ory.sh/contact). |
| 14 | + |
| 15 | +## Overview |
| 16 | + |
| 17 | +By default, Ory Hydra persists all access tokens to the database, regardless of the token strategy (opaque or JWT). This |
| 18 | +persistence enables features like token introspection, revocation, and userinfo endpoint support. However, for workloads using JWT |
| 19 | +access tokens that do not require these stateful operations, database writes introduce unnecessary overhead. |
| 20 | + |
| 21 | +The stateless JWT feature optimizes performance by skipping database persistence for JWT access tokens. When enabled, access |
| 22 | +tokens are issued as self-contained JWTs with a configurable boolean claim that identifies them as stateless. Operations that |
| 23 | +require token state (introspection, revocation, and userinfo) will return an error for these tokens. |
| 24 | + |
| 25 | +This feature applies when either the OAuth2 client is configured to use the JWT access token strategy or the global access token |
| 26 | +strategy is set to JWT (instead of opaque). |
| 27 | + |
| 28 | +## How it works |
| 29 | + |
| 30 | +When stateless JWT tokens are enabled: |
| 31 | + |
| 32 | +1. **Token Generation**: JWT access tokens are issued with an additional boolean claim (default: `sl`) set to `true`. This claim |
| 33 | + identifies the token as stateless. |
| 34 | + |
| 35 | +2. **No Database Writes**: Access token sessions are not written to the database, eliminating write operations and improving |
| 36 | + performance. |
| 37 | + |
| 38 | +3. **Stateful Operations Unavailable**: Operations that require token state return HTTP 501 (Not Implemented) with error |
| 39 | + `unsupported_token_type`: |
| 40 | + |
| 41 | + - **Token Introspection** (`/oauth2/introspect`): Returns 501 for stateless JWT tokens |
| 42 | + - **Token Revocation** (`/oauth2/revoke`): Returns 501 for stateless JWT tokens |
| 43 | + - **Userinfo Endpoint** (`/userinfo`): Returns 501 for stateless JWT tokens |
| 44 | + |
| 45 | +4. **Standard JWT Validation**: Token validation continues to work through standard JWT signature verification and claims |
| 46 | + validation. |
| 47 | + |
| 48 | +## Configuration |
| 49 | + |
| 50 | +Configure stateless JWT access tokens using the `strategies.jwt.stateless` configuration namespace. |
| 51 | + |
| 52 | +### Configuration keys |
| 53 | + |
| 54 | +Two configuration keys control stateless JWT behavior: |
| 55 | + |
| 56 | +- `strategies.jwt.stateless.enabled`: Boolean flag to enable or disable stateless JWT tokens. Default: `false` |
| 57 | +- `strategies.jwt.stateless.claim_name`: String value specifying the claim name used to identify stateless tokens. Default: `sl` |
| 58 | + |
| 59 | +### Example configuration |
| 60 | + |
| 61 | +```yaml |
| 62 | +strategies: |
| 63 | + jwt: |
| 64 | + stateless: |
| 65 | + enabled: true |
| 66 | + claim_name: sl |
| 67 | +``` |
| 68 | +
|
| 69 | +In this configuration: |
| 70 | +
|
| 71 | +- Stateless JWT tokens are enabled |
| 72 | +- JWT access tokens will include a top-level claim `"sl": true` |
| 73 | +- Database writes for JWT access token sessions are skipped |
| 74 | + |
| 75 | +### Custom claim name |
| 76 | + |
| 77 | +You can customize the claim name used to identify stateless tokens: |
| 78 | + |
| 79 | +```yaml |
| 80 | +strategies: |
| 81 | + jwt: |
| 82 | + stateless: |
| 83 | + enabled: true |
| 84 | + claim_name: stateless |
| 85 | +``` |
| 86 | + |
| 87 | +With this configuration, JWT access tokens will contain `"stateless": true` instead of the default `"sl": true`. |
| 88 | + |
| 89 | +## Token format |
| 90 | + |
| 91 | +When stateless JWT tokens are enabled, the generated JWT access token includes the configured stateless claim as a top-level |
| 92 | +boolean claim. |
| 93 | + |
| 94 | +### Example JWT payload |
| 95 | + |
| 96 | +```json |
| 97 | +{ |
| 98 | + "iss": "https://your-hydra-instance.com", |
| 99 | + "sub": "user-id", |
| 100 | + "aud": ["api-resource"], |
| 101 | + "exp": 1735689600, |
| 102 | + "iat": 1735686000, |
| 103 | + "scope": "openid profile email", |
| 104 | + "sl": true |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +The `sl` claim (or your custom claim name) with a boolean value of `true` identifies this token as stateless. |
| 109 | + |
| 110 | +## Functional limitations |
| 111 | + |
| 112 | +Enabling stateless JWT tokens disables certain OAuth2 and OpenID Connect features that require access to persisted token state. |
| 113 | + |
| 114 | +### Token introspection |
| 115 | + |
| 116 | +**Endpoint**: `/oauth2/introspect` |
| 117 | + |
| 118 | +When introspecting a stateless JWT access token, the endpoint returns: |
| 119 | + |
| 120 | +- **HTTP Status**: 501 Not Implemented |
| 121 | +- **Error**: `unsupported_token_type` |
| 122 | + |
| 123 | +Standard opaque and JWT access tokens (with stateless disabled) continue to support introspection normally. |
| 124 | + |
| 125 | +### Token revocation |
| 126 | + |
| 127 | +**Endpoint**: `/oauth2/revoke` |
| 128 | + |
| 129 | +Attempting to revoke a stateless JWT access token returns: |
| 130 | + |
| 131 | +- **HTTP Status**: 501 Not Implemented |
| 132 | +- **Error**: `unsupported_token_type` |
| 133 | + |
| 134 | +Since stateless tokens are not persisted in the database, they cannot be revoked. Token expiration is enforced through the JWT |
| 135 | +`exp` claim during validation. |
| 136 | + |
| 137 | +### Userinfo endpoint |
| 138 | + |
| 139 | +**Endpoint**: `/userinfo` |
| 140 | + |
| 141 | +Requesting user information with a stateless JWT access token returns: |
| 142 | + |
| 143 | +- **HTTP Status**: 501 Not Implemented |
| 144 | +- **Error**: `unsupported_token_type` |
| 145 | + |
| 146 | +The userinfo endpoint requires database lookups to retrieve the consent session data associated with the access token, which is |
| 147 | +not available for stateless tokens. |
| 148 | + |
| 149 | +## When to use stateless JWT tokens |
| 150 | + |
| 151 | +Stateless JWT access tokens are suitable for scenarios where: |
| 152 | + |
| 153 | +- **High throughput is required**: Applications with high token issuance rates benefit from eliminating database writes |
| 154 | +- **Token revocation is not needed**: Workloads that rely solely on JWT expiration for token lifecycle management |
| 155 | +- **Introspection is not used**: Resource servers validate tokens using JWT signature verification rather than introspection |
| 156 | +- **Userinfo endpoint is not required**: Client applications do not call the userinfo endpoint for user information |
| 157 | +- **JWT access tokens are used**: The feature only applies when clients or the global strategy is configured for JWT tokens (not |
| 158 | + opaque tokens) |
| 159 | + |
| 160 | +## When not to use stateless JWT tokens |
| 161 | + |
| 162 | +Do not enable stateless JWT tokens if your application requires: |
| 163 | + |
| 164 | +- **Token revocation**: Immediate invalidation of access tokens before expiration |
| 165 | +- **Token introspection**: Validating tokens through the introspection endpoint |
| 166 | +- **Userinfo endpoint support**: Retrieving user information associated with access tokens |
| 167 | +- **Audit trail of active tokens**: Database records of issued tokens for compliance or auditing purposes |
| 168 | + |
| 169 | +## Performance considerations |
| 170 | + |
| 171 | +Enabling stateless JWT tokens provides performance benefits by: |
| 172 | + |
| 173 | +- Eliminating database write operations for access token sessions |
| 174 | +- Reducing database connection pool usage during token issuance |
| 175 | +- Decreasing storage requirements by not persisting JWT access tokens |
0 commit comments