authentication/
user.rs

1//! User management functionality for the Authentication module.
2
3//!
4//! This module provides comprehensive user account management including:
5//! - User creation and authentication
6//! - Password management with secure hashing
7//! - User profile management (name, primary group)
8//! - File-based persistent storage of user data
9//!
10//! All user data is stored as JSON files in the `/System/Users/` directory,
11//! with each user having their own file named after their username.
12
13use alloc::{
14    borrow::ToOwned,
15    string::{String, ToString},
16    vec::Vec,
17};
18use file_system::{Flags, Mode, Open, Path, PathOwned};
19use miniserde::{Deserialize, Serialize};
20use users::{GroupIdentifier, GroupIdentifierInner, UserIdentifier, UserIdentifierInner};
21use virtual_file_system::{Directory, File, VirtualFileSystem};
22
23use crate::{
24    Error, Result, USERS_FOLDER_PATH,
25    hash::{generate_salt, hash_password},
26};
27
28/// Represents a user account with all associated metadata.
29///
30/// This structure contains all the information needed to represent a user
31/// in the system, including their unique identifier, name, primary group,
32/// and hashed password with salt for security.
33#[derive(Clone, Debug, Deserialize, Serialize)]
34pub struct User {
35    /// Unique identifier for the user
36    identifier: UserIdentifierInner,
37    /// Human-readable username
38    name: String,
39    /// Identifier of the user's primary group
40    primary_group: GroupIdentifierInner,
41    /// SHA-512 hash of the user's password combined with salt
42    hash: String,
43    /// Random salt used for password hashing
44    salt: String,
45}
46
47impl User {
48    /// Creates a new user instance with the provided information.
49    ///
50    /// # Arguments
51    ///
52    /// * `Identifier` - Unique numerical identifier for the user
53    /// * `Name` - Human-readable username
54    /// * `Primary_group` - Identifier of the user's primary group
55    /// * `Hash` - Pre-computed SHA-512 hash of password+salt
56    /// * `Salt` - Random salt used for password hashing
57    ///
58    /// # Returns
59    ///
60    /// A new `User_type` instance with the provided data.
61    pub fn new(
62        identifier: UserIdentifierInner,
63        name: String,
64        primary_group: GroupIdentifierInner,
65        hash: String,
66        salt: String,
67    ) -> Self {
68        Self {
69            identifier,
70            name,
71            primary_group,
72            hash,
73            salt,
74        }
75    }
76
77    /// Returns the user's unique identifier.
78    ///
79    /// # Returns
80    ///
81    /// A `User_identifier_type` containing the user's unique ID.
82    pub fn get_identifier(&self) -> UserIdentifier {
83        UserIdentifier::new(self.identifier)
84    }
85
86    /// Returns the user's primary group identifier.
87    ///
88    /// # Returns
89    ///
90    /// A `Group_identifier_type` containing the user's primary group ID.
91    pub fn get_primary_group(&self) -> GroupIdentifier {
92        GroupIdentifier::new(self.primary_group)
93    }
94
95    /// Returns the user's name as a string slice.
96    ///
97    /// # Returns
98    ///
99    /// A string slice containing the username.
100    pub fn get_name(&self) -> &str {
101        &self.name
102    }
103
104    /// Returns the user's password hash as a string slice.
105    ///
106    /// # Returns
107    ///
108    /// A string slice containing the SHA-512 hash of password+salt.
109    pub fn get_hash(&self) -> &str {
110        &self.hash
111    }
112
113    /// Returns the user's salt as a string slice.
114    ///
115    /// # Returns
116    ///
117    /// A string slice containing the random salt used for password hashing.
118    pub fn get_salt(&self) -> &str {
119        &self.salt
120    }
121
122    /// Updates the user's password hash.
123    ///
124    /// # Arguments
125    ///
126    /// * `Hash` - New SHA-512 hash to store
127    pub fn set_hash(&mut self, hash: String) {
128        self.hash = hash;
129    }
130
131    /// Updates the user's salt.
132    ///
133    /// # Arguments
134    ///
135    /// * `Salt` - New salt to store
136    pub fn set_salt(&mut self, salt: String) {
137        self.salt = salt;
138    }
139
140    /// Updates the user's primary group.
141    ///
142    /// # Arguments
143    ///
144    /// * `Primary_group` - New primary group identifier
145    pub fn set_primary_group(&mut self, primary_group: GroupIdentifierInner) {
146        self.primary_group = primary_group;
147    }
148
149    /// Updates the user's name.
150    ///
151    /// # Arguments
152    ///
153    /// * `Name` - New username to store
154    pub fn set_name(&mut self, name: String) {
155        self.name = name;
156    }
157}
158
159/// Constructs the file system path for a user's data file.
160///
161/// # Arguments
162///
163/// * `User_name` - The username to generate a path for
164///
165/// # Returns
166///
167/// Returns `Ok(Path_owned_type)` with the complete path to the user file,
168/// or `Err(Error::Failed_to_get_user_file_path)` if path construction fails.
169pub fn get_user_file_path(user_name: &str) -> Result<PathOwned> {
170    Path::new(USERS_FOLDER_PATH)
171        .to_owned()
172        .append(user_name)
173        .ok_or(Error::FailedToGetUserFilePath)
174}
175
176/// Authenticates a user with their username and password.
177///
178/// This function reads the user's file from the filesystem, compares the
179/// provided password hash with the stored hash, and returns the user's
180/// identifier if authentication succeeds.
181///
182/// # Arguments
183///
184/// * `Virtual_file_system` - Reference to the virtual file system
185/// * `User_name` - Username to authenticate
186/// * `Password` - Plain text password to verify
187///
188/// # Returns
189///
190/// Returns `Ok(User_identifier_type)` if authentication succeeds,
191/// or an appropriate error if authentication fails or file operations fail.
192///
193/// # Errors
194///
195/// - `Failed_to_get_user_file_path` - Invalid username or path construction failure
196/// - `Failed_to_open_user_file` - User file doesn't exist or permission denied
197/// - `Failed_to_read_user_file` - I/O error reading user file
198/// - `Failed_to_parse_user_file` - Invalid JSON format in user file
199/// - `Invalid_password` - Password doesn't match stored hash
200pub async fn authenticate_user<'a>(
201    virtual_file_system: &'a VirtualFileSystem<'a>,
202    user_name: &str,
203    password: &str,
204) -> Result<UserIdentifier> {
205    let path = get_user_file_path(user_name)?;
206
207    let user_file = File::open(virtual_file_system, path, Mode::READ_ONLY.into())
208        .await
209        .map_err(Error::FailedToOpenUserFile)?;
210
211    let mut buffer = Vec::new();
212
213    user_file
214        .read_to_end(&mut buffer)
215        .await
216        .map_err(Error::FailedToReadUserFile)?;
217
218    let user: User = miniserde::json::from_str(core::str::from_utf8(&buffer).unwrap())
219        .map_err(Error::FailedToParseUserFile)?;
220
221    if hash_password(password, user.get_salt()) == user.get_hash() {
222        Ok(user.get_identifier())
223    } else {
224        Err(Error::InvalidPassword)
225    }
226}
227
228/// Creates a new user account with the specified parameters.
229///
230/// This function creates a new user in the system by:
231/// 1. Generating a new user identifier (if not provided)
232/// 2. Adding the user to the Users manager
233/// 3. Generating a random salt and hashing the password
234/// 4. Creating the user file with all user data
235///
236/// # Arguments
237///
238/// * `Virtual_file_system` - Reference to the virtual file system
239/// * `User_name` - Username for the new account
240/// * `Password` - Plain text password for the new account
241/// * `Primary_group` - Primary group identifier for the user
242/// * `User_identifier` - Optional specific user identifier (auto-generated if None)
243///
244/// # Returns
245///
246/// Returns `Ok(User_identifier_type)` with the new user's identifier,
247/// or an appropriate error if creation fails.
248///
249/// # Errors
250///
251/// This function can fail for various reasons including:
252/// - User identifier generation or assignment failures
253/// - File system operations (directory creation, file writing)
254/// - Users manager operations (adding user)
255/// - Random salt generation failures
256pub async fn create_user<'a>(
257    virtual_file_system: &'a VirtualFileSystem<'a>,
258    user_name: &str,
259    password: &str,
260    primary_group: GroupIdentifier,
261    user_identifier: Option<UserIdentifier>,
262) -> Result<UserIdentifier> {
263    let users_manager = users::get_instance();
264
265    // - New user identifier if not provided.
266    let user_identifier = if let Some(user_identifier) = user_identifier {
267        user_identifier
268    } else {
269        users_manager
270            .get_new_user_identifier()
271            .await
272            .map_err(Error::FailedToGetNewUserIdentifier)?
273    };
274
275    // - Add it to the users manager.
276    users_manager
277        .add_user(user_identifier, user_name, primary_group)
278        .await
279        .map_err(Error::FailedToCreateUser)?;
280
281    // - Hash password.
282    let salt = generate_salt().await?;
283
284    let hash = hash_password(password, &salt);
285
286    // - Write user file.
287    let user = User::new(
288        user_identifier.as_u16(),
289        user_name.to_string(),
290        primary_group.as_u16(),
291        hash,
292        salt,
293    );
294
295    match Directory::create(virtual_file_system, USERS_FOLDER_PATH).await {
296        Ok(_) | Err(file_system::Error::AlreadyExists) => {}
297        Err(error) => Err(Error::FailedToCreateUsersDirectory(error))?,
298    }
299
300    let user_file_path = Path::new(USERS_FOLDER_PATH)
301        .to_owned()
302        .append(user_name)
303        .ok_or(Error::FailedToGetUserFilePath)?;
304
305    let user_file: File<'_> = File::open(
306        virtual_file_system,
307        user_file_path,
308        Flags::new(Mode::WRITE_ONLY, Some(Open::CREATE_ONLY), None),
309    )
310    .await
311    .map_err(Error::FailedToOpenUserFile)?;
312
313    let user_json = miniserde::json::to_string(&user);
314
315    user_file
316        .write(user_json.as_bytes())
317        .await
318        .map_err(Error::FailedToWriteUserFile)?;
319
320    Ok(user_identifier)
321}
322
323/// Changes a user's password by generating a new salt and hash.
324///
325/// This function updates a user's password by:
326/// 1. Generating a new random salt
327/// 2. Hashing the new password with the salt
328/// 3. Reading the existing user file
329/// 4. Updating the hash and salt fields
330/// 5. Writing the updated data back to the file
331///
332/// # Arguments
333///
334/// * `Virtual_file_system` - Reference to the virtual file system
335/// * `User_name` - Username of the account to update
336/// * `New_password` - New plain text password
337///
338/// # Returns
339///
340/// Returns `Ok(())` if the password was changed successfully,
341/// or an appropriate error if the operation fails.
342///
343/// # Errors
344///
345/// - File system errors (opening, reading, writing user file)
346/// - Salt generation failures
347/// - JSON parsing errors
348pub async fn change_user_password<'a>(
349    virtual_file_system: &'a VirtualFileSystem<'a>,
350    user_name: &str,
351    new_password: &str,
352) -> Result<()> {
353    let salt = generate_salt().await?;
354
355    let hash = hash_password(new_password, &salt);
356
357    let user_file_path = Path::new(USERS_FOLDER_PATH)
358        .to_owned()
359        .append(user_name)
360        .ok_or(Error::FailedToGetUserFilePath)?;
361
362    let user_file = File::open(
363        virtual_file_system,
364        user_file_path,
365        Flags::new(Mode::READ_WRITE, Some(Open::TRUNCATE), None),
366    )
367    .await
368    .map_err(Error::FailedToOpenUserFile)?;
369
370    let mut buffer = Vec::new();
371
372    user_file
373        .read_to_end(&mut buffer)
374        .await
375        .map_err(Error::FailedToReadUserFile)?;
376
377    let mut user: User = miniserde::json::from_str(core::str::from_utf8(&buffer).unwrap())
378        .map_err(Error::FailedToParseUserFile)?;
379
380    user.set_hash(hash);
381    user.set_salt(salt);
382
383    let user_json = miniserde::json::to_string(&user);
384
385    user_file
386        .write(user_json.as_bytes())
387        .await
388        .map_err(Error::FailedToWriteUserFile)?;
389
390    Ok(())
391}
392
393/// Changes a user's username by updating their user file.
394///
395/// This function reads the user's existing data, updates the name field,
396/// and writes the modified data back to the file system.
397///
398/// # Arguments
399///
400/// * `Virtual_file_system` - Reference to the virtual file system
401/// * `Current_name` - Current username of the account
402/// * `New_name` - New username to assign
403///
404/// # Returns
405///
406/// Returns `Ok(())` if the username was changed successfully,
407/// or an appropriate error if the operation fails.
408///
409/// # Errors
410///
411/// - File system errors (opening, reading, writing user file)
412/// - JSON parsing errors
413/// - Path construction failures
414pub async fn change_user_name<'a>(
415    virtual_file_system: &'a VirtualFileSystem<'a>,
416    current_name: &str,
417    new_name: &str,
418) -> Result<()> {
419    let file_path = get_user_file_path(current_name)?;
420
421    let user_file = File::open(
422        virtual_file_system,
423        file_path,
424        Flags::new(Mode::READ_WRITE, Some(Open::TRUNCATE), None),
425    )
426    .await
427    .map_err(Error::FailedToOpenUserFile)?;
428
429    let mut buffer = Vec::new();
430
431    user_file
432        .read_to_end(&mut buffer)
433        .await
434        .map_err(Error::FailedToReadUserFile)?;
435
436    let mut user: User = miniserde::json::from_str(core::str::from_utf8(&buffer).unwrap())
437        .map_err(Error::FailedToParseUserFile)?;
438
439    user.set_name(new_name.to_string());
440
441    let user_json = miniserde::json::to_string(&user);
442
443    user_file
444        .write(user_json.as_bytes())
445        .await
446        .map_err(Error::FailedToWriteUserFile)?;
447
448    Ok(())
449}
450
451/// Reads and parses a user file from the filesystem.
452///
453/// This function is used internally to load user data from JSON files.
454/// It reads the file contents into the provided buffer and deserializes
455/// the JSON data into a `User_type` structure.
456///
457/// # Arguments
458///
459/// * `Virtual_file_system` - Reference to the virtual file system
460/// * `Buffer` - Mutable buffer to use for reading file contents
461/// * `File` - Name of the user file to read
462///
463/// # Returns
464///
465/// Returns `Ok(User_type)` with the parsed user data,
466/// or an appropriate error if reading or parsing fails.
467///
468/// # Errors
469///
470/// - Path construction failures
471/// - File system errors (opening, reading)
472/// - JSON parsing errors
473pub async fn read_user_file<'a>(
474    virtual_file_system: &'a VirtualFileSystem<'a>,
475    buffer: &mut Vec<u8>,
476    file: &str,
477) -> Result<User> {
478    let user_file_path = get_user_file_path(file)?;
479
480    let user_file = File::open(virtual_file_system, user_file_path, Mode::READ_ONLY.into())
481        .await
482        .map_err(Error::FailedToReadUsersDirectory)?;
483
484    buffer.clear();
485
486    user_file
487        .read_to_end(buffer)
488        .await
489        .map_err(Error::FailedToReadUserFile)?;
490
491    miniserde::json::from_str(core::str::from_utf8(buffer).unwrap())
492        .map_err(Error::FailedToParseUserFile)
493}