Skip to content

Commit 19d5cd8

Browse files
committed
feat: Allow to use custom checkAuth middleware
fixes issue #42
1 parent 9fab5db commit 19d5cd8

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

docs/Cube.js-Backend/@cubejs-backend-server-core.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Create an instance of `CubejsServerCore` to embed it in an `Express` application
1919
* `logger(msg, params)` - pass function for your custom logger.
2020
* `schemaPath` - Path to the `schema` location. By default, it is `/schema`.
2121
* `devServer` - Enable development server. By default, it is `true`.
22+
* `checkAuthMiddleware` - Pass express-style middleware to check authentication. Set `req.authInfo = { u: { ...userContextObj } }` inside middleware if you want to provide `USER_CONTEXT`. [Learn more](/cube#context-variables-user-context).
2223

2324
```javascript
2425
import * as CubejsServerCore from "@cubejs-backend/server-core";

packages/cubejs-api-gateway/index.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,22 +172,27 @@ const normalizeQuery = (query) => {
172172
};
173173
};
174174

175-
const coerceForSqlQuery = (query) => ({
175+
const coerceForSqlQuery = (query, req) => ({
176176
...query,
177177
timeDimensions: (query.timeDimensions || [])
178-
.map(td => (td.granularity === 'day' ? { ...td, granularity: 'date' } : td))
178+
.map(td => (td.granularity === 'day' ? { ...td, granularity: 'date' } : td)),
179+
contextSymbols: {
180+
userContext: req.authInfo && req.authInfo.u || {}
181+
}
179182
});
180183

181184
class ApiGateway {
182-
constructor(apiSecret, compilerApi, adapterApi, logger) {
185+
constructor(apiSecret, compilerApi, adapterApi, logger, options) {
186+
options = options || {};
183187
this.apiSecret = apiSecret;
184188
this.compilerApi = compilerApi;
185189
this.adapterApi = adapterApi;
186190
this.logger = logger;
191+
this.checkAuthMiddleware = options.checkAuthMiddleware || this.checkAuth.bind(this);
187192
}
188193

189194
initApp(app) {
190-
app.get('/cubejs-api/v1/load', this.checkAuth.bind(this), (async (req, res) => {
195+
app.get('/cubejs-api/v1/load', this.checkAuthMiddleware, (async (req, res) => {
191196
try {
192197
const query = JSON.parse(req.query.query);
193198
this.log(req, {
@@ -196,7 +201,7 @@ class ApiGateway {
196201
});
197202
const normalizedQuery = normalizeQuery(query);
198203
const [compilerSqlResult, metaConfigResult] = await Promise.all([
199-
this.compilerApi.getSql(coerceForSqlQuery(normalizedQuery)),
204+
this.compilerApi.getSql(coerceForSqlQuery(normalizedQuery, req)),
200205
this.compilerApi.metaConfig()
201206
]);
202207
const sqlQuery = compilerSqlResult;
@@ -226,11 +231,11 @@ class ApiGateway {
226231
}
227232
}));
228233

229-
app.get('/cubejs-api/v1/sql', this.checkAuth.bind(this), (async (req, res) => {
234+
app.get('/cubejs-api/v1/sql', this.checkAuthMiddleware, (async (req, res) => {
230235
try {
231236
const query = JSON.parse(req.query.query);
232237
const normalizedQuery = normalizeQuery(query);
233-
const sqlQuery = await this.compilerApi.getSql(coerceForSqlQuery(normalizedQuery));
238+
const sqlQuery = await this.compilerApi.getSql(coerceForSqlQuery(normalizedQuery, req));
234239
res.json({
235240
sql: sqlQuery
236241
});
@@ -239,7 +244,7 @@ class ApiGateway {
239244
}
240245
}));
241246

242-
app.get('/cubejs-api/v1/meta', this.checkAuth.bind(this), (async (req, res) => {
247+
app.get('/cubejs-api/v1/meta', this.checkAuthMiddleware, (async (req, res) => {
243248
try {
244249
const metaConfig = await this.compilerApi.metaConfig();
245250
const cubes = metaConfig.map(c => c.config);

packages/cubejs-server-core/core/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ class CubejsServerCore {
116116
this.apiSecret,
117117
this.compilerApi,
118118
this.orchestratorApi,
119-
this.logger
119+
this.logger, {
120+
checkAuthMiddleware: this.options.checkAuthMiddleware
121+
}
120122
);
121123
apiGateway.initApp(app);
122124
if (this.options.devServer) {

0 commit comments

Comments
 (0)