summaryrefslogtreecommitdiff
path: root/src/handlers/admin.rs
diff options
context:
space:
mode:
authorWriterV2 <primary-v2@protonmail.com>2024-10-27 13:58:51 +0100
committerWriterV2 <primary-v2@protonmail.com>2024-10-27 14:00:34 +0100
commit04e19765aca471fd85199714699aaf827d04608d (patch)
treea2bfa782c1db2a6b783206087309a9eed3c6e9b9 /src/handlers/admin.rs
parente074949d96fe29929e99890b3035dc1a5ba57184 (diff)
Refactor: Organize modules initially
Diffstat (limited to 'src/handlers/admin.rs')
-rw-r--r--src/handlers/admin.rs39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/handlers/admin.rs b/src/handlers/admin.rs
new file mode 100644
index 0000000..349275b
--- /dev/null
+++ b/src/handlers/admin.rs
@@ -0,0 +1,39 @@
+use std::env;
+
+use axum::extract::Request;
+use axum::http::{HeaderMap, StatusCode};
+use axum::middleware::Next;
+use axum::response::Response;
+use subtle::ConstantTimeEq;
+
+pub async fn admin_middleware(
+ headers: HeaderMap,
+ request: Request,
+ next: Next,
+) -> Result<Response, StatusCode> {
+ let expected_token = env::var("TOKEN").map_err(|_| {
+ eprintln!("Error: TOKEN environment variable is not set");
+ StatusCode::INTERNAL_SERVER_ERROR
+ })?;
+
+ let token = headers
+ .get("X-Admin-Token")
+ .and_then(|header| header.to_str().ok())
+ .ok_or(StatusCode::UNAUTHORIZED)?;
+
+ if !expected_token
+ .as_bytes()
+ .ct_eq(token.as_bytes())
+ .unwrap_u8()
+ == 1
+ {
+ eprintln!("Warning: Unauthorized access attempt with invalid token");
+ return Err(StatusCode::UNAUTHORIZED);
+ }
+
+ Ok(next.run(request).await)
+}
+
+pub async fn admin_handler() -> &'static str {
+ "Hello World"
+}