Skip to content

Commit 39c2a8f

Browse files
committed
refactor: [#1191] replace authentication methods with extracted service in the core tracker
1 parent 986a2f6 commit 39c2a8f

File tree

23 files changed

+243
-252
lines changed

23 files changed

+243
-252
lines changed

src/app_test.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use torrust_tracker_configuration::Configuration;
55

66
use crate::core::databases::Database;
77
use crate::core::services::initialize_database;
8-
use crate::core::whitelist;
98
use crate::core::whitelist::repository::in_memory::InMemoryWhitelist;
9+
use crate::core::{authentication, whitelist};
1010

1111
/// Initialize the tracker dependencies.
1212
#[allow(clippy::type_complexity)]
@@ -17,13 +17,15 @@ pub fn initialize_tracker_dependencies(
1717
Arc<Box<dyn Database>>,
1818
Arc<InMemoryWhitelist>,
1919
Arc<whitelist::authorization::Authorization>,
20+
Arc<authentication::Facade>,
2021
) {
2122
let database = initialize_database(config);
2223
let in_memory_whitelist = Arc::new(InMemoryWhitelist::default());
2324
let whitelist_authorization = Arc::new(whitelist::authorization::Authorization::new(
2425
&config.core,
2526
&in_memory_whitelist.clone(),
2627
));
28+
let authentication = Arc::new(authentication::Facade::new(&config.core, &database.clone()));
2729

28-
(database, in_memory_whitelist, whitelist_authorization)
30+
(database, in_memory_whitelist, whitelist_authorization, authentication)
2931
}

src/bootstrap/app.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ use super::config::initialize_configuration;
2323
use crate::bootstrap;
2424
use crate::container::AppContainer;
2525
use crate::core::services::{initialize_database, initialize_tracker, initialize_whitelist_manager, statistics};
26-
use crate::core::whitelist;
2726
use crate::core::whitelist::repository::in_memory::InMemoryWhitelist;
27+
use crate::core::{authentication, whitelist};
2828
use crate::servers::udp::server::banning::BanService;
2929
use crate::servers::udp::server::launcher::MAX_CONNECTION_ID_ERRORS_PER_IP;
3030
use crate::shared::crypto::ephemeral_instance_keys;
@@ -89,8 +89,14 @@ pub fn initialize_app_container(configuration: &Configuration) -> AppContainer {
8989
&in_memory_whitelist.clone(),
9090
));
9191
let whitelist_manager = initialize_whitelist_manager(database.clone(), in_memory_whitelist.clone());
92+
let authentication = Arc::new(authentication::Facade::new(&configuration.core, &database.clone()));
9293

93-
let tracker = Arc::new(initialize_tracker(configuration, &database, &whitelist_authorization));
94+
let tracker = Arc::new(initialize_tracker(
95+
configuration,
96+
&database,
97+
&whitelist_authorization,
98+
&authentication,
99+
));
94100

95101
AppContainer {
96102
tracker,
@@ -99,6 +105,7 @@ pub fn initialize_app_container(configuration: &Configuration) -> AppContainer {
99105
stats_event_sender,
100106
stats_repository,
101107
whitelist_manager,
108+
authentication,
102109
}
103110
}
104111

src/bootstrap/jobs/http_tracker.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ mod tests {
101101
use crate::bootstrap::app::initialize_global_services;
102102
use crate::bootstrap::jobs::http_tracker::start_job;
103103
use crate::core::services::{initialize_database, initialize_tracker, statistics};
104-
use crate::core::whitelist;
105104
use crate::core::whitelist::repository::in_memory::InMemoryWhitelist;
105+
use crate::core::{authentication, whitelist};
106106
use crate::servers::http::Version;
107107
use crate::servers::registar::Registar;
108108

@@ -123,7 +123,9 @@ mod tests {
123123
&cfg.core,
124124
&in_memory_whitelist.clone(),
125125
));
126-
let tracker = Arc::new(initialize_tracker(&cfg, &database, &whitelist_authorization));
126+
let authentication = Arc::new(authentication::Facade::new(&cfg.core, &database.clone()));
127+
128+
let tracker = Arc::new(initialize_tracker(&cfg, &database, &whitelist_authorization, &authentication));
127129

128130
let version = Version::V1;
129131

src/bootstrap/jobs/tracker_apis.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ mod tests {
150150
use crate::bootstrap::app::initialize_global_services;
151151
use crate::bootstrap::jobs::tracker_apis::start_job;
152152
use crate::core::services::{initialize_database, initialize_tracker, initialize_whitelist_manager, statistics};
153-
use crate::core::whitelist;
154153
use crate::core::whitelist::repository::in_memory::InMemoryWhitelist;
154+
use crate::core::{authentication, whitelist};
155155
use crate::servers::apis::Version;
156156
use crate::servers::registar::Registar;
157157
use crate::servers::udp::server::banning::BanService;
@@ -176,8 +176,9 @@ mod tests {
176176
&in_memory_whitelist.clone(),
177177
));
178178
let whitelist_manager = initialize_whitelist_manager(database.clone(), in_memory_whitelist.clone());
179+
let authentication = Arc::new(authentication::Facade::new(&cfg.core, &database.clone()));
179180

180-
let tracker = Arc::new(initialize_tracker(&cfg, &database, &whitelist_authorization));
181+
let tracker = Arc::new(initialize_tracker(&cfg, &database, &whitelist_authorization, &authentication));
181182

182183
let version = Version::V1;
183184

src/container.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use tokio::sync::RwLock;
55
use crate::core::statistics::event::sender::Sender;
66
use crate::core::statistics::repository::Repository;
77
use crate::core::whitelist::manager::WhiteListManager;
8-
use crate::core::{whitelist, Tracker};
8+
use crate::core::{authentication, whitelist, Tracker};
99
use crate::servers::udp::server::banning::BanService;
1010

1111
pub struct AppContainer {
@@ -15,4 +15,5 @@ pub struct AppContainer {
1515
pub stats_event_sender: Arc<Option<Box<dyn Sender>>>,
1616
pub stats_repository: Arc<Repository>,
1717
pub whitelist_manager: Arc<WhiteListManager>,
18+
pub authentication: Arc<authentication::Facade>,
1819
}

src/core/authentication/mod.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl Facade {
8181
/// # Errors
8282
///
8383
/// Will return a `key::Error` if unable to get any `auth_key`.
84-
async fn verify_auth_key(&self, key: &Key) -> Result<(), Error> {
84+
pub async fn verify_auth_key(&self, key: &Key) -> Result<(), Error> {
8585
match self.keys.read().await.get(key) {
8686
None => Err(Error::UnableToReadKey {
8787
location: Location::caller(),
@@ -268,10 +268,17 @@ impl Facade {
268268
/// Will return a `database::Error` if unable to remove the `key` to the database.
269269
pub async fn remove_auth_key(&self, key: &Key) -> Result<(), databases::error::Error> {
270270
self.database.remove_key_from_keys(key)?;
271-
self.keys.write().await.remove(key);
271+
self.remove_in_memory_auth_key(key).await;
272272
Ok(())
273273
}
274274

275+
/// It removes an authentication key from memory.
276+
///
277+
/// # Context: Authentication
278+
pub async fn remove_in_memory_auth_key(&self, key: &Key) {
279+
self.keys.write().await.remove(key);
280+
}
281+
275282
/// The `Tracker` stores the authentication keys in memory and in the database.
276283
/// In case you need to restart the `Tracker` you can load the keys from the database
277284
/// into memory with this function. Keys are automatically stored in the database when they

0 commit comments

Comments
 (0)