-
Notifications
You must be signed in to change notification settings - Fork 32
logout to upstream OIDC Provider when logging out from MAS #4249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mcalinghee
wants to merge
4
commits into
element-hq:main
Choose a base branch
from
tchapgouv:feat/upstream_logout_end_session
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// Copyright 2025 New Vector Ltd. | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
// Please see LICENSE in the repository root for full details. | ||
|
||
use mas_data_model::{AuthenticationMethod, BrowserSession}; | ||
use mas_router::UrlBuilder; | ||
use mas_storage::{RepositoryAccess, upstream_oauth2::UpstreamOAuthProviderRepository}; | ||
use serde::{Deserialize, Serialize}; | ||
use thiserror::Error; | ||
use tracing::error; | ||
|
||
use super::cache::LazyProviderInfos; | ||
use crate::{MetadataCache, impl_from_error_for_route}; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct LogoutToken { | ||
logout_token: String, | ||
} | ||
|
||
/// Structure to collect upstream RP-initiated logout endpoints for a user | ||
#[derive(Debug, Default)] | ||
pub struct UpstreamLogoutInfo { | ||
/// Collection of logout endpoints that the user needs to be redirected to | ||
pub logout_endpoints: String, | ||
/// Optional post-logout redirect URI to come back to our app | ||
pub post_logout_redirect_uri: Option<String>, | ||
} | ||
|
||
#[derive(Debug, Error)] | ||
pub enum RouteError { | ||
#[error(transparent)] | ||
Internal(Box<dyn std::error::Error + Send + Sync + 'static>), | ||
|
||
#[error("provider was not found")] | ||
ProviderNotFound, | ||
|
||
#[error("session was not found")] | ||
SessionNotFound, | ||
} | ||
|
||
impl_from_error_for_route!(mas_storage::RepositoryError); | ||
impl_from_error_for_route!(mas_oidc_client::error::DiscoveryError); | ||
|
||
impl From<reqwest::Error> for RouteError { | ||
fn from(err: reqwest::Error) -> Self { | ||
Self::Internal(Box::new(err)) | ||
} | ||
} | ||
|
||
/// Get RP-initiated logout URLs for a user's upstream providers | ||
/// | ||
/// This retrieves logout endpoints from all connected upstream providers that | ||
/// support RP-initiated logout. | ||
/// | ||
/// # Parameters | ||
/// | ||
/// * `repo`: The repository to use | ||
/// * `url_builder`: URL builder for constructing redirect URIs | ||
/// * `cookie_jar`: Cookie from user's browser session | ||
/// | ||
/// # Returns | ||
/// | ||
/// Information about upstream logout endpoints the user should be redirected to | ||
/// | ||
/// # Errors | ||
/// | ||
/// Returns a `RouteError` if there's an issue accessing the repository | ||
pub async fn get_rp_initiated_logout_endpoints<E>( | ||
url_builder: &UrlBuilder, | ||
metadata_cache: &MetadataCache, | ||
client: &reqwest::Client, | ||
repo: &mut impl RepositoryAccess<Error = E>, | ||
browser_session: &BrowserSession, | ||
) -> Result<UpstreamLogoutInfo, RouteError> | ||
where | ||
RouteError: std::convert::From<E>, | ||
{ | ||
let mut result: UpstreamLogoutInfo = UpstreamLogoutInfo::default(); | ||
let post_logout_redirect_uri = url_builder | ||
.absolute_url_for(&mas_router::Login::default()) | ||
.to_string(); | ||
result.post_logout_redirect_uri = Some(post_logout_redirect_uri.clone()); | ||
|
||
let upstream_oauth2_session_id = repo | ||
.browser_session() | ||
.get_last_authentication(browser_session) | ||
.await? | ||
.ok_or(RouteError::SessionNotFound) | ||
.map(|auth| match auth.authentication_method { | ||
AuthenticationMethod::UpstreamOAuth2 { | ||
upstream_oauth2_session_id, | ||
} => Some(upstream_oauth2_session_id), | ||
_ => None, | ||
})? | ||
.ok_or(RouteError::SessionNotFound)?; | ||
|
||
let upstream_session = repo | ||
.upstream_oauth_session() | ||
.lookup(upstream_oauth2_session_id) | ||
.await? | ||
.ok_or(RouteError::SessionNotFound)?; | ||
|
||
let provider = repo | ||
.upstream_oauth_provider() | ||
.lookup(upstream_session.provider_id) | ||
.await? | ||
.filter(|provider| provider.allow_rp_initiated_logout) | ||
.ok_or(RouteError::ProviderNotFound)?; | ||
|
||
// Add post_logout_redirect_uri | ||
if let Some(post_uri) = &result.post_logout_redirect_uri { | ||
let mut lazy_metadata = LazyProviderInfos::new(metadata_cache, &provider, client); | ||
let mut end_session_url = lazy_metadata.end_session_endpoint().await?.clone(); | ||
end_session_url | ||
.query_pairs_mut() | ||
.append_pair("post_logout_redirect_uri", post_uri); | ||
end_session_url | ||
.query_pairs_mut() | ||
.append_pair("client_id", &provider.client_id); | ||
// Add id_token_hint if available | ||
if let Some(id_token) = upstream_session.id_token() { | ||
end_session_url | ||
.query_pairs_mut() | ||
.append_pair("id_token_hint", id_token); | ||
} | ||
result | ||
.logout_endpoints | ||
.clone_from(&end_session_url.to_string()); | ||
} | ||
|
||
Ok(result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 4 additions & 2 deletions
6
...2eab5efe330f60ef8c6c813c747424ba7ec9.json → ...f8afd0e2a75125197f157aab6cb10a8b3faa.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to make sure that this logic also happens when the logout is done through the React frontend. I don't have a good suggestion on how to do so for now though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am looking on how to integrate this here but I am not sure this is the way to go
We need to do a redirect to the upsteam
end_session_endpoint
.We might also need to integrate some component in the State to perform such as a http client to get/discover the URL.
Let me what your thoughts are.