Skip to content

Commit 757126d

Browse files
committed
Merge #1192: Overhaul core Tracker: extract authentication
6584fe4 refactor: [#1191] remove tracker dependency for authentication tests (Jose Celano) f41a524 refactor: [#1191] remove duplicate tests for private tracker (Jose Celano) 9a60c0c refactor: [#11191] copy private tracker tests to authentication module (Jose Celano) a093680 refactor: [#1191] remove authentication wrapper methods from core tracker (Jose Celano) 39c2a8f refactor: [#1191] replace authentication methods with extracted service in the core tracker (Jose Celano) 986a2f6 fix: [#1191] format (Jose Celano) e75728a refactor: [#1191] extract core::authentication::Facade type (Jose Celano) 2b7373a refactor: [#1191] rename mod core::auth to core::authentication (Jose Celano) f216b05 refactor: [#1191] extract mod auth::key (Jose Celano) 88560ce refactor: [#1191] create dir for mod (Jose Celano) Pull request description: Overhaul core Tracker: extract authentication. ACKs for top commit: josecelano: ACK 6584fe4 Tree-SHA512: 22665b50de218932cc516942c12fadd23f30176aa9b4322206d8ae75ce619231b31acb15dfa2841a1b188437d0fe737fac8393a3a747e1c5504681636d058677
2 parents e630e5f + 6584fe4 commit 757126d

File tree

36 files changed

+872
-688
lines changed

36 files changed

+872
-688
lines changed

src/app.rs

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ pub async fn start(config: &Configuration, app_container: &AppContainer) -> Vec<
5353
if app_container.tracker.is_private() {
5454
app_container
5555
.tracker
56+
.authentication
5657
.load_keys_from_database()
5758
.await
5859
.expect("Could not retrieve keys from database.");

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/auth.rs src/core/authentication/key.rs

+19-17
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,30 @@
1212
//! Keys are stored in this struct:
1313
//!
1414
//! ```rust,no_run
15-
//! use torrust_tracker_lib::core::auth::Key;
15+
//! use torrust_tracker_lib::core::authentication::Key;
1616
//! use torrust_tracker_primitives::DurationSinceUnixEpoch;
1717
//!
18-
//! pub struct ExpiringKey {
18+
//! pub struct PeerKey {
1919
//! /// Random 32-char string. For example: `YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ`
2020
//! pub key: Key,
21-
//! /// Timestamp, the key will be no longer valid after this timestamp
21+
//!
22+
//! /// Timestamp, the key will be no longer valid after this timestamp.
23+
//! /// If `None` the keys will not expire (permanent key).
2224
//! pub valid_until: Option<DurationSinceUnixEpoch>,
2325
//! }
2426
//! ```
2527
//!
2628
//! You can generate a new key valid for `9999` seconds and `0` nanoseconds from the current time with the following:
2729
//!
2830
//! ```rust,no_run
29-
//! use torrust_tracker_lib::core::auth;
31+
//! use torrust_tracker_lib::core::authentication;
3032
//! use std::time::Duration;
3133
//!
32-
//! let expiring_key = auth::generate_key(Some(Duration::new(9999, 0)));
34+
//! let expiring_key = authentication::key::generate_key(Some(Duration::new(9999, 0)));
3335
//!
3436
//! // And you can later verify it with:
3537
//!
36-
//! assert!(auth::verify_key_expiration(&expiring_key).is_ok());
38+
//! assert!(authentication::key::verify_key_expiration(&expiring_key).is_ok());
3739
//! ```
3840
3941
use std::panic::Location;
@@ -197,7 +199,7 @@ impl Key {
197199
/// Error returned when a key cannot be parsed from a string.
198200
///
199201
/// ```text
200-
/// use torrust_tracker_lib::core::auth::Key;
202+
/// use torrust_tracker_lib::core::authentication::Key;
201203
/// use std::str::FromStr;
202204
///
203205
/// let key_string = "YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ";
@@ -227,7 +229,7 @@ impl FromStr for Key {
227229
}
228230

229231
/// Verification error. Error returned when an [`PeerKey`] cannot be
230-
/// verified with the (`crate::core::auth::verify_key`) function.
232+
/// verified with the (`crate::core::authentication::verify_key`) function.
231233
#[derive(Debug, Error)]
232234
#[allow(dead_code)]
233235
pub enum Error {
@@ -258,7 +260,7 @@ mod tests {
258260
mod key {
259261
use std::str::FromStr;
260262

261-
use crate::core::auth::Key;
263+
use crate::core::authentication::Key;
262264

263265
#[test]
264266
fn should_be_parsed_from_an_string() {
@@ -293,12 +295,12 @@ mod tests {
293295
use torrust_tracker_clock::clock;
294296
use torrust_tracker_clock::clock::stopped::Stopped as _;
295297

296-
use crate::core::auth;
298+
use crate::core::authentication;
297299

298300
#[test]
299301
fn should_be_parsed_from_an_string() {
300302
let key_string = "YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ";
301-
let auth_key = auth::Key::from_str(key_string);
303+
let auth_key = authentication::Key::from_str(key_string);
302304

303305
assert!(auth_key.is_ok());
304306
assert_eq!(auth_key.unwrap().to_string(), key_string);
@@ -309,7 +311,7 @@ mod tests {
309311
// Set the time to the current time.
310312
clock::Stopped::local_set_to_unix_epoch();
311313

312-
let expiring_key = auth::generate_key(Some(Duration::from_secs(0)));
314+
let expiring_key = authentication::key::generate_key(Some(Duration::from_secs(0)));
313315

314316
assert_eq!(
315317
expiring_key.to_string(),
@@ -319,9 +321,9 @@ mod tests {
319321

320322
#[test]
321323
fn should_be_generated_with_a_expiration_time() {
322-
let expiring_key = auth::generate_key(Some(Duration::new(9999, 0)));
324+
let expiring_key = authentication::key::generate_key(Some(Duration::new(9999, 0)));
323325

324-
assert!(auth::verify_key_expiration(&expiring_key).is_ok());
326+
assert!(authentication::key::verify_key_expiration(&expiring_key).is_ok());
325327
}
326328

327329
#[test]
@@ -330,17 +332,17 @@ mod tests {
330332
clock::Stopped::local_set_to_system_time_now();
331333

332334
// Make key that is valid for 19 seconds.
333-
let expiring_key = auth::generate_key(Some(Duration::from_secs(19)));
335+
let expiring_key = authentication::key::generate_key(Some(Duration::from_secs(19)));
334336

335337
// Mock the time has passed 10 sec.
336338
clock::Stopped::local_add(&Duration::from_secs(10)).unwrap();
337339

338-
assert!(auth::verify_key_expiration(&expiring_key).is_ok());
340+
assert!(authentication::key::verify_key_expiration(&expiring_key).is_ok());
339341

340342
// Mock the time has passed another 10 sec.
341343
clock::Stopped::local_add(&Duration::from_secs(10)).unwrap();
342344

343-
assert!(auth::verify_key_expiration(&expiring_key).is_err());
345+
assert!(authentication::key::verify_key_expiration(&expiring_key).is_err());
344346
}
345347
}
346348
}

0 commit comments

Comments
 (0)