-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathmod.rs
More file actions
242 lines (185 loc) · 9.8 KB
/
mod.rs
File metadata and controls
242 lines (185 loc) · 9.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//! Tracker authentication services and structs.
//!
//! One of the crate responsibilities is to create and keep authentication keys.
//! Auth keys are used by HTTP trackers when the tracker is running in `private`
//! mode.
//!
//! HTTP tracker's clients need to obtain an authentication key before starting
//! requesting the tracker. Once they get one they have to include a `PATH`
//! param with the key in all the HTTP requests. For example, when a peer wants
//! to `announce` itself it has to use the HTTP tracker endpoint:
//!
//! `GET /announce/:key`
//!
//! The common way to obtain the keys is by using the tracker API directly or
//! via other applications like the [Torrust Index](https://github.com/torrust/torrust-index).
use crate::CurrentClock;
pub mod handler;
pub mod key;
pub mod service;
pub type PeerKey = key::PeerKey;
pub type Key = key::Key;
pub type Error = key::Error;
#[cfg(test)]
mod tests {
// Integration tests for authentication.
mod the_tracker_configured_as_private {
use std::sync::Arc;
use std::time::Duration;
use torrust_tracker_configuration::v2_0_0::core::PrivateMode;
use torrust_tracker_configuration::Configuration;
use torrust_tracker_test_helpers::configuration;
use crate::authentication::handler::KeysHandler;
use crate::authentication::key::repository::in_memory::InMemoryKeyRepository;
use crate::authentication::key::repository::persisted::DatabaseKeyRepository;
use crate::authentication::service;
use crate::authentication::service::AuthenticationService;
use crate::databases::setup::initialize_database;
fn instantiate_keys_manager_and_authentication() -> (Arc<KeysHandler>, Arc<AuthenticationService>) {
let config = configuration::ephemeral_private();
instantiate_keys_manager_and_authentication_with_configuration(&config)
}
fn instantiate_keys_manager_and_authentication_with_checking_keys_expiration_disabled(
) -> (Arc<KeysHandler>, Arc<AuthenticationService>) {
let mut config = configuration::ephemeral_private();
config.core.private_mode = Some(PrivateMode {
check_keys_expiration: false,
});
instantiate_keys_manager_and_authentication_with_configuration(&config)
}
fn instantiate_keys_manager_and_authentication_with_configuration(
config: &Configuration,
) -> (Arc<KeysHandler>, Arc<AuthenticationService>) {
let database = initialize_database(&config.core);
let db_key_repository = Arc::new(DatabaseKeyRepository::new(&database));
let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default());
let authentication_service = Arc::new(service::AuthenticationService::new(&config.core, &in_memory_key_repository));
let keys_handler = Arc::new(KeysHandler::new(
&db_key_repository.clone(),
&in_memory_key_repository.clone(),
));
(keys_handler, authentication_service)
}
#[tokio::test]
async fn it_should_remove_an_authentication_key() {
let (keys_manager, authentication_service) = instantiate_keys_manager_and_authentication();
let expiring_key = keys_manager
.generate_expiring_peer_key(Some(Duration::from_secs(100)))
.await
.unwrap();
let result = keys_manager.remove_peer_key(&expiring_key.key()).await;
assert!(result.is_ok());
// The key should no longer be valid
assert!(authentication_service.authenticate(&expiring_key.key()).await.is_err());
}
#[tokio::test]
async fn it_should_load_authentication_keys_from_the_database() {
let (keys_manager, authentication_service) = instantiate_keys_manager_and_authentication();
let expiring_key = keys_manager
.generate_expiring_peer_key(Some(Duration::from_secs(100)))
.await
.unwrap();
// Remove the newly generated key in memory
keys_manager.remove_in_memory_auth_key(&expiring_key.key()).await;
let result = keys_manager.load_peer_keys_from_database().await;
assert!(result.is_ok());
// The key should no longer be valid
assert!(authentication_service.authenticate(&expiring_key.key()).await.is_ok());
}
mod with_expiring_and {
mod randomly_generated_keys {
use std::time::Duration;
use crate::authentication::tests::the_tracker_configured_as_private::{
instantiate_keys_manager_and_authentication,
instantiate_keys_manager_and_authentication_with_checking_keys_expiration_disabled,
};
use crate::authentication::Key;
#[tokio::test]
async fn it_should_authenticate_a_peer_with_the_key() {
let (keys_manager, authentication_service) = instantiate_keys_manager_and_authentication();
let peer_key = keys_manager
.generate_expiring_peer_key(Some(Duration::from_secs(100)))
.await
.unwrap();
let result = authentication_service.authenticate(&peer_key.key()).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn it_should_accept_an_expired_key_when_checking_expiration_is_disabled_in_configuration() {
let (keys_manager, authentication_service) =
instantiate_keys_manager_and_authentication_with_checking_keys_expiration_disabled();
let past_timestamp = Duration::ZERO;
let peer_key = keys_manager
.add_expiring_peer_key(Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap(), Some(past_timestamp))
.await
.unwrap();
assert!(authentication_service.authenticate(&peer_key.key()).await.is_ok());
}
}
mod pre_generated_keys {
use crate::authentication::handler::AddKeyRequest;
use crate::authentication::tests::the_tracker_configured_as_private::{
instantiate_keys_manager_and_authentication,
instantiate_keys_manager_and_authentication_with_checking_keys_expiration_disabled,
};
use crate::authentication::Key;
#[tokio::test]
async fn it_should_authenticate_a_peer_with_the_key() {
let (keys_manager, authentication_service) = instantiate_keys_manager_and_authentication();
let peer_key = keys_manager
.add_peer_key(AddKeyRequest {
opt_key: Some(Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap().to_string()),
opt_seconds_valid: Some(100),
})
.await
.unwrap();
let result = authentication_service.authenticate(&peer_key.key()).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn it_should_accept_an_expired_key_when_checking_expiration_is_disabled_in_configuration() {
let (keys_manager, authentication_service) =
instantiate_keys_manager_and_authentication_with_checking_keys_expiration_disabled();
let peer_key = keys_manager
.add_peer_key(AddKeyRequest {
opt_key: Some(Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap().to_string()),
opt_seconds_valid: Some(0),
})
.await
.unwrap();
assert!(authentication_service.authenticate(&peer_key.key()).await.is_ok());
}
}
}
mod with_permanent_and {
mod randomly_generated_keys {
use crate::authentication::tests::the_tracker_configured_as_private::instantiate_keys_manager_and_authentication;
#[tokio::test]
async fn it_should_authenticate_a_peer_with_the_key() {
let (keys_manager, authentication_service) = instantiate_keys_manager_and_authentication();
let peer_key = keys_manager.generate_permanent_peer_key().await.unwrap();
let result = authentication_service.authenticate(&peer_key.key()).await;
assert!(result.is_ok());
}
}
mod pre_generated_keys {
use crate::authentication::handler::AddKeyRequest;
use crate::authentication::tests::the_tracker_configured_as_private::instantiate_keys_manager_and_authentication;
use crate::authentication::Key;
#[tokio::test]
async fn it_should_authenticate_a_peer_with_the_key() {
let (keys_manager, authentication_service) = instantiate_keys_manager_and_authentication();
let peer_key = keys_manager
.add_peer_key(AddKeyRequest {
opt_key: Some(Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap().to_string()),
opt_seconds_valid: None,
})
.await
.unwrap();
let result = authentication_service.authenticate(&peer_key.key()).await;
assert!(result.is_ok());
}
}
}
}
}