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 embedded_i18n::t;
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::IoErrorKind),
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::IoErrorKind),
33    /// Failed to read the contents of a user file
34    FailedToReadUserFile(virtual_file_system::IoErrorKind),
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::IoErrorKind),
45    /// Failed to create the users directory
46    FailedToCreateUsersDirectory(virtual_file_system::IoErrorKind),
47    /// Failed to read the groups directory from the filesystem
48    FailedToReadGroupDirectory(virtual_file_system::IoErrorKind),
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::IoErrorKind),
53    /// Failed to read the contents of a group file
54    FailedToReadGroupFile(virtual_file_system::IoErrorKind),
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::IoErrorKind),
65    /// Failed to create the groups directory
66    FailedToCreateGroupsDirectory(virtual_file_system::IoErrorKind),
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::IoErrorKind),
71    /// Failed to read random data from the random device
72    FailedToReadRandomDevice(virtual_file_system::IoErrorKind),
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::IoErrorKind),
77    /// Failed to hash
78    FailedToHashPassword(virtual_file_system::IoErrorKind),
79    /// The user name is not accepted for a new or renamed account
80    InvalidUserName,
81    /// An account record has no valid recorded home
82    InvalidUserHome,
83    /// An account record is not valid UTF-8 text
84    InvalidUserRecord,
85    /// An account record uses a format version this firmware does not know
86    UnsupportedUserRecordVersion,
87    /// Another account already uses the home a new account would get
88    UserHomeAlreadyUsed,
89    /// Failed to rename a user in the Users manager
90    FailedToRenameUser(users::Error),
91}
92
93impl Display for Error {
94    fn fmt(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
95        match self {
96            Self::FailedToGetCurrentTaskIdentifier(error) => {
97                write!(
98                    formatter,
99                    "{}",
100                    t!("errors.failed_to_get_current_task_identifier", arg0 = error)
101                )
102            }
103            Self::FailedToReadUsersDirectory(error) => {
104                write!(
105                    formatter,
106                    "{}",
107                    t!("errors.failed_to_read_users_directory", arg0 = error)
108                )
109            }
110            Self::FailedToGetUserFilePath => {
111                write!(formatter, "{}", t!("errors.failed_to_get_user_file_path"))
112            }
113            Self::FailedToReadUserFile(error) => {
114                write!(
115                    formatter,
116                    "{}",
117                    t!("errors.failed_to_read_user_file", arg0 = error)
118                )
119            }
120            Self::FailedToOpenUserFile(error) => {
121                write!(
122                    formatter,
123                    "{}",
124                    t!("errors.failed_to_open_user_file", arg0 = error)
125                )
126            }
127            Self::FailedToParseUserFile(error) => {
128                write!(
129                    formatter,
130                    "{}",
131                    t!("errors.failed_to_parse_user_file", arg0 = error)
132                )
133            }
134            Self::FailedToAddUser(error) => {
135                write!(
136                    formatter,
137                    "{}",
138                    t!("errors.failed_to_add_user", arg0 = error)
139                )
140            }
141            Self::FailedToCreateUsersDirectory(error) => {
142                write!(
143                    formatter,
144                    "{}",
145                    t!("errors.failed_to_create_users_directory", arg0 = error)
146                )
147            }
148            Self::FailedToReadGroupDirectory(error) => {
149                write!(
150                    formatter,
151                    "{}",
152                    t!("errors.failed_to_read_group_directory", arg0 = error)
153                )
154            }
155            Self::FailedToGetGroupFilePath => {
156                write!(formatter, "{}", t!("errors.failed_to_get_group_file_path"))
157            }
158            Self::FailedToOpenGroupFile(error) => {
159                write!(
160                    formatter,
161                    "{}",
162                    t!("errors.failed_to_open_group_file", arg0 = error)
163                )
164            }
165            Self::FailedToReadGroupFile(error) => {
166                write!(
167                    formatter,
168                    "{}",
169                    t!("errors.failed_to_read_group_file", arg0 = error)
170                )
171            }
172            Self::FailedToParseGroupFile(error) => {
173                write!(
174                    formatter,
175                    "{}",
176                    t!("errors.failed_to_parse_group_file", arg0 = error)
177                )
178            }
179            Self::FailedToAddGroup(error) => {
180                write!(
181                    formatter,
182                    "{}",
183                    t!("errors.failed_to_add_group", arg0 = error)
184                )
185            }
186            Self::FailedToCreateGroupsDirectory(error) => {
187                write!(
188                    formatter,
189                    "{}",
190                    t!("errors.failed_to_create_group_directory", arg0 = error)
191                )
192            }
193            Self::InvalidPassword => {
194                write!(formatter, "{}", t!("errors.invalid_password"))
195            }
196            Self::FailedToOpenRandomDevice(error) => {
197                write!(
198                    formatter,
199                    "{}",
200                    t!("errors.failed_to_open_random_device", arg0 = error)
201                )
202            }
203            Self::FailedToReadRandomDevice(error) => {
204                write!(
205                    formatter,
206                    "{}",
207                    t!("errors.failed_to_read_random_device", arg0 = error)
208                )
209            }
210            Self::FailedToCreateUser(error) => {
211                write!(
212                    formatter,
213                    "{}",
214                    t!("errors.failed_to_create_user", arg0 = error)
215                )
216            }
217            Self::FailedToGetNewUserIdentifier(error) => {
218                write!(
219                    formatter,
220                    "{}",
221                    t!("errors.failed_to_get_new_user_identifier", arg0 = error)
222                )
223            }
224            Self::FailedToWriteUserFile(error) => {
225                write!(
226                    formatter,
227                    "{}",
228                    t!("errors.failed_to_write_user_file", arg0 = error)
229                )
230            }
231            Self::FailedToGetNewGroupIdentifier(error) => {
232                write!(
233                    formatter,
234                    "{}",
235                    t!("errors.failed_to_get_new_groupe_identifier", arg0 = error)
236                )
237            }
238            Self::FailedToCreateGroup(error) => {
239                write!(
240                    formatter,
241                    "{}",
242                    t!("errors.failed_to_create_group", arg0 = error)
243                )
244            }
245            Self::FailedToWriteGroupFile(error) => {
246                write!(
247                    formatter,
248                    "{}",
249                    t!("errors.failed_to_write_group_file", arg0 = error)
250                )
251            }
252            Self::FailedToGetUserIdentifier(error) => {
253                write!(
254                    formatter,
255                    "{}",
256                    t!("errors.failed_to_get_user_identifier", arg0 = error)
257                )
258            }
259            Self::FailedToCloseFile(error) => {
260                write!(
261                    formatter,
262                    "{}",
263                    t!("errors.failed_to_close_file", arg0 = error)
264                )
265            }
266            Self::FailedToHashPassword(error) => {
267                write!(
268                    formatter,
269                    "{}",
270                    t!("errors.failed_to_hash_password", arg0 = error)
271                )
272            }
273            Self::InvalidUserName => {
274                write!(formatter, "{}", t!("errors.invalid_user_name"))
275            }
276            Self::InvalidUserHome => {
277                write!(formatter, "{}", t!("errors.invalid_user_home"))
278            }
279            Self::InvalidUserRecord => {
280                write!(formatter, "{}", t!("errors.invalid_user_record"))
281            }
282            Self::UnsupportedUserRecordVersion => {
283                write!(
284                    formatter,
285                    "{}",
286                    t!("errors.unsupported_user_record_version")
287                )
288            }
289            Self::UserHomeAlreadyUsed => {
290                write!(formatter, "{}", t!("errors.user_home_already_used"))
291            }
292            Self::FailedToRenameUser(error) => {
293                write!(
294                    formatter,
295                    "{}",
296                    t!("errors.failed_to_rename_user", arg0 = error)
297                )
298            }
299        }
300    }
301}
302
303impl From<task::Error> for Error {
304    fn from(error: task::Error) -> Self {
305        Self::FailedToGetCurrentTaskIdentifier(error)
306    }
307}