torrust_tracker/servers/http/v1/handlers/common/
auth.rs

1//! HTTP server authentication error and conversion to
2//! [`responses::error::Error`]
3//! response.
4use std::panic::Location;
5
6use thiserror::Error;
7
8use crate::core::auth;
9use crate::servers::http::v1::responses;
10
11/// Authentication error.
12///
13/// When the tracker is private, the authentication key is required in the URL
14/// path. These are the possible errors that can occur when extracting the key
15/// from the URL path.
16#[derive(Debug, Error)]
17pub enum Error {
18    #[error("Missing authentication key param for private tracker. Error in {location}")]
19    MissingAuthKey { location: &'static Location<'static> },
20    #[error("Invalid format for authentication key param. Error in {location}")]
21    InvalidKeyFormat { location: &'static Location<'static> },
22    #[error("Cannot extract authentication key param from URL path. Error in {location}")]
23    CannotExtractKeyParam { location: &'static Location<'static> },
24}
25
26impl From<Error> for responses::error::Error {
27    fn from(err: Error) -> Self {
28        responses::error::Error {
29            failure_reason: format!("Authentication error: {err}"),
30        }
31    }
32}
33
34impl From<auth::Error> for responses::error::Error {
35    fn from(err: auth::Error) -> Self {
36        responses::error::Error {
37            failure_reason: format!("Authentication error: {err}"),
38        }
39    }
40}