summaryrefslogtreecommitdiff
path: root/src/auth.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/auth.rs')
-rw-r--r--src/auth.rs35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/auth.rs b/src/auth.rs
new file mode 100644
index 0000000..1f4518a
--- /dev/null
+++ b/src/auth.rs
@@ -0,0 +1,35 @@
1use crate::AppState;
2use axum::{
3 extract::{Request, State},
4 http::{HeaderMap, StatusCode},
5 middleware::Next,
6 response::Response,
7};
8use serde::Deserialize;
9
10#[derive(Debug, Clone, Deserialize)]
11pub enum Methods {
12 Key,
13 None,
14}
15
16pub async fn auth(
17 State(state): State<AppState>,
18 headers: HeaderMap,
19 request: Request,
20 next: Next,
21) -> Result<Response, StatusCode> {
22 let auth = state.config.auth;
23 match auth.method {
24 Methods::Key => {
25 if let Some(secret) = headers.get("authorization") {
26 if !(auth.secret.as_str() == secret) { return Err(StatusCode::UNAUTHORIZED); };
27 let response = next.run(request).await;
28 Ok(response)
29 } else {
30 return Err(StatusCode::UNAUTHORIZED);
31 }
32 }
33 Methods::None => Ok(next.run(request).await),
34 }
35}