Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
68c9798
Update openid-client to v6
Feb 8, 2025
a031459
available -> required
Feb 11, 2025
3096157
wip
Feb 11, 2025
955d26d
resolve TODO
Feb 11, 2025
3f04f0c
Merge branch 'master' into openid-6
alxndrsn Feb 12, 2025
d8849cb
??
Feb 12, 2025
d94e0e1
Merge branch 'master' into openid-6
Feb 12, 2025
b9e195d
revert test changes
Feb 12, 2025
1790465
revert error handling change
Feb 12, 2025
97a524e
rename tokens
Feb 12, 2025
83fe52c
Merge branch 'master' into openid-6
alxndrsn Feb 18, 2025
543c499
Merge branch 'master' into openid-6
Mar 24, 2025
208fa85
Merge branch 'master' into openid-6
alxndrsn Mar 26, 2025
848ccb5
Merge branch 'master' into openid-6
alxndrsn Mar 26, 2025
05bd36f
Update to latest openid-client release
Mar 26, 2025
7456fb4
Merge branch 'master' into openid-6
alxndrsn Apr 4, 2025
aee86bb
Merge branch 'master' into openid-6
alxndrsn Apr 23, 2025
62e0029
Merge branch 'master' into openid-6
alxndrsn Apr 28, 2025
a1a6dff
Merge branch 'master' into openid-6
alxndrsn Apr 30, 2025
74b32cf
Merge branch 'master' into openid-6
Jul 14, 2025
c504b07
bump openid-client
Jul 14, 2025
efa3196
Merge branch 'master' into openid-6
alxndrsn Jul 14, 2025
a12c73f
Merge branch 'master' into openid-6
alxndrsn Jul 28, 2025
a7482be
Merge branch 'master' into openid-6
Sep 4, 2025
a93fa34
Merge branch 'master' into openid-6
Oct 21, 2025
3e51141
Update to latest `openid-client`
Oct 21, 2025
0155a2f
Merge branch 'master' into openid-6
alxndrsn Nov 20, 2025
85eec7f
Merge branch 'master' into openid-6
alxndrsn Apr 20, 2026
0a65c09
wip
Apr 20, 2026
37cfc71
update openid-client to latest release
Apr 20, 2026
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
42 changes: 25 additions & 17 deletions lib/resources/oidc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,24 @@

const { createHash } = require('node:crypto');

const { generators } = require('openid-client');
const config = require('config');
const { parse, render } = require('mustache');
const { // eslint-disable-line object-curly-newline
authorizationCodeGrant,
buildAuthorizationUrl,
calculatePKCECodeChallenge,
fetchUserInfo,
randomPKCECodeVerifier,
randomState,
} = require('openid-client'); // eslint-disable-line object-curly-newline
Comment thread
alxndrsn marked this conversation as resolved.

const { safeNextPathFrom } = require('../util/html');
const { redirect } = require('../util/http');
const { createUserSession } = require('../http/sessions');
const { // eslint-disable-line object-curly-newline
CODE_CHALLENGE_METHOD,
RESPONSE_TYPE,
SCOPES,
getClient,
getOidcConfig,
getRedirectUri,
isEnabled,
} = require('../util/oidc'); // eslint-disable-line camelcase,object-curly-newline
Expand Down Expand Up @@ -100,7 +106,7 @@ const loaderTemplate = `
`;
parse(loaderTemplate); // caches template for future perf.

const stateFor = next => [ generators.state(), Buffer.from(next).toString('base64url') ].join(':');
const stateFor = next => [ randomState(), Buffer.from(next).toString('base64url') ].join(':');
const nextFrom = state => {
if (state) return Buffer.from(state.split(':')[1], 'base64url').toString();
Comment thread
alxndrsn marked this conversation as resolved.
};
Expand All @@ -110,19 +116,20 @@ module.exports = (service, __, anonymousEndpoint) => {

service.get('/oidc/login', anonymousEndpoint.html(async ({ Sentry }, _, req, res) => {
try {
const client = await getClient();
const code_verifier = generators.codeVerifier(); // eslint-disable-line camelcase
const oidcConfig = await getOidcConfig();
const code_verifier = randomPKCECodeVerifier(); // eslint-disable-line camelcase

const code_challenge = generators.codeChallenge(code_verifier); // eslint-disable-line camelcase
const code_challenge = await calculatePKCECodeChallenge(code_verifier); // eslint-disable-line camelcase

const next = req.query.next ?? '';
const state = stateFor(next);

const authUrl = client.authorizationUrl({
const authUrl = buildAuthorizationUrl(oidcConfig, {
scope: SCOPES.join(' '),
resource: `${envDomain}/v1`,
code_challenge,
code_challenge_method: CODE_CHALLENGE_METHOD,
redirect_uri: getRedirectUri(),
state,
});

Expand All @@ -142,20 +149,21 @@ module.exports = (service, __, anonymousEndpoint) => {

service.get('/oidc/callback', anonymousEndpoint.html(async (container, _, req, res) => {
try {
const code_verifier = req.cookies[CODE_VERIFIER_COOKIE]; // eslint-disable-line camelcase
const state = req.cookies[STATE_COOKIE]; // eslint-disable-line no-multi-spaces
const pkceCodeVerifier = req.cookies[CODE_VERIFIER_COOKIE];
const expectedState = req.cookies[STATE_COOKIE]; // eslint-disable-line no-multi-spaces
res.clearCookie(CODE_VERIFIER_COOKIE, callbackCookieProps);
res.clearCookie(STATE_COOKIE, callbackCookieProps); // eslint-disable-line no-multi-spaces

const client = await getClient();

const params = client.callbackParams(req);
const oidcConfig = await getOidcConfig();

const tokenSet = await client.callback(getRedirectUri(), params, { response_type: RESPONSE_TYPE, code_verifier, state });
// N.B. use req.originalUrl in preference to req.url, as the latter is corrupted somewhere upstream.
const requestUrl = new URL(req.originalUrl, getRedirectUri());

const { access_token } = tokenSet;
const tokens = await authorizationCodeGrant(oidcConfig, requestUrl, { pkceCodeVerifier, expectedState });

const userinfo = await client.userinfo(access_token);
const { access_token } = tokens;
const expectedSubject = tokens.claims().sub;
const userinfo = await fetchUserInfo(oidcConfig, access_token, expectedSubject);

const { email, email_verified } = userinfo;
if (!email) {
Expand All @@ -169,7 +177,7 @@ module.exports = (service, __, anonymousEndpoint) => {

await initSession(container, req, res, user);

const nextPath = safeNextPathFrom(nextFrom(state));
const nextPath = safeNextPathFrom(nextFrom(expectedState));

// This redirect would be ideal, but breaks `SameSite: Secure` cookies.
// return redirect(303, nextPath);
Expand Down
33 changes: 13 additions & 20 deletions lib/util/oidc.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ module.exports = {
CODE_CHALLENGE_METHOD,
RESPONSE_TYPE,
SCOPES,
getClient,
getOidcConfig,
getRedirectUri,
isEnabled,
};

const config = require('config');
const { Issuer } = require('openid-client');
const { allowInsecureRequests, discovery } = require('openid-client');

const oidcConfig = (config.has('default.oidc') && config.get('default.oidc')) || {};

Expand All @@ -48,19 +48,21 @@ function getRedirectUri() {
return `${config.get('default.env.domain')}/v1/oidc/callback`;
}

let clientLoader; // single instance, initialised lazily
function getClient() {
if (!clientLoader) clientLoader = initClient();
return clientLoader;
let configLoader; // single instance, initialised lazily
function getOidcConfig() {
if (!configLoader) configLoader = initConfig();
return configLoader;
}
async function initClient() {
async function initConfig() {
if (!isEnabled()) throw new Error('OIDC is not enabled.');

try {
assertHasAll('config keys', Object.keys(oidcConfig), ['issuerUrl', 'clientId', 'clientSecret']);

const { issuerUrl } = oidcConfig;
const issuer = await Issuer.discover(issuerUrl);
const { issuerUrl, clientId, clientSecret } = oidcConfig;
const _issuerUrl = new URL(issuerUrl);
const execute = _issuerUrl.hostname === 'localhost' ? [ allowInsecureRequests ] : undefined;
const discoveredConfig = await discovery(_issuerUrl, clientId, clientSecret, undefined, { execute });

// eslint-disable-next-line object-curly-newline
const {
Expand All @@ -70,7 +72,7 @@ async function initClient() {
response_types_supported,
scopes_supported,
token_endpoint_auth_methods_supported,
} = issuer.metadata; // eslint-disable-line object-curly-newline
} = discoveredConfig.serverMetadata(); // eslint-disable-line object-curly-newline

// This code uses email to verify a user's identity. An unverified email
// address is not suitable for verification.
Expand Down Expand Up @@ -106,16 +108,7 @@ async function initClient() {
assertHas('token signing alg', id_token_signing_alg_values_supported, TOKEN_SIGNING_ALG);
assertHas('token endpoint auth method', token_endpoint_auth_methods_supported, TOKEN_ENDPOINT_AUTH_METHOD);

const client = new issuer.Client({
client_id: oidcConfig.clientId,
client_secret: oidcConfig.clientSecret,
redirect_uris: [getRedirectUri()],
response_types: [RESPONSE_TYPE],
id_token_signed_response_alg: TOKEN_SIGNING_ALG,
token_endpoint_auth_method: TOKEN_ENDPOINT_AUTH_METHOD,
});

return client;
return discoveredConfig;
} catch (cause) {
Comment thread
alxndrsn marked this conversation as resolved.
// N.B. don't include the config here - it might include the client secret, perhaps in the wrong place.
throw new Error('Failed to configure OpenID Connect client', { cause });
Expand Down
70 changes: 34 additions & 36 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"mustache": "^4.2.0",
"nodemailer": "^8.0.4",
"odata-v4-parser": "~0.1",
"openid-client": "^5.7.1",
"openid-client": "^6.8.3",
"path-to-regexp": "^8.3.0",
"pg": "~8.8.0",
"pg-query-stream": "^4.14.0",
Expand Down