Skip to content

Commit 747b608

Browse files
committed
refactor: [torrust#1195] use AuthenticationService directy
Instead of via the authentication::Facade. The Facade will be removed.
1 parent 457d01b commit 747b608

File tree

18 files changed

+227
-65
lines changed

18 files changed

+227
-65
lines changed

src/app.rs

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ pub async fn start(config: &Configuration, app_container: &AppContainer) -> Vec<
100100
if let Some(job) = http_tracker::start_job(
101101
http_tracker_config,
102102
app_container.tracker.clone(),
103+
app_container.authentication_service.clone(),
103104
app_container.whitelist_authorization.clone(),
104105
app_container.stats_event_sender.clone(),
105106
registar.give_form(),

src/app_test.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use torrust_tracker_configuration::Configuration;
66
use crate::core::authentication::handler::KeysHandler;
77
use crate::core::authentication::key::repository::in_memory::InMemoryKeyRepository;
88
use crate::core::authentication::key::repository::persisted::DatabaseKeyRepository;
9-
use crate::core::authentication::service;
9+
use crate::core::authentication::service::{self, AuthenticationService};
1010
use crate::core::databases::Database;
1111
use crate::core::services::initialize_database;
1212
use crate::core::whitelist::repository::in_memory::InMemoryWhitelist;
@@ -22,6 +22,7 @@ pub fn initialize_tracker_dependencies(
2222
Arc<InMemoryWhitelist>,
2323
Arc<whitelist::authorization::Authorization>,
2424
Arc<authentication::Facade>,
25+
Arc<AuthenticationService>,
2526
) {
2627
let database = initialize_database(config);
2728
let in_memory_whitelist = Arc::new(InMemoryWhitelist::default());
@@ -36,7 +37,13 @@ pub fn initialize_tracker_dependencies(
3637
&db_key_repository.clone(),
3738
&in_memory_key_repository.clone(),
3839
));
39-
let authentication = Arc::new(authentication::Facade::new(&authentication_service, &keys_handler));
40+
let authentication_facade = Arc::new(authentication::Facade::new(&authentication_service, &keys_handler));
4041

41-
(database, in_memory_whitelist, whitelist_authorization, authentication)
42+
(
43+
database,
44+
in_memory_whitelist,
45+
whitelist_authorization,
46+
authentication_facade,
47+
authentication_service,
48+
)
4249
}

src/bootstrap/app.rs

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ pub fn initialize_app_container(configuration: &Configuration) -> AppContainer {
114114

115115
AppContainer {
116116
tracker,
117+
authentication_service,
117118
whitelist_authorization,
118119
ban_service,
119120
stats_event_sender,

src/bootstrap/jobs/http_tracker.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use torrust_tracker_configuration::HttpTracker;
1919
use tracing::instrument;
2020

2121
use super::make_rust_tls;
22+
use crate::core::authentication::service::AuthenticationService;
2223
use crate::core::statistics::event::sender::Sender;
2324
use crate::core::{self, statistics, whitelist};
2425
use crate::servers::http::server::{HttpServer, Launcher};
@@ -34,10 +35,11 @@ use crate::servers::registar::ServiceRegistrationForm;
3435
///
3536
/// It would panic if the `config::HttpTracker` struct would contain inappropriate values.
3637
///
37-
#[instrument(skip(config, tracker, whitelist_authorization, stats_event_sender, form))]
38+
#[instrument(skip(config, tracker, authentication_service, whitelist_authorization, stats_event_sender, form))]
3839
pub async fn start_job(
3940
config: &HttpTracker,
4041
tracker: Arc<core::Tracker>,
42+
authentication_service: Arc<AuthenticationService>,
4143
whitelist_authorization: Arc<whitelist::authorization::Authorization>,
4244
stats_event_sender: Arc<Option<Box<dyn Sender>>>,
4345
form: ServiceRegistrationForm,
@@ -55,6 +57,7 @@ pub async fn start_job(
5557
socket,
5658
tls,
5759
tracker.clone(),
60+
authentication_service.clone(),
5861
whitelist_authorization.clone(),
5962
stats_event_sender.clone(),
6063
form,
@@ -70,12 +73,19 @@ async fn start_v1(
7073
socket: SocketAddr,
7174
tls: Option<RustlsConfig>,
7275
tracker: Arc<core::Tracker>,
76+
authentication_service: Arc<AuthenticationService>,
7377
whitelist_authorization: Arc<whitelist::authorization::Authorization>,
7478
stats_event_sender: Arc<Option<Box<dyn statistics::event::sender::Sender>>>,
7579
form: ServiceRegistrationForm,
7680
) -> JoinHandle<()> {
7781
let server = HttpServer::new(Launcher::new(socket, tls))
78-
.start(tracker, whitelist_authorization, stats_event_sender, form)
82+
.start(
83+
tracker,
84+
authentication_service,
85+
whitelist_authorization,
86+
stats_event_sender,
87+
form,
88+
)
7989
.await
8090
.expect("it should be able to start to the http tracker");
8191

@@ -143,6 +153,7 @@ mod tests {
143153
start_job(
144154
config,
145155
tracker,
156+
authentication_service,
146157
whitelist_authorization,
147158
stats_event_sender,
148159
Registar::default().give_form(),

src/container.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::sync::Arc;
22

33
use tokio::sync::RwLock;
44

5+
use crate::core::authentication::service::AuthenticationService;
56
use crate::core::statistics::event::sender::Sender;
67
use crate::core::statistics::repository::Repository;
78
use crate::core::whitelist::manager::WhiteListManager;
@@ -10,6 +11,7 @@ use crate::servers::udp::server::banning::BanService;
1011

1112
pub struct AppContainer {
1213
pub tracker: Arc<Tracker>,
14+
pub authentication_service: Arc<AuthenticationService>,
1315
pub whitelist_authorization: Arc<whitelist::authorization::Authorization>,
1416
pub ban_service: Arc<RwLock<BanService>>,
1517
pub stats_event_sender: Arc<Option<Box<dyn Sender>>>,

src/core/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ mod tests {
816816
fn public_tracker() -> Tracker {
817817
let config = configuration::ephemeral_public();
818818

819-
let (database, _in_memory_whitelist, whitelist_authorization, authentication) =
819+
let (database, _in_memory_whitelist, whitelist_authorization, authentication, _authentication_service) =
820820
initialize_tracker_dependencies(&config);
821821

822822
initialize_tracker(&config, &database, &whitelist_authorization, &authentication)
@@ -825,7 +825,7 @@ mod tests {
825825
fn whitelisted_tracker() -> (Tracker, Arc<whitelist::authorization::Authorization>, Arc<WhiteListManager>) {
826826
let config = configuration::ephemeral_listed();
827827

828-
let (database, in_memory_whitelist, whitelist_authorization, authentication) =
828+
let (database, in_memory_whitelist, whitelist_authorization, authentication, _authentication_service) =
829829
initialize_tracker_dependencies(&config);
830830

831831
let whitelist_manager = initialize_whitelist_manager(database.clone(), in_memory_whitelist.clone());
@@ -839,7 +839,7 @@ mod tests {
839839
let mut config = configuration::ephemeral_listed();
840840
config.core.tracker_policy.persistent_torrent_completed_stat = true;
841841

842-
let (database, _in_memory_whitelist, whitelist_authorization, authentication) =
842+
let (database, _in_memory_whitelist, whitelist_authorization, authentication, _authentication_service) =
843843
initialize_tracker_dependencies(&config);
844844

845845
initialize_tracker(&config, &database, &whitelist_authorization, &authentication)

src/core/services/statistics/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ mod tests {
132132
async fn the_statistics_service_should_return_the_tracker_metrics() {
133133
let config = tracker_configuration();
134134

135-
let (database, _in_memory_whitelist, whitelist_authorization, authentication) = initialize_tracker_dependencies(&config);
135+
let (database, _in_memory_whitelist, whitelist_authorization, authentication, _authentication_service) =
136+
initialize_tracker_dependencies(&config);
136137
let (_stats_event_sender, stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics);
137138
let stats_repository = Arc::new(stats_repository);
138139

src/core/services/torrent.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ mod tests {
142142
async fn should_return_none_if_the_tracker_does_not_have_the_torrent() {
143143
let config = tracker_configuration();
144144

145-
let (database, _in_memory_whitelist, whitelist_authorization, authentication) =
145+
let (database, _in_memory_whitelist, whitelist_authorization, authentication, _authentication_service) =
146146
initialize_tracker_dependencies(&config);
147147

148148
let tracker = initialize_tracker(&config, &database, &whitelist_authorization, &authentication);
@@ -162,7 +162,7 @@ mod tests {
162162
async fn should_return_the_torrent_info_if_the_tracker_has_the_torrent() {
163163
let config = tracker_configuration();
164164

165-
let (database, _in_memory_whitelist, whitelist_authorization, authentication) =
165+
let (database, _in_memory_whitelist, whitelist_authorization, authentication, _authentication_service) =
166166
initialize_tracker_dependencies(&config);
167167

168168
let tracker = Arc::new(initialize_tracker(
@@ -213,7 +213,7 @@ mod tests {
213213
async fn should_return_an_empty_result_if_the_tracker_does_not_have_any_torrent() {
214214
let config = tracker_configuration();
215215

216-
let (database, _in_memory_whitelist, whitelist_authorization, authentication) =
216+
let (database, _in_memory_whitelist, whitelist_authorization, authentication, _authentication_service) =
217217
initialize_tracker_dependencies(&config);
218218

219219
let tracker = Arc::new(initialize_tracker(
@@ -232,7 +232,7 @@ mod tests {
232232
async fn should_return_a_summarized_info_for_all_torrents() {
233233
let config = tracker_configuration();
234234

235-
let (database, _in_memory_whitelist, whitelist_authorization, authentication) =
235+
let (database, _in_memory_whitelist, whitelist_authorization, authentication, _authentication_service) =
236236
initialize_tracker_dependencies(&config);
237237

238238
let tracker = Arc::new(initialize_tracker(
@@ -264,7 +264,7 @@ mod tests {
264264
async fn should_allow_limiting_the_number_of_torrents_in_the_result() {
265265
let config = tracker_configuration();
266266

267-
let (database, _in_memory_whitelist, whitelist_authorization, authentication) =
267+
let (database, _in_memory_whitelist, whitelist_authorization, authentication, _authentication_service) =
268268
initialize_tracker_dependencies(&config);
269269

270270
let tracker = Arc::new(initialize_tracker(
@@ -294,7 +294,7 @@ mod tests {
294294
async fn should_allow_using_pagination_in_the_result() {
295295
let config = tracker_configuration();
296296

297-
let (database, _in_memory_whitelist, whitelist_authorization, authentication) =
297+
let (database, _in_memory_whitelist, whitelist_authorization, authentication, _authentication_service) =
298298
initialize_tracker_dependencies(&config);
299299

300300
let tracker = Arc::new(initialize_tracker(
@@ -333,7 +333,7 @@ mod tests {
333333
async fn should_return_torrents_ordered_by_info_hash() {
334334
let config = tracker_configuration();
335335

336-
let (database, _in_memory_whitelist, whitelist_authorization, authentication) =
336+
let (database, _in_memory_whitelist, whitelist_authorization, authentication, _authentication_service) =
337337
initialize_tracker_dependencies(&config);
338338

339339
let tracker = Arc::new(initialize_tracker(

src/servers/http/server.rs

+34-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use tracing::instrument;
1111

1212
use super::v1::routes::router;
1313
use crate::bootstrap::jobs::Started;
14+
use crate::core::authentication::service::AuthenticationService;
1415
use crate::core::{statistics, whitelist, Tracker};
1516
use crate::servers::custom_axum_server::{self, TimeoutAcceptor};
1617
use crate::servers::http::HTTP_TRACKER_LOG_TARGET;
@@ -42,10 +43,19 @@ pub struct Launcher {
4243
}
4344

4445
impl Launcher {
45-
#[instrument(skip(self, tracker, whitelist_authorization, stats_event_sender, tx_start, rx_halt))]
46+
#[instrument(skip(
47+
self,
48+
tracker,
49+
authentication_service,
50+
whitelist_authorization,
51+
stats_event_sender,
52+
tx_start,
53+
rx_halt
54+
))]
4655
fn start(
4756
&self,
4857
tracker: Arc<Tracker>,
58+
authentication_service: Arc<AuthenticationService>,
4959
whitelist_authorization: Arc<whitelist::authorization::Authorization>,
5060
stats_event_sender: Arc<Option<Box<dyn statistics::event::sender::Sender>>>,
5161
tx_start: Sender<Started>,
@@ -67,7 +77,13 @@ impl Launcher {
6777

6878
tracing::info!(target: HTTP_TRACKER_LOG_TARGET, "Starting on: {protocol}://{}", address);
6979

70-
let app = router(tracker, whitelist_authorization, stats_event_sender, address);
80+
let app = router(
81+
tracker,
82+
authentication_service,
83+
whitelist_authorization,
84+
stats_event_sender,
85+
address,
86+
);
7187

7288
let running = Box::pin(async {
7389
match tls {
@@ -163,6 +179,7 @@ impl HttpServer<Stopped> {
163179
pub async fn start(
164180
self,
165181
tracker: Arc<Tracker>,
182+
authentication_service: Arc<AuthenticationService>,
166183
whitelist_authorization: Arc<whitelist::authorization::Authorization>,
167184
stats_event_sender: Arc<Option<Box<dyn statistics::event::sender::Sender>>>,
168185
form: ServiceRegistrationForm,
@@ -173,7 +190,14 @@ impl HttpServer<Stopped> {
173190
let launcher = self.state.launcher;
174191

175192
let task = tokio::spawn(async move {
176-
let server = launcher.start(tracker, whitelist_authorization, stats_event_sender, tx_start, rx_halt);
193+
let server = launcher.start(
194+
tracker,
195+
authentication_service,
196+
whitelist_authorization,
197+
stats_event_sender,
198+
tx_start,
199+
rx_halt,
200+
);
177201

178202
server.await;
179203

@@ -296,7 +320,13 @@ mod tests {
296320

297321
let stopped = HttpServer::new(Launcher::new(bind_to, tls));
298322
let started = stopped
299-
.start(tracker, whitelist_authorization, stats_event_sender, register.give_form())
323+
.start(
324+
tracker,
325+
authentication_service,
326+
whitelist_authorization,
327+
stats_event_sender,
328+
register.give_form(),
329+
)
300330
.await
301331
.expect("it should start the server");
302332
let stopped = started.stop().await.expect("it should stop the server");

0 commit comments

Comments
 (0)