|
1 | 1 | use std::sync::Arc; |
2 | 2 |
|
3 | 3 | use axum::extract::{Query, State}; |
4 | | -use axum::http::{header, Request, StatusCode}; |
| 4 | +use axum::http::Request; |
5 | 5 | use axum::middleware::Next; |
6 | 6 | use axum::response::{IntoResponse, Response}; |
7 | 7 | use serde::Deserialize; |
8 | 8 |
|
| 9 | +use crate::apis::responses::unhandled_rejection_response; |
9 | 10 | use crate::config::{Configuration, HttpApi}; |
10 | 11 |
|
11 | 12 | #[derive(Deserialize, Debug)] |
@@ -43,20 +44,23 @@ enum AuthError { |
43 | 44 |
|
44 | 45 | impl IntoResponse for AuthError { |
45 | 46 | fn into_response(self) -> Response { |
46 | | - let body = match self { |
47 | | - AuthError::Unauthorized => "Unhandled rejection: Err { reason: \"unauthorized\" }", |
48 | | - AuthError::TokenNotValid => "Unhandled rejection: Err { reason: \"token not valid\" }", |
49 | | - }; |
50 | | - |
51 | | - ( |
52 | | - StatusCode::INTERNAL_SERVER_ERROR, |
53 | | - [(header::CONTENT_TYPE, "text/plain; charset=utf-8")], |
54 | | - body, |
55 | | - ) |
56 | | - .into_response() |
| 47 | + match self { |
| 48 | + AuthError::Unauthorized => unauthorized_response(), |
| 49 | + AuthError::TokenNotValid => token_not_valid_response(), |
| 50 | + } |
57 | 51 | } |
58 | 52 | } |
59 | 53 |
|
60 | 54 | fn authenticate(token: &str, http_api_config: &HttpApi) -> bool { |
61 | 55 | http_api_config.contains_token(token) |
62 | 56 | } |
| 57 | + |
| 58 | +#[must_use] |
| 59 | +pub fn unauthorized_response() -> Response { |
| 60 | + unhandled_rejection_response("unauthorized".to_string()) |
| 61 | +} |
| 62 | + |
| 63 | +#[must_use] |
| 64 | +pub fn token_not_valid_response() -> Response { |
| 65 | + unhandled_rejection_response("token not valid".to_string()) |
| 66 | +} |
0 commit comments