1- ---
2- redirect_from :
3- - /security
4- ---
1+ # Access control
52
6- # Overview
3+ Cube supports a few methods to authenticates requests to [ APIs & integrations] [ ref-apis ] .
4+ Usually, an API supports authentication via [ one of these methods] [ ref-apis-methods ] :
5+
6+ * [ User name and password] ( #user-name-and-password ) (e.g., used by the [ SQL API] [ ref-sql-api ] ).
7+ * [ Identity provider] ( #identity-provider ) (e.g., used by the [ Cube Cloud for Sheets] [ ref-cube-cloud-sheets ] ).
8+ * [ Access token] ( #access-token ) (e.g., used by the [ REST API] [ ref-rest-api ] ).
9+
10+ Regardless of the method, the authentication flow includes the following steps:
11+
12+ * User identity information (e.g., password or access token) is passed to Cube.
13+ * Cube validates the identity information or trusts the identity provider.
14+ * User identity information is optionally enriched with additional attributes,
15+ such the user's role, via [ authentication integration] ( #authentication-integration ) .
16+ * Finally, the API request is associated with a [ security context] [ ref-sec-ctx ] .
17+ It is then used to configure [ member-level security] [ ref-mls ]
18+ and [ row-level security] [ ref-rls ] as well as set [ data access policies] [ ref-dap ] .
19+
20+ ## User name and password
21+
22+ Some visualization tools (e.g., BI tools) can pass user name and, sometimes, password to Cube.
23+
24+ ### Configuration
25+
26+ Relevant configuration options: [ ` check_sql_auth ` ] [ ref-config-check-sql-auth ] and [ ` can_switch_sql_user ` ] [ ref-config-can-switch-sql-user ] .
27+
28+ Relevant environment variables: ` CUBEJS_SQL_USER ` , ` CUBEJS_SQL_PASSWORD ` , ` CUBEJS_SQL_SUPER_USER ` .
29+
30+ ## Identity provider
31+
32+ Some visualization tools (e.g., [ Cube Cloud for Sheets] [ ref-cube-cloud-sheets ] ) implement
33+ an OAuth 2.0 flow to authenticate users.
34+
35+ <SuccessBox >
36+
37+ OAuth 2.0 flow is available in Cube Cloud on [ Premium and above] ( https://cube.dev/pricing ) product tiers.
38+
39+ </SuccessBox >
40+
41+ During this flow, users are redirected to a login page of an identity provider (e.g., Google)
42+ where they enter their credentials. Then, the identity provider passes the user identity
43+ information to Cube Cloud.
44+
45+ This method does not require any configuration.
46+
47+ ## Access token
48+
49+ Some visualization tools (e.g., custom front-end applications) can pass access tokens based
50+ on the [ JSON Web Token] [ wiki-jwt ] (JWT) standard to Cube. These tokens can be either generated
51+ by these applications or obtained from an identity provider. Cube then validates these tokens.
752
8- In Cube, authorization (or access control) is based on the ** security context** .
953The diagram below shows how it works during the request processing in Cube:
1054
1155<div style = { { textAlign: " center" }} >
@@ -16,18 +60,44 @@ The diagram below shows how it works during the request processing in Cube:
1660 />
1761</div >
1862
19- Authentication is handled outside of Cube. A typical use case would be:
63+ ### Configuration
64+
65+ Relevant configuration options: [ ` check_auth ` ] [ ref-config-check-auth ] and [ ` jwt ` ] [ ref-config-jwt ] .
66+
67+ Relevant environment variables: ` CUBEJS_API_SECRET ` , ` CUBEJS_JWT_KEY ` , ` CUBEJS_JWK_URL ` ,
68+ ` CUBEJS_JWT_AUDIENCE ` , ` CUBEJS_JWT_ISSUER ` , ` CUBEJS_JWT_SUBJECT ` , ` CUBEJS_JWT_CLAIMS_NAMESPACE ` .
69+
70+ ### Custom authentication
71+
72+ Cube allows you to provide your own JWT verification logic. You can use the
73+ [ ` check_auth ` ] [ ref-config-check-auth ] configuration option to verify a JWT and set the security context.
2074
21- 1 . A web server serves an HTML page containing the Cube client, which needs to
22- communicate securely with the Cube API.
23- 2 . The web server should generate a JWT with an expiry to achieve this. The
24- server could include the token in the HTML it serves or provide the token to
25- the frontend via an XHR request, which is then stored it in local storage or
75+ For example, if you needed to retrieve user information from an LDAP server,
76+ you might do the following:
77+
78+ ``` javascript
79+ module .exports = {
80+ checkAuth: async (req , auth ) => {
81+ try {
82+ const userInfo = await getUserFromLDAP (req .get (" X-LDAP-User-ID" ))
83+ return { security_context: userInfo }
84+ }
85+ catch {
86+ throw new Error (" Could not authenticate user from LDAP" )
87+ }
88+ }
89+ }
90+ ```
91+
92+ A typical use case would be:
93+
94+ 1 . A web server serves a page which needs to communicate with the Cube API.
95+ 2 . The web server generates a JWT. The
96+ server includes the token in the page or provides the token to
97+ the frontend via an XHR request. The token is then stored in the local storage or
2698 a cookie.
27- 3 . The JavaScript client is initialized using this token, and includes it in
28- calls to the Cube API.
29- 4 . The token is received by Cube, and verified using any available JWKS (if
30- configured)
99+ 3 . The token is used for calls to the Cube API.
100+ 4 . The token is received by Cube, and verified using any available JWKS (if configured)
311015 . Once decoded, the token claims are injected into the [ security
32102 context] [ ref-sec-ctx ] .
33103
@@ -38,7 +108,7 @@ can still use it to [pass a security context][ref-sec-ctx].
38108
39109</InfoBox >
40110
41- ## Generating JSON Web Tokens (JWT)
111+ ### Generating JSON Web Tokens
42112
43113Authentication tokens are generated based on your API secret. Cube CLI generates
44114an API Secret when a project is scaffolded and saves this value in the ` .env `
@@ -118,7 +188,7 @@ const cubeApi = cube(
118188You can optionally store this token in local storage or in a cookie, so that you
119189can then use it to query the Cube API.
120190
121- ## Using JSON Web Key Sets (JWKS)
191+ ### Using JSON Web Key Sets
122192
123193<InfoBox >
124194
@@ -128,8 +198,6 @@ Cognito][ref-recipe-cognito] with Cube.
128198
129199</InfoBox >
130200
131- ### Configuration
132-
133201As mentioned previously, Cube supports verifying JWTs using industry-standard
134202JWKS. The JWKS can be provided either from a URL, or as a JSON object conforming
135203to [ JWK specification RFC 7517 Section 4] [ link-jwk-ref ] , encoded as a string.
@@ -177,7 +245,7 @@ Or configure the same using environment variables:
177245CUBEJS_JWK_URL='<URL_TO_JWKS_JSON>'
178246```
179247
180- ### Verifying claims
248+ #### Verifying claims
181249
182250Cube can also verify the audience, subject and issuer claims in JWTs. Similarly
183251to JWK configuration, these can also be configured in the ` cube.js `
@@ -201,7 +269,7 @@ CUBEJS_JWT_ISSUER='<ISSUER_FROM_IDENTITY_PROVIDER>'
201269CUBEJS_JWT_SUBJECT='<SUBJECT_FROM_IDENTITY_PROVIDER>'
202270```
203271
204- ### Custom claims namespace
272+ #### Custom claims namespace
205273
206274Cube can also extract claims defined in custom namespaces. Simply specify the
207275namespace in your ` cube.js ` configuration file:
@@ -214,49 +282,42 @@ module.exports = {
214282};
215283```
216284
217- ### Caching
285+ ## Authentication integration
218286
219- Cube caches JWKS by default when
220- [ ` CUBEJS_JWK_URL ` or ` jwt.jwkUrl ` is specified] ( #configuration ) .
287+ When using Cube Cloud, you can enrich the security context with information about
288+ an authenticated user, obtained during their authentication or loaded via an
289+ [ LDAP integration] [ ref-ldap-integration ] .
221290
222- - If the response contains a ` Cache-Control ` header, then Cube uses it to
223- determine cache expiry.
224- - The keys inside the JWKS are checked for expiry values and used for cache
225- expiry.
226- - If an inbound request supplies a JWT referencing a key not found in the cache,
227- the cache is refreshed.
291+ <SuccessBox >
228292
229- ## Custom authentication
293+ Authentication integration is available in Cube Cloud on [ all product tiers ] ( https://cube.dev/pricing ) .
230294
231- Cube also allows you to provide your own JWT verification logic by setting a
232- [ ` checkAuth() ` ] [ ref-config-check-auth ] function in the ` cube.js ` configuration
233- file. This function is expected to verify a JWT and return its claims as the
234- security context.
295+ </SuccessBox >
235296
236- As an example, if you needed to retrieve user information from an LDAP server,
237- you might do the following:
297+ You can enable the authentication integration by navigating to the < Btn >Settings → Configuration</ Btn >
298+ of your Cube Cloud deployment and using the < Btn >Enable Cloud Auth Integration</ Btn > toggle.
238299
239- ``` javascript
240- module .exports = {
241- checkAuth: async (req , auth ) => {
242- try {
243- const userInfo = await getUserFromLDAP (req .get (" X-LDAP-User-ID" ));
244- return { security_context: userInfo };
245- } catch {
246- throw new Error (" Could not authenticate user from LDAP" );
247- }
248- },
249- };
250- ```
251300
252- [ link -jwt-docs ] :
253- https://github.com/auth0/node-jsonwebtoken#token-expiration-exp-claim
301+ [ wiki -jwt] : https://en.wikipedia.org/wiki/JSON_Web_Token
302+ [ link-jwt-docs ] : https://github.com/auth0/node-jsonwebtoken#token-expiration-exp-claim
254303[ link-jwt-libs ] : https://jwt.io/#libraries-io
255304[ link-jwk-ref ] : https://tools.ietf.org/html/rfc7517#section-4
256- [ ref-config-check-auth ] : /reference/configuration/config#checkauth
305+ [ ref-config-check-auth ] : /reference/configuration/config#check_auth
306+ [ ref-config-jwt ] : /reference/configuration/config#jwt
307+ [ ref-config-check-sql-auth ] : /reference/configuration/config#check_sql_auth
308+ [ ref-config-can-switch-sql-user ] : /reference/configuration/config#can_switch_sql_user
257309[ ref-config-migrate-cube] :
258310 /product/configuration#migration-from-express-to-docker-template
259311[ ref-recipe-auth0 ] : /guides/recipes/auth/auth0-guide
260312[ ref-recipe-cognito ] : /guides/recipes/auth/aws-cognito
261313[ ref-sec-ctx ] : /product/auth/context
262- [ link-slack ] : https://slack.cube.dev/
314+ [ ref-apis ] : /product/apis-integrations
315+ [ ref-apis-methods ] : /product/apis-integrations#authentication-methods
316+ [ ref-rest-api ] : /product/apis-integrations/rest-api
317+ [ ref-sql-api ] : /product/apis-integrations/sql-api
318+ [ ref-dap ] : /product/auth/data-access-policies
319+ [ ref-mls ] : /product/auth/member-level-security
320+ [ ref-rls ] : /product/auth/row-level-security
321+ [ ref-auth-sso ] : /product/workspace/sso
322+ [ ref-ldap-integration ] : /product/workspace/sso#ldap-integration
323+ [ ref-cube-cloud-sheets ] : /product/apis-integrations/google-sheets
0 commit comments