mas_storage/compat/refresh_token.rs
1// Copyright 2024 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_trait::async_trait;
8use mas_data_model::{CompatAccessToken, CompatRefreshToken, CompatSession};
9use rand_core::RngCore;
10use ulid::Ulid;
11
12use crate::{Clock, repository_impl};
13
14/// A [`CompatRefreshTokenRepository`] helps interacting with
15/// [`CompatRefreshToken`] saved in the storage backend
16#[async_trait]
17pub trait CompatRefreshTokenRepository: Send + Sync {
18 /// The error type returned by the repository
19 type Error;
20
21 /// Lookup a compat refresh token by its ID
22 ///
23 /// Returns the compat refresh token if it exists, `None` otherwise
24 ///
25 /// # Parameters
26 ///
27 /// * `id`: The ID of the compat refresh token to lookup
28 ///
29 /// # Errors
30 ///
31 /// Returns [`Self::Error`] if the underlying repository fails
32 async fn lookup(&mut self, id: Ulid) -> Result<Option<CompatRefreshToken>, Self::Error>;
33
34 /// Find a compat refresh token by its token
35 ///
36 /// Returns the compat refresh token if found, `None` otherwise
37 ///
38 /// # Parameters
39 ///
40 /// * `refresh_token`: The token of the compat refresh token to lookup
41 ///
42 /// # Errors
43 ///
44 /// Returns [`Self::Error`] if the underlying repository fails
45 async fn find_by_token(
46 &mut self,
47 refresh_token: &str,
48 ) -> Result<Option<CompatRefreshToken>, Self::Error>;
49
50 /// Add a new compat refresh token to the database
51 ///
52 /// Returns the newly created compat refresh token
53 ///
54 /// # Parameters
55 ///
56 /// * `rng`: The random number generator to use
57 /// * `clock`: The clock used to generate timestamps
58 /// * `compat_session`: The compat session associated with this refresh
59 /// token
60 /// * `compat_access_token`: The compat access token created alongside this
61 /// refresh token
62 /// * `token`: The token of the refresh token
63 async fn add(
64 &mut self,
65 rng: &mut (dyn RngCore + Send),
66 clock: &dyn Clock,
67 compat_session: &CompatSession,
68 compat_access_token: &CompatAccessToken,
69 token: String,
70 ) -> Result<CompatRefreshToken, Self::Error>;
71
72 /// Consume a compat refresh token.
73 ///
74 /// This also marks other refresh tokens in the same session as consumed.
75 /// This is desirable because the syn2mas migration process can import
76 /// multiple refresh tokens for one device (compat session).
77 /// But once the user uses one of those, the others should no longer
78 /// be valid.
79 ///
80 /// Returns the consumed compat refresh token
81 ///
82 /// # Parameters
83 ///
84 /// * `clock`: The clock used to generate timestamps
85 /// * `compat_refresh_token`: The compat refresh token to consume
86 ///
87 /// # Errors
88 ///
89 /// Returns [`Self::Error`] if the underlying repository fails
90 async fn consume(
91 &mut self,
92 clock: &dyn Clock,
93 compat_refresh_token: CompatRefreshToken,
94 ) -> Result<CompatRefreshToken, Self::Error>;
95}
96
97repository_impl!(CompatRefreshTokenRepository:
98 async fn lookup(&mut self, id: Ulid) -> Result<Option<CompatRefreshToken>, Self::Error>;
99
100 async fn find_by_token(
101 &mut self,
102 refresh_token: &str,
103 ) -> Result<Option<CompatRefreshToken>, Self::Error>;
104
105 async fn add(
106 &mut self,
107 rng: &mut (dyn RngCore + Send),
108 clock: &dyn Clock,
109 compat_session: &CompatSession,
110 compat_access_token: &CompatAccessToken,
111 token: String,
112 ) -> Result<CompatRefreshToken, Self::Error>;
113
114 async fn consume(
115 &mut self,
116 clock: &dyn Clock,
117 compat_refresh_token: CompatRefreshToken,
118 ) -> Result<CompatRefreshToken, Self::Error>;
119);