Skip to main content

authentication/
error.rs

1//! Error handling for the Authentication module.
2//!
3//! This module defines all possible errors that can occur during authentication operations,
4//! including user and group management, file I/O, and password validation.
5
6use core::fmt::Display;
7use internationalization::translate;
8
9/// Result type alias for authentication operations.
10///
11/// This is a convenience type that wraps `Result<T, Error>` for all
12/// authentication-related operations.
13pub type Result<T> = core::result::Result<T, Error>;
14
15/// Comprehensive error enumeration for authentication operations.
16///
17/// This enum covers all possible error conditions that can occur during:
18/// - User authentication and management
19/// - Group management
20/// - File system operations
21/// - Password hashing and validation
22/// - Random salt generation
23#[derive(Debug, Clone)]
24pub enum Error {
25    /// Failed to get the current task identifier
26    FailedToGetCurrentTaskIdentifier(task::Error),
27    /// Failed to read the users directory from the filesystem
28    FailedToReadUsersDirectory(virtual_file_system::Error),
29    /// Failed to construct a valid user file path
30    FailedToGetUserFilePath,
31    /// Failed to open a user file for reading or writing
32    FailedToOpenUserFile(virtual_file_system::Error),
33    /// Failed to read the contents of a user file
34    FailedToReadUserFile(virtual_file_system::Error),
35    /// Failed to parse JSON content from a user file
36    FailedToParseUserFile(miniserde::Error),
37    /// Failed to add a user to the Users manager
38    FailedToAddUser(users::Error),
39    /// Failed to generate a new unique user identifier
40    FailedToGetNewUserIdentifier(users::Error),
41    /// Failed to create a new user account
42    FailedToCreateUser(users::Error),
43    /// Failed to write user data to a file
44    FailedToWriteUserFile(virtual_file_system::Error),
45    /// Failed to create the users directory
46    FailedToCreateUsersDirectory(virtual_file_system::Error),
47    /// Failed to read the groups directory from the filesystem
48    FailedToReadGroupDirectory(virtual_file_system::Error),
49    /// Failed to construct a valid group file path
50    FailedToGetGroupFilePath,
51    /// Failed to open a group file for reading or writing
52    FailedToOpenGroupFile(virtual_file_system::Error),
53    /// Failed to read the contents of a group file
54    FailedToReadGroupFile(virtual_file_system::Error),
55    /// Failed to parse JSON content from a group file
56    FailedToParseGroupFile(miniserde::Error),
57    /// Failed to add a group to the Users manager
58    FailedToAddGroup(users::Error),
59    /// Failed to generate a new unique group identifier
60    FailedToGetNewGroupIdentifier(users::Error),
61    /// Failed to create a new group
62    FailedToCreateGroup(users::Error),
63    /// Failed to write group data to a file
64    FailedToWriteGroupFile(virtual_file_system::Error),
65    /// Failed to create the groups directory
66    FailedToCreateGroupsDirectory(virtual_file_system::Error),
67    /// The provided password is invalid or incorrect
68    InvalidPassword,
69    /// Failed to open the random device for salt generation
70    FailedToOpenRandomDevice(virtual_file_system::Error),
71    /// Failed to read random data from the random device
72    FailedToReadRandomDevice(virtual_file_system::Error),
73    /// Failed to get user identifier from the Users manager
74    FailedToGetUserIdentifier(users::Error),
75    /// Failed to close a file
76    FailedToCloseFile(virtual_file_system::Error),
77    /// Failed to hash
78    FailedToHashPassword(virtual_file_system::Error),
79}
80
81impl Display for Error {
82    fn fmt(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
83        match self {
84            Self::FailedToGetCurrentTaskIdentifier(error) => {
85                write!(
86                    formatter,
87                    translate!("Failed to get current task identifier: {}"),
88                    error
89                )
90            }
91            Self::FailedToReadUsersDirectory(error) => {
92                write!(
93                    formatter,
94                    translate!("Failed to read users directory: {}"),
95                    error
96                )
97            }
98            Self::FailedToGetUserFilePath => {
99                write!(formatter, translate!("Failed to get user file path"))
100            }
101            Self::FailedToReadUserFile(error) => {
102                write!(formatter, translate!("Failed to read user file: {}"), error)
103            }
104            Self::FailedToOpenUserFile(error) => {
105                write!(formatter, translate!("Failed to open user file: {}"), error)
106            }
107            Self::FailedToParseUserFile(error) => {
108                write!(
109                    formatter,
110                    translate!("Failed to parse user file: {}"),
111                    error
112                )
113            }
114            Self::FailedToAddUser(error) => {
115                write!(formatter, translate!("Failed to add user: {}"), error)
116            }
117            Self::FailedToCreateUsersDirectory(error) => {
118                write!(
119                    formatter,
120                    translate!("Failed to create users directory: {}"),
121                    error
122                )
123            }
124            Self::FailedToReadGroupDirectory(error) => {
125                write!(
126                    formatter,
127                    translate!("Failed to read group directory: {}"),
128                    error
129                )
130            }
131            Self::FailedToGetGroupFilePath => {
132                write!(formatter, translate!("Failed to get group file path"))
133            }
134            Self::FailedToOpenGroupFile(error) => {
135                write!(
136                    formatter,
137                    translate!("Failed to open group file: {}"),
138                    error
139                )
140            }
141            Self::FailedToReadGroupFile(error) => {
142                write!(
143                    formatter,
144                    translate!("Failed to read group file: {}"),
145                    error
146                )
147            }
148            Self::FailedToParseGroupFile(error) => {
149                write!(
150                    formatter,
151                    translate!("Failed to parse group file: {}"),
152                    error
153                )
154            }
155            Self::FailedToAddGroup(error) => {
156                write!(formatter, translate!("Failed to add group: {}"), error)
157            }
158            Self::FailedToCreateGroupsDirectory(error) => {
159                write!(
160                    formatter,
161                    translate!("Failed to create group directory: {}"),
162                    error
163                )
164            }
165            Self::InvalidPassword => {
166                write!(formatter, translate!("Invalid password"))
167            }
168            Self::FailedToOpenRandomDevice(error) => {
169                write!(
170                    formatter,
171                    translate!("Failed to open random device: {}"),
172                    error
173                )
174            }
175            Self::FailedToReadRandomDevice(error) => {
176                write!(
177                    formatter,
178                    translate!("Failed to read random device: {}"),
179                    error
180                )
181            }
182            Self::FailedToCreateUser(error) => {
183                write!(formatter, translate!("Failed to create user: {}"), error)
184            }
185            Self::FailedToGetNewUserIdentifier(error) => {
186                write!(
187                    formatter,
188                    translate!("Failed to get new user identifier: {}"),
189                    error
190                )
191            }
192            Self::FailedToWriteUserFile(error) => {
193                write!(
194                    formatter,
195                    translate!("Failed to write user file: {}"),
196                    error
197                )
198            }
199            Self::FailedToGetNewGroupIdentifier(error) => {
200                write!(
201                    formatter,
202                    translate!("Failed to get new groupe identifier: {}"),
203                    error
204                )
205            }
206            Self::FailedToCreateGroup(error) => {
207                write!(formatter, translate!("Failed to create group: {}"), error)
208            }
209            Self::FailedToWriteGroupFile(error) => {
210                write!(
211                    formatter,
212                    translate!("Failed to write group file: {}"),
213                    error
214                )
215            }
216            Self::FailedToGetUserIdentifier(error) => {
217                write!(
218                    formatter,
219                    translate!("Failed to get user identifier: {}"),
220                    error
221                )
222            }
223            Self::FailedToCloseFile(error) => {
224                write!(formatter, translate!("Failed to close file: {}"), error)
225            }
226            Self::FailedToHashPassword(error) => {
227                write!(formatter, translate!("Failed to hash password: {}"), error)
228            }
229        }
230    }
231}
232
233impl From<task::Error> for Error {
234    fn from(error: task::Error) -> Self {
235        Self::FailedToGetCurrentTaskIdentifier(error)
236    }
237}