-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): [#143] authentication with GET param for Axum API
It keeps the same contract of the API. It returns 500 status code with error message in "debug" format.
- Loading branch information
1 parent
1c6db6e
commit 43dbed9
Showing
7 changed files
with
111 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ | |
"leechers", | ||
"libtorrent", | ||
"Lphant", | ||
"middlewares", | ||
"mockall", | ||
"myacicontext", | ||
"nanos", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use std::sync::Arc; | ||
|
||
use axum::extract::{Query, State}; | ||
use axum::http::{header, Request, StatusCode}; | ||
use axum::middleware::Next; | ||
use axum::response::{IntoResponse, Response}; | ||
use serde::Deserialize; | ||
|
||
use crate::config::{Configuration, HttpApi}; | ||
|
||
#[derive(Deserialize, Debug)] | ||
pub struct QueryParams { | ||
pub token: Option<String>, | ||
} | ||
|
||
/// Middleware for authentication using a "token" GET param. | ||
/// The token must be one of the tokens in the tracker HTTP API configuration. | ||
pub async fn auth<B>( | ||
State(config): State<Arc<Configuration>>, | ||
Query(params): Query<QueryParams>, | ||
request: Request<B>, | ||
next: Next<B>, | ||
) -> Response | ||
where | ||
B: Send, | ||
{ | ||
let token = match params.token { | ||
None => return AuthError::Unauthorized.into_response(), | ||
Some(token) => token, | ||
}; | ||
|
||
if !authenticate(&token, &config.http_api) { | ||
return AuthError::TokenNotValid.into_response(); | ||
} | ||
|
||
next.run(request).await | ||
} | ||
|
||
enum AuthError { | ||
Unauthorized, | ||
TokenNotValid, | ||
} | ||
|
||
impl IntoResponse for AuthError { | ||
fn into_response(self) -> Response { | ||
let body = match self { | ||
AuthError::Unauthorized => "Unhandled rejection: Err { reason: \"unauthorized\" }", | ||
AuthError::TokenNotValid => "Unhandled rejection: Err { reason: \"token not valid\" }", | ||
}; | ||
|
||
( | ||
StatusCode::INTERNAL_SERVER_ERROR, | ||
[(header::CONTENT_TYPE, "text/plain")], | ||
body, | ||
) | ||
.into_response() | ||
} | ||
} | ||
|
||
fn authenticate(token: &str, http_api_config: &HttpApi) -> bool { | ||
http_api_config.contains_token(token) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod auth; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod middlewares; | ||
pub mod routes; | ||
pub mod server; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters