Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

feat[oidc-middleware] Allow multiple instances of ExpressOIDC to co-exist #498

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/oidc-middleware/src/ExpressOIDC.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const merge = require('lodash/merge');
const oidcUtil = require('./oidcUtil');
const connectUtil = require('./connectUtil');
const logout = require('./logout');
const Passport = require('passport').Passport;

const {
assertIssuer,
Expand All @@ -39,7 +40,7 @@ module.exports = class ExpressOIDC extends EventEmitter {
* @param {string} options.issuer The OpenId Connect issuer
* @param {string} options.client_id This app's OpenId Connect client id
* @param {string} options.client_secret This app's OpenId Connect client secret
* @param {string} options.loginRedirectUri The location of the login authorization callback if not redirecting to this app
* @param {string} options.loginRedirectUri The location of the login authorization callback if not redirecting to this app
* @param {string} options.logoutRedirectUri The location of the logout callback if not redirecting to this app
* @param {string} [options.scope=openid] The scopes that will determine the claims on the tokens
* @param {string} [options.response_type=code] The OpenId Connect response type
Expand Down Expand Up @@ -111,7 +112,10 @@ module.exports = class ExpressOIDC extends EventEmitter {

const context = {
options,
emitter: this
emitter: this,
// Allow multiple instances of ExpressOIDC to co-exist by using a separate Passport instance for each context.
// This is useful e.g. in combination with the 'vhost' package.
passport: new Passport()
};

/**
Expand Down
4 changes: 3 additions & 1 deletion packages/oidc-middleware/src/connectUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/

const csrf = require('csurf');
const passport = require('passport');
const { Router } = require('express');
const querystring = require('querystring');
const uuid = require('uuid');
Expand All @@ -22,6 +21,7 @@ const connectUtil = module.exports;

// Create a router to easily add routes
connectUtil.createOIDCRouter = context => {
const passport = context.passport;
const routes = context.options.routes;
const oidcRouter = new Router();
oidcRouter.use(passport.initialize({ userProperty: 'userContext' }));
Expand All @@ -46,6 +46,7 @@ connectUtil.createOIDCRouter = context => {
};

connectUtil.createLoginHandler = context => {
const passport = context.passport;
const passportHandler = passport.authenticate('oidc');
const csrfProtection = csrf();

Expand Down Expand Up @@ -83,6 +84,7 @@ connectUtil.createLoginHandler = context => {
};

connectUtil.createLoginCallbackHandler = context => {
const passport = context.passport;
const routes = context.options.routes;
const customHandler = routes.loginCallback.handler;

Expand Down
2 changes: 1 addition & 1 deletion packages/oidc-middleware/src/oidcUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
* See the License for the specific language governing permissions and limitations under the License.
*/

const passport = require('passport');
const OpenIdClientStrategy = require('openid-client').Strategy;
const Issuer = require('openid-client').Issuer;
const ensureLoggedIn = require('connect-ensure-login').ensureLoggedIn;
Expand Down Expand Up @@ -68,6 +67,7 @@ oidcUtil.createClient = context => {
};

oidcUtil.bootstrapPassportStrategy = context => {
const passport = context.passport;
const oidcStrategy = new OpenIdClientStrategy({
params: {
scope: context.options.scope
Expand Down