JSON Web Tokens (JWTs)
Configure JSON Web Tokens to your needs
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
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
Currently we default to using symmetric keys for signing JWTs. However, we plan to change this to use asymmetric keys in the near future.
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:
We recommend using a secret to configure the key.
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:
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:
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:
You 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
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:
Alternatively, you can configure the public key directly:
When using external signing the Auth service will be automatically disabled.
Verify a JWT
Symmetric Keys
To verify a JWT signed with a symmetric key in a serverless function or third party service you can use code similar to the following:
Keep in mind that you need access to the same key that was used to sign the JWT in order to verify it so this mechanism may not be suitable for all use cases.
Asymmetric Keys
To verify a JWT signed with an asymmetric key you can leverage the JWKS endpoint that is automatically enabled in your project when you configure it to use asymmetric keys. The JWKS endpoint can be found at https://<subdomain>.auth.<region>.nhost.run/v1/.well-known/jwks.json
. For instance:
Using the public key from the JWKS endpoint you can verify the JWT in a serverless function using code similar to the following:
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.