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