@@ -453,16 +453,17 @@ use std::panic::Location;
453
453
use std:: sync:: Arc ;
454
454
use std:: time:: Duration ;
455
455
456
+ use auth:: ExpiringKey ;
456
457
use databases:: driver:: Driver ;
457
458
use derive_more:: Constructor ;
458
459
use tokio:: sync:: mpsc:: error:: SendError ;
459
460
use torrust_tracker_clock:: clock:: Time ;
460
461
use torrust_tracker_configuration:: v2:: database;
461
462
use torrust_tracker_configuration:: { AnnouncePolicy , Core , TORRENT_PEERS_LIMIT } ;
462
463
use torrust_tracker_primitives:: info_hash:: InfoHash ;
463
- use torrust_tracker_primitives:: peer;
464
464
use torrust_tracker_primitives:: swarm_metadata:: SwarmMetadata ;
465
465
use torrust_tracker_primitives:: torrent_metrics:: TorrentsMetrics ;
466
+ use torrust_tracker_primitives:: { peer, DurationSinceUnixEpoch } ;
466
467
use torrust_tracker_torrent_repository:: entry:: EntrySync ;
467
468
use torrust_tracker_torrent_repository:: repository:: Repository ;
468
469
use tracing:: debug;
@@ -804,6 +805,37 @@ impl Tracker {
804
805
/// Will return a `database::Error` if unable to add the `auth_key` to the database.
805
806
pub async fn generate_auth_key ( & self , lifetime : Duration ) -> Result < auth:: ExpiringKey , databases:: error:: Error > {
806
807
let auth_key = auth:: generate ( lifetime) ;
808
+
809
+ self . database . add_key_to_keys ( & auth_key) ?;
810
+ self . keys . write ( ) . await . insert ( auth_key. key . clone ( ) , auth_key. clone ( ) ) ;
811
+ Ok ( auth_key)
812
+ }
813
+
814
+ /// It adds a pre-generated authentication key.
815
+ ///
816
+ /// Authentication keys are used by HTTP trackers.
817
+ ///
818
+ /// # Context: Authentication
819
+ ///
820
+ /// # Errors
821
+ ///
822
+ /// Will return a `database::Error` if unable to add the `auth_key` to the
823
+ /// database. For example, if the key already exist.
824
+ ///
825
+ /// # Arguments
826
+ ///
827
+ /// * `lifetime` - The duration in seconds for the new key. The key will be
828
+ /// no longer valid after `lifetime` seconds.
829
+ pub async fn add_auth_key (
830
+ & self ,
831
+ key : Key ,
832
+ valid_until : DurationSinceUnixEpoch ,
833
+ ) -> Result < auth:: ExpiringKey , databases:: error:: Error > {
834
+ let auth_key = ExpiringKey { key, valid_until } ;
835
+
836
+ // code-review: should we return a friendly error instead of the DB
837
+ // constrain error when the key already exist? For now, it's returning
838
+ // the specif error for each DB driver when a UNIQUE constrain fails.
807
839
self . database . add_key_to_keys ( & auth_key) ?;
808
840
self . keys . write ( ) . await . insert ( auth_key. key . clone ( ) , auth_key. clone ( ) ) ;
809
841
Ok ( auth_key)
@@ -816,10 +848,6 @@ impl Tracker {
816
848
/// # Errors
817
849
///
818
850
/// Will return a `database::Error` if unable to remove the `key` to the database.
819
- ///
820
- /// # Panics
821
- ///
822
- /// Will panic if key cannot be converted into a valid `Key`.
823
851
pub async fn remove_auth_key ( & self , key : & Key ) -> Result < ( ) , databases:: error:: Error > {
824
852
self . database . remove_key_from_keys ( key) ?;
825
853
self . keys . write ( ) . await . remove ( key) ;
0 commit comments