Skip to main content

tuwunel_admin/user/
reset_password.rs

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