tuwunel_admin/user/
create_user.rs1use tuwunel_core::{Err, Result, utils};
2use tuwunel_service::users::Register;
3
4use super::AUTO_GEN_PASSWORD_LENGTH;
5use crate::{admin_command, utils::parse_local_user_id};
6
7#[admin_command]
8pub(super) async fn create_user(&self, username: String, password: Option<String>) -> Result {
9 let user_id = parse_local_user_id(self.services, &username)?;
11
12 if let Err(e) = user_id.validate_strict()
13 && self
14 .services
15 .config
16 .authentication
17 .emergency_password
18 .is_none()
19 {
20 return Err!("Username {user_id} contains disallowed characters or spaces: {e}");
21 }
22
23 if self.services.users.exists(&user_id).await {
24 return Err!("User {user_id} already exists");
25 }
26
27 let password = password.unwrap_or_else(|| utils::random_string(AUTO_GEN_PASSWORD_LENGTH));
28
29 self.services
30 .users
31 .full_register(Register {
32 user_id: Some(&user_id),
33 password: Some(&password),
34 grant_first_user_admin: true,
35 ..Default::default()
36 })
37 .await?;
38
39 write!(self, "Created user with user_id: {user_id} and password: `{password}`").await
40}