aboutsummaryrefslogtreecommitdiff
path: root/src/auth.rs
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2024-04-08 15:14:21 +0200
committerFxQnLr <[email protected]>2024-04-08 15:14:21 +0200
commit52851787329c48c1e70f98a3610ad52fe1fa4aa4 (patch)
treeaf1a1adeff7cf3fd8f3f1aba093aacf2a337e78d /src/auth.rs
parentd8060da1180545df5d03a76cd2860191ecf87507 (diff)
downloadwebol-52851787329c48c1e70f98a3610ad52fe1fa4aa4.tar
webol-52851787329c48c1e70f98a3610ad52fe1fa4aa4.tar.gz
webol-52851787329c48c1e70f98a3610ad52fe1fa4aa4.zip
Closes #25. Apikey not required anymore
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}