Skip to content

Commit

Permalink
do an idp logout even when oidc.isAuthenticated is false
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharpandey13 committed Jan 2, 2025
1 parent 7dbc3b4 commit 91341f4
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,31 @@ class ResponseContext {
try {
const { client } = await getClient(config);

/**
* Generates the logout URL.
*
* Depending on the configuration, this function will either perform a local only logout
* or a federated logout by redirecting to the appropriate URL.
*
* @param {string} idTokenHint - The ID token hint to be used for the logout request.
* @returns {string} The URL to redirect the user to for logout.
*/
const getLogoutUrl = (idTokenHint) => {
// if idpLogout is not configured, perform a local only logout
if (!config.idpLogout) {
debug('performing a local only logout, redirecting to %s', returnURL);
return returnURL;
}

// if idpLogout is configured, perform a federated logout
return client.endSessionUrl({
...config.logoutParams,
...(idTokenHint && { id_token_hint: idTokenHint }),
post_logout_redirect_uri: returnURL,
...params.logoutParams,
});
};

if (url.parse(returnURL).host === null) {
returnURL = urlJoin(config.baseURL, returnURL);
}
Expand All @@ -311,23 +336,15 @@ class ResponseContext {

if (!req.oidc.isAuthenticated()) {
debug('end-user already logged out, redirecting to %s', returnURL);
return res.redirect(returnURL);

// perform idp logout with no token hint
return res.redirect(getLogoutUrl(undefined));
}

const { idToken: id_token_hint } = req.oidc;
req[config.session.name] = undefined;

if (!config.idpLogout) {
debug('performing a local only logout, redirecting to %s', returnURL);
return res.redirect(returnURL);
}

returnURL = client.endSessionUrl({
...config.logoutParams,
id_token_hint,
post_logout_redirect_uri: returnURL,
...params.logoutParams,
});
returnURL = getLogoutUrl(id_token_hint);
} catch (err) {
return next(err);
}
Expand Down

0 comments on commit 91341f4

Please sign in to comment.