Skip to content

Commit

Permalink
refactor(http): [#200] move function to Tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Mar 1, 2023
1 parent d5e52ec commit ea24973
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/http/axum_implementation/handlers/announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
Expand Down
15 changes: 1 addition & 14 deletions src/http/axum_implementation/handlers/auth.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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<Tracker>) -> Result<(), auth::Error> {
if tracker.is_private() {
tracker.verify_auth_key(key_id).await
} else {
Ok(())
}
}

impl From<Error> for responses::error::Error {
fn from(err: Error) -> Self {
responses::error::Error {
Expand Down
2 changes: 1 addition & 1 deletion src/http/axum_implementation/handlers/scrape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
11 changes: 11 additions & 0 deletions src/tracker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ea24973

Please sign in to comment.