@@ -453,16 +453,17 @@ use std::panic::Location;
453453use std:: sync:: Arc ;
454454use std:: time:: Duration ;
455455
456+ use auth:: ExpiringKey ;
456457use databases:: driver:: Driver ;
457458use derive_more:: Constructor ;
458459use tokio:: sync:: mpsc:: error:: SendError ;
459460use torrust_tracker_clock:: clock:: Time ;
460461use torrust_tracker_configuration:: v2:: database;
461462use torrust_tracker_configuration:: { AnnouncePolicy , Core , TORRENT_PEERS_LIMIT } ;
462463use torrust_tracker_primitives:: info_hash:: InfoHash ;
463- use torrust_tracker_primitives:: peer;
464464use torrust_tracker_primitives:: swarm_metadata:: SwarmMetadata ;
465465use torrust_tracker_primitives:: torrent_metrics:: TorrentsMetrics ;
466+ use torrust_tracker_primitives:: { peer, DurationSinceUnixEpoch } ;
466467use torrust_tracker_torrent_repository:: entry:: EntrySync ;
467468use torrust_tracker_torrent_repository:: repository:: Repository ;
468469use tracing:: debug;
@@ -804,6 +805,37 @@ impl Tracker {
804805 /// Will return a `database::Error` if unable to add the `auth_key` to the database.
805806 pub async fn generate_auth_key ( & self , lifetime : Duration ) -> Result < auth:: ExpiringKey , databases:: error:: Error > {
806807 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.
807839 self . database . add_key_to_keys ( & auth_key) ?;
808840 self . keys . write ( ) . await . insert ( auth_key. key . clone ( ) , auth_key. clone ( ) ) ;
809841 Ok ( auth_key)
@@ -816,10 +848,6 @@ impl Tracker {
816848 /// # Errors
817849 ///
818850 /// 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`.
823851 pub async fn remove_auth_key ( & self , key : & Key ) -> Result < ( ) , databases:: error:: Error > {
824852 self . database . remove_key_from_keys ( key) ?;
825853 self . keys . write ( ) . await . remove ( key) ;
0 commit comments