mas_storage/user/registration.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
// Copyright 2025 New Vector Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.
use std::net::IpAddr;
use async_trait::async_trait;
use mas_data_model::{UserAgent, UserEmailAuthentication, UserRegistration};
use rand_core::RngCore;
use ulid::Ulid;
use url::Url;
use crate::{repository_impl, Clock};
/// A [`UserRegistrationRepository`] helps interacting with [`UserRegistration`]
/// saved in the storage backend
#[async_trait]
pub trait UserRegistrationRepository: Send + Sync {
/// The error type returned by the repository
type Error;
/// Lookup a [`UserRegistration`] by its ID
///
/// Returns `None` if no [`UserRegistration`] was found
///
/// # Parameters
///
/// * `id`: The ID of the [`UserRegistration`] to lookup
///
/// # Errors
///
/// Returns [`Self::Error`] if the underlying repository fails
async fn lookup(&mut self, id: Ulid) -> Result<Option<UserRegistration>, Self::Error>;
/// Create a new [`UserRegistration`] session
///
/// Returns the newly created [`UserRegistration`]
///
/// # Parameters
///
/// * `rng`: The random number generator to use
/// * `clock`: The clock used to generate timestamps
/// * `username`: The username of the user
/// * `ip_address`: The IP address of the user agent, if any
/// * `user_agent`: The user agent of the user agent, if any
/// * `post_auth_action`: The post auth action to execute after the
/// registration, if any
///
/// # Errors
///
/// Returns [`Self::Error`] if the underlying repository fails
async fn add(
&mut self,
rng: &mut (dyn RngCore + Send),
clock: &dyn Clock,
username: String,
ip_address: Option<IpAddr>,
user_agent: Option<UserAgent>,
post_auth_action: Option<serde_json::Value>,
) -> Result<UserRegistration, Self::Error>;
/// Set the display name of a [`UserRegistration`]
///
/// Returns the updated [`UserRegistration`]
///
/// # Parameters
///
/// * `user_registration`: The [`UserRegistration`] to update
/// * `display_name`: The display name to set
///
/// # Errors
///
/// Returns [`Self::Error`] if the underlying repository fails or if the
/// registration is already completed
async fn set_display_name(
&mut self,
user_registration: UserRegistration,
display_name: String,
) -> Result<UserRegistration, Self::Error>;
/// Set the terms URL of a [`UserRegistration`]
///
/// Returns the updated [`UserRegistration`]
///
/// # Parameters
///
/// * `user_registration`: The [`UserRegistration`] to update
/// * `terms_url`: The terms URL to set
///
/// # Errors
///
/// Returns [`Self::Error`] if the underlying repository fails or if the
/// registration is already completed
async fn set_terms_url(
&mut self,
user_registration: UserRegistration,
terms_url: Url,
) -> Result<UserRegistration, Self::Error>;
/// Set the email authentication code of a [`UserRegistration`]
///
/// Returns the updated [`UserRegistration`]
///
/// # Parameters
///
/// * `user_registration`: The [`UserRegistration`] to update
/// * `email_authentication`: The [`UserEmailAuthentication`] to set
///
/// # Errors
///
/// Returns [`Self::Error`] if the underlying repository fails or if the
/// registration is already completed
async fn set_email_authentication(
&mut self,
user_registration: UserRegistration,
email_authentication: &UserEmailAuthentication,
) -> Result<UserRegistration, Self::Error>;
/// Set the password of a [`UserRegistration`]
///
/// Returns the updated [`UserRegistration`]
///
/// # Parameters
///
/// * `user_registration`: The [`UserRegistration`] to update
/// * `hashed_password`: The hashed password to set
/// * `version`: The version of the hashing scheme
///
/// # Errors
///
/// Returns [`Self::Error`] if the underlying repository fails or if the
/// registration is already completed
async fn set_password(
&mut self,
user_registration: UserRegistration,
hashed_password: String,
version: u16,
) -> Result<UserRegistration, Self::Error>;
/// Complete a [`UserRegistration`]
///
/// Returns the updated [`UserRegistration`]
///
/// # Parameters
///
/// * `clock`: The clock used to generate timestamps
/// * `user_registration`: The [`UserRegistration`] to complete
///
/// # Errors
///
/// Returns [`Self::Error`] if the underlying repository fails or if the
/// registration is already completed
async fn complete(
&mut self,
clock: &dyn Clock,
user_registration: UserRegistration,
) -> Result<UserRegistration, Self::Error>;
}
repository_impl!(UserRegistrationRepository:
async fn lookup(&mut self, id: Ulid) -> Result<Option<UserRegistration>, Self::Error>;
async fn add(
&mut self,
rng: &mut (dyn RngCore + Send),
clock: &dyn Clock,
username: String,
ip_address: Option<IpAddr>,
user_agent: Option<UserAgent>,
post_auth_action: Option<serde_json::Value>,
) -> Result<UserRegistration, Self::Error>;
async fn set_display_name(
&mut self,
user_registration: UserRegistration,
display_name: String,
) -> Result<UserRegistration, Self::Error>;
async fn set_terms_url(
&mut self,
user_registration: UserRegistration,
terms_url: Url,
) -> Result<UserRegistration, Self::Error>;
async fn set_email_authentication(
&mut self,
user_registration: UserRegistration,
email_authentication: &UserEmailAuthentication,
) -> Result<UserRegistration, Self::Error>;
async fn set_password(
&mut self,
user_registration: UserRegistration,
hashed_password: String,
version: u16,
) -> Result<UserRegistration, Self::Error>;
async fn complete(
&mut self,
clock: &dyn Clock,
user_registration: UserRegistration,
) -> Result<UserRegistration, Self::Error>;
);