Skip to content

Commit a6501d7

Browse files
committed
BREAKING CHANGE: Remove support for checkAuthMiddleware
checkAuthMiddleware option was deprecated in 0.26.0, because this option was tightly bound to Express. Since Cube.js supports HTTP **and** WebSockets as transports, we want our authentication API to not rely on transport-specific details. We now recommend using checkAuth.
1 parent 8103896 commit a6501d7

File tree

8 files changed

+4
-168
lines changed

8 files changed

+4
-168
lines changed

DEPRECATION.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ features:
4343
| Removed | [`contextToDataSourceId`](#contexttodatasourceid) | v0.25.0 | v0.25.0 |
4444
| Removed | [Absolute import for `@cubejs-backend/server-core`](#absolute-import-for-@cubejs-backendserver-core) | v0.25.4 | v0.32.0 |
4545
| Removed | [Absolute import for `@cubejs-backend/schema-compiler`](#absolute-import-for-@cubejs-backendschema-compiler) | v0.25.21 | v0.32.0 |
46-
| Deprecated | [`checkAuthMiddleware`](#checkauthmiddleware) | v0.26.0 | |
46+
| Deprecated | [`checkAuthMiddleware`](#checkauthmiddleware) | v0.26.0 | v0.36.0 |
4747
| Removed | [Node.js 10](#nodejs-10) | v0.26.0 | v0.29.0 |
4848
| Removed | [Node.js 15](#nodejs-15) | v0.26.0 | v0.32.0 |
4949
| Deprecated | [`USER_CONTEXT`](#user_context) | v0.26.0 | |
@@ -179,7 +179,7 @@ const { BaseQuery } = require("@cubejs-backend/schema-compiler");
179179

180180
### `checkAuthMiddleware`
181181

182-
**Deprecated in Release: v0.26.0**
182+
**Removed in Release: v0.36.0**
183183

184184
The `checkAuthMiddleware` option was tightly bound to Express,
185185
[which has been deprecated](#embedding-cubejs-within-express). Since Cube.js

packages/cubejs-api-gateway/src/gateway.ts

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ import {
6060
ApiGatewayOptions,
6161
} from './types/gateway';
6262
import {
63-
CheckAuthMiddlewareFn,
6463
RequestLoggerMiddlewareFn,
6564
ContextRejectionMiddlewareFn,
6665
ContextAcceptorFn,
@@ -146,8 +145,6 @@ class ApiGateway {
146145
protected readonly contextToApiScopesDefFn: ContextToApiScopesFn =
147146
async () => ['graphql', 'meta', 'data'];
148147

149-
protected readonly checkAuthMiddleware: CheckAuthMiddlewareFn;
150-
151148
protected readonly requestLoggerMiddleware: RequestLoggerMiddlewareFn;
152149

153150
protected readonly securityContextExtractor: SecurityContextExtractorFn;
@@ -188,9 +185,6 @@ class ApiGateway {
188185
this.checkAuthFn = this.createCheckAuthFn(options);
189186
this.checkAuthSystemFn = this.createCheckAuthSystemFn();
190187
this.contextToApiScopesFn = this.createContextToApiScopesFn(options);
191-
this.checkAuthMiddleware = options.checkAuthMiddleware
192-
? this.wrapCheckAuthMiddleware(options.checkAuthMiddleware)
193-
: this.checkAuth;
194188
this.securityContextExtractor = this.createSecurityContextExtractor(options.jwt);
195189
this.requestLoggerMiddleware = options.requestLoggerMiddleware || this.requestLogger;
196190
this.contextRejectionMiddleware = options.contextRejectionMiddleware || (async (req, res, next) => next());
@@ -214,7 +208,7 @@ class ApiGateway {
214208

215209
public initApp(app: ExpressApplication) {
216210
const userMiddlewares: RequestHandler[] = [
217-
this.checkAuthMiddleware,
211+
this.checkAuth,
218212
this.requestContextMiddleware,
219213
this.contextRejectionMiddleware,
220214
this.logNetworkUsage,
@@ -2089,40 +2083,6 @@ class ApiGateway {
20892083
}
20902084
}
20912085

2092-
protected wrapCheckAuthMiddleware(fn: CheckAuthMiddlewareFn): CheckAuthMiddlewareFn {
2093-
this.logger('CheckAuthMiddleware Middleware Deprecation', {
2094-
warning: (
2095-
'Option checkAuthMiddleware is now deprecated in favor of checkAuth, please migrate: ' +
2096-
'https://github.com/cube-js/cube.js/blob/master/DEPRECATION.md#checkauthmiddleware'
2097-
)
2098-
});
2099-
2100-
let showWarningAboutNotObject = false;
2101-
2102-
return (req, res, next) => {
2103-
fn(req, res, (e) => {
2104-
// We renamed authInfo to securityContext, but users can continue to use both ways
2105-
if (req.securityContext && !req.authInfo) {
2106-
req.authInfo = req.securityContext;
2107-
} else if (req.authInfo) {
2108-
req.securityContext = req.authInfo;
2109-
}
2110-
2111-
if ((typeof req.securityContext !== 'object' || req.securityContext === null) && !showWarningAboutNotObject) {
2112-
this.logger('Security Context Should Be Object', {
2113-
warning: (
2114-
`Value of securityContext (previously authInfo) expected to be object, actual: ${getRealType(req.securityContext)}`
2115-
)
2116-
});
2117-
2118-
showWarningAboutNotObject = true;
2119-
}
2120-
2121-
next(e);
2122-
});
2123-
};
2124-
}
2125-
21262086
protected wrapCheckAuth(fn: CheckAuthFn): PreparedCheckAuthFn {
21272087
// We dont need to span all logs with deprecation message
21282088
let warningShowed = false;

packages/cubejs-api-gateway/src/interfaces.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,6 @@ export {
7070
ContextToApiScopesFn,
7171
};
7272

73-
/**
74-
* Auth middleware.
75-
* @deprecated
76-
*/
77-
export type CheckAuthMiddlewareFn =
78-
(
79-
req: Request,
80-
res: ExpressResponse,
81-
next: ExpressNextFunction,
82-
) => void;
83-
8473
/**
8574
* Context rejection middleware.
8675
*/

packages/cubejs-api-gateway/src/types/gateway.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
CheckAuthFn,
1515
} from './auth';
1616
import {
17-
CheckAuthMiddlewareFn,
1817
RequestLoggerMiddlewareFn,
1918
ContextRejectionMiddlewareFn,
2019
ContextAcceptorFn,
@@ -66,10 +65,6 @@ interface ApiGatewayOptions {
6665
contextRejectionMiddleware?: ContextRejectionMiddlewareFn;
6766
wsContextAcceptor?: ContextAcceptorFn;
6867
checkAuth?: CheckAuthFn;
69-
/**
70-
* @deprecated Use checkAuth property instead.
71-
*/
72-
checkAuthMiddleware?: CheckAuthMiddlewareFn;
7368
contextToApiScopes?: ContextToApiScopesFn;
7469
event?: (name: string, props?: object) => void;
7570
}

packages/cubejs-api-gateway/test/auth.test.ts

Lines changed: 1 addition & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function createApiGateway(handler: RequestHandler, logger: () => any, options: P
2121

2222
public initApp(app: ExpressApplication) {
2323
const userMiddlewares: RequestHandler[] = [
24-
this.checkAuthMiddleware,
24+
this.checkAuth,
2525
this.requestContextMiddleware,
2626
];
2727

@@ -388,110 +388,6 @@ describe('test authorization', () => {
388388
expect(handlerMock.mock.calls[0][0].context.authInfo).toEqual(EXPECTED_SECURITY_CONTEXT);
389389
});
390390

391-
test('custom checkAuthMiddleware with deprecated authInfo', async () => {
392-
const loggerMock = jest.fn(() => {
393-
//
394-
});
395-
396-
const expectSecurityContext = (securityContext) => {
397-
expect(securityContext.uid).toEqual(5);
398-
expect(securityContext.iat).toBeDefined();
399-
expect(securityContext.exp).toBeDefined();
400-
};
401-
402-
const handlerMock = jest.fn((req, res) => {
403-
expectSecurityContext(req.context.securityContext);
404-
expectSecurityContext(req.context.authInfo);
405-
406-
res.status(200).end();
407-
});
408-
409-
const { app } = createApiGateway(handlerMock, loggerMock, {
410-
checkAuthMiddleware: (req: Request, res, next) => {
411-
try {
412-
if (req.headers.authorization) {
413-
req.authInfo = jwt.verify(req.headers.authorization, 'secret');
414-
}
415-
416-
next();
417-
} catch (e) {
418-
next(e);
419-
}
420-
}
421-
});
422-
423-
const token = generateAuthToken({ uid: 5, });
424-
425-
await request(app)
426-
.get('/test-auth-fake')
427-
.set('Authorization', token)
428-
.expect(200);
429-
430-
expect(loggerMock.mock.calls.length).toEqual(1);
431-
expect(loggerMock.mock.calls[0]).toEqual([
432-
'CheckAuthMiddleware Middleware Deprecation',
433-
{
434-
warning: 'Option checkAuthMiddleware is now deprecated in favor of checkAuth, please migrate: https://github.com/cube-js/cube.js/blob/master/DEPRECATION.md#checkauthmiddleware',
435-
}
436-
]);
437-
expect(handlerMock.mock.calls.length).toEqual(1);
438-
439-
expectSecurityContext(handlerMock.mock.calls[0][0].context.securityContext);
440-
// authInfo was deprecated, but should exists as computability
441-
expectSecurityContext(handlerMock.mock.calls[0][0].context.authInfo);
442-
});
443-
444-
test('custom checkAuthMiddleware with securityInfo (not object)', async () => {
445-
const loggerMock = jest.fn();
446-
447-
const EXPECTED_SECURITY_CONTEXT = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjUsImlhdCI6MTYxMTg1NzcwNSwiZXhwIjoyNDc1ODU3NzA1fQ.tTieqdIcxDLG8fHv8YWwfvg_rPVe1XpZKUvrCdzVn3g';
448-
449-
const handlerMock = jest.fn((req, res) => {
450-
expect(req.context.securityContext).toEqual(EXPECTED_SECURITY_CONTEXT);
451-
expect(req.context.authInfo).toEqual(EXPECTED_SECURITY_CONTEXT);
452-
453-
res.status(200).end();
454-
});
455-
456-
const { app } = createApiGateway(handlerMock, loggerMock, {
457-
checkAuthMiddleware: (req: Request, res, next) => {
458-
if (req.headers.authorization) {
459-
// It must be object, but some users are using string for securityContext
460-
req.authInfo = req.headers.authorization;
461-
}
462-
463-
if (next) {
464-
next();
465-
}
466-
}
467-
});
468-
469-
await request(app)
470-
.get('/test-auth-fake')
471-
// console.log(generateAuthToken({ uid: 5, }));
472-
.set('Authorization', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjUsImlhdCI6MTYxMTg1NzcwNSwiZXhwIjoyNDc1ODU3NzA1fQ.tTieqdIcxDLG8fHv8YWwfvg_rPVe1XpZKUvrCdzVn3g')
473-
.expect(200);
474-
475-
expect(loggerMock.mock.calls.length).toEqual(2);
476-
expect(loggerMock.mock.calls[0]).toEqual([
477-
'CheckAuthMiddleware Middleware Deprecation',
478-
{
479-
warning: 'Option checkAuthMiddleware is now deprecated in favor of checkAuth, please migrate: https://github.com/cube-js/cube.js/blob/master/DEPRECATION.md#checkauthmiddleware',
480-
}
481-
]);
482-
expect(loggerMock.mock.calls[1]).toEqual([
483-
'Security Context Should Be Object',
484-
{
485-
warning: 'Value of securityContext (previously authInfo) expected to be object, actual: string',
486-
}
487-
]);
488-
489-
expect(handlerMock.mock.calls.length).toEqual(1);
490-
expect(handlerMock.mock.calls[0][0].context.securityContext).toEqual(EXPECTED_SECURITY_CONTEXT);
491-
// authInfo was deprecated, but should exists as computability
492-
expect(handlerMock.mock.calls[0][0].context.authInfo).toEqual(EXPECTED_SECURITY_CONTEXT);
493-
});
494-
495391
test('coerceForSqlQuery multiple', async () => {
496392
const loggerMock = jest.fn(() => {
497393
//

packages/cubejs-server-core/src/core/optionsValidate.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ const schemaOptions = Joi.object().keys({
7777
contextToApiScopes: Joi.func(),
7878
repositoryFactory: Joi.func(),
7979
checkAuth: Joi.func(),
80-
checkAuthMiddleware: Joi.func(),
8180
jwt: jwtOptions,
8281
queryTransformer: Joi.func(),
8382
queryRewrite: Joi.func(),

packages/cubejs-server-core/src/core/server.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,6 @@ export class CubejsServerCore {
443443
standalone: this.standalone,
444444
dataSourceStorage: this.orchestratorStorage,
445445
basePath: this.options.basePath,
446-
checkAuthMiddleware: this.options.checkAuthMiddleware,
447446
contextRejectionMiddleware: this.contextRejectionMiddleware.bind(this),
448447
wsContextAcceptor: this.contextAcceptor.shouldAcceptWs.bind(this.contextAcceptor),
449448
checkAuth: this.options.checkAuth,

packages/cubejs-server-core/src/core/types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Required, SchemaFileRepository } from '@cubejs-backend/shared';
22
import {
33
CheckAuthFn,
4-
CheckAuthMiddlewareFn,
54
ExtendContextFn,
65
JWTOptions,
76
UserBackgroundContext,
@@ -180,7 +179,6 @@ export interface CreateOptions {
180179
contextToOrchestratorId?: ContextToOrchestratorIdFn;
181180
contextToApiScopes?: ContextToApiScopesFn;
182181
repositoryFactory?: (context: RequestContext) => SchemaFileRepository;
183-
checkAuthMiddleware?: CheckAuthMiddlewareFn;
184182
checkAuth?: CheckAuthFn;
185183
checkSqlAuth?: CheckSQLAuthFn;
186184
canSwitchSqlUser?: CanSwitchSQLUserFn;

0 commit comments

Comments
 (0)