JSON Web Tokens (JWTs)
Configure how Nhost Auth signs JWT access tokens using symmetric keys, asymmetric keys, or an external signing service.
JWT JSON Web Token access token refresh token claims token expiration bearer tokenIntroduction
Section titled “Introduction”JSON Web Tokens (JWT) are encoded strings designed to securely transmit information between parties in the form of a JSON object. Each JWT consists of three parts:
- header
- payload
- signature
JWTs are commonly used for authentication post-login. The server generates a token containing user claims (like identity and permissions) that subsequent requests can include to prove authorization.
Here’s how JWTs typically work in an authentication flow:
- User logs in with credentials (username/password)
- Server validates credentials and generates a signed JWT containing user information and permissions
- Server sends the JWT to the client, which stores it (usually in browser storage)
- For subsequent requests, the client includes the JWT in the Authorization header
- Server verifies the token’s signature and grants access based on the encoded permissions
The main advantage is that the server doesn’t need to store session information - all necessary data is contained within the token itself, making it ideal for stateless authentication.
JWT Configuration
Section titled “JWT Configuration”You can configure your project to use three different kinds of JWTs:
- JWTs signed with symmetric keys
- JWTs signed with asymmetric keys
- JWTs signed externally via a third-party service
Symmetric Keys
Section titled “Symmetric Keys”With symmetric keys, your project uses a single key for both signing and verifying JWTs. This key is stored in the project’s configuration and is responsible for signing JWTs. When a client sends a JWT to the server, the server uses the same key to verify the JWT’s signature. If you need to verify JWTs in a different service, the same key can be used for verification. Since the same key is used for both signing and verification, it is crucial to keep it secret, as sharing it with others can compromise the security of your JWTs.
Below you can see an example of a symmetric key configuration:
[[hasura.jwtSecrets]]type = 'HS256'key = 'f03d5f5a0ed055e3fcbc0a3639405aca0511e6abe6d60e40d1fff610c6248f2a'
In addition to HS256, you can also use HS384 and HS512 for extra security. To quickly generate a key, you can use the following command:
openssl rand -base64 32openssl rand -base64 48openssl rand -base64 64Asymmetric Keys
Section titled “Asymmetric Keys”With asymmetric keys, your project uses a pair of public and private keys for signing and verifying JWTs. The private key, stored securely in the project’s configuration, is used to sign the JWTs. The public key, on the other hand, is made available to clients and is used to verify the JWTs. When a client sends a JWT to the server, the server uses the public key to validate the JWT’s signature. If verification is needed in a different service, the public key can be used without compromising security. Since the public key is only used for verification and the private key for signing, sharing the public key is safe and does not jeopardize the security of your JWTs.
Below you can see an example of an asymmetric key configuration:
[[hasura.jwtSecrets]]type = "RS256"kid = "bskhwtelkajsd"key = ""-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqSFS8Kx9LuiYpIms+NoZ(omitted for brevity)jwIDAQAB-----END PUBLIC KEY-----""signingKey = ""-----BEGIN PRIVATE KEY-----MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCpIVLwrH0u6Jik(omitted for brevity)s6fJmz3ZeArPI8KFSI3Q2xqm-----END PRIVATE KEY-----""
In addition to RS256, you can also use RS384 and RS512 for extra security. To quickly generate a key pair, you can use the following commands:
# Generate a private keyopenssl genpkey -algorithm RSA -out jwt_private.pem -pkeyopt rsa_keygen_bits:2048
# Generate a public key from the private keyopenssl rsa -pubout -in jwt_private.pem -out jwt_public.pem# Generate a private keyopenssl genpkey -algorithm RSA -out jwt_private.pem -pkeyopt rsa_keygen_bits:3072
# Generate a public key from the private keyopenssl rsa -pubout -in jwt_private.pem -out jwt_public.pem# Generate a private keyopenssl genpkey -algorithm RSA -out jwt_private.pem -pkeyopt rsa_keygen_bits:4096
# Generate a public key from the private keyopenssl rsa -pubout -in jwt_private.pem -out jwt_public.pemYou can then copy the contents of jwt_private.pem into the signingKey field and the contents of jwt_public.pem into the key field.
The kid value in your configuration can be any unique string of your choice and must be distinct for each key. It is used to identify the correct key when verifying JWTs through the JWKS endpoint.
External Signing
Section titled “External Signing”If you are using a third party service like Auth0 or Clerk you can configure your project to use their JWK endpoint to verify JWTs. Below you can see an example of an external signing configuration:
[[hasura.jwtSecrets]]jwk_url = "https://mythirdpartyservice.com/jwks.json"Alternatively, you can configure the public key directly:
[[hasura.jwtSecrets]]type = "RS256"key = ""-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqSFS8Kx9LuiYpIms+NoZ(omitted for brevity)jwIDAQAB-----END PUBLIC KEY-----""
Claims Mapping and Namespace
Section titled “Claims Mapping and Namespace”When verifying tokens — especially those issued by an external provider — you can remap their claims onto the session variables your permissions expect, and control where those claims are read from. The claims_namespace defaults to https://hasura.io/jwt/claims (a retained identifier kept for compatibility) and can be overridden with claims_namespace or claims_namespace_path. Use claims_map to project individual claims from a literal value or a JSON path within the token.
[[hasura.jwtSecrets]]jwk_url = "https://my-auth-server.com/.well-known/jwks.json"issuer = "https://my-auth-server.com"
[[hasura.jwtSecrets.claims_map]]claim = "x-some-claim"value = "some-value"
[[hasura.jwtSecrets.claims_map]]claim = "x-other-claim"path = "$.user.claim.id"default = "default-value"#JWTSecret: ({ type: "HS384" | "HS512" | *"HS256" key: string } | { type: "RS256" | "RS384" | "RS512" key: string signingKey?: string kid?: string } | { jwk_url: #Url | *null }) & { claims_format?: "stringified_json" | *"json" audience?: string issuer?: string allowed_skew?: uint32 header?: string } & { claims_map?: [...#ClaimMap] } & ({ claims_namespace: string | *"https://hasura.io/jwt/claims" } | { claims_namespace_path: string } | *{})
#ClaimMap: { claim: string { value: string } | { path: string default?: string }}The complete #JWTSecret schema, with every field and default, is documented in the configuration reference.
Token Types and Claims
Section titled “Token Types and Claims”Nhost Auth issues four kinds of JWT: first-party session tokens for your app, OAuth2 access tokens for delegated API access, OIDC ID tokens that identify users to OAuth2 clients, and short-lived provider-state tokens used by provider callbacks. All four use the same configured signing key and algorithm (alg), include a kid header when a key ID is configured, and carry the Auth issuer in iss and the issuance time in iat.
The table below compares their headers and claims. typ is a JWT header field; the remaining rows describe payload claims. See Tokens and Scopes for more about the OAuth2 claims and their scope requirements.
| Field | First-party session token | OAuth2 access token | OIDC ID token | Provider-state token |
|---|---|---|---|---|
typ (header) |
nhost-session+jwt |
at+jwt |
JWT |
nhost-provider-state+jwt |
sub |
User ID | User ID | User ID | — |
aud |
Project audience when configured; otherwise omitted | Project audience when configured; otherwise OAuth2 client ID | OAuth2 client ID | Project audience with the /auth/provider-callback suffix when configured; otherwise omitted |
client_id |
— | OAuth2 client ID | — | — |
sid |
Session refresh-token record ID | OAuth2 refresh-token record ID | — | — |
jti |
Unique access-token ID | Unique access-token ID | — | — |
exp |
iat plus auth.session.accessToken.expiresIn |
iat plus auth.oauth2Provider.accessToken.expiresIn |
Same as the OAuth2 access token | iat plus one minute |
scope |
— | Granted scopes as a space-separated string | — | — |
https://hasura.io/jwt/claims (default namespace) |
User ID, allowed and default roles, anonymous status, custom claims, and x-hasura-auth-elevated after elevation |
Included for graphql or graphql:role:…; roles follow the granted scopes |
— | — |
| Other claims | — | — | Identity claims; see ID Token | Internal sign-in state, not meant to be read by your code |
Refresh tokens and personal access tokens (PATs) are opaque credentials with no JWT headers or claims. Signing in with a PAT creates a first-party session token.
Session Access Token Claims
Section titled “Session Access Token Claims”Auth-issued session access tokens include a payload like this (timestamps and IDs are illustrative):
{ "iss": "https://your-project.auth.nhost.run/v1", "sub": "f5765cb0-1c45-4b6e-8a30-0b2abc1a2f3d", "aud": "https://your-project.nhost.run", "sid": "3fb25293-8b81-44eb-b5c9-465741e54148", "jti": "64d9576c-16a5-4c08-a38b-0ead97d3b462", "iat": 1234567890, "exp": 1234568790, "https://hasura.io/jwt/claims": { "x-hasura-user-id": "f5765cb0-1c45-4b6e-8a30-0b2abc1a2f3d", "x-hasura-default-role": "user", "x-hasura-allowed-roles": ["user", "me"], "x-hasura-user-is-anonymous": "false" }}The JWT header has typ: nhost-session+jwt. The sid is the ID of the session’s refresh-token record (auth.refresh_tokens.id); it stays the same when the session is refreshed. The jti is a new random UUID for each access token, including each refreshed token. Neither is the refresh token value itself. aud is a single project-audience string when configured, and is omitted when unset. See OAuth2 tokens for the different access-token audience and session ID used by OAuth2 clients.
Configuring the Audience
Section titled “Configuring the Audience”With an Auth release that supports AUTH_JWT_AUDIENCE, set the audience in nhost.toml using a CLI release that supports [auth].audience:
[auth]audience = "https://your-project.nhost.run"Enabling an audience also affects tokens issued before it was configured. During the upgrade transition, Auth accepts tokens without aud, older OAuth2 access tokens without client_id, and OAuth2 access tokens whose sole audience is their own client_id. This keeps existing tokens usable until their normal expiry while newly issued tokens receive the configured audience. Signature, issuer, expiry, and token-purpose checks still apply.
Tokens carrying a different project audience are rejected unless they meet one of those compatibility exceptions. Clients can refresh to obtain tokens for the configured audience. Resource servers that enforce audience themselves may reject legacy tokens during the transition.
Verify a JWT
Section titled “Verify a JWT”To verify JWTs inside Nhost Functions (for both symmetric and asymmetric keys), see the JWT Verification guide.
When your own code expects a session token, for example in Functions, check for typ: nhost-session+jwt to reject tokens issued for other purposes, such as those the OAuth2 provider issues to third-party apps. Older Auth versions used typ: JWT for all token types.
The GraphQL API and Storage don’t need this check: they authorize from the token’s role claims, which OAuth2 access tokens carry only when the app was granted a graphql scope.
Custom Claims
Section titled “Custom Claims”You can attach extra information to your JWTs in the form of custom claims. These claims can be used for authorization purposes in your application. For more details on how to add custom claims to your JWTs and how to use them, see the Permissions Variables documentation.