Skip to main content

tuwunel_admin/user/
reset_password.rs

1use tuwunel_core::{Err, Result, err, utils::random_string};
2
3use super::AUTO_GEN_PASSWORD_LENGTH;
4use crate::{admin_command, utils::parse_existing_local_user_id};
5
6#[admin_command]
7pub(super) async fn reset_password(
8	&self,
9	logout_devices: bool,
10	username: String,
11	password: Option<String>,
12) -> Result {
13	let user_id = parse_existing_local_user_id(self.services, &username).await?;
14
15	if user_id == self.services.globals.server_user {
16		return Err!(
17			"Not allowed to set the password for the server account. Please use the emergency \
18			 password config option.",
19		);
20	}
21
22	let new_password = password.unwrap_or_else(|| random_string(AUTO_GEN_PASSWORD_LENGTH));
23
24	self.services
25		.users
26		.set_password(&user_id, Some(new_password.as_str()))
27		.await
28		.map_err(|e| err!("Couldn't reset the password for user {user_id}: {e}"))?;
29
30	if logout_devices {
31		self.services
32			.users
33			.remove_devices(&user_id, None)
34			.await;
35	}
36
37	write!(self, "Successfully reset the password for user {user_id}: `{new_password}`").await
38}