Skip to content

Commit a093680

Browse files
committed
refactor: [#1191] remove authentication wrapper methods from core tracker
1 parent 39c2a8f commit a093680

File tree

7 files changed

+72
-157
lines changed

7 files changed

+72
-157
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/core/mod.rs

+39-150
Original file line numberDiff line numberDiff line change
@@ -454,19 +454,16 @@ use std::net::IpAddr;
454454
use std::sync::Arc;
455455
use std::time::Duration;
456456

457-
use authentication::AddKeyRequest;
458457
use bittorrent_primitives::info_hash::InfoHash;
459-
use error::PeerKeyError;
460458
use torrust_tracker_clock::clock::Time;
461459
use torrust_tracker_configuration::{AnnouncePolicy, Core, TORRENT_PEERS_LIMIT};
462460
use torrust_tracker_primitives::core::{AnnounceData, ScrapeData};
461+
use torrust_tracker_primitives::peer;
463462
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
464463
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
465-
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch};
466464
use torrust_tracker_torrent_repository::entry::EntrySync;
467465
use torrust_tracker_torrent_repository::repository::Repository;
468466

469-
use self::authentication::Key;
470467
use self::torrent::Torrents;
471468
use crate::core::databases::Database;
472469
use crate::CurrentClock;
@@ -495,7 +492,7 @@ pub struct Tracker {
495492
torrents: Arc<Torrents>,
496493

497494
/// The service to authenticate peers.
498-
authentication: Arc<authentication::Facade>,
495+
pub authentication: Arc<authentication::Facade>,
499496
}
500497

501498
/// How many peers the peer announcing wants in the announce response.
@@ -773,136 +770,6 @@ impl Tracker {
773770
}
774771
}
775772

776-
/// It authenticates the peer `key` against the `Tracker` authentication
777-
/// key list.
778-
///
779-
/// # Context: Authentication
780-
///
781-
/// # Errors
782-
///
783-
/// Will return an error if the the authentication key cannot be verified.
784-
pub async fn authenticate(&self, key: &Key) -> Result<(), authentication::Error> {
785-
self.authentication.authenticate(key).await
786-
}
787-
788-
/// Adds new peer keys to the tracker.
789-
///
790-
/// Keys can be pre-generated or randomly created. They can also be permanent or expire.
791-
///
792-
/// # Context: Authentication
793-
///
794-
/// # Errors
795-
///
796-
/// Will return an error if:
797-
///
798-
/// - The key duration overflows the duration type maximum value.
799-
/// - The provided pre-generated key is invalid.
800-
/// - The key could not been persisted due to database issues.
801-
pub async fn add_peer_key(&self, add_key_req: AddKeyRequest) -> Result<authentication::PeerKey, PeerKeyError> {
802-
self.authentication.add_peer_key(add_key_req).await
803-
}
804-
805-
/// It generates a new permanent authentication key.
806-
///
807-
/// Authentication keys are used by HTTP trackers.
808-
///
809-
/// # Context: Authentication
810-
///
811-
/// # Errors
812-
///
813-
/// Will return a `database::Error` if unable to add the `auth_key` to the database.
814-
pub async fn generate_permanent_auth_key(&self) -> Result<authentication::PeerKey, databases::error::Error> {
815-
self.authentication.generate_auth_key(None).await
816-
}
817-
818-
/// It generates a new expiring authentication key.
819-
///
820-
/// Authentication keys are used by HTTP trackers.
821-
///
822-
/// # Context: Authentication
823-
///
824-
/// # Errors
825-
///
826-
/// Will return a `database::Error` if unable to add the `auth_key` to the database.
827-
///
828-
/// # Arguments
829-
///
830-
/// * `lifetime` - The duration in seconds for the new key. The key will be
831-
/// no longer valid after `lifetime` seconds.
832-
pub async fn generate_auth_key(
833-
&self,
834-
lifetime: Option<Duration>,
835-
) -> Result<authentication::PeerKey, databases::error::Error> {
836-
self.authentication.generate_auth_key(lifetime).await
837-
}
838-
839-
/// It adds a pre-generated permanent authentication key.
840-
///
841-
/// Authentication keys are used by HTTP trackers.
842-
///
843-
/// # Context: Authentication
844-
///
845-
/// # Errors
846-
///
847-
/// Will return a `database::Error` if unable to add the `auth_key` to the
848-
/// database. For example, if the key already exist.
849-
///
850-
/// # Arguments
851-
///
852-
/// * `key` - The pre-generated key.
853-
pub async fn add_permanent_auth_key(&self, key: Key) -> Result<authentication::PeerKey, databases::error::Error> {
854-
self.authentication.add_auth_key(key, None).await
855-
}
856-
857-
/// It adds a pre-generated authentication key.
858-
///
859-
/// Authentication keys are used by HTTP trackers.
860-
///
861-
/// # Context: Authentication
862-
///
863-
/// # Errors
864-
///
865-
/// Will return a `database::Error` if unable to add the `auth_key` to the
866-
/// database. For example, if the key already exist.
867-
///
868-
/// # Arguments
869-
///
870-
/// * `key` - The pre-generated key.
871-
/// * `lifetime` - The duration in seconds for the new key. The key will be
872-
/// no longer valid after `lifetime` seconds.
873-
pub async fn add_auth_key(
874-
&self,
875-
key: Key,
876-
valid_until: Option<DurationSinceUnixEpoch>,
877-
) -> Result<authentication::PeerKey, databases::error::Error> {
878-
self.authentication.add_auth_key(key, valid_until).await
879-
}
880-
881-
/// It removes an authentication key.
882-
///
883-
/// # Context: Authentication
884-
///
885-
/// # Errors
886-
///
887-
/// Will return a `database::Error` if unable to remove the `key` to the database.
888-
pub async fn remove_auth_key(&self, key: &Key) -> Result<(), databases::error::Error> {
889-
self.authentication.remove_auth_key(key).await
890-
}
891-
892-
/// The `Tracker` stores the authentication keys in memory and in the database.
893-
/// In case you need to restart the `Tracker` you can load the keys from the database
894-
/// into memory with this function. Keys are automatically stored in the database when they
895-
/// are generated.
896-
///
897-
/// # Context: Authentication
898-
///
899-
/// # Errors
900-
///
901-
/// Will return a `database::Error` if unable to `load_keys` from the database.
902-
pub async fn load_keys_from_database(&self) -> Result<(), databases::error::Error> {
903-
self.authentication.load_keys_from_database().await
904-
}
905-
906773
/// It drops the database tables.
907774
///
908775
/// # Errors
@@ -1664,7 +1531,7 @@ mod tests {
16641531

16651532
let unregistered_key = authentication::Key::from_str("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap();
16661533

1667-
let result = tracker.authenticate(&unregistered_key).await;
1534+
let result = tracker.authentication.authenticate(&unregistered_key).await;
16681535

16691536
assert!(result.is_err());
16701537
}
@@ -1682,9 +1549,13 @@ mod tests {
16821549
async fn it_should_remove_an_authentication_key() {
16831550
let tracker = private_tracker();
16841551

1685-
let expiring_key = tracker.generate_auth_key(Some(Duration::from_secs(100))).await.unwrap();
1552+
let expiring_key = tracker
1553+
.authentication
1554+
.generate_auth_key(Some(Duration::from_secs(100)))
1555+
.await
1556+
.unwrap();
16861557

1687-
let result = tracker.remove_auth_key(&expiring_key.key()).await;
1558+
let result = tracker.authentication.remove_auth_key(&expiring_key.key()).await;
16881559

16891560
assert!(result.is_ok());
16901561
assert!(tracker.authentication.verify_auth_key(&expiring_key.key()).await.is_err());
@@ -1694,12 +1565,16 @@ mod tests {
16941565
async fn it_should_load_authentication_keys_from_the_database() {
16951566
let tracker = private_tracker();
16961567

1697-
let expiring_key = tracker.generate_auth_key(Some(Duration::from_secs(100))).await.unwrap();
1568+
let expiring_key = tracker
1569+
.authentication
1570+
.generate_auth_key(Some(Duration::from_secs(100)))
1571+
.await
1572+
.unwrap();
16981573

16991574
// Remove the newly generated key in memory
17001575
tracker.authentication.remove_in_memory_auth_key(&expiring_key.key()).await;
17011576

1702-
let result = tracker.load_keys_from_database().await;
1577+
let result = tracker.authentication.load_keys_from_database().await;
17031578

17041579
assert!(result.is_ok());
17051580
assert!(tracker.authentication.verify_auth_key(&expiring_key.key()).await.is_ok());
@@ -1722,7 +1597,11 @@ mod tests {
17221597
async fn it_should_generate_the_key() {
17231598
let tracker = private_tracker();
17241599

1725-
let peer_key = tracker.generate_auth_key(Some(Duration::from_secs(100))).await.unwrap();
1600+
let peer_key = tracker
1601+
.authentication
1602+
.generate_auth_key(Some(Duration::from_secs(100)))
1603+
.await
1604+
.unwrap();
17261605

17271606
assert_eq!(
17281607
peer_key.valid_until,
@@ -1734,9 +1613,13 @@ mod tests {
17341613
async fn it_should_authenticate_a_peer_with_the_key() {
17351614
let tracker = private_tracker();
17361615

1737-
let peer_key = tracker.generate_auth_key(Some(Duration::from_secs(100))).await.unwrap();
1616+
let peer_key = tracker
1617+
.authentication
1618+
.generate_auth_key(Some(Duration::from_secs(100)))
1619+
.await
1620+
.unwrap();
17381621

1739-
let result = tracker.authenticate(&peer_key.key()).await;
1622+
let result = tracker.authentication.authenticate(&peer_key.key()).await;
17401623

17411624
assert!(result.is_ok());
17421625
}
@@ -1748,11 +1631,12 @@ mod tests {
17481631
let past_timestamp = Duration::ZERO;
17491632

17501633
let peer_key = tracker
1634+
.authentication
17511635
.add_auth_key(Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap(), Some(past_timestamp))
17521636
.await
17531637
.unwrap();
17541638

1755-
assert!(tracker.authenticate(&peer_key.key()).await.is_ok());
1639+
assert!(tracker.authentication.authenticate(&peer_key.key()).await.is_ok());
17561640
}
17571641
}
17581642

@@ -1771,6 +1655,7 @@ mod tests {
17711655
let tracker = private_tracker();
17721656

17731657
let peer_key = tracker
1658+
.authentication
17741659
.add_peer_key(AddKeyRequest {
17751660
opt_key: Some(Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap().to_string()),
17761661
opt_seconds_valid: Some(100),
@@ -1789,14 +1674,15 @@ mod tests {
17891674
let tracker = private_tracker();
17901675

17911676
let peer_key = tracker
1677+
.authentication
17921678
.add_peer_key(AddKeyRequest {
17931679
opt_key: Some(Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap().to_string()),
17941680
opt_seconds_valid: Some(100),
17951681
})
17961682
.await
17971683
.unwrap();
17981684

1799-
let result = tracker.authenticate(&peer_key.key()).await;
1685+
let result = tracker.authentication.authenticate(&peer_key.key()).await;
18001686

18011687
assert!(result.is_ok());
18021688
}
@@ -1810,14 +1696,15 @@ mod tests {
18101696
});
18111697

18121698
let peer_key = tracker
1699+
.authentication
18131700
.add_peer_key(AddKeyRequest {
18141701
opt_key: Some(Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap().to_string()),
18151702
opt_seconds_valid: Some(0),
18161703
})
18171704
.await
18181705
.unwrap();
18191706

1820-
assert!(tracker.authenticate(&peer_key.key()).await.is_ok());
1707+
assert!(tracker.authentication.authenticate(&peer_key.key()).await.is_ok());
18211708
}
18221709
}
18231710
}
@@ -1831,7 +1718,7 @@ mod tests {
18311718
async fn it_should_generate_the_key() {
18321719
let tracker = private_tracker();
18331720

1834-
let peer_key = tracker.generate_permanent_auth_key().await.unwrap();
1721+
let peer_key = tracker.authentication.generate_permanent_auth_key().await.unwrap();
18351722

18361723
assert_eq!(peer_key.valid_until, None);
18371724
}
@@ -1840,9 +1727,9 @@ mod tests {
18401727
async fn it_should_authenticate_a_peer_with_the_key() {
18411728
let tracker = private_tracker();
18421729

1843-
let peer_key = tracker.generate_permanent_auth_key().await.unwrap();
1730+
let peer_key = tracker.authentication.generate_permanent_auth_key().await.unwrap();
18441731

1845-
let result = tracker.authenticate(&peer_key.key()).await;
1732+
let result = tracker.authentication.authenticate(&peer_key.key()).await;
18461733

18471734
assert!(result.is_ok());
18481735
}
@@ -1857,6 +1744,7 @@ mod tests {
18571744
let tracker = private_tracker();
18581745

18591746
let peer_key = tracker
1747+
.authentication
18601748
.add_peer_key(AddKeyRequest {
18611749
opt_key: Some(Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap().to_string()),
18621750
opt_seconds_valid: None,
@@ -1872,14 +1760,15 @@ mod tests {
18721760
let tracker = private_tracker();
18731761

18741762
let peer_key = tracker
1763+
.authentication
18751764
.add_peer_key(AddKeyRequest {
18761765
opt_key: Some(Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap().to_string()),
18771766
opt_seconds_valid: None,
18781767
})
18791768
.await
18801769
.unwrap();
18811770

1882-
let result = tracker.authenticate(&peer_key.key()).await;
1771+
let result = tracker.authentication.authenticate(&peer_key.key()).await;
18831772

18841773
assert!(result.is_ok());
18851774
}

src/servers/apis/v1/context/auth_key/handlers.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub async fn add_auth_key_handler(
3535
extract::Json(add_key_form): extract::Json<AddKeyForm>,
3636
) -> Response {
3737
match tracker
38+
.authentication
3839
.add_peer_key(AddKeyRequest {
3940
opt_key: add_key_form.opt_key.clone(),
4041
opt_seconds_valid: add_key_form.opt_seconds_valid,
@@ -67,7 +68,11 @@ pub async fn add_auth_key_handler(
6768
/// This endpoint has been deprecated. Use [`add_auth_key_handler`].
6869
pub async fn generate_auth_key_handler(State(tracker): State<Arc<Tracker>>, Path(seconds_valid_or_key): Path<u64>) -> Response {
6970
let seconds_valid = seconds_valid_or_key;
70-
match tracker.generate_auth_key(Some(Duration::from_secs(seconds_valid))).await {
71+
match tracker
72+
.authentication
73+
.generate_auth_key(Some(Duration::from_secs(seconds_valid)))
74+
.await
75+
{
7176
Ok(auth_key) => auth_key_response(&AuthKey::from(auth_key)),
7277
Err(e) => failed_to_generate_key_response(e),
7378
}
@@ -108,7 +113,7 @@ pub async fn delete_auth_key_handler(
108113
) -> Response {
109114
match Key::from_str(&seconds_valid_or_key.0) {
110115
Err(_) => invalid_auth_key_param_response(&seconds_valid_or_key.0),
111-
Ok(key) => match tracker.remove_auth_key(&key).await {
116+
Ok(key) => match tracker.authentication.remove_auth_key(&key).await {
112117
Ok(()) => ok_response(),
113118
Err(e) => failed_to_delete_key_response(e),
114119
},
@@ -128,7 +133,7 @@ pub async fn delete_auth_key_handler(
128133
/// Refer to the [API endpoint documentation](crate::servers::apis::v1::context::auth_key#reload-authentication-keys)
129134
/// for more information about this endpoint.
130135
pub async fn reload_keys_handler(State(tracker): State<Arc<Tracker>>) -> Response {
131-
match tracker.load_keys_from_database().await {
136+
match tracker.authentication.load_keys_from_database().await {
132137
Ok(()) => ok_response(),
133138
Err(e) => failed_to_reload_keys_response(e),
134139
}

src/servers/http/v1/handlers/announce.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ async fn handle_announce(
113113
// Authentication
114114
if tracker.requires_authentication() {
115115
match maybe_key {
116-
Some(key) => match tracker.authenticate(&key).await {
116+
Some(key) => match tracker.authentication.authenticate(&key).await {
117117
Ok(()) => (),
118118
Err(error) => return Err(responses::error::Error::from(error)),
119119
},

0 commit comments

Comments
 (0)