mas_handlers/graphql/
state.rs

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.
6
7use 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};
13
14use crate::{Limiter, graphql::Requester, passwords::PasswordManager};
15
16const CLEAR_SESSION_SENTINEL: &str = "__CLEAR_SESSION__";
17
18#[async_trait::async_trait]
19pub trait State {
20    async fn repository(&self) -> Result<BoxRepository, RepositoryError>;
21    async fn policy(&self) -> Result<Policy, mas_policy::InstantiateError>;
22    fn password_manager(&self) -> PasswordManager;
23    fn homeserver_connection(&self) -> &dyn HomeserverConnection;
24    fn clock(&self) -> BoxClock;
25    fn rng(&self) -> BoxRng;
26    fn site_config(&self) -> &SiteConfig;
27    fn url_builder(&self) -> &UrlBuilder;
28    fn limiter(&self) -> &Limiter;
29}
30
31pub type BoxState = Box<dyn State + Send + Sync + 'static>;
32
33pub trait ContextExt {
34    fn state(&self) -> &BoxState;
35
36    fn mark_session_ended(&self);
37
38    fn requester(&self) -> &Requester;
39}
40
41impl ContextExt for async_graphql::Context<'_> {
42    fn state(&self) -> &BoxState {
43        self.data_unchecked()
44    }
45
46    fn 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
51        self.add_error(ServerError::new(CLEAR_SESSION_SENTINEL, None));
52    }
53
54    fn requester(&self) -> &Requester {
55        self.data_unchecked()
56    }
57}
58
59/// 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 {
64    let errors = std::mem::take(&mut response.errors);
65    let mut must_clear_session = false;
66    for error in errors {
67        if error.message == CLEAR_SESSION_SENTINEL {
68            must_clear_session = true;
69        } else {
70            response.errors.push(error);
71        }
72    }
73    must_clear_session
74}