summaryrefslogtreecommitdiff
path: root/src
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
parentd8060da1180545df5d03a76cd2860191ecf87507 (diff)
downloadwebol-52851787329c48c1e70f98a3610ad52fe1fa4aa4.tar
webol-52851787329c48c1e70f98a3610ad52fe1fa4aa4.tar.gz
webol-52851787329c48c1e70f98a3610ad52fe1fa4aa4.zip
Closes #25. Apikey not required anymore
Diffstat (limited to 'src')
-rw-r--r--src/auth.rs35
-rw-r--r--src/config.rs10
-rw-r--r--src/extractors.rs24
-rw-r--r--src/main.rs4
-rw-r--r--src/routes/start.rs2
5 files changed, 47 insertions, 28 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}
diff --git a/src/config.rs b/src/config.rs
index 9605361..9636af4 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -1,14 +1,22 @@
1use config::File; 1use config::File;
2use serde::Deserialize; 2use serde::Deserialize;
3 3
4use crate::auth;
5
4#[derive(Debug, Clone, Deserialize)] 6#[derive(Debug, Clone, Deserialize)]
5pub struct Config { 7pub struct Config {
6 pub database_url: String, 8 pub database_url: String,
7 pub apikey: String,
8 pub serveraddr: String, 9 pub serveraddr: String,
9 pub pingtimeout: i64, 10 pub pingtimeout: i64,
10 pub pingthreshold: i64, 11 pub pingthreshold: i64,
11 pub timeoffset: i8, 12 pub timeoffset: i8,
13 pub auth: Auth,
14}
15
16#[derive(Debug, Clone, Deserialize)]
17pub struct Auth {
18 pub method: auth::Methods,
19 pub secret: String,
12} 20}
13 21
14impl Config { 22impl Config {
diff --git a/src/extractors.rs b/src/extractors.rs
deleted file mode 100644
index 4d441e9..0000000
--- a/src/extractors.rs
+++ /dev/null
@@ -1,24 +0,0 @@
1use axum::{
2 extract::{Request, State},
3 http::{HeaderMap, StatusCode},
4 middleware::Next,
5 response::Response,
6};
7
8use crate::AppState;
9
10pub async fn auth(
11 State(state): State<AppState>,
12 headers: HeaderMap,
13 request: Request,
14 next: Next,
15) -> Result<Response, StatusCode> {
16 let secret = headers.get("authorization");
17 match secret {
18 Some(token) if token == state.config.apikey.as_str() => {
19 let response = next.run(request).await;
20 Ok(response)
21 }
22 _ => Err(StatusCode::UNAUTHORIZED),
23 }
24}
diff --git a/src/main.rs b/src/main.rs
index 75f491a..43957ff 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -29,7 +29,7 @@ use utoipa_swagger_ui::SwaggerUi;
29mod config; 29mod config;
30mod db; 30mod db;
31mod error; 31mod error;
32mod extractors; 32mod auth;
33mod routes; 33mod routes;
34mod services; 34mod services;
35mod wol; 35mod wol;
@@ -126,7 +126,7 @@ async fn main() -> color_eyre::eyre::Result<()> {
126 ) 126 )
127 .route("/device/:id", get(device::get)) 127 .route("/device/:id", get(device::get))
128 .route("/status", get(status::status)) 128 .route("/status", get(status::status))
129 .route_layer(from_fn_with_state(shared_state.clone(), extractors::auth)) 129 .route_layer(from_fn_with_state(shared_state.clone(), auth::auth))
130 .merge(SwaggerUi::new("/swagger-ui").url("/api-docs/openapi.json", ApiDoc::openapi())) 130 .merge(SwaggerUi::new("/swagger-ui").url("/api-docs/openapi.json", ApiDoc::openapi()))
131 .with_state(Arc::new(shared_state)); 131 .with_state(Arc::new(shared_state));
132 132
diff --git a/src/routes/start.rs b/src/routes/start.rs
index fa226d8..c61d5a3 100644
--- a/src/routes/start.rs
+++ b/src/routes/start.rs
@@ -63,7 +63,7 @@ pub async fn start_payload(
63#[utoipa::path( 63#[utoipa::path(
64 post, 64 post,
65 path = "/start/{id}", 65 path = "/start/{id}",
66 request_body = Payload, 66 request_body = Option<Payload>,
67 responses( 67 responses(
68 (status = 200, description = "Start the device with the given id", body = [Response]) 68 (status = 200, description = "Start the device with the given id", body = [Response])
69 ), 69 ),