ips/pkg6depotd/src/http/admin.rs
Till Wegmueller d2d1c297cc
Refactor to align with Rust formatting guidelines and enhance code readability.
- Adjusted indentation and line breaks for structs, functions, and method calls to comply with Rust formatting standards.
- Improved error message formatting and consistency across `PkgTreeError` instances.
- Restructured long function arguments and chained calls for clarity and maintainability.
- Simplified conditional statements and loops for better readability.
- No functional changes introduced.
2025-12-22 20:10:17 +01:00

58 lines
1.6 KiB
Rust

use axum::{
Json,
extract::State,
http::{HeaderMap, StatusCode},
response::{IntoResponse, Response},
};
use serde::Serialize;
use std::sync::Arc;
use crate::repo::DepotRepo;
#[derive(Serialize)]
struct HealthResponse {
status: &'static str,
}
pub async fn health(_state: State<Arc<DepotRepo>>) -> impl IntoResponse {
// Basic liveness/readiness for now. Future: include repo checks.
(StatusCode::OK, Json(HealthResponse { status: "ok" }))
}
#[derive(Serialize)]
struct AuthCheckResponse<'a> {
authenticated: bool,
token_present: bool,
subject: Option<&'a str>,
scopes: Vec<&'a str>,
decision: &'static str,
}
/// Admin auth-check endpoint.
/// For now, this is a minimal placeholder that only checks for the presence of a Bearer token.
/// TODO: Validate JWT via OIDC JWKs using configured issuer/jwks_uri and required scopes.
pub async fn auth_check(_state: State<Arc<DepotRepo>>, headers: HeaderMap) -> Response {
let auth = headers
.get(axum::http::header::AUTHORIZATION)
.and_then(|v| v.to_str().ok());
let (authenticated, token_present) = match auth {
Some(h) if h.to_ascii_lowercase().starts_with("bearer ") => (true, true),
Some(_) => (false, true),
None => (false, false),
};
let resp = AuthCheckResponse {
authenticated,
token_present,
subject: None,
scopes: vec![],
decision: if authenticated { "allow" } else { "deny" },
};
let status = if authenticated {
StatusCode::OK
} else {
StatusCode::UNAUTHORIZED
};
(status, Json(resp)).into_response()
}