@@ -19,7 +19,7 @@ import type {
1919} from 'express' ;
2020import {
2121 QueryType ,
22- Permission ,
22+ ApiScopes ,
2323} from './types/strings' ;
2424import {
2525 QueryType as QueryTypeEnum , ResultType
@@ -44,7 +44,7 @@ import {
4444 CheckAuthInternalOptions ,
4545 JWTOptions ,
4646 CheckAuthFn ,
47- ContextToPermissionsFn ,
47+ ContextToApiScopesFn ,
4848} from './types/auth' ;
4949import {
5050 Query ,
@@ -117,9 +117,9 @@ class ApiGateway {
117117
118118 public readonly checkAuthSystemFn : CheckAuthFn ;
119119
120- protected readonly contextToPermFn : ContextToPermissionsFn ;
120+ protected readonly contextToApiScopesFn : ContextToApiScopesFn ;
121121
122- protected readonly contextToPermDefFn : ContextToPermissionsFn =
122+ protected readonly contextToApiScopesDefFn : ContextToApiScopesFn =
123123 async ( ) => [ 'liveliness' , 'graphql' , 'meta' , 'data' ] ;
124124
125125 protected readonly checkAuthMiddleware : CheckAuthMiddlewareFn ;
@@ -158,7 +158,7 @@ class ApiGateway {
158158
159159 this . checkAuthFn = this . createCheckAuthFn ( options ) ;
160160 this . checkAuthSystemFn = this . createCheckAuthSystemFn ( ) ;
161- this . contextToPermFn = this . createContextToPermissionsFn ( options ) ;
161+ this . contextToApiScopesFn = this . createContextToApiScopesFn ( options ) ;
162162 this . checkAuthMiddleware = options . checkAuthMiddleware
163163 ? this . wrapCheckAuthMiddleware ( options . checkAuthMiddleware )
164164 : this . checkAuth ;
@@ -178,19 +178,19 @@ class ApiGateway {
178178 ] ;
179179
180180 /** **************************************************************
181- * Liveliness permission *
181+ * Liveliness scope *
182182 *************************************************************** */
183183
184184 // @todo Should we pass requestLoggerMiddleware?
185- // @todo Should we add permission assert here?
185+ // @todo Should we add scope assert here?
186186
187187 const guestMiddlewares = [ ] ;
188188
189189 app . get ( '/readyz' , guestMiddlewares , cachedHandler ( this . readiness ) ) ;
190190 app . get ( '/livez' , guestMiddlewares , cachedHandler ( this . liveness ) ) ;
191191
192192 /** **************************************************************
193- * Graphql permission *
193+ * Graphql scope *
194194 *************************************************************** */
195195
196196 app . use (
@@ -199,7 +199,7 @@ class ApiGateway {
199199 ...userMiddlewares ,
200200 async ( req , res , next ) => {
201201 try {
202- await this . assertPermission (
202+ await this . assertApiScope (
203203 'graphql' ,
204204 req ?. context ?. securityContext
205205 ) ;
@@ -236,7 +236,7 @@ class ApiGateway {
236236 ) ;
237237
238238 /** **************************************************************
239- * Data permission *
239+ * Data scope *
240240 *************************************************************** */
241241
242242 app . get ( `${ this . basePath } /v1/load` , userMiddlewares , ( async ( req , res ) => {
@@ -300,7 +300,7 @@ class ApiGateway {
300300 } ) ) ;
301301
302302 /** **************************************************************
303- * Meta permission *
303+ * Meta scope *
304304 *************************************************************** */
305305
306306 app . get (
@@ -327,7 +327,7 @@ class ApiGateway {
327327 ...userMiddlewares ,
328328 async ( req , res , next ) => {
329329 try {
330- await this . assertPermission (
330+ await this . assertApiScope (
331331 'meta' ,
332332 req ?. context ?. securityContext
333333 ) ;
@@ -353,7 +353,7 @@ class ApiGateway {
353353 ) ;
354354
355355 /** **************************************************************
356- * Jobs permission *
356+ * Jobs scope *
357357 *************************************************************** */
358358
359359 app . get (
@@ -375,7 +375,7 @@ class ApiGateway {
375375 ) ;
376376
377377 /** **************************************************************
378- * Private API (no permissions) *
378+ * Private API (no scopes) *
379379 *************************************************************** */
380380
381381 if ( this . playgroundAuthSecret ) {
@@ -473,7 +473,7 @@ class ApiGateway {
473473 } ) {
474474 const requestStarted = new Date ( ) ;
475475 try {
476- await this . assertPermission ( 'jobs' , context . securityContext ) ;
476+ await this . assertApiScope ( 'jobs' , context . securityContext ) ;
477477 const refreshScheduler = this . refreshScheduler ( ) ;
478478 res ( await refreshScheduler . runScheduledRefresh ( context , {
479479 ...this . parseQueryParam ( queryingOptions || { } ) ,
@@ -507,7 +507,7 @@ class ApiGateway {
507507 const requestStarted = new Date ( ) ;
508508
509509 try {
510- await this . assertPermission ( 'meta' , context . securityContext ) ;
510+ await this . assertApiScope ( 'meta' , context . securityContext ) ;
511511 const metaConfig = await this . getCompilerApi ( context ) . metaConfig ( {
512512 requestId : context . requestId ,
513513 } ) ;
@@ -527,7 +527,7 @@ class ApiGateway {
527527 const requestStarted = new Date ( ) ;
528528
529529 try {
530- await this . assertPermission ( 'meta' , context . securityContext ) ;
530+ await this . assertApiScope ( 'meta' , context . securityContext ) ;
531531 const metaConfigExtended = await this . getCompilerApi ( context ) . metaConfigExtended ( {
532532 requestId : context . requestId ,
533533 } ) ;
@@ -737,7 +737,7 @@ class ApiGateway {
737737 const query = < PreAggsJobsRequest > req . body ;
738738 let result ;
739739 try {
740- await this . assertPermission ( 'jobs' , req ?. context ?. securityContext ) ;
740+ await this . assertApiScope ( 'jobs' , req ?. context ?. securityContext ) ;
741741 switch ( query . action ) {
742742 case 'post' :
743743 if (
@@ -1158,7 +1158,7 @@ class ApiGateway {
11581158 const requestStarted = new Date ( ) ;
11591159
11601160 try {
1161- await this . assertPermission ( 'data' , context . securityContext ) ;
1161+ await this . assertApiScope ( 'data' , context . securityContext ) ;
11621162
11631163 query = this . parseQueryParam ( query ) ;
11641164 const [ queryType , normalizedQueries ] = await this . getNormalizedQueries ( query , context ) ;
@@ -1246,7 +1246,7 @@ class ApiGateway {
12461246 const requestStarted = new Date ( ) ;
12471247
12481248 try {
1249- await this . assertPermission ( 'data' , context . securityContext ) ;
1249+ await this . assertApiScope ( 'data' , context . securityContext ) ;
12501250
12511251 const [ queryType , normalizedQueries ] = await this . getNormalizedQueries ( query , context ) ;
12521252
@@ -1484,7 +1484,7 @@ class ApiGateway {
14841484 const requestStarted = new Date ( ) ;
14851485
14861486 try {
1487- await this . assertPermission ( 'data' , context . securityContext ) ;
1487+ await this . assertApiScope ( 'data' , context . securityContext ) ;
14881488
14891489 query = this . parseQueryParam ( request . query ) ;
14901490 let resType : ResultType = ResultType . DEFAULT ;
@@ -1960,49 +1960,56 @@ class ApiGateway {
19601960 } ;
19611961 }
19621962
1963- protected createContextToPermissionsFn (
1963+ protected createContextToApiScopesFn (
19641964 options : ApiGatewayOptions ,
1965- ) : ContextToPermissionsFn {
1966- return options . contextToPermissions
1967- ? async ( securityContext ?: any , defaultPermissions ?: Permission [ ] ) => {
1968- const permissions = options . contextToPermissions &&
1969- await options . contextToPermissions (
1965+ ) : ContextToApiScopesFn {
1966+ return options . contextToApiScopes
1967+ ? async ( securityContext ?: any , defaultApiScopes ?: ApiScopes [ ] ) => {
1968+ const scopes = options . contextToApiScopes &&
1969+ await options . contextToApiScopes (
19701970 securityContext ,
1971- defaultPermissions ,
1971+ defaultApiScopes ,
19721972 ) ;
1973- if ( ! permissions || ! Array . isArray ( permissions ) ) {
1973+ if ( ! scopes || ! Array . isArray ( scopes ) ) {
19741974 throw new Error (
1975- 'A user-defined contextToPermissions function returns an inconsistent type.'
1975+ 'A user-defined contextToApiScopes function returns an inconsistent type.'
19761976 ) ;
19771977 } else {
1978- permissions . forEach ( ( p ) => {
1978+ scopes . forEach ( ( p ) => {
19791979 if ( [ 'liveliness' , 'graphql' , 'meta' , 'data' , 'jobs' ] . indexOf ( p ) === - 1 ) {
19801980 throw new Error (
1981- `A user-defined contextToPermissions function returns a wrong permission : ${ p } `
1981+ `A user-defined contextToApiScopes function returns a wrong scope : ${ p } `
19821982 ) ;
19831983 }
19841984 } ) ;
19851985 }
1986- return permissions ;
1986+ return scopes ;
19871987 }
1988- : this . contextToPermDefFn ;
1988+ : async ( ) => {
1989+ const defaultApiScope = getEnv ( 'defaultApiScope' ) ;
1990+ if ( defaultApiScope ) {
1991+ return defaultApiScope ;
1992+ } else {
1993+ return this . contextToApiScopesDefFn ( ) ;
1994+ }
1995+ } ;
19891996 }
19901997
1991- protected async assertPermission (
1992- permission : Permission ,
1998+ protected async assertApiScope (
1999+ scope : ApiScopes ,
19932000 securityContext ?: any ,
19942001 ) : Promise < void > {
1995- const permissions =
1996- await this . contextToPermFn (
2002+ const scopes =
2003+ await this . contextToApiScopesFn (
19972004 securityContext || { } ,
1998- await this . contextToPermDefFn ( ) ,
2005+ await this . contextToApiScopesDefFn ( ) ,
19992006 ) ;
2000- const permited = permissions . indexOf ( permission ) >= 0 ;
2007+ const permited = scopes . indexOf ( scope ) >= 0 ;
20012008 if ( ! permited ) {
20022009 throw new CubejsHandlerError (
20032010 403 ,
20042011 'Forbidden' ,
2005- `Permission is missed: ${ permission } `
2012+ `Api scope is missed: ${ scope } `
20062013 ) ;
20072014 }
20082015 }
0 commit comments