Files
wekan/models/lib/oauth2ClientSecretJwt.js
T
Lauri Ojansivu b7589737f5 File docs/Features the way the WeKan menus are.
A feature's guide now sits under the menu it is used from, read from the
templates rather than from where somebody once put it: Header-Bar,
All-Boards, Pages, Board-Header (search and every Board View), Board-Sidebar
(Board Settings, Filter, Members, Labels, Archive), Rules, Swimlanes, Lists,
Cards, Member-Settings, Admin-Panel and Login-Page. Topics used from several
menus (editor, history, theme, translations, email, troubleshooting) are in
General.

- Each area has a README mapping its menu entries to their guides and to the
  template or server module that implements them. Board-Header/README.md
  lists all 40 Board View entries with the template boardBody.jade renders;
  five views (Table, Calendar Mode, Time, Timeline, Map) have no guide of
  their own yet and say so.
- Board-View.md listed 25 views while BOARD_VIEWS has 40; it now lists all
  of them with their English labels.
- Pages keep their file names. Where a released changelog links an old path,
  a "# Moved" page stays there and points to the new one; test-menu.sh skips
  folders holding only those.
- Undo was filed under Login; it is General/Undo now.
- Every link in docs, code comments, scripts and tests follows the move;
  docsLinksResolve checks them all.

Tests that read a doc follow it to its new place. boardViewSettings now fails
when Board-View.md or Board-Header/README.md differs from BOARD_VIEWS, or
names a template boardBody.jade does not render.

Thanks to xet7 !
2026-10-16 23:45:35 +03:00

109 lines
4.1 KiB
JavaScript

'use strict';
// A generic-OAuth2 client secret, for providers whose "secret" is not a
// static string at all but a short-lived JWT the CLIENT must mint itself.
//
// Every generic-OAuth2 provider WeKan already talks to (Keycloak, Authelia,
// Nextcloud, Openshift, ADFS, Azure B2C, ...) hands out a static
// OAUTH2_SECRET string that is sent to the token endpoint as-is
// (packages/wekan-oidc/oidc_server.js: `OAuth.openSecret(config.secret)`).
// Sign in with Apple (#2458) is OIDC-shaped in every other respect, but its
// "client secret" is instead a JWT that:
// - is signed with ES256, using the PRIVATE KEY downloaded once from
// Apple's developer portal (never sent to Apple itself);
// - carries iss (the Apple Team ID), sub (the Services ID / client_id),
// aud (https://appleid.apple.com) and the key id (kid) of that private
// key, plus iat/exp;
// - is short-lived (Apple allows up to ~6 months, but there is no reason
// not to mint a fresh one per token-exchange request, which is what
// this module is for) - unlike a static secret, so it cannot be reused
// once it has expired even if leaked.
// See docs/Features/Login-Page/Apple.md for the operator-facing setup.
//
// This is plain Node `crypto` - no new dependency. Node has supported ES256
// (ECDSA P-256 + SHA-256) signing since 8.x, and the IEEE-P1363 (raw r||s)
// signature encoding JWT requires (rather than crypto's DER default) since
// 12.x via the `dsaEncoding` option.
const crypto = require('crypto');
const fs = require('fs');
function base64url(input) {
return Buffer.from(input)
.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=+$/, '');
}
// Pure: given the private key PEM and the claims, returns a signed ES256 JWT
// client secret. No filesystem or environment access, so this is the
// function the unit test drives directly.
function generateClientSecretJwt({
privateKeyPem,
keyId,
issuer,
subject,
audience,
now = Math.floor(Date.now() / 1000),
expiresInSeconds = 300,
} = {}) {
if (!privateKeyPem) throw new Error('generateClientSecretJwt: privateKeyPem is required');
if (!issuer) throw new Error('generateClientSecretJwt: issuer is required');
if (!subject) throw new Error('generateClientSecretJwt: subject is required');
if (!audience) throw new Error('generateClientSecretJwt: audience is required');
const header = { alg: 'ES256', typ: 'JWT' };
if (keyId) header.kid = keyId;
const claims = {
iss: issuer,
iat: now,
exp: now + Math.max(1, parseInt(expiresInSeconds, 10) || 300),
aud: audience,
sub: subject,
};
const signingInput = base64url(JSON.stringify(header)) + '.' + base64url(JSON.stringify(claims));
const signature = crypto.sign('sha256', Buffer.from(signingInput), {
key: privateKeyPem,
dsaEncoding: 'ieee-p1363',
});
return signingInput + '.' + base64url(signature);
}
// Reads the operator's configuration from the environment and, only when
// OAUTH2_SECRET_JWT_KEY_PATH is set, mints a fresh client-secret JWT. Returns
// null when unset, so every existing OAuth2 provider (Keycloak, Authelia,
// ...) is completely unaffected - the caller then falls back to the static
// OAUTH2_SECRET exactly as before.
function maybeGenerateOauth2ClientSecretJwt(env, clientId) {
const keyPath = env.OAUTH2_SECRET_JWT_KEY_PATH;
if (!keyPath) return null;
const privateKeyPem = fs.readFileSync(keyPath, 'utf8');
const issuer = env.OAUTH2_SECRET_JWT_ISSUER;
const keyId = env.OAUTH2_SECRET_JWT_KEY_ID;
// Apple's token endpoint is the default audience since this feature exists
// for #2458; a generic-OIDC provider that also needs this quirk can point
// it elsewhere.
const audience = env.OAUTH2_SECRET_JWT_AUDIENCE || 'https://appleid.apple.com';
const subject = env.OAUTH2_SECRET_JWT_SUBJECT || clientId;
const expiresInSeconds = env.OAUTH2_SECRET_JWT_EXPIRES_IN
? parseInt(env.OAUTH2_SECRET_JWT_EXPIRES_IN, 10)
: 300;
return generateClientSecretJwt({
privateKeyPem,
keyId,
issuer,
subject,
audience,
expiresInSeconds,
});
}
module.exports = { generateClientSecretJwt, maybeGenerateOauth2ClientSecretJwt };