@@ -2,6 +2,7 @@ use std::panic::Location;
2
2
use std:: sync:: Arc ;
3
3
use std:: time:: Duration ;
4
4
5
+ use key:: repository:: persisted:: DatabaseKeyRepository ;
5
6
use torrust_tracker_clock:: clock:: Time ;
6
7
use torrust_tracker_configuration:: Core ;
7
8
use torrust_tracker_located_error:: Located ;
@@ -35,21 +36,20 @@ pub struct Facade {
35
36
/// The tracker configuration.
36
37
config : Core ,
37
38
38
- /// A database driver implementation: [`Sqlite3`](crate::core::databases::sqlite)
39
- /// or [`MySQL`](crate::core::databases::mysql)
40
- database : Arc < Box < dyn Database > > ,
41
-
42
39
/// Tracker users' keys. Only for private trackers.
43
40
keys : tokio:: sync:: RwLock < std:: collections:: HashMap < Key , PeerKey > > ,
41
+
42
+ /// The database repository for the authentication keys.
43
+ db_key_repository : DatabaseKeyRepository ,
44
44
}
45
45
46
46
impl Facade {
47
47
#[ must_use]
48
48
pub fn new ( config : & Core , database : & Arc < Box < dyn Database > > ) -> Self {
49
49
Self {
50
50
config : config. clone ( ) ,
51
- database : database. clone ( ) ,
52
51
keys : tokio:: sync:: RwLock :: new ( std:: collections:: HashMap :: new ( ) ) ,
52
+ db_key_repository : DatabaseKeyRepository :: new ( database) ,
53
53
}
54
54
}
55
55
@@ -205,7 +205,8 @@ impl Facade {
205
205
pub async fn generate_auth_key ( & self , lifetime : Option < Duration > ) -> Result < PeerKey , databases:: error:: Error > {
206
206
let auth_key = key:: generate_key ( lifetime) ;
207
207
208
- self . database . add_key_to_keys ( & auth_key) ?;
208
+ self . db_key_repository . add ( & auth_key) ?;
209
+
209
210
self . keys . write ( ) . await . insert ( auth_key. key . clone ( ) , auth_key. clone ( ) ) ;
210
211
Ok ( auth_key)
211
212
}
@@ -254,7 +255,8 @@ impl Facade {
254
255
// code-review: should we return a friendly error instead of the DB
255
256
// constrain error when the key already exist? For now, it's returning
256
257
// the specif error for each DB driver when a UNIQUE constrain fails.
257
- self . database . add_key_to_keys ( & auth_key) ?;
258
+ self . db_key_repository . add ( & auth_key) ?;
259
+
258
260
self . keys . write ( ) . await . insert ( auth_key. key . clone ( ) , auth_key. clone ( ) ) ;
259
261
Ok ( auth_key)
260
262
}
@@ -267,8 +269,10 @@ impl Facade {
267
269
///
268
270
/// Will return a `database::Error` if unable to remove the `key` to the database.
269
271
pub async fn remove_auth_key ( & self , key : & Key ) -> Result < ( ) , databases:: error:: Error > {
270
- self . database . remove_key_from_keys ( key) ?;
272
+ self . db_key_repository . remove ( key) ?;
273
+
271
274
self . remove_in_memory_auth_key ( key) . await ;
275
+
272
276
Ok ( ( ) )
273
277
}
274
278
@@ -290,7 +294,8 @@ impl Facade {
290
294
///
291
295
/// Will return a `database::Error` if unable to `load_keys` from the database.
292
296
pub async fn load_keys_from_database ( & self ) -> Result < ( ) , databases:: error:: Error > {
293
- let keys_from_database = self . database . load_keys ( ) ?;
297
+ let keys_from_database = self . db_key_repository . load_keys ( ) ?;
298
+
294
299
let mut keys = self . keys . write ( ) . await ;
295
300
296
301
keys. clear ( ) ;
0 commit comments