Express.js

Tested Versions

Before You Begin

Important Reading

This section contains important elements that you should carefully consider before configuration of an OpenID Connect 1.0 Registered Client.

Common Notes

  1. The OpenID Connect 1.0 client_id parameter:
    1. This must be a unique value for every client.
    2. The value used in this guide is merely for readability and demonstration purposes and you should not use this value in production and should instead utilize the How do I generate a client identifier or client secret? FAQ. We recommend 64 random characters but you can use any arbitrary value that meets the other criteria.
    3. This must only contain RFC3986 Unreserved Characters.
    4. This must be no more than 100 characters in length.
  2. The OpenID Connect 1.0 client_secret parameter:
    1. The value used in this guide is merely for demonstration purposes and you should absolutely not use this value in production and should instead utilize the How do I generate a client identifier or client secret? FAQ.
    2. This string may be stored as plaintext in the Authelia configuration but this behaviour is deprecated and is not guaranteed to be supported in the future. See the Plaintext guide for more information.
    3. When the secret is stored in hashed form in the Authelia configuration (heavily recommended), the cost of hashing can, if too great, cause timeouts for clients. See the Tuning the work factors guide for more information.
  3. The configuration example for Authelia:
    1. Only contains an example configuration for the client registration and you MUST also configure the required elements from the OpenID Connect 1.0 Provider Configuration guide.
    2. Only contains a small portion of all of the available options for a registered client and users may wish to configure portions that are not part of this guide or configure them differently, as such it’s important to both familiarize yourself with the other options available and the effect of each of the options configured in this section by looking at the OpenID Connect 1.0 Clients Configuration guide.

Assumptions

This example makes the following assumptions:

  • Application Root URL: https://express.example.com/
  • Authelia Root URL: https://auth.example.com/
  • Client ID: Express.js
  • Client Secret: insecure_secret

Configuration

Authelia

The following YAML configuration is an example Authelia client configuration for use with Express.js which will operate with the application example:

configuration.yml
identity_providers:
  oidc:
    ## The other portions of the mandatory OpenID Connect 1.0 configuration go here.
    ## See: https://www.authelia.com/c/oidc
    clients:
      - client_id: 'Express.js'
        client_name: 'Express.js App'
        client_secret: '$pbkdf2-sha512$310000$c8p78n7pUMln0jzvd4aK4Q$JNRBzwAo0ek5qKn50cFzzvE9RXV88h1wJn5KGiHrD0YKtZaR/nCb2CJPOsKaPK0hjf.9yHxzQGZziziccp6Yng'  # The digest of 'insecure_secret'.
        public: false
        authorization_policy: 'two_factor'
        require_pkce: true
        require_pushed_authorization_requests: true
        redirect_uris:
          - 'https://express.example.com/callback'
        scopes:
          - 'openid'
          - 'profile'
          - 'email'
          - 'groups'
        userinfo_signed_response_alg: 'none'
        token_endpoint_auth_method: 'client_secret_basic'

Application

To configure Express.js to utilize Authelia as an OpenID Connect 1.0 Provider:

Project Initialization

mkdir authelia-example && cd authelia-example && npm init -y && npm install express express-openid-connect

Create The Application

This application example assumes you’re proxying the Node service with a proxy handling TLS termination for https://express.example.com.

server.js
"use strict";

const express = require('express');
const { auth, requiresAuth } = require('express-openid-connect');
const { randomBytes } = require('crypto');

const app = express();

app.use(
  auth({
    authRequired: false,
    baseURL: `${process.env.APP_BASE_URL || 'https://express.example.com'}/callback`,
    secret: process.env.SESSION_ENCRYPTION_SECRET || randomBytes(64).toString('hex'),
    clientID: process.env.OIDC_CLIENT_ID || 'Express.js',
    clientSecret: process.env.OIDC_CLIENT_SECRET || 'insecure_secret',
    clientAuthMethod: 'client_secret_basic',
    issuerBaseURL: process.env.OIDC_ISSUER || 'https://auth.example.com',
    pushedAuthorizationRequests: true,
    authorizationParams: {
      response_type: 'code',
      scope: 'openid profile email groups',
    },
  })
);

app.get('/', requiresAuth(), (req, res) => {
  req.oidc.fetchUserInfo().then((userInfo) => {
    const data = JSON.stringify(
      {
        accessToken: req.oidc.accessToken,
        refreshToken: req.oidc.refreshToken,
        idToken: req.oidc.idToken,
        claims: req.oidc.idTokenClaims,
        scopes: req.oidc.scope,
        userInfo,
      }, null, 2);

    res.send(`<html lang='en'><body><pre><code>${data}</code></pre></body></html>`);
  });
});

app.listen(3000, function () {
  console.log("Listening on port 3000")
});

Environment Example:

APP_BASE_URL=https://express.example.com
SESSION_ENCRYPTION_SECRET=
OIDC_ISSUER=https://auth.example.com
OIDC_CLIENT_ID=Express.js
OIDC_CLIENT_SECRET=insecure_secret

See Also