From 52851787329c48c1e70f98a3610ad52fe1fa4aa4 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Mon, 8 Apr 2024 15:14:21 +0200 Subject: Closes #25. Apikey not required anymore --- src/auth.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/auth.rs (limited to 'src/auth.rs') 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 @@ +use crate::AppState; +use axum::{ + extract::{Request, State}, + http::{HeaderMap, StatusCode}, + middleware::Next, + response::Response, +}; +use serde::Deserialize; + +#[derive(Debug, Clone, Deserialize)] +pub enum Methods { + Key, + None, +} + +pub async fn auth( + State(state): State, + headers: HeaderMap, + request: Request, + next: Next, +) -> Result { + let auth = state.config.auth; + match auth.method { + Methods::Key => { + if let Some(secret) = headers.get("authorization") { + if !(auth.secret.as_str() == secret) { return Err(StatusCode::UNAUTHORIZED); }; + let response = next.run(request).await; + Ok(response) + } else { + return Err(StatusCode::UNAUTHORIZED); + } + } + Methods::None => Ok(next.run(request).await), + } +} -- cgit v1.2.3 From a91a2ca5c88403e905bf0f798393587fc4d900fa Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Mon, 8 Apr 2024 15:44:31 +0200 Subject: Closes #26. Addtional GET request for /start --- src/auth.rs | 6 ++++-- src/main.rs | 6 ++++-- src/routes/start.rs | 32 +++++++++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 7 deletions(-) (limited to 'src/auth.rs') diff --git a/src/auth.rs b/src/auth.rs index 1f4518a..74008b5 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -23,11 +23,13 @@ pub async fn auth( match auth.method { Methods::Key => { if let Some(secret) = headers.get("authorization") { - if !(auth.secret.as_str() == secret) { return Err(StatusCode::UNAUTHORIZED); }; + if auth.secret.as_str() != secret { + return Err(StatusCode::UNAUTHORIZED); + }; let response = next.run(request).await; Ok(response) } else { - return Err(StatusCode::UNAUTHORIZED); + Err(StatusCode::UNAUTHORIZED) } } Methods::None => Ok(next.run(request).await), diff --git a/src/main.rs b/src/main.rs index 43957ff..a8acc5f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,7 +37,8 @@ mod wol; #[derive(OpenApi)] #[openapi( paths( - start::start, + start::post, + start::get, start::start_payload, device::get, device::get_payload, @@ -119,13 +120,14 @@ async fn main() -> color_eyre::eyre::Result<()> { let app = Router::new() .route("/start", post(start::start_payload)) - .route("/start/:id", post(start::start)) + .route("/start/:id", post(start::post).get(start::get)) .route( "/device", post(device::post).get(device::get_payload).put(device::put), ) .route("/device/:id", get(device::get)) .route("/status", get(status::status)) + // TODO: Don't load on `None` Auth .route_layer(from_fn_with_state(shared_state.clone(), auth::auth)) .merge(SwaggerUi::new("/swagger-ui").url("/api-docs/openapi.json", ApiDoc::openapi())) .with_state(Arc::new(shared_state)); diff --git a/src/routes/start.rs b/src/routes/start.rs index c61d5a3..e74a943 100644 --- a/src/routes/start.rs +++ b/src/routes/start.rs @@ -18,7 +18,7 @@ use uuid::Uuid; responses( (status = 200, description = "List matching todos by query", body = [Response]) ), - security(("api_key" = [])) + security((), ("api_key" = [])) )] #[deprecated] pub async fn start_payload( @@ -70,12 +70,38 @@ pub async fn start_payload( params( ("id" = String, Path, description = "Device id") ), - security(("api_key" = [])) + security((), ("api_key" = [])) )] -pub async fn start( +pub async fn post( State(state): State>, Path(id): Path, payload: Option>, +) -> Result, Error> { + send_wol(state, &id, payload).await +} + +#[utoipa::path( + get, + path = "/start/{id}", + responses( + (status = 200, description = "Start the device with the given id", body = [Response]) + ), + params( + ("id" = String, Path, description = "Device id") + ), + security((), ("api_key" = [])) +)] +pub async fn get( + State(state): State>, + Path(id): Path, +) -> Result, Error> { + send_wol(state, &id, None).await +} + +async fn send_wol( + state: Arc, + id: &str, + payload: Option>, ) -> Result, Error> { info!("Start request for {id}"); let device = sqlx::query_as!( -- cgit v1.2.3