@@ -454,19 +454,16 @@ use std::net::IpAddr;
454
454
use std:: sync:: Arc ;
455
455
use std:: time:: Duration ;
456
456
457
- use authentication:: AddKeyRequest ;
458
457
use bittorrent_primitives:: info_hash:: InfoHash ;
459
- use error:: PeerKeyError ;
460
458
use torrust_tracker_clock:: clock:: Time ;
461
459
use torrust_tracker_configuration:: { AnnouncePolicy , Core , TORRENT_PEERS_LIMIT } ;
462
460
use torrust_tracker_primitives:: core:: { AnnounceData , ScrapeData } ;
461
+ use torrust_tracker_primitives:: peer;
463
462
use torrust_tracker_primitives:: swarm_metadata:: SwarmMetadata ;
464
463
use torrust_tracker_primitives:: torrent_metrics:: TorrentsMetrics ;
465
- use torrust_tracker_primitives:: { peer, DurationSinceUnixEpoch } ;
466
464
use torrust_tracker_torrent_repository:: entry:: EntrySync ;
467
465
use torrust_tracker_torrent_repository:: repository:: Repository ;
468
466
469
- use self :: authentication:: Key ;
470
467
use self :: torrent:: Torrents ;
471
468
use crate :: core:: databases:: Database ;
472
469
use crate :: CurrentClock ;
@@ -495,7 +492,7 @@ pub struct Tracker {
495
492
torrents : Arc < Torrents > ,
496
493
497
494
/// The service to authenticate peers.
498
- authentication : Arc < authentication:: Facade > ,
495
+ pub authentication : Arc < authentication:: Facade > ,
499
496
}
500
497
501
498
/// How many peers the peer announcing wants in the announce response.
@@ -773,136 +770,6 @@ impl Tracker {
773
770
}
774
771
}
775
772
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
-
906
773
/// It drops the database tables.
907
774
///
908
775
/// # Errors
@@ -1664,7 +1531,7 @@ mod tests {
1664
1531
1665
1532
let unregistered_key = authentication:: Key :: from_str ( "YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ" ) . unwrap ( ) ;
1666
1533
1667
- let result = tracker. authenticate ( & unregistered_key) . await ;
1534
+ let result = tracker. authentication . authenticate ( & unregistered_key) . await ;
1668
1535
1669
1536
assert ! ( result. is_err( ) ) ;
1670
1537
}
@@ -1682,9 +1549,13 @@ mod tests {
1682
1549
async fn it_should_remove_an_authentication_key ( ) {
1683
1550
let tracker = private_tracker ( ) ;
1684
1551
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 ( ) ;
1686
1557
1687
- let result = tracker. remove_auth_key ( & expiring_key. key ( ) ) . await ;
1558
+ let result = tracker. authentication . remove_auth_key ( & expiring_key. key ( ) ) . await ;
1688
1559
1689
1560
assert ! ( result. is_ok( ) ) ;
1690
1561
assert ! ( tracker. authentication. verify_auth_key( & expiring_key. key( ) ) . await . is_err( ) ) ;
@@ -1694,12 +1565,16 @@ mod tests {
1694
1565
async fn it_should_load_authentication_keys_from_the_database ( ) {
1695
1566
let tracker = private_tracker ( ) ;
1696
1567
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 ( ) ;
1698
1573
1699
1574
// Remove the newly generated key in memory
1700
1575
tracker. authentication . remove_in_memory_auth_key ( & expiring_key. key ( ) ) . await ;
1701
1576
1702
- let result = tracker. load_keys_from_database ( ) . await ;
1577
+ let result = tracker. authentication . load_keys_from_database ( ) . await ;
1703
1578
1704
1579
assert ! ( result. is_ok( ) ) ;
1705
1580
assert ! ( tracker. authentication. verify_auth_key( & expiring_key. key( ) ) . await . is_ok( ) ) ;
@@ -1722,7 +1597,11 @@ mod tests {
1722
1597
async fn it_should_generate_the_key ( ) {
1723
1598
let tracker = private_tracker ( ) ;
1724
1599
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 ( ) ;
1726
1605
1727
1606
assert_eq ! (
1728
1607
peer_key. valid_until,
@@ -1734,9 +1613,13 @@ mod tests {
1734
1613
async fn it_should_authenticate_a_peer_with_the_key ( ) {
1735
1614
let tracker = private_tracker ( ) ;
1736
1615
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 ( ) ;
1738
1621
1739
- let result = tracker. authenticate ( & peer_key. key ( ) ) . await ;
1622
+ let result = tracker. authentication . authenticate ( & peer_key. key ( ) ) . await ;
1740
1623
1741
1624
assert ! ( result. is_ok( ) ) ;
1742
1625
}
@@ -1748,11 +1631,12 @@ mod tests {
1748
1631
let past_timestamp = Duration :: ZERO ;
1749
1632
1750
1633
let peer_key = tracker
1634
+ . authentication
1751
1635
. add_auth_key ( Key :: new ( "YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ" ) . unwrap ( ) , Some ( past_timestamp) )
1752
1636
. await
1753
1637
. unwrap ( ) ;
1754
1638
1755
- assert ! ( tracker. authenticate( & peer_key. key( ) ) . await . is_ok( ) ) ;
1639
+ assert ! ( tracker. authentication . authenticate( & peer_key. key( ) ) . await . is_ok( ) ) ;
1756
1640
}
1757
1641
}
1758
1642
@@ -1771,6 +1655,7 @@ mod tests {
1771
1655
let tracker = private_tracker ( ) ;
1772
1656
1773
1657
let peer_key = tracker
1658
+ . authentication
1774
1659
. add_peer_key ( AddKeyRequest {
1775
1660
opt_key : Some ( Key :: new ( "YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ" ) . unwrap ( ) . to_string ( ) ) ,
1776
1661
opt_seconds_valid : Some ( 100 ) ,
@@ -1789,14 +1674,15 @@ mod tests {
1789
1674
let tracker = private_tracker ( ) ;
1790
1675
1791
1676
let peer_key = tracker
1677
+ . authentication
1792
1678
. add_peer_key ( AddKeyRequest {
1793
1679
opt_key : Some ( Key :: new ( "YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ" ) . unwrap ( ) . to_string ( ) ) ,
1794
1680
opt_seconds_valid : Some ( 100 ) ,
1795
1681
} )
1796
1682
. await
1797
1683
. unwrap ( ) ;
1798
1684
1799
- let result = tracker. authenticate ( & peer_key. key ( ) ) . await ;
1685
+ let result = tracker. authentication . authenticate ( & peer_key. key ( ) ) . await ;
1800
1686
1801
1687
assert ! ( result. is_ok( ) ) ;
1802
1688
}
@@ -1810,14 +1696,15 @@ mod tests {
1810
1696
} ) ;
1811
1697
1812
1698
let peer_key = tracker
1699
+ . authentication
1813
1700
. add_peer_key ( AddKeyRequest {
1814
1701
opt_key : Some ( Key :: new ( "YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ" ) . unwrap ( ) . to_string ( ) ) ,
1815
1702
opt_seconds_valid : Some ( 0 ) ,
1816
1703
} )
1817
1704
. await
1818
1705
. unwrap ( ) ;
1819
1706
1820
- assert ! ( tracker. authenticate( & peer_key. key( ) ) . await . is_ok( ) ) ;
1707
+ assert ! ( tracker. authentication . authenticate( & peer_key. key( ) ) . await . is_ok( ) ) ;
1821
1708
}
1822
1709
}
1823
1710
}
@@ -1831,7 +1718,7 @@ mod tests {
1831
1718
async fn it_should_generate_the_key ( ) {
1832
1719
let tracker = private_tracker ( ) ;
1833
1720
1834
- let peer_key = tracker. generate_permanent_auth_key ( ) . await . unwrap ( ) ;
1721
+ let peer_key = tracker. authentication . generate_permanent_auth_key ( ) . await . unwrap ( ) ;
1835
1722
1836
1723
assert_eq ! ( peer_key. valid_until, None ) ;
1837
1724
}
@@ -1840,9 +1727,9 @@ mod tests {
1840
1727
async fn it_should_authenticate_a_peer_with_the_key ( ) {
1841
1728
let tracker = private_tracker ( ) ;
1842
1729
1843
- let peer_key = tracker. generate_permanent_auth_key ( ) . await . unwrap ( ) ;
1730
+ let peer_key = tracker. authentication . generate_permanent_auth_key ( ) . await . unwrap ( ) ;
1844
1731
1845
- let result = tracker. authenticate ( & peer_key. key ( ) ) . await ;
1732
+ let result = tracker. authentication . authenticate ( & peer_key. key ( ) ) . await ;
1846
1733
1847
1734
assert ! ( result. is_ok( ) ) ;
1848
1735
}
@@ -1857,6 +1744,7 @@ mod tests {
1857
1744
let tracker = private_tracker ( ) ;
1858
1745
1859
1746
let peer_key = tracker
1747
+ . authentication
1860
1748
. add_peer_key ( AddKeyRequest {
1861
1749
opt_key : Some ( Key :: new ( "YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ" ) . unwrap ( ) . to_string ( ) ) ,
1862
1750
opt_seconds_valid : None ,
@@ -1872,14 +1760,15 @@ mod tests {
1872
1760
let tracker = private_tracker ( ) ;
1873
1761
1874
1762
let peer_key = tracker
1763
+ . authentication
1875
1764
. add_peer_key ( AddKeyRequest {
1876
1765
opt_key : Some ( Key :: new ( "YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ" ) . unwrap ( ) . to_string ( ) ) ,
1877
1766
opt_seconds_valid : None ,
1878
1767
} )
1879
1768
. await
1880
1769
. unwrap ( ) ;
1881
1770
1882
- let result = tracker. authenticate ( & peer_key. key ( ) ) . await ;
1771
+ let result = tracker. authentication . authenticate ( & peer_key. key ( ) ) . await ;
1883
1772
1884
1773
assert ! ( result. is_ok( ) ) ;
1885
1774
}
0 commit comments