From ea249733f4f74ae1c35e47fda965d97b83681f11 Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Wed, 1 Mar 2023 11:20:20 +0000 Subject: [PATCH] refactor(http): [#200] move function to Tracker --- src/http/axum_implementation/handlers/announce.rs | 2 +- src/http/axum_implementation/handlers/auth.rs | 15 +-------------- src/http/axum_implementation/handlers/scrape.rs | 2 +- src/tracker/mod.rs | 11 +++++++++++ 4 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/http/axum_implementation/handlers/announce.rs b/src/http/axum_implementation/handlers/announce.rs index b9b964605..9f39a5bdf 100644 --- a/src/http/axum_implementation/handlers/announce.rs +++ b/src/http/axum_implementation/handlers/announce.rs @@ -55,7 +55,7 @@ pub async fn handle_with_key( .into_response() }; - match auth::authenticate(&key_id, &tracker).await { + match tracker.authenticate(&key_id).await { Ok(_) => (), Err(error) => return responses::error::Error::from(error).into_response(), } diff --git a/src/http/axum_implementation/handlers/auth.rs b/src/http/axum_implementation/handlers/auth.rs index 3b9aebc23..366526664 100644 --- a/src/http/axum_implementation/handlers/auth.rs +++ b/src/http/axum_implementation/handlers/auth.rs @@ -1,12 +1,10 @@ use std::panic::Location; -use std::sync::Arc; use serde::Deserialize; use thiserror::Error; use crate::http::axum_implementation::responses; -use crate::tracker::auth::{self, KeyId}; -use crate::tracker::Tracker; +use crate::tracker::auth; #[derive(Deserialize)] pub struct KeyIdParam(String); @@ -26,17 +24,6 @@ pub enum Error { InvalidKeyFormat { location: &'static Location<'static> }, } -/// # Errors -/// -/// Will return an error if the the authentication key cannot be verified. -pub async fn authenticate(key_id: &KeyId, tracker: &Arc) -> Result<(), auth::Error> { - if tracker.is_private() { - tracker.verify_auth_key(key_id).await - } else { - Ok(()) - } -} - impl From for responses::error::Error { fn from(err: Error) -> Self { responses::error::Error { diff --git a/src/http/axum_implementation/handlers/scrape.rs b/src/http/axum_implementation/handlers/scrape.rs index 814cdbfa4..6edf2fdb8 100644 --- a/src/http/axum_implementation/handlers/scrape.rs +++ b/src/http/axum_implementation/handlers/scrape.rs @@ -47,7 +47,7 @@ pub async fn handle_with_key( .into_response() }; - match auth::authenticate(&key_id, &tracker).await { + match tracker.authenticate(&key_id).await { Ok(_) => (), Err(_) => return handle_fake_scrape(&tracker, &scrape_request, &remote_client_ip).await, } diff --git a/src/tracker/mod.rs b/src/tracker/mod.rs index 2604c5045..31eeef6dc 100644 --- a/src/tracker/mod.rs +++ b/src/tracker/mod.rs @@ -334,6 +334,17 @@ impl Tracker { Ok(()) } + /// # Errors + /// + /// Will return an error if the the authentication key cannot be verified. + pub async fn authenticate(&self, key_id: &KeyId) -> Result<(), auth::Error> { + if self.is_private() { + self.verify_auth_key(key_id).await + } else { + Ok(()) + } + } + /// Loading the torrents from database into memory /// /// # Errors