1// Copyright 2024, 2025 New Vector Ltd.
2// Copyright 2023, 2024 The Matrix.org Foundation C.I.C.
3//
4// SPDX-License-Identifier: AGPL-3.0-only
5// Please see LICENSE in the repository root for full details.
67use async_graphql::{Response, ServerError};
8use mas_data_model::SiteConfig;
9use mas_matrix::HomeserverConnection;
10use mas_policy::Policy;
11use mas_router::UrlBuilder;
12use mas_storage::{BoxClock, BoxRepository, BoxRng, RepositoryError};
1314use crate::{Limiter, graphql::Requester, passwords::PasswordManager};
1516const CLEAR_SESSION_SENTINEL: &str = "__CLEAR_SESSION__";
1718#[async_trait::async_trait]
19pub trait State {
20async fn repository(&self) -> Result<BoxRepository, RepositoryError>;
21async fn policy(&self) -> Result<Policy, mas_policy::InstantiateError>;
22fn password_manager(&self) -> PasswordManager;
23fn homeserver_connection(&self) -> &dyn HomeserverConnection;
24fn clock(&self) -> BoxClock;
25fn rng(&self) -> BoxRng;
26fn site_config(&self) -> &SiteConfig;
27fn url_builder(&self) -> &UrlBuilder;
28fn limiter(&self) -> &Limiter;
29}
3031pub type BoxState = Box<dyn State + Send + Sync + 'static>;
3233pub trait ContextExt {
34fn state(&self) -> &BoxState;
3536fn mark_session_ended(&self);
3738fn requester(&self) -> &Requester;
39}
4041impl ContextExt for async_graphql::Context<'_> {
42fn state(&self) -> &BoxState {
43self.data_unchecked()
44 }
4546fn mark_session_ended(&self) {
47// Add a sentinel to the error context, so that we can know that we need to
48 // clear the session
49 // XXX: this is a bit of a hack, but the only sane way to get infos from within
50 // a mutation up to the HTTP handler
51self.add_error(ServerError::new(CLEAR_SESSION_SENTINEL, None));
52 }
5354fn requester(&self) -> &Requester {
55self.data_unchecked()
56 }
57}
5859/// Returns true if the response contains a sentinel error indicating that the
60/// current cookie session has ended, and the session cookie should be cleared.
61///
62/// Also removes the sentinel error from the response.
63pub fn has_session_ended(response: &mut Response) -> bool {
64let errors = std::mem::take(&mut response.errors);
65let mut must_clear_session = false;
66for error in errors {
67if error.message == CLEAR_SESSION_SENTINEL {
68 must_clear_session = true;
69 } else {
70 response.errors.push(error);
71 }
72 }
73 must_clear_session
74}