11# Access control
22
3- In Cube, authorization (or access control) is based on the ** security context** .
3+ Cube authenticates requests to [ APIs & integrations] [ ref-apis ] via a number
4+ of API-specific mechanisms. Most APIs use the following mechanisms:
5+
6+ * [ User name and password] ( #user-name-and-password ) authentication (e.g., used by the [ SQL API] [ ref-sql-api ] ).
7+ * [ JSON Web Tokens] ( #json-web-tokens ) (JWT) (e.g., used by the [ REST API] [ ref-rest-api ] ).
8+
9+ Once the authentication is completed, the API request is associated with a
10+ [ security context] [ ref-sec-ctx ] . It is then used to configure [ member-level security] [ ref-mls ]
11+ and [ row-level security] [ ref-rls ] as well as set [ data access policies] [ ref-dap ] .
12+
13+ ## User name and password
14+
15+ With this mechanism, user name and password are checked.
16+
17+ ### Configuration
18+
19+ Relevant configuration options: [ ` check_sql_auth ` ] [ ref-config-check-sql-auth ] and [ ` can_switch_sql_user ` ] [ ref-config-can-switch-sql-user ] .
20+
21+ Relevant environment variables: ` CUBEJS_SQL_USER ` , ` CUBEJS_SQL_PASSWORD ` , ` CUBEJS_SQL_SUPER_USER ` .
22+
23+ ### Authentication integration
24+
25+ When using Cube Cloud, you can also validate user name and password pairs via
26+ the Cube Cloud [ authentication mechanism] [ ref-auth-sso ] , including its [ LDAP integration] [ ref-ldap-integration ] .
27+
28+ <SuccessBox >
29+
30+ Authentication integration is available in Cube Cloud on the [ Enterprise and above] ( https://cube.dev/pricing ) product tiers.
31+
32+ </SuccessBox >
33+
34+ You can enable the authentication integration by navigating to the <Btn >Settings → Configuration</Btn >
35+ of your Cube Cloud deployment and using the <Btn >Enable Cloud Auth Integration</Btn > toggle.
36+
37+ ## JSON Web Tokens
38+
39+ With this mechanism, a JSON Web Token (JWT) is checked.
40+
41+ ### Configuration
42+
43+ Relevant configuration options: [ ` check_auth ` ] [ ref-config-check-auth ] and [ ` jwt ` ] [ ref-config-jwt ] .
44+
45+ Relevant environment variables: ` CUBEJS_API_SECRET ` , ` CUBEJS_JWT_KEY ` , ` CUBEJS_JWK_URL ` ,
46+ ` CUBEJS_JWT_AUDIENCE ` , ` CUBEJS_JWT_ISSUER ` , ` CUBEJS_JWT_SUBJECT ` , ` CUBEJS_JWT_CLAIMS_NAMESPACE ` .
47+
448The diagram below shows how it works during the request processing in Cube:
549
650<div style = { { textAlign: " center" }} >
@@ -11,18 +55,37 @@ The diagram below shows how it works during the request processing in Cube:
1155 />
1256</div >
1357
14- Authentication is handled outside of Cube. A typical use case would be:
58+ ### Custom authentication
59+
60+ Cube also allows you to provide your own JWT verification logic. You can use the
61+ [ ` check_auth ` ] [ ref-config-check-auth ] configuration option to verify a JWT and set the security context.
62+
63+ For example, if you needed to retrieve user information from an LDAP server,
64+ you might do the following:
1565
16- 1 . A web server serves an HTML page containing the Cube client, which needs to
17- communicate securely with the Cube API.
18- 2 . The web server should generate a JWT with an expiry to achieve this. The
19- server could include the token in the HTML it serves or provide the token to
20- the frontend via an XHR request, which is then stored it in local storage or
66+ ``` javascript
67+ module .exports = {
68+ checkAuth: async (req , auth ) => {
69+ try {
70+ const userInfo = await getUserFromLDAP (req .get (" X-LDAP-User-ID" ))
71+ return { security_context: userInfo }
72+ }
73+ catch {
74+ throw new Error (" Could not authenticate user from LDAP" )
75+ }
76+ }
77+ }
78+ ```
79+
80+ A typical use case would be:
81+
82+ 1 . A web server serves a page which needs to communicate with the Cube API.
83+ 2 . The web server generates a JWT. The
84+ server includes the token in the page or provides the token to
85+ the frontend via an XHR request. The token is then stored in the local storage or
2186 a cookie.
22- 3 . The JavaScript client is initialized using this token, and includes it in
23- calls to the Cube API.
24- 4 . The token is received by Cube, and verified using any available JWKS (if
25- configured)
87+ 3 . The token is used for calls to the Cube API.
88+ 4 . The token is received by Cube, and verified using any available JWKS (if configured)
26895 . Once decoded, the token claims are injected into the [ security
2790 context] [ ref-sec-ctx ] .
2891
@@ -33,7 +96,7 @@ can still use it to [pass a security context][ref-sec-ctx].
3396
3497</InfoBox >
3598
36- ## Generating JSON Web Tokens (JWT)
99+ ### Generating JSON Web Tokens
37100
38101Authentication tokens are generated based on your API secret. Cube CLI generates
39102an API Secret when a project is scaffolded and saves this value in the ` .env `
@@ -113,7 +176,7 @@ const cubeApi = cube(
113176You can optionally store this token in local storage or in a cookie, so that you
114177can then use it to query the Cube API.
115178
116- ## Using JSON Web Key Sets (JWKS)
179+ ### Using JSON Web Key Sets
117180
118181<InfoBox >
119182
@@ -123,8 +186,6 @@ Cognito][ref-recipe-cognito] with Cube.
123186
124187</InfoBox >
125188
126- ### Configuration
127-
128189As mentioned previously, Cube supports verifying JWTs using industry-standard
129190JWKS. The JWKS can be provided either from a URL, or as a JSON object conforming
130191to [ JWK specification RFC 7517 Section 4] [ link-jwk-ref ] , encoded as a string.
@@ -172,7 +233,7 @@ Or configure the same using environment variables:
172233CUBEJS_JWK_URL='<URL_TO_JWKS_JSON>'
173234```
174235
175- ### Verifying claims
236+ #### Verifying claims
176237
177238Cube can also verify the audience, subject and issuer claims in JWTs. Similarly
178239to JWK configuration, these can also be configured in the ` cube.js `
@@ -196,7 +257,7 @@ CUBEJS_JWT_ISSUER='<ISSUER_FROM_IDENTITY_PROVIDER>'
196257CUBEJS_JWT_SUBJECT='<SUBJECT_FROM_IDENTITY_PROVIDER>'
197258```
198259
199- ### Custom claims namespace
260+ #### Custom claims namespace
200261
201262Cube can also extract claims defined in custom namespaces. Simply specify the
202263namespace in your ` cube.js ` configuration file:
@@ -209,49 +270,25 @@ module.exports = {
209270};
210271```
211272
212- ### Caching
213-
214- Cube caches JWKS by default when
215- [ ` CUBEJS_JWK_URL ` or ` jwt.jwkUrl ` is specified] ( #configuration ) .
216-
217- - If the response contains a ` Cache-Control ` header, then Cube uses it to
218- determine cache expiry.
219- - The keys inside the JWKS are checked for expiry values and used for cache
220- expiry.
221- - If an inbound request supplies a JWT referencing a key not found in the cache,
222- the cache is refreshed.
223-
224- ## Custom authentication
225-
226- Cube also allows you to provide your own JWT verification logic by setting a
227- [ ` checkAuth() ` ] [ ref-config-check-auth ] function in the ` cube.js ` configuration
228- file. This function is expected to verify a JWT and return its claims as the
229- security context.
230-
231- As an example, if you needed to retrieve user information from an LDAP server,
232- you might do the following:
233-
234- ``` javascript
235- module .exports = {
236- checkAuth: async (req , auth ) => {
237- try {
238- const userInfo = await getUserFromLDAP (req .get (" X-LDAP-User-ID" ));
239- return { security_context: userInfo };
240- } catch {
241- throw new Error (" Could not authenticate user from LDAP" );
242- }
243- },
244- };
245- ```
246273
247274[ link-jwt-docs] :
248275 https://github.com/auth0/node-jsonwebtoken#token-expiration-exp-claim
249276[ link-jwt-libs ] : https://jwt.io/#libraries-io
250277[ link-jwk-ref ] : https://tools.ietf.org/html/rfc7517#section-4
251- [ ref-config-check-auth ] : /reference/configuration/config#checkauth
278+ [ ref-config-check-auth ] : /reference/configuration/config#check_auth
279+ [ ref-config-jwt ] : /reference/configuration/config#jwt
280+ [ ref-config-check-sql-auth ] : /reference/configuration/config#check_sql_auth
281+ [ ref-config-can-switch-sql-user ] : /reference/configuration/config#can_switch_sql_user
252282[ ref-config-migrate-cube] :
253283 /product/configuration#migration-from-express-to-docker-template
254284[ ref-recipe-auth0 ] : /guides/recipes/auth/auth0-guide
255285[ ref-recipe-cognito ] : /guides/recipes/auth/aws-cognito
256286[ ref-sec-ctx ] : /product/auth/context
257- [ link-slack ] : https://slack.cube.dev/
287+ [ ref-apis ] : /product/apis-integrations
288+ [ ref-rest-api ] : /product/apis-integrations/rest-api
289+ [ ref-sql-api ] : /product/apis-integrations/sql-api
290+ [ ref-dap ] : /product/auth/data-access-policies
291+ [ ref-mls ] : /product/auth/member-level-security
292+ [ ref-rls ] : /product/auth/row-level-security
293+ [ ref-auth-sso ] : /product/workspace/sso
294+ [ ref-ldap-integration ] : /product/workspace/sso#ldap-integration
0 commit comments