Skip to main content

mas_storage/compat/
access_token.rs

1// Copyright 2025, 2026 Element Creations Ltd.
2// Copyright 2024, 2025 New Vector Ltd.
3// Copyright 2022-2024 The Matrix.org Foundation C.I.C.
4//
5// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
6// Please see LICENSE files in the repository root for full details.
7
8use async_trait::async_trait;
9use chrono::Duration;
10use mas_data_model::{Clock, CompatAccessToken, CompatSession};
11use rand_core::Rng;
12use ulid::Ulid;
13
14use crate::repository_impl;
15
16/// A [`CompatAccessTokenRepository`] helps interacting with
17/// [`CompatAccessToken`] saved in the storage backend
18#[async_trait]
19pub trait CompatAccessTokenRepository: Send + Sync {
20    /// The error type returned by the repository
21    type Error;
22
23    /// Lookup a compat access token by its ID
24    ///
25    /// Returns the compat access token if it exists, `None` otherwise
26    ///
27    /// # Parameters
28    ///
29    /// * `id`: The ID of the compat access token to lookup
30    ///
31    /// # Errors
32    ///
33    /// Returns [`Self::Error`] if the underlying repository fails
34    async fn lookup(&mut self, id: Ulid) -> Result<Option<CompatAccessToken>, Self::Error>;
35
36    /// Find a compat access token by its token
37    ///
38    /// Returns the compat access token if found, `None` otherwise
39    ///
40    /// # Parameters
41    ///
42    /// * `access_token`: The token of the compat access token to lookup
43    ///
44    /// # Errors
45    ///
46    /// Returns [`Self::Error`] if the underlying repository fails
47    async fn find_by_token(
48        &mut self,
49        access_token: &str,
50    ) -> Result<Option<CompatAccessToken>, Self::Error>;
51
52    /// Add a new compat access token to the database
53    ///
54    /// Returns the newly created compat access token
55    ///
56    /// # Parameters
57    ///
58    /// * `rng`: The random number generator to use
59    /// * `clock`: The clock used to generate timestamps
60    /// * `compat_session`: The compat session associated with the access token
61    /// * `token`: The token of the access token
62    /// * `expires_after`: The duration after which the access token expires, if
63    ///   specified
64    ///
65    /// # Errors
66    ///
67    /// Returns [`Self::Error`] if the underlying repository fails
68    async fn add(
69        &mut self,
70        rng: &mut (dyn Rng + Send),
71        clock: &dyn Clock,
72        compat_session: &CompatSession,
73        token: String,
74        expires_after: Option<Duration>,
75    ) -> Result<CompatAccessToken, Self::Error>;
76
77    /// Set the expiration time of the compat access token to now
78    ///
79    /// Returns the expired compat access token
80    ///
81    /// # Parameters
82    ///
83    /// * `clock`: The clock used to generate timestamps
84    /// * `compat_access_token`: The compat access token to expire
85    async fn expire(
86        &mut self,
87        clock: &dyn Clock,
88        compat_access_token: CompatAccessToken,
89    ) -> Result<CompatAccessToken, Self::Error>;
90}
91
92repository_impl!(CompatAccessTokenRepository:
93    async fn lookup(&mut self, id: Ulid) -> Result<Option<CompatAccessToken>, Self::Error>;
94
95    async fn find_by_token(
96        &mut self,
97        access_token: &str,
98    ) -> Result<Option<CompatAccessToken>, Self::Error>;
99
100    async fn add(
101        &mut self,
102        rng: &mut (dyn Rng + Send),
103        clock: &dyn Clock,
104        compat_session: &CompatSession,
105        token: String,
106        expires_after: Option<Duration>,
107    ) -> Result<CompatAccessToken, Self::Error>;
108
109    async fn expire(
110        &mut self,
111        clock: &dyn Clock,
112        compat_access_token: CompatAccessToken,
113    ) -> Result<CompatAccessToken, Self::Error>;
114);