Files
wekan/server/lib/oauthLogoutUrl.js
T
Lauri Ojansivu 2e752c1e96 Extract the OIDC RP-Initiated Logout URL builder into a pure, tested helper.
#2905 asked for Single Logout (SLO): logging out of Wekan should also end the
identity provider's own SSO session for OIDC/OAuth2 providers that support
RP-Initiated Logout (e.g. Keycloak's
/realms/<realm>/protocol/openid-connect/logout), via the optional
OAUTH2_LOGOUT_ENDPOINT env var. This was already implemented for #6158
(getOauthLogoutUrl() in server/models/settings.js, wired into
config/accounts.js's onLogoutHook(), documented in
docs/Features/Login/Keycloak/Keycloak.md and the docker-compose.yml OAuth2
example blocks) - when unset (the default), logout is unchanged.

The URL-building logic lived inline in the Meteor method, so it had no direct
test coverage. Extracted it to server/lib/oauthLogoutUrl.js's
buildOauthLogoutUrl(), a plain-Node pure function (endpoint/serverUrl/
clientId/redirectUri in, the end_session URL out - or '' when endpoint is
unset), following the same pattern as server/lib/ldapPasswordLoginGuard.js.
getOauthLogoutUrl() now just calls it; behavior is unchanged.

Added tests/oauthLogoutUrl.test.cjs covering: the default no-op when
OAUTH2_LOGOUT_ENDPOINT is unset/empty, a Keycloak-shaped path endpoint
resolved against OAUTH2_SERVER_URL (with and without a trailing slash), an
absolute endpoint that ignores serverUrl, an endpoint that already carries a
query string, optional client_id/post_logout_redirect_uri, and percent-
encoding of the redirect URI.

Thanks to Dzordzu and xet7 !

Closes #2905,
2026-09-10 20:27:34 +03:00

57 lines
2.4 KiB
JavaScript

'use strict';
// Pure helper for building the OIDC RP-Initiated Logout URL (issue #2905,
// already implemented for #6158 - see server/models/settings.js'
// getOauthLogoutUrl()). Extracted so the URL-building logic is unit-testable
// in plain Node without Meteor (mirrors server/lib/ldapPasswordLoginGuard.js).
//
// OpenID Connect RP-Initiated Logout 1.0
// (https://openid.net/specs/openid-connect-rpinitiated-1_0.html) defines an
// end_session_endpoint that a relying party redirects the browser to on
// logout, optionally passing post_logout_redirect_uri (and client_id) so the
// provider sends the browser back afterwards. Without this, WeKan's own
// Meteor.logout() only ends the local WeKan session - with an IdP that keeps
// its own SSO session (Keycloak, for example) the user is immediately signed
// back in on the next visit, or an autologin flow bounces them straight back
// to the provider. When OAUTH2_LOGOUT_ENDPOINT is unset (the default), this
// is never called and logout behaves exactly as before.
//
// buildOauthLogoutUrl({ endpoint, serverUrl, clientId, redirectUri }):
// - endpoint: OAUTH2_LOGOUT_ENDPOINT - either an absolute URL, or a path
// (e.g. Keycloak's "/realms/<realm>/protocol/openid-connect/logout")
// that is resolved against serverUrl (OAUTH2_SERVER_URL);
// - serverUrl: OAUTH2_SERVER_URL, used only when endpoint is a path;
// - clientId: OAUTH2_CLIENT_ID, appended as client_id when present (some
// providers, Keycloak included, use it to validate the redirect URI);
// - redirectUri: where the provider should send the browser back to -
// WeKan's own root URL.
// Returns '' when endpoint is not set (the "do nothing, unchanged behavior"
// case), otherwise the full end_session URL.
function buildOauthLogoutUrl({ endpoint, serverUrl, clientId, redirectUri } = {}) {
if (!endpoint) {
return '';
}
const isAbsolute = /^https?:\/\//.test(endpoint);
const base = isAbsolute
? endpoint
: String(serverUrl || '').replace(/\/$/, '') + endpoint;
const params = [];
if (redirectUri) {
params.push('post_logout_redirect_uri=' + encodeURIComponent(redirectUri));
}
if (clientId) {
params.push('client_id=' + encodeURIComponent(clientId));
}
if (params.length === 0) {
return base;
}
return base + (base.includes('?') ? '&' : '?') + params.join('&');
}
module.exports = {
buildOauthLogoutUrl,
};