@@ -17,32 +17,18 @@ pub type Key = key::Key;
17
17
pub type Error = key:: Error ;
18
18
19
19
pub struct Facade {
20
- /// The authentication service.
21
- authentication_service : Arc < service:: AuthenticationService > ,
22
-
23
20
/// The keys handler.
24
21
keys_handler : Arc < handler:: KeysHandler > ,
25
22
}
26
23
27
24
impl Facade {
28
25
#[ must_use]
29
- pub fn new ( authentication_service : & Arc < service :: AuthenticationService > , keys_handler : & Arc < handler:: KeysHandler > ) -> Self {
26
+ pub fn new ( keys_handler : & Arc < handler:: KeysHandler > ) -> Self {
30
27
Self {
31
- authentication_service : authentication_service. clone ( ) ,
32
28
keys_handler : keys_handler. clone ( ) ,
33
29
}
34
30
}
35
31
36
- /// It authenticates the peer `key` against the `Tracker` authentication
37
- /// key list.
38
- ///
39
- /// # Errors
40
- ///
41
- /// Will return an error if the the authentication key cannot be verified.
42
- pub async fn authenticate ( & self , key : & Key ) -> Result < ( ) , Error > {
43
- self . authentication_service . authenticate ( key) . await
44
- }
45
-
46
32
/// Adds new peer keys to the tracker.
47
33
///
48
34
/// Keys can be pre-generated or randomly created. They can also be permanent or expire.
@@ -149,26 +135,30 @@ mod tests {
149
135
use crate :: core:: authentication:: handler:: KeysHandler ;
150
136
use crate :: core:: authentication:: key:: repository:: in_memory:: InMemoryKeyRepository ;
151
137
use crate :: core:: authentication:: key:: repository:: persisted:: DatabaseKeyRepository ;
138
+ use crate :: core:: authentication:: service:: AuthenticationService ;
152
139
use crate :: core:: authentication:: { self , service} ;
153
140
use crate :: core:: services:: initialize_database;
154
141
155
- fn instantiate_authentication_facade ( ) -> authentication:: Facade {
142
+ fn instantiate_keys_manager_and_authentication ( ) -> ( authentication:: Facade , Arc < AuthenticationService > ) {
156
143
let config = configuration:: ephemeral_private ( ) ;
157
144
158
- instantiate_authentication_facade_with_configuration ( & config)
145
+ instantiate_keys_manager_and_authentication_with_configuration ( & config)
159
146
}
160
147
161
- fn instantiate_authentication_facade_with_checking_keys_expiration_disabled ( ) -> authentication:: Facade {
148
+ fn instantiate_keys_manager_and_authentication_with_checking_keys_expiration_disabled (
149
+ ) -> ( authentication:: Facade , Arc < AuthenticationService > ) {
162
150
let mut config = configuration:: ephemeral_private ( ) ;
163
151
164
152
config. core . private_mode = Some ( PrivateMode {
165
153
check_keys_expiration : false ,
166
154
} ) ;
167
155
168
- instantiate_authentication_facade_with_configuration ( & config)
156
+ instantiate_keys_manager_and_authentication_with_configuration ( & config)
169
157
}
170
158
171
- fn instantiate_authentication_facade_with_configuration ( config : & Configuration ) -> authentication:: Facade {
159
+ fn instantiate_keys_manager_and_authentication_with_configuration (
160
+ config : & Configuration ,
161
+ ) -> ( authentication:: Facade , Arc < AuthenticationService > ) {
172
162
let database = initialize_database ( config) ;
173
163
174
164
let db_key_repository = Arc :: new ( DatabaseKeyRepository :: new ( & database) ) ;
@@ -180,52 +170,40 @@ mod tests {
180
170
& in_memory_key_repository. clone ( ) ,
181
171
) ) ;
182
172
183
- authentication:: Facade :: new ( & authentication_service, & keys_handler)
173
+ let facade = authentication:: Facade :: new ( & keys_handler) ;
174
+
175
+ ( facade, authentication_service)
184
176
}
185
177
186
178
#[ tokio:: test]
187
179
async fn it_should_remove_an_authentication_key ( ) {
188
- let authentication = instantiate_authentication_facade ( ) ;
180
+ let ( keys_manager , authentication_service ) = instantiate_keys_manager_and_authentication ( ) ;
189
181
190
- let expiring_key = authentication
191
- . generate_auth_key ( Some ( Duration :: from_secs ( 100 ) ) )
192
- . await
193
- . unwrap ( ) ;
182
+ let expiring_key = keys_manager. generate_auth_key ( Some ( Duration :: from_secs ( 100 ) ) ) . await . unwrap ( ) ;
194
183
195
- let result = authentication . remove_auth_key ( & expiring_key. key ( ) ) . await ;
184
+ let result = keys_manager . remove_auth_key ( & expiring_key. key ( ) ) . await ;
196
185
197
186
assert ! ( result. is_ok( ) ) ;
198
187
199
188
// The key should no longer be valid
200
- assert ! ( authentication
201
- . authentication_service
202
- . authenticate( & expiring_key. key( ) )
203
- . await
204
- . is_err( ) ) ;
189
+ assert ! ( authentication_service. authenticate( & expiring_key. key( ) ) . await . is_err( ) ) ;
205
190
}
206
191
207
192
#[ tokio:: test]
208
193
async fn it_should_load_authentication_keys_from_the_database ( ) {
209
- let authentication = instantiate_authentication_facade ( ) ;
194
+ let ( keys_manager , authentication_service ) = instantiate_keys_manager_and_authentication ( ) ;
210
195
211
- let expiring_key = authentication
212
- . generate_auth_key ( Some ( Duration :: from_secs ( 100 ) ) )
213
- . await
214
- . unwrap ( ) ;
196
+ let expiring_key = keys_manager. generate_auth_key ( Some ( Duration :: from_secs ( 100 ) ) ) . await . unwrap ( ) ;
215
197
216
198
// Remove the newly generated key in memory
217
- authentication . remove_in_memory_auth_key ( & expiring_key. key ( ) ) . await ;
199
+ keys_manager . remove_in_memory_auth_key ( & expiring_key. key ( ) ) . await ;
218
200
219
- let result = authentication . load_keys_from_database ( ) . await ;
201
+ let result = keys_manager . load_keys_from_database ( ) . await ;
220
202
221
203
assert ! ( result. is_ok( ) ) ;
222
204
223
205
// The key should no longer be valid
224
- assert ! ( authentication
225
- . authentication_service
226
- . authenticate( & expiring_key. key( ) )
227
- . await
228
- . is_ok( ) ) ;
206
+ assert ! ( authentication_service. authenticate( & expiring_key. key( ) ) . await . is_ok( ) ) ;
229
207
}
230
208
231
209
mod with_expiring_and {
@@ -234,114 +212,115 @@ mod tests {
234
212
use std:: time:: Duration ;
235
213
236
214
use crate :: core:: authentication:: tests:: the_tracker_configured_as_private:: {
237
- instantiate_authentication_facade, instantiate_authentication_facade_with_checking_keys_expiration_disabled,
215
+ instantiate_keys_manager_and_authentication,
216
+ instantiate_keys_manager_and_authentication_with_checking_keys_expiration_disabled,
238
217
} ;
239
218
use crate :: core:: authentication:: Key ;
240
219
241
220
#[ tokio:: test]
242
221
async fn it_should_authenticate_a_peer_with_the_key ( ) {
243
- let authentication = instantiate_authentication_facade ( ) ;
222
+ let ( keys_manager , authentication_service ) = instantiate_keys_manager_and_authentication ( ) ;
244
223
245
- let peer_key = authentication
246
- . generate_auth_key ( Some ( Duration :: from_secs ( 100 ) ) )
247
- . await
248
- . unwrap ( ) ;
224
+ let peer_key = keys_manager. generate_auth_key ( Some ( Duration :: from_secs ( 100 ) ) ) . await . unwrap ( ) ;
249
225
250
- let result = authentication . authenticate ( & peer_key. key ( ) ) . await ;
226
+ let result = authentication_service . authenticate ( & peer_key. key ( ) ) . await ;
251
227
252
228
assert ! ( result. is_ok( ) ) ;
253
229
}
254
230
255
231
#[ tokio:: test]
256
232
async fn it_should_accept_an_expired_key_when_checking_expiration_is_disabled_in_configuration ( ) {
257
- let authentication = instantiate_authentication_facade_with_checking_keys_expiration_disabled ( ) ;
233
+ let ( keys_manager, authentication_service) =
234
+ instantiate_keys_manager_and_authentication_with_checking_keys_expiration_disabled ( ) ;
258
235
259
236
let past_timestamp = Duration :: ZERO ;
260
237
261
- let peer_key = authentication
238
+ let peer_key = keys_manager
262
239
. add_auth_key ( Key :: new ( "YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ" ) . unwrap ( ) , Some ( past_timestamp) )
263
240
. await
264
241
. unwrap ( ) ;
265
242
266
- assert ! ( authentication . authenticate( & peer_key. key( ) ) . await . is_ok( ) ) ;
243
+ assert ! ( authentication_service . authenticate( & peer_key. key( ) ) . await . is_ok( ) ) ;
267
244
}
268
245
}
269
246
270
247
mod pre_generated_keys {
271
248
272
249
use crate :: core:: authentication:: tests:: the_tracker_configured_as_private:: {
273
- instantiate_authentication_facade, instantiate_authentication_facade_with_checking_keys_expiration_disabled,
250
+ instantiate_keys_manager_and_authentication,
251
+ instantiate_keys_manager_and_authentication_with_checking_keys_expiration_disabled,
274
252
} ;
275
253
use crate :: core:: authentication:: { AddKeyRequest , Key } ;
276
254
277
255
#[ tokio:: test]
278
256
async fn it_should_authenticate_a_peer_with_the_key ( ) {
279
- let authentication = instantiate_authentication_facade ( ) ;
257
+ let ( keys_manager , authentication_service ) = instantiate_keys_manager_and_authentication ( ) ;
280
258
281
- let peer_key = authentication
259
+ let peer_key = keys_manager
282
260
. add_peer_key ( AddKeyRequest {
283
261
opt_key : Some ( Key :: new ( "YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ" ) . unwrap ( ) . to_string ( ) ) ,
284
262
opt_seconds_valid : Some ( 100 ) ,
285
263
} )
286
264
. await
287
265
. unwrap ( ) ;
288
266
289
- let result = authentication . authenticate ( & peer_key. key ( ) ) . await ;
267
+ let result = authentication_service . authenticate ( & peer_key. key ( ) ) . await ;
290
268
291
269
assert ! ( result. is_ok( ) ) ;
292
270
}
293
271
294
272
#[ tokio:: test]
295
273
async fn it_should_accept_an_expired_key_when_checking_expiration_is_disabled_in_configuration ( ) {
296
- let authentication = instantiate_authentication_facade_with_checking_keys_expiration_disabled ( ) ;
274
+ let ( keys_manager, authentication_service) =
275
+ instantiate_keys_manager_and_authentication_with_checking_keys_expiration_disabled ( ) ;
297
276
298
- let peer_key = authentication
277
+ let peer_key = keys_manager
299
278
. add_peer_key ( AddKeyRequest {
300
279
opt_key : Some ( Key :: new ( "YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ" ) . unwrap ( ) . to_string ( ) ) ,
301
280
opt_seconds_valid : Some ( 0 ) ,
302
281
} )
303
282
. await
304
283
. unwrap ( ) ;
305
284
306
- assert ! ( authentication . authenticate( & peer_key. key( ) ) . await . is_ok( ) ) ;
285
+ assert ! ( authentication_service . authenticate( & peer_key. key( ) ) . await . is_ok( ) ) ;
307
286
}
308
287
}
309
288
}
310
289
311
290
mod with_permanent_and {
312
291
313
292
mod randomly_generated_keys {
314
- use crate :: core:: authentication:: tests:: the_tracker_configured_as_private:: instantiate_authentication_facade ;
293
+ use crate :: core:: authentication:: tests:: the_tracker_configured_as_private:: instantiate_keys_manager_and_authentication ;
315
294
316
295
#[ tokio:: test]
317
296
async fn it_should_authenticate_a_peer_with_the_key ( ) {
318
- let authentication = instantiate_authentication_facade ( ) ;
297
+ let ( keys_manager , authentication_service ) = instantiate_keys_manager_and_authentication ( ) ;
319
298
320
- let peer_key = authentication . generate_permanent_auth_key ( ) . await . unwrap ( ) ;
299
+ let peer_key = keys_manager . generate_permanent_auth_key ( ) . await . unwrap ( ) ;
321
300
322
- let result = authentication . authenticate ( & peer_key. key ( ) ) . await ;
301
+ let result = authentication_service . authenticate ( & peer_key. key ( ) ) . await ;
323
302
324
303
assert ! ( result. is_ok( ) ) ;
325
304
}
326
305
}
327
306
328
307
mod pre_generated_keys {
329
- use crate :: core:: authentication:: tests:: the_tracker_configured_as_private:: instantiate_authentication_facade ;
308
+ use crate :: core:: authentication:: tests:: the_tracker_configured_as_private:: instantiate_keys_manager_and_authentication ;
330
309
use crate :: core:: authentication:: { AddKeyRequest , Key } ;
331
310
332
311
#[ tokio:: test]
333
312
async fn it_should_authenticate_a_peer_with_the_key ( ) {
334
- let authentication = instantiate_authentication_facade ( ) ;
313
+ let ( keys_manager , authentication_service ) = instantiate_keys_manager_and_authentication ( ) ;
335
314
336
- let peer_key = authentication
315
+ let peer_key = keys_manager
337
316
. add_peer_key ( AddKeyRequest {
338
317
opt_key : Some ( Key :: new ( "YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ" ) . unwrap ( ) . to_string ( ) ) ,
339
318
opt_seconds_valid : None ,
340
319
} )
341
320
. await
342
321
. unwrap ( ) ;
343
322
344
- let result = authentication . authenticate ( & peer_key. key ( ) ) . await ;
323
+ let result = authentication_service . authenticate ( & peer_key. key ( ) ) . await ;
345
324
346
325
assert ! ( result. is_ok( ) ) ;
347
326
}
0 commit comments