3.2.0 • Published 2 years ago

@bcgov-cas/sso-express v3.2.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 years ago

sso-express

A node module exposing a developer-friendly sso/keycloak middleware for express servers

Prerequisites

  • express and openid-client peer dependencies (see package.json for supported versions)
  • The express-session middleware must be added to the stack before this middleware, as it assumes that req.session exists

Usage

This package exposes an express middleware

:warning: When using the exposed middleware, any request to the express server - including to static endpoints - will extend the session.

Exposed endpoints

The package configures a middleware with the following configurable endpoints:

EndpointDefault URLcan be disabled
Login/login
Logout/logout
Auth Callback/auth-callback
Session Remaining Time/session-idle-remaining-timex

Example usage

const ssoUtils = require("@bcgov-cas/sso-express").default;

const ssoMiddleware = await ssoUtils({
  applicationDomain: ".gov.bc.ca",
  getLandingRoute: (req) => {
    // Depending on your sso configuration
    return getLanding(req.claims);
  },
  getRedirectUri: (defaultRedirectUri, req) => {
    // can be used to add additional query params to the default redirect uri:
    const redirectUri = new URL(defaultRedirectUri);
    redirectUri.searchParams.set("redirect", "/some/path");
    return redirectUri;
  },
  bypassAuthentication: {
    login: process.env.BYPASS_AUTH_ON_LOCAL,
    sessionIdleRemainingTime: process.env.BYPASS_AUTH_ON_LOCAL,
  },
  oidcConfig: {
    oidcIssuer: `https://oidc.gov.bc.ca/auth/realm/myrealm`,
    clientId: "myappresource",
    clientSecret: "verysecuresecret", // optional
    baseUrl: "http://localhost:3000",
  },
  authorizationUrlParams: { kc_idp_hint: "idir" },
});

server.use(ssoMiddleware);

Authentication data

This middleware adds the following authentication data to the express request (req):

  • The OpenId TokenSet (see the openid-client documentation) is available at req.session.tokenSet
  • The OpenId claims are available at req.claims

Configuration

The constructor expects a single configuration object, with required and optional keys

Required configuration

Only the OpenId configuration key oidcConfig is mandatory

Example:

const configOptions = {
  oidcConfig: {
    oidcIssuer: `https://oidc.gov.bc.ca/auth/realm/myrealm`,
    clientId: "myappresource",
    baseUrl: "http://localhost:3000",
  },
};

Optional configuration

In addition, all these configuration keys are accepted:

KeyDescriptionDefault value
applicationDomainRestricts clearing the session cookie to this domain.gov.bc.ca
getLandingRouteFunction (req) => string used to redirect the user after login.() => '/'
getRedirectUriFunction (defaultRedirectUri: URL, req) => URL can be used to modify the redirect uri with the request's context.defaultRedirectUri
bypassAuthenticationSet to true, false or { login: t/f , sessionIdleRemainingTime: t/f } to configurefalse
routesOverrides the default routes below. Set to false or '' to disable (unavailable for login, logout, and authCallback)see below
onAuthCallbackCallback function called after the user is authenticated, but before the user is redirected to the landing page.undefined
authorizationUrlParamsAdditional parameters to be added to the authorization url. This can be either an object literal or a function receiving the request as a parameter.undefined
  routes: {
    login: '/login',
    logout: '/logout',
    sessionIdleRemainingTime: '/session-idle-remaining-time',
    authCallback: '/auth-callback'
  }
3.2.0

2 years ago

3.1.0

2 years ago

3.0.0

2 years ago

2.1.0

2 years ago

1.0.0

2 years ago

1.0.0-alpha.9

2 years ago

2.0.2

2 years ago

1.0.0-alpha.10

2 years ago

1.0.0-alpha.12

2 years ago

1.0.0-alpha.11

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.0.0-alpha.8

3 years ago

1.0.0-alpha.7

3 years ago

1.0.0-alpha.6

3 years ago

1.0.0-alpha.5

3 years ago

1.0.0-alpha.4

3 years ago

1.0.0-alpha.3

3 years ago

1.0.0-alpha.2

3 years ago

1.0.0-alpha.1

3 years ago

1.0.0-alpha.0

3 years ago