Skip to content

Commit 27c1cab

Browse files
committed
Improve type safety and memory usage for ServerHandler
Closes #8
1 parent 1253b3e commit 27c1cab

File tree

2 files changed

+201
-137
lines changed

2 files changed

+201
-137
lines changed

src/fingerprints.rs

+20-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{
55
time::Duration,
66
};
77

8-
use crate::{directory::watch_directory, ssh::Authentication};
8+
use crate::directory::watch_directory;
99
use log::{error, warn};
1010
use notify::RecommendedWatcher;
1111
use ssh_key::{HashAlg, PublicKey};
@@ -15,6 +15,16 @@ use tokio::{
1515
task::JoinHandle,
1616
};
1717

18+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
19+
pub(crate) enum AuthenticationType {
20+
/// Not authenticated.
21+
None,
22+
/// Authenticated as a valid user.
23+
User,
24+
/// Authenticated as an admin.
25+
Admin,
26+
}
27+
1828
#[derive(Debug)]
1929
pub(crate) struct FingerprintsValidator {
2030
user_fingerprints: Arc<RwLock<HashSet<String>>>,
@@ -146,18 +156,18 @@ impl FingerprintsValidator {
146156
})
147157
}
148158

149-
pub(crate) fn authenticate_fingerprint(&self, fingerprint: &str) -> Authentication {
159+
pub(crate) fn authenticate_fingerprint(&self, fingerprint: &str) -> AuthenticationType {
150160
if self
151161
.admin_fingerprints
152162
.read()
153163
.unwrap()
154164
.contains(fingerprint)
155165
{
156-
Authentication::Admin
166+
AuthenticationType::Admin
157167
} else if self.user_fingerprints.read().unwrap().contains(fingerprint) {
158-
Authentication::User
168+
AuthenticationType::User
159169
} else {
160-
Authentication::None
170+
AuthenticationType::None
161171
}
162172
}
163173
}
@@ -172,9 +182,7 @@ impl Drop for FingerprintsValidator {
172182
mod fingerprints_validator_tests {
173183
use std::sync::LazyLock;
174184

175-
use crate::ssh::Authentication;
176-
177-
use super::FingerprintsValidator;
185+
use super::{AuthenticationType, FingerprintsValidator};
178186
use russh_keys::{key::PublicKey, parse_public_key_base64};
179187

180188
static USER_KEYS_DIRECTORY: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/data/user_keys");
@@ -214,19 +222,19 @@ mod fingerprints_validator_tests {
214222
.unwrap();
215223
assert_eq!(
216224
validator.authenticate_fingerprint(&ADMIN_KEY.fingerprint()),
217-
Authentication::Admin
225+
AuthenticationType::Admin
218226
);
219227
assert_eq!(
220228
validator.authenticate_fingerprint(&KEY_ONE.fingerprint()),
221-
Authentication::User
229+
AuthenticationType::User
222230
);
223231
assert_eq!(
224232
validator.authenticate_fingerprint(&KEY_TWO.fingerprint()),
225-
Authentication::User
233+
AuthenticationType::User
226234
);
227235
assert_eq!(
228236
validator.authenticate_fingerprint(&UNKNOWN_KEY.fingerprint()),
229-
Authentication::None
237+
AuthenticationType::None
230238
);
231239
}
232240
}

0 commit comments

Comments
 (0)