Skip to content

Commit cd542dc

Browse files
committed
refactor: [torrust#1195] extract authentication::Service
1 parent a93a79c commit cd542dc

File tree

2 files changed

+95
-44
lines changed

2 files changed

+95
-44
lines changed

src/core/authentication/mod.rs

+25-44
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::panic::Location;
21
use std::sync::Arc;
32
use std::time::Duration;
43

@@ -14,6 +13,7 @@ use super::error::PeerKeyError;
1413
use crate::CurrentClock;
1514

1615
pub mod key;
16+
pub mod service;
1717

1818
pub type PeerKey = key::PeerKey;
1919
pub type Key = key::Key;
@@ -34,23 +34,25 @@ pub struct AddKeyRequest {
3434
}
3535

3636
pub struct Facade {
37-
/// The tracker configuration.
38-
config: Core,
39-
4037
/// The database repository for the authentication keys.
4138
db_key_repository: DatabaseKeyRepository,
4239

4340
/// In-memory implementation of the authentication key repository.
44-
in_memory_key_repository: InMemoryKeyRepository,
41+
in_memory_key_repository: Arc<InMemoryKeyRepository>,
42+
43+
/// The authentication service.
44+
authentication_service: service::Service,
4545
}
4646

4747
impl Facade {
4848
#[must_use]
4949
pub fn new(config: &Core, database: &Arc<Box<dyn Database>>) -> Self {
50+
let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default());
51+
5052
Self {
51-
config: config.clone(),
5253
db_key_repository: DatabaseKeyRepository::new(database),
53-
in_memory_key_repository: InMemoryKeyRepository::default(),
54+
in_memory_key_repository: in_memory_key_repository.clone(),
55+
authentication_service: service::Service::new(config, &in_memory_key_repository),
5456
}
5557
}
5658

@@ -61,40 +63,7 @@ impl Facade {
6163
///
6264
/// Will return an error if the the authentication key cannot be verified.
6365
pub async fn authenticate(&self, key: &Key) -> Result<(), Error> {
64-
if self.is_private() {
65-
self.verify_auth_key(key).await
66-
} else {
67-
Ok(())
68-
}
69-
}
70-
71-
/// Returns `true` is the tracker is in private mode.
72-
pub fn is_private(&self) -> bool {
73-
self.config.private
74-
}
75-
76-
/// It verifies an authentication key.
77-
///
78-
/// # Errors
79-
///
80-
/// Will return a `key::Error` if unable to get any `auth_key`.
81-
pub async fn verify_auth_key(&self, key: &Key) -> Result<(), Error> {
82-
match self.in_memory_key_repository.get(key).await {
83-
None => Err(Error::UnableToReadKey {
84-
location: Location::caller(),
85-
key: Box::new(key.clone()),
86-
}),
87-
Some(key) => match self.config.private_mode {
88-
Some(private_mode) => {
89-
if private_mode.check_keys_expiration {
90-
return key::verify_key_expiration(&key);
91-
}
92-
93-
Ok(())
94-
}
95-
None => key::verify_key_expiration(&key),
96-
},
97-
}
66+
self.authentication_service.authenticate(key).await
9867
}
9968

10069
/// Adds new peer keys to the tracker.
@@ -340,7 +309,11 @@ mod tests {
340309

341310
let unregistered_key = authentication::Key::from_str("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap();
342311

343-
assert!(authentication.verify_auth_key(&unregistered_key).await.is_err());
312+
assert!(authentication
313+
.authentication_service
314+
.verify_auth_key(&unregistered_key)
315+
.await
316+
.is_err());
344317
}
345318

346319
#[tokio::test]
@@ -355,7 +328,11 @@ mod tests {
355328
let result = authentication.remove_auth_key(&expiring_key.key()).await;
356329

357330
assert!(result.is_ok());
358-
assert!(authentication.verify_auth_key(&expiring_key.key()).await.is_err());
331+
assert!(authentication
332+
.authentication_service
333+
.verify_auth_key(&expiring_key.key())
334+
.await
335+
.is_err());
359336
}
360337

361338
#[tokio::test]
@@ -373,7 +350,11 @@ mod tests {
373350
let result = authentication.load_keys_from_database().await;
374351

375352
assert!(result.is_ok());
376-
assert!(authentication.verify_auth_key(&expiring_key.key()).await.is_ok());
353+
assert!(authentication
354+
.authentication_service
355+
.verify_auth_key(&expiring_key.key())
356+
.await
357+
.is_ok());
377358
}
378359

379360
mod with_expiring_and {

src/core/authentication/service.rs

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
use std::panic::Location;
2+
use std::sync::Arc;
3+
4+
use torrust_tracker_configuration::Core;
5+
6+
use super::key::repository::in_memory::InMemoryKeyRepository;
7+
use super::{key, Error, Key};
8+
9+
#[derive(Debug)]
10+
pub struct Service {
11+
/// The tracker configuration.
12+
config: Core,
13+
14+
/// In-memory implementation of the authentication key repository.
15+
in_memory_key_repository: Arc<InMemoryKeyRepository>,
16+
}
17+
18+
impl Service {
19+
#[must_use]
20+
pub fn new(config: &Core, in_memory_key_repository: &Arc<InMemoryKeyRepository>) -> Self {
21+
Self {
22+
config: config.clone(),
23+
in_memory_key_repository: in_memory_key_repository.clone(),
24+
}
25+
}
26+
27+
/// It authenticates the peer `key` against the `Tracker` authentication
28+
/// key list.
29+
///
30+
/// # Errors
31+
///
32+
/// Will return an error if the the authentication key cannot be verified.
33+
pub async fn authenticate(&self, key: &Key) -> Result<(), Error> {
34+
if self.is_private() {
35+
self.verify_auth_key(key).await
36+
} else {
37+
Ok(())
38+
}
39+
}
40+
41+
/// Returns `true` is the tracker is in private mode.
42+
#[must_use]
43+
pub fn is_private(&self) -> bool {
44+
self.config.private
45+
}
46+
47+
/// It verifies an authentication key.
48+
///
49+
/// # Errors
50+
///
51+
/// Will return a `key::Error` if unable to get any `auth_key`.
52+
pub async fn verify_auth_key(&self, key: &Key) -> Result<(), Error> {
53+
match self.in_memory_key_repository.get(key).await {
54+
None => Err(Error::UnableToReadKey {
55+
location: Location::caller(),
56+
key: Box::new(key.clone()),
57+
}),
58+
Some(key) => match self.config.private_mode {
59+
Some(private_mode) => {
60+
if private_mode.check_keys_expiration {
61+
return key::verify_key_expiration(&key);
62+
}
63+
64+
Ok(())
65+
}
66+
None => key::verify_key_expiration(&key),
67+
},
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)