allowthem_server/
error.rs1use axum::http::StatusCode;
2use axum::response::{IntoResponse, Response};
3use serde_json::json;
4
5use allowthem_core::AuthError;
6
7#[derive(Debug)]
12pub enum AuthExtractError {
13 Unauthenticated,
16 Internal(AuthError),
18}
19
20impl IntoResponse for AuthExtractError {
21 fn into_response(self) -> Response {
22 match self {
23 Self::Unauthenticated => (
24 StatusCode::UNAUTHORIZED,
25 axum::Json(json!({"error": "unauthenticated"})),
26 )
27 .into_response(),
28 Self::Internal(err) => {
29 tracing::error!("auth extraction error: {err}");
30 (
31 StatusCode::INTERNAL_SERVER_ERROR,
32 axum::Json(json!({"error": "internal error"})),
33 )
34 .into_response()
35 }
36 }
37 }
38}
39
40#[derive(Debug)]
45pub struct BrowserAuthRedirect(pub(crate) String);
46
47impl BrowserAuthRedirect {
48 pub fn new(path: &str) -> Self {
49 Self(format!("/login?next={path}"))
50 }
51}
52
53impl IntoResponse for BrowserAuthRedirect {
54 fn into_response(self) -> Response {
55 (
56 StatusCode::SEE_OTHER,
57 [(axum::http::header::LOCATION, self.0)],
58 )
59 .into_response()
60 }
61}
62
63pub struct BrowserAdminForbidden;
68
69impl IntoResponse for BrowserAdminForbidden {
70 fn into_response(self) -> Response {
71 (
72 StatusCode::FORBIDDEN,
73 axum::response::Html("<h1>403 Forbidden</h1><p>Admin access required.</p>"),
74 )
75 .into_response()
76 }
77}