1
- use std:: panic:: Location ;
2
1
use std:: sync:: Arc ;
3
2
use std:: time:: Duration ;
4
3
@@ -14,6 +13,7 @@ use super::error::PeerKeyError;
14
13
use crate :: CurrentClock ;
15
14
16
15
pub mod key;
16
+ pub mod service;
17
17
18
18
pub type PeerKey = key:: PeerKey ;
19
19
pub type Key = key:: Key ;
@@ -34,23 +34,25 @@ pub struct AddKeyRequest {
34
34
}
35
35
36
36
pub struct Facade {
37
- /// The tracker configuration.
38
- config : Core ,
39
-
40
37
/// The database repository for the authentication keys.
41
38
db_key_repository : DatabaseKeyRepository ,
42
39
43
40
/// In-memory implementation of the authentication key repository.
44
- in_memory_key_repository : InMemoryKeyRepository ,
41
+ in_memory_key_repository : Arc < InMemoryKeyRepository > ,
42
+
43
+ /// The authentication service.
44
+ authentication_service : service:: Service ,
45
45
}
46
46
47
47
impl Facade {
48
48
#[ must_use]
49
49
pub fn new ( config : & Core , database : & Arc < Box < dyn Database > > ) -> Self {
50
+ let in_memory_key_repository = Arc :: new ( InMemoryKeyRepository :: default ( ) ) ;
51
+
50
52
Self {
51
- config : config. clone ( ) ,
52
53
db_key_repository : DatabaseKeyRepository :: new ( database) ,
53
- in_memory_key_repository : InMemoryKeyRepository :: default ( ) ,
54
+ in_memory_key_repository : in_memory_key_repository. clone ( ) ,
55
+ authentication_service : service:: Service :: new ( config, & in_memory_key_repository) ,
54
56
}
55
57
}
56
58
@@ -61,40 +63,7 @@ impl Facade {
61
63
///
62
64
/// Will return an error if the the authentication key cannot be verified.
63
65
pub async fn authenticate ( & self , key : & Key ) -> Result < ( ) , Error > {
64
- if self . is_private ( ) {
65
- self . verify_auth_key ( key) . await
66
- } else {
67
- Ok ( ( ) )
68
- }
69
- }
70
-
71
- /// Returns `true` is the tracker is in private mode.
72
- pub fn is_private ( & self ) -> bool {
73
- self . config . private
74
- }
75
-
76
- /// It verifies an authentication key.
77
- ///
78
- /// # Errors
79
- ///
80
- /// Will return a `key::Error` if unable to get any `auth_key`.
81
- pub async fn verify_auth_key ( & self , key : & Key ) -> Result < ( ) , Error > {
82
- match self . in_memory_key_repository . get ( key) . await {
83
- None => Err ( Error :: UnableToReadKey {
84
- location : Location :: caller ( ) ,
85
- key : Box :: new ( key. clone ( ) ) ,
86
- } ) ,
87
- Some ( key) => match self . config . private_mode {
88
- Some ( private_mode) => {
89
- if private_mode. check_keys_expiration {
90
- return key:: verify_key_expiration ( & key) ;
91
- }
92
-
93
- Ok ( ( ) )
94
- }
95
- None => key:: verify_key_expiration ( & key) ,
96
- } ,
97
- }
66
+ self . authentication_service . authenticate ( key) . await
98
67
}
99
68
100
69
/// Adds new peer keys to the tracker.
@@ -340,7 +309,11 @@ mod tests {
340
309
341
310
let unregistered_key = authentication:: Key :: from_str ( "YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ" ) . unwrap ( ) ;
342
311
343
- assert ! ( authentication. verify_auth_key( & unregistered_key) . await . is_err( ) ) ;
312
+ assert ! ( authentication
313
+ . authentication_service
314
+ . verify_auth_key( & unregistered_key)
315
+ . await
316
+ . is_err( ) ) ;
344
317
}
345
318
346
319
#[ tokio:: test]
@@ -355,7 +328,11 @@ mod tests {
355
328
let result = authentication. remove_auth_key ( & expiring_key. key ( ) ) . await ;
356
329
357
330
assert ! ( result. is_ok( ) ) ;
358
- assert ! ( authentication. verify_auth_key( & expiring_key. key( ) ) . await . is_err( ) ) ;
331
+ assert ! ( authentication
332
+ . authentication_service
333
+ . verify_auth_key( & expiring_key. key( ) )
334
+ . await
335
+ . is_err( ) ) ;
359
336
}
360
337
361
338
#[ tokio:: test]
@@ -373,7 +350,11 @@ mod tests {
373
350
let result = authentication. load_keys_from_database ( ) . await ;
374
351
375
352
assert ! ( result. is_ok( ) ) ;
376
- assert ! ( authentication. verify_auth_key( & expiring_key. key( ) ) . await . is_ok( ) ) ;
353
+ assert ! ( authentication
354
+ . authentication_service
355
+ . verify_auth_key( & expiring_key. key( ) )
356
+ . await
357
+ . is_ok( ) ) ;
377
358
}
378
359
379
360
mod with_expiring_and {
0 commit comments