Skip to main content

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