JSON Web Token authentication
Some visualization tools (e.g., custom front-end applications) can pass access tokens based on the JSON Web Token (JWT) standard to Cube. These tokens can be either generated by these applications or obtained from an identity provider. Cube then validates these tokens. The diagram below shows how it works during the request processing in Cube:Configuration
Relevant configuration options:check_auth and jwt.
Relevant environment variables: CUBEJS_API_SECRET, CUBEJS_JWT_KEY, CUBEJS_JWK_URL,
CUBEJS_JWT_AUDIENCE, CUBEJS_JWT_ISSUER, CUBEJS_JWT_SUBJECT, CUBEJS_JWT_CLAIMS_NAMESPACE.
Custom authentication
Cube allows you to provide your own JWT verification logic. You can use thecheck_auth configuration option to verify a JWT and set the security context.
A typical use case would be:
- A web server serves a page which needs to communicate with the Cube API.
- The web server generates a JWT. The server includes the token in the page or provides the token to the frontend via an XHR request. The token is then stored in the local storage or a cookie.
- The token is used for calls to the Cube API.
- The token is received by Cube, and verified using any available JWKS (if configured)
- Once decoded, the token claims are injected into the security context.
In development mode, the token is not required for authorization, but you
can still use it to pass a security context.
Generating JSON Web Tokens
Authentication tokens are generated based on your API secret. Cube CLI generates an API Secret when a project is scaffolded and saves this value in the.env
file as CUBEJS_API_SECRET.
You can generate two types of tokens:
- Without security context, which will mean that all users will have the same data access permissions.
- With security context, which will allow you to implement role-based security models where users will have different levels of access to data.
It is considered best practice to use an
exp expiration claim to limit the
lifetime of your public tokens. Learn more in the JWT docs.jsonwebtoken package for this.
Using JSON Web Key Sets
Looking for a guide on how to connect a specific identity provider? Check out
our recipes for using Auth0 or AWS
Cognito with Cube.
Using a key as a JSON string
Add the following to yourcube.js configuration file:
Using a key from a URL
When using a URL to fetch the JWKS, Cube will automatically cache the response,
re-use it and update if a key rotation has occurred.
cube.js configuration file:
Verifying claims
Cube can also verify the audience, subject and issuer claims in JWTs. Similarly to JWK configuration, these can also be configured in thecube.js
configuration file:
Custom claims namespace
Cube can also extract claims defined in custom namespaces. Simply specify the namespace in yourcube.js configuration file: