@@ -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:: in_memory:: InMemoryKeyRepository ;
5
6
use key:: repository:: persisted:: DatabaseKeyRepository ;
6
7
use torrust_tracker_clock:: clock:: Time ;
7
8
use torrust_tracker_configuration:: Core ;
@@ -36,20 +37,20 @@ pub struct Facade {
36
37
/// The tracker configuration.
37
38
config : Core ,
38
39
39
- /// Tracker users' keys. Only for private trackers.
40
- keys : tokio:: sync:: RwLock < std:: collections:: HashMap < Key , PeerKey > > ,
41
-
42
40
/// The database repository for the authentication keys.
43
41
db_key_repository : DatabaseKeyRepository ,
42
+
43
+ /// In-memory implementation of the authentication key repository.
44
+ in_memory_key_repository : InMemoryKeyRepository ,
44
45
}
45
46
46
47
impl Facade {
47
48
#[ must_use]
48
49
pub fn new ( config : & Core , database : & Arc < Box < dyn Database > > ) -> Self {
49
50
Self {
50
51
config : config. clone ( ) ,
51
- keys : tokio:: sync:: RwLock :: new ( std:: collections:: HashMap :: new ( ) ) ,
52
52
db_key_repository : DatabaseKeyRepository :: new ( database) ,
53
+ in_memory_key_repository : InMemoryKeyRepository :: default ( ) ,
53
54
}
54
55
}
55
56
@@ -82,20 +83,20 @@ impl Facade {
82
83
///
83
84
/// Will return a `key::Error` if unable to get any `auth_key`.
84
85
pub async fn verify_auth_key ( & self , key : & Key ) -> Result < ( ) , Error > {
85
- match self . keys . read ( ) . await . get ( key) {
86
+ match self . in_memory_key_repository . get ( key) . await {
86
87
None => Err ( Error :: UnableToReadKey {
87
88
location : Location :: caller ( ) ,
88
89
key : Box :: new ( key. clone ( ) ) ,
89
90
} ) ,
90
91
Some ( key) => match self . config . private_mode {
91
92
Some ( private_mode) => {
92
93
if private_mode. check_keys_expiration {
93
- return key:: verify_key_expiration ( key) ;
94
+ return key:: verify_key_expiration ( & key) ;
94
95
}
95
96
96
97
Ok ( ( ) )
97
98
}
98
- None => key:: verify_key_expiration ( key) ,
99
+ None => key:: verify_key_expiration ( & key) ,
99
100
} ,
100
101
}
101
102
}
@@ -203,12 +204,13 @@ impl Facade {
203
204
/// * `lifetime` - The duration in seconds for the new key. The key will be
204
205
/// no longer valid after `lifetime` seconds.
205
206
pub async fn generate_auth_key ( & self , lifetime : Option < Duration > ) -> Result < PeerKey , databases:: error:: Error > {
206
- let auth_key = key:: generate_key ( lifetime) ;
207
+ let peer_key = key:: generate_key ( lifetime) ;
208
+
209
+ self . db_key_repository . add ( & peer_key) ?;
207
210
208
- self . db_key_repository . add ( & auth_key ) ? ;
211
+ self . in_memory_key_repository . insert ( & peer_key ) . await ;
209
212
210
- self . keys . write ( ) . await . insert ( auth_key. key . clone ( ) , auth_key. clone ( ) ) ;
211
- Ok ( auth_key)
213
+ Ok ( peer_key)
212
214
}
213
215
214
216
/// It adds a pre-generated permanent authentication key.
@@ -250,15 +252,16 @@ impl Facade {
250
252
key : Key ,
251
253
valid_until : Option < DurationSinceUnixEpoch > ,
252
254
) -> Result < PeerKey , databases:: error:: Error > {
253
- let auth_key = PeerKey { key, valid_until } ;
255
+ let peer_key = PeerKey { key, valid_until } ;
254
256
255
257
// code-review: should we return a friendly error instead of the DB
256
258
// constrain error when the key already exist? For now, it's returning
257
259
// the specif error for each DB driver when a UNIQUE constrain fails.
258
- self . db_key_repository . add ( & auth_key ) ?;
260
+ self . db_key_repository . add ( & peer_key ) ?;
259
261
260
- self . keys . write ( ) . await . insert ( auth_key. key . clone ( ) , auth_key. clone ( ) ) ;
261
- Ok ( auth_key)
262
+ self . in_memory_key_repository . insert ( & peer_key) . await ;
263
+
264
+ Ok ( peer_key)
262
265
}
263
266
264
267
/// It removes an authentication key.
@@ -280,7 +283,7 @@ impl Facade {
280
283
///
281
284
/// # Context: Authentication
282
285
pub async fn remove_in_memory_auth_key ( & self , key : & Key ) {
283
- self . keys . write ( ) . await . remove ( key) ;
286
+ self . in_memory_key_repository . remove ( key) . await ;
284
287
}
285
288
286
289
/// The `Tracker` stores the authentication keys in memory and in the database.
@@ -296,12 +299,10 @@ impl Facade {
296
299
pub async fn load_keys_from_database ( & self ) -> Result < ( ) , databases:: error:: Error > {
297
300
let keys_from_database = self . db_key_repository . load_keys ( ) ?;
298
301
299
- let mut keys = self . keys . write ( ) . await ;
300
-
301
- keys. clear ( ) ;
302
+ self . in_memory_key_repository . clear ( ) . await ;
302
303
303
304
for key in keys_from_database {
304
- keys . insert ( key. key . clone ( ) , key ) ;
305
+ self . in_memory_key_repository . insert ( & key) . await ;
305
306
}
306
307
307
308
Ok ( ( ) )
0 commit comments