From e78e573aca6a9e1a1ae8d0b77d69160cda7838e9 Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Tue, 3 Mar 2020 11:06:44 +0100 Subject: [PATCH] refactor: provider.callback is now a function instead of a getter BREAKING CHANGE: `Provider.prototype.callback` is now a function instead of a getter. --- README.md | 2 +- certification/docker.js | 2 +- certification/fapi/index.js | 2 +- docs/README.md | 12 ++++++------ example/express.js | 2 +- lib/helpers/oidc_context.js | 2 +- lib/provider.js | 3 +-- test/test_helper.js | 10 +++++----- types/index.d.ts | 2 +- 9 files changed, 18 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index ec9b11ef1..ff7f28107 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,7 @@ const configuration = { const oidc = new Provider('http://localhost:3000', configuration); // express/nodejs style application callback (req, res, next) for use with express apps, see /examples/express.js -oidc.callback +oidc.callback() // koa application for use with koa apps, see /examples/koa.js oidc.app diff --git a/certification/docker.js b/certification/docker.js index 81beaf597..169d1b357 100644 --- a/certification/docker.js +++ b/certification/docker.js @@ -39,5 +39,5 @@ render(provider.app, { root: path.join(__dirname, '..', 'example', 'views'), }); provider.use(routes(provider).routes()); -const server = https.createServer(pem, provider.callback); +const server = https.createServer(pem, provider.callback()); server.listen(PORT); diff --git a/certification/fapi/index.js b/certification/fapi/index.js index 9634d08b1..cbdfad76e 100644 --- a/certification/fapi/index.js +++ b/certification/fapi/index.js @@ -253,7 +253,7 @@ if (SUITE_BASE_URL === OFFICIAL_CERTIFICATION) { requestCert: true, rejectUnauthorized: false, ...pem, - }, fapi.callback); + }, fapi.callback()); server.listen(PORT); } diff --git a/docs/README.md b/docs/README.md index 3efc5f01e..71ef86185 100644 --- a/docs/README.md +++ b/docs/README.md @@ -63,7 +63,7 @@ const configuration = { const oidc = new Provider('http://localhost:3000', configuration); // express/nodejs style application callback (req, res, next) for use with express apps, see /examples/express.js -oidc.callback +oidc.callback() // koa application for use with koa apps, see /examples/koa.js oidc.app @@ -270,7 +270,7 @@ provider.registerGrantType(grantType, tokenExchangeHandler, parameters, allowedD ## Registering module middlewares (helmet, ip-filters, rate-limiters, etc) -When using `provider.app` or `provider.callback` as a mounted application in your own koa or express +When using `provider.app` or `provider.callback()` as a mounted application in your own koa or express stack just follow the respective module's documentation. However, when using the `provider.app` Koa instance directly to register i.e. koa-helmet you must push the middleware in front of oidc-provider in the middleware stack. @@ -346,7 +346,7 @@ Note: if you mount oidc-provider to a path it's likely you will have to also upd ### to a `connect` application ```js // assumes connect ^3.0.0 -connectApp.use('/oidc', oidc.callback); +connectApp.use('/oidc', oidc.callback()); ``` ### to a `fastify` application @@ -356,13 +356,13 @@ await app.register(require('fastify-express')); // or // await app.register(require('middie')); -fastifyApp.use('/oidc', oidc.callback); +fastifyApp.use('/oidc', oidc.callback()); ``` ### to a `hapi` application ```js // assumes @hapi/hapi ^20.0.0 -const { callback } = oidc; +const callback = oidc.callback(); hapiApp.route({ path: `/oidc/{any*}`, method: '*', @@ -403,7 +403,7 @@ export class OidcController { ### to an `express` application ```js // assumes express ^4.0.0 -expressApp.use('/oidc', oidc.callback); +expressApp.use('/oidc', oidc.callback()); ``` ### to a `koa` application diff --git a/example/express.js b/example/express.js index 3ff03a57f..ce049371b 100644 --- a/example/express.js +++ b/example/express.js @@ -62,7 +62,7 @@ let server; } routes(app, provider); - app.use(provider.callback); + app.use(provider.callback()); server = app.listen(PORT, () => { console.log(`application is listening on port ${PORT}, check its /.well-known/openid-configuration`); }); diff --git a/lib/helpers/oidc_context.js b/lib/helpers/oidc_context.js index 7a3e071ec..79cc99e40 100644 --- a/lib/helpers/oidc_context.js +++ b/lib/helpers/oidc_context.js @@ -86,7 +86,7 @@ module.exports = function getContext(provider) { this.ctx.req.originalUrl.indexOf(this.ctx.request.url), )) || this.ctx.mountPath // koa-mount - || this.ctx.req.baseUrl // expressApp.use('/op', provider.callback); + || this.ctx.req.baseUrl // expressApp.use('/op', provider.callback()); || ''; // no mount return url.resolve(this.ctx.href, provider.pathFor(name, { mountPath, ...opt })); diff --git a/lib/provider.js b/lib/provider.js index f2036861a..e75777daa 100644 --- a/lib/provider.js +++ b/lib/provider.js @@ -297,8 +297,7 @@ class Provider extends events.EventEmitter { return instance(this).app; } - // TODO: in v7.x make this a function - get callback() { + callback() { return this.app.callback(); } diff --git a/test/test_helper.js b/test/test_helper.js index fd210d2ef..c6c6fac19 100644 --- a/test/test_helper.js +++ b/test/test_helper.js @@ -402,20 +402,20 @@ module.exports = function testHelper(dir, { } case 'express': { const app = new Express(); - app.use(mountTo, provider.callback); + app.use(mountTo, provider.callback()); global.server.on('request', app); break; } case 'connect': { const app = new Connect(); - app.use(mountTo, provider.callback); + app.use(mountTo, provider.callback()); global.server.on('request', app); break; } case 'fastify': { const app = new Fastify(); await app.register(middie); - app.use(mountTo, provider.callback); + app.use(mountTo, provider.callback()); await new Promise((resolve) => global.server.close(resolve)); await app.listen(port); global.server = app.server; @@ -429,7 +429,7 @@ module.exports = function testHelper(dir, { case 'hapi': { const Hapi = require('@hapi/hapi'); // eslint-disable-line global-require const app = new Hapi.Server({ port }); - const { callback } = provider; + const callback = provider.callback(); app.route({ path: `${mountTo}/{any*}`, method: '*', @@ -460,7 +460,7 @@ module.exports = function testHelper(dir, { break; } default: - global.server.on('request', provider.callback); + global.server.on('request', provider.callback()); } agent = supertest(global.server); diff --git a/types/index.d.ts b/types/index.d.ts index aad6d9e5d..2786397a0 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1172,7 +1172,6 @@ export class Provider extends events.EventEmitter { readonly issuer: string; readonly app: Koa; - readonly callback: (req: http.IncomingMessage | http2.Http2ServerRequest, res: http.ServerResponse | http2.Http2ServerResponse) => void; env?: Koa['env']; proxy?: Koa['proxy']; @@ -1181,6 +1180,7 @@ export class Provider extends events.EventEmitter { maxIpsCount?: number; keys?: Koa['keys']; listen: Koa['listen']; + callback: Koa['callback']; interactionResult( req: http.IncomingMessage | http2.Http2ServerRequest,