1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright 2022 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use anyhow::Context as _;
use async_graphql::{Context, Description, Enum, Object, ID};
use chrono::{DateTime, Utc};
use mas_storage::{compat::CompatSessionRepository, user::UserRepository};
use url::Url;

use super::{BrowserSession, NodeType, SessionState, User, UserAgent};
use crate::graphql::state::ContextExt;

/// Lazy-loaded reverse reference.
///
/// XXX: maybe we want to stick that in a utility module
#[derive(Clone, Debug, Default)]
enum ReverseReference<T> {
    Loaded(T),
    #[default]
    Lazy,
}

/// A compat session represents a client session which used the legacy Matrix
/// login API.
#[derive(Description)]
pub struct CompatSession {
    session: mas_data_model::CompatSession,
    sso_login: ReverseReference<Option<mas_data_model::CompatSsoLogin>>,
}

impl CompatSession {
    pub fn new(session: mas_data_model::CompatSession) -> Self {
        Self {
            session,
            sso_login: ReverseReference::Lazy,
        }
    }

    /// Save an eagerly loaded SSO login.
    pub fn with_loaded_sso_login(
        mut self,
        sso_login: Option<mas_data_model::CompatSsoLogin>,
    ) -> Self {
        self.sso_login = ReverseReference::Loaded(sso_login);
        self
    }
}

/// The type of a compatibility session.
#[derive(Enum, Copy, Clone, Eq, PartialEq)]
pub enum CompatSessionType {
    /// The session was created by a SSO login.
    SsoLogin,

    /// The session was created by an unknown method.
    Unknown,
}

#[Object(use_type_description)]
impl CompatSession {
    /// ID of the object.
    pub async fn id(&self) -> ID {
        NodeType::CompatSession.id(self.session.id)
    }

    /// The user authorized for this session.
    async fn user(&self, ctx: &Context<'_>) -> Result<User, async_graphql::Error> {
        let state = ctx.state();
        let mut repo = state.repository().await?;
        let user = repo
            .user()
            .lookup(self.session.user_id)
            .await?
            .context("Could not load user")?;
        repo.cancel().await?;

        Ok(User(user))
    }

    /// The Matrix Device ID of this session.
    async fn device_id(&self) -> &str {
        self.session.device.as_str()
    }

    /// When the object was created.
    pub async fn created_at(&self) -> DateTime<Utc> {
        self.session.created_at
    }

    /// When the session ended.
    pub async fn finished_at(&self) -> Option<DateTime<Utc>> {
        self.session.finished_at()
    }

    /// The user-agent with which the session was created.
    pub async fn user_agent(&self) -> Option<UserAgent> {
        self.session.user_agent.clone().map(UserAgent::from)
    }

    /// The associated SSO login, if any.
    pub async fn sso_login(
        &self,
        ctx: &Context<'_>,
    ) -> Result<Option<CompatSsoLogin>, async_graphql::Error> {
        if let ReverseReference::Loaded(sso_login) = &self.sso_login {
            return Ok(sso_login.clone().map(CompatSsoLogin));
        }

        // We need to load it on the fly
        let state = ctx.state();
        let mut repo = state.repository().await?;
        let sso_login = repo
            .compat_sso_login()
            .find_for_session(&self.session)
            .await
            .context("Could not load SSO login")?;
        repo.cancel().await?;

        Ok(sso_login.map(CompatSsoLogin))
    }

    /// The browser session which started this session, if any.
    pub async fn browser_session(
        &self,
        ctx: &Context<'_>,
    ) -> Result<Option<BrowserSession>, async_graphql::Error> {
        let Some(user_session_id) = self.session.user_session_id else {
            return Ok(None);
        };

        let state = ctx.state();
        let mut repo = state.repository().await?;
        let browser_session = repo
            .browser_session()
            .lookup(user_session_id)
            .await?
            .context("Could not load browser session")?;
        repo.cancel().await?;

        Ok(Some(BrowserSession(browser_session)))
    }

    /// The state of the session.
    pub async fn state(&self) -> SessionState {
        match &self.session.state {
            mas_data_model::CompatSessionState::Valid => SessionState::Active,
            mas_data_model::CompatSessionState::Finished { .. } => SessionState::Finished,
        }
    }

    /// The last IP address used by the session.
    pub async fn last_active_ip(&self) -> Option<String> {
        self.session.last_active_ip.map(|ip| ip.to_string())
    }

    /// The last time the session was active.
    pub async fn last_active_at(&self) -> Option<DateTime<Utc>> {
        self.session.last_active_at
    }
}

/// A compat SSO login represents a login done through the legacy Matrix login
/// API, via the `m.login.sso` login method.
#[derive(Description)]
pub struct CompatSsoLogin(pub mas_data_model::CompatSsoLogin);

#[Object(use_type_description)]
impl CompatSsoLogin {
    /// ID of the object.
    pub async fn id(&self) -> ID {
        NodeType::CompatSsoLogin.id(self.0.id)
    }

    /// When the object was created.
    pub async fn created_at(&self) -> DateTime<Utc> {
        self.0.created_at
    }

    /// The redirect URI used during the login.
    async fn redirect_uri(&self) -> &Url {
        &self.0.redirect_uri
    }

    /// When the login was fulfilled, and the user was redirected back to the
    /// client.
    async fn fulfilled_at(&self) -> Option<DateTime<Utc>> {
        self.0.fulfilled_at()
    }

    /// When the client exchanged the login token sent during the redirection.
    async fn exchanged_at(&self) -> Option<DateTime<Utc>> {
        self.0.exchanged_at()
    }

    /// The compat session which was started by this login.
    async fn session(
        &self,
        ctx: &Context<'_>,
    ) -> Result<Option<CompatSession>, async_graphql::Error> {
        let Some(session_id) = self.0.session_id() else {
            return Ok(None);
        };

        let state = ctx.state();
        let mut repo = state.repository().await?;
        let session = repo
            .compat_session()
            .lookup(session_id)
            .await?
            .context("Could not load compat session")?;
        repo.cancel().await?;

        Ok(Some(
            CompatSession::new(session).with_loaded_sso_login(Some(self.0.clone())),
        ))
    }
}