summaryrefslogtreecommitdiff
path: root/src/auth.rs
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2024-04-09 22:07:10 +0200
committerGitHub <[email protected]>2024-04-09 22:07:10 +0200
commit907e5cb5bc48899b444f7fedd85af7b5974d9a2e (patch)
tree7206da6c47e6c9e2da0982344c8f76f1385e0ae2 /src/auth.rs
parent8dca7e83519b6c3531653cdedf60b2a14e1035b7 (diff)
parentd91edaf85a1a179a915ac99e7c17b5647d8d9f7d (diff)
downloadwebol-907e5cb5bc48899b444f7fedd85af7b5974d9a2e.tar
webol-907e5cb5bc48899b444f7fedd85af7b5974d9a2e.tar.gz
webol-907e5cb5bc48899b444f7fedd85af7b5974d9a2e.zip
Merge pull request #28 from FxQnLr/0.3.4
0.3.4
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}