summaryrefslogtreecommitdiff
path: root/src/auth.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/auth.rs')
-rw-r--r--src/auth.rs37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/auth.rs b/src/auth.rs
new file mode 100644
index 0000000..74008b5
--- /dev/null
+++ b/src/auth.rs
@@ -0,0 +1,37 @@
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 {
27 return Err(StatusCode::UNAUTHORIZED);
28 };
29 let response = next.run(request).await;
30 Ok(response)
31 } else {
32 Err(StatusCode::UNAUTHORIZED)
33 }
34 }
35 Methods::None => Ok(next.run(request).await),
36 }
37}