mas_storage/user/password.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 mas_data_model::{Password, User};
9use rand_core::RngCore;
10
11use crate::{Clock, repository_impl};
12
13/// A [`UserPasswordRepository`] helps interacting with [`Password`] saved in
14/// the storage backend
15#[async_trait]
16pub trait UserPasswordRepository: Send + Sync {
17 /// The error type returned by the repository
18 type Error;
19
20 /// Get the active password for a user
21 ///
22 /// Returns `None` if the user has no password set
23 ///
24 /// # Parameters
25 ///
26 /// * `user`: The user to get the password for
27 ///
28 /// # Errors
29 ///
30 /// Returns [`Self::Error`] if underlying repository fails
31 async fn active(&mut self, user: &User) -> Result<Option<Password>, Self::Error>;
32
33 /// Set a new password for a user
34 ///
35 /// Returns the newly created [`Password`]
36 ///
37 /// # Parameters
38 ///
39 /// * `rng`: The random number generator to use
40 /// * `clock`: The clock used to generate timestamps
41 /// * `user`: The user to set the password for
42 /// * `version`: The version of the hashing scheme used
43 /// * `hashed_password`: The hashed password
44 /// * `upgraded_from`: The password this password was upgraded from, if any
45 ///
46 /// # Errors
47 ///
48 /// Returns [`Self::Error`] if underlying repository fails
49 async fn add(
50 &mut self,
51 rng: &mut (dyn RngCore + Send),
52 clock: &dyn Clock,
53 user: &User,
54 version: u16,
55 hashed_password: String,
56 upgraded_from: Option<&Password>,
57 ) -> Result<Password, Self::Error>;
58}
59
60repository_impl!(UserPasswordRepository:
61 async fn active(&mut self, user: &User) -> Result<Option<Password>, Self::Error>;
62 async fn add(
63 &mut self,
64 rng: &mut (dyn RngCore + Send),
65 clock: &dyn Clock,
66 user: &User,
67 version: u16,
68 hashed_password: String,
69 upgraded_from: Option<&Password>,
70 ) -> Result<Password, Self::Error>;
71);