From 9058f191b69ecafc8fdeace227ac113412d03888 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Sun, 25 Feb 2024 15:15:19 +0100 Subject: Closes #16. Impl auth as extractor --- src/routes/device.rs | 113 ++++++++++++++++++++------------------------------- src/routes/start.rs | 65 +++++++++++++---------------- 2 files changed, 73 insertions(+), 105 deletions(-) (limited to 'src/routes') diff --git a/src/routes/device.rs b/src/routes/device.rs index 2f0093d..d39d98e 100644 --- a/src/routes/device.rs +++ b/src/routes/device.rs @@ -1,8 +1,6 @@ -use crate::auth::auth; use crate::db::Device; use crate::error::Error; use axum::extract::State; -use axum::http::HeaderMap; use axum::Json; use mac_address::MacAddress; use serde::{Deserialize, Serialize}; @@ -13,31 +11,24 @@ use tracing::{debug, info}; pub async fn get( State(state): State>, - headers: HeaderMap, Json(payload): Json, ) -> Result, Error> { info!("get device {}", payload.id); - let secret = headers.get("authorization"); - let authorized = matches!(auth(&state.config, secret)?, crate::auth::Response::Success); - if authorized { - let device = sqlx::query_as!( - Device, - r#" - SELECT id, mac, broadcast_addr, ip, times - FROM devices - WHERE id = $1; - "#, - payload.id - ) - .fetch_one(&state.db) - .await?; + let device = sqlx::query_as!( + Device, + r#" + SELECT id, mac, broadcast_addr, ip, times + FROM devices + WHERE id = $1; + "#, + payload.id + ) + .fetch_one(&state.db) + .await?; - debug!("got device {:?}", device); + debug!("got device {:?}", device); - Ok(Json(json!(device))) - } else { - Err(Error::Generic) - } + Ok(Json(json!(device))) } #[derive(Deserialize)] @@ -47,7 +38,6 @@ pub struct GetDevicePayload { pub async fn put( State(state): State>, - headers: HeaderMap, Json(payload): Json, ) -> Result, Error> { info!( @@ -55,28 +45,22 @@ pub async fn put( payload.id, payload.mac, payload.broadcast_addr, payload.ip ); - let secret = headers.get("authorization"); - let authorized = matches!(auth(&state.config, secret)?, crate::auth::Response::Success); - if authorized { - let ip = IpNetwork::from_str(&payload.ip)?; - let mac = MacAddress::from_str(&payload.mac)?; - sqlx::query!( - r#" - INSERT INTO devices (id, mac, broadcast_addr, ip) - VALUES ($1, $2, $3, $4); - "#, - payload.id, - mac, - payload.broadcast_addr, - ip - ) - .execute(&state.db) - .await?; + let ip = IpNetwork::from_str(&payload.ip)?; + let mac = MacAddress::from_str(&payload.mac)?; + sqlx::query!( + r#" + INSERT INTO devices (id, mac, broadcast_addr, ip) + VALUES ($1, $2, $3, $4); + "#, + payload.id, + mac, + payload.broadcast_addr, + ip + ) + .execute(&state.db) + .await?; - Ok(Json(json!(PutDeviceResponse { success: true }))) - } else { - Err(Error::Generic) - } + Ok(Json(json!(PutDeviceResponse { success: true }))) } #[derive(Deserialize)] @@ -94,37 +78,30 @@ pub struct PutDeviceResponse { pub async fn post( State(state): State>, - headers: HeaderMap, Json(payload): Json, ) -> Result, Error> { info!( "edit device {} ({}, {}, {})", payload.id, payload.mac, payload.broadcast_addr, payload.ip ); - let secret = headers.get("authorization"); - let authorized = matches!(auth(&state.config, secret)?, crate::auth::Response::Success); - if authorized { - let ip = IpNetwork::from_str(&payload.ip)?; - let mac = MacAddress::from_str(&payload.mac)?; - let device = sqlx::query_as!( - Device, - r#" - UPDATE devices - SET mac = $1, broadcast_addr = $2, ip = $3 WHERE id = $4 - RETURNING id, mac, broadcast_addr, ip, times; - "#, - mac, - payload.broadcast_addr, - ip, - payload.id - ) - .fetch_one(&state.db) - .await?; + let ip = IpNetwork::from_str(&payload.ip)?; + let mac = MacAddress::from_str(&payload.mac)?; + let device = sqlx::query_as!( + Device, + r#" + UPDATE devices + SET mac = $1, broadcast_addr = $2, ip = $3 WHERE id = $4 + RETURNING id, mac, broadcast_addr, ip, times; + "#, + mac, + payload.broadcast_addr, + ip, + payload.id + ) + .fetch_one(&state.db) + .await?; - Ok(Json(json!(device))) - } else { - Err(Error::Generic) - } + Ok(Json(json!(device))) } #[derive(Deserialize)] diff --git a/src/routes/start.rs b/src/routes/start.rs index 4888325..d4c0802 100644 --- a/src/routes/start.rs +++ b/src/routes/start.rs @@ -1,10 +1,8 @@ -use crate::auth::auth; use crate::db::Device; use crate::error::Error; use crate::services::ping::Value as PingValue; use crate::wol::{create_buffer, send_packet}; use axum::extract::State; -use axum::http::HeaderMap; use axum::Json; use serde::{Deserialize, Serialize}; use serde_json::{json, Value}; @@ -14,48 +12,41 @@ use uuid::Uuid; pub async fn start( State(state): State>, - headers: HeaderMap, Json(payload): Json, ) -> Result, Error> { info!("POST request"); - let secret = headers.get("authorization"); - let authorized = matches!(auth(&state.config, secret)?, crate::auth::Response::Success); - if authorized { - let device = sqlx::query_as!( - Device, - r#" - SELECT id, mac, broadcast_addr, ip, times - FROM devices - WHERE id = $1; - "#, - payload.id - ) - .fetch_one(&state.db) - .await?; + let device = sqlx::query_as!( + Device, + r#" + SELECT id, mac, broadcast_addr, ip, times + FROM devices + WHERE id = $1; + "#, + payload.id + ) + .fetch_one(&state.db) + .await?; - info!("starting {}", device.id); + info!("starting {}", device.id); - let bind_addr = "0.0.0.0:0"; + let bind_addr = "0.0.0.0:0"; - let _ = send_packet( - bind_addr, - &device.broadcast_addr, - &create_buffer(&device.mac.to_string())?, - )?; - let dev_id = device.id.clone(); - let uuid = if payload.ping.is_some_and(|ping| ping) { - Some(setup_ping(state, device)) - } else { - None - }; - Ok(Json(json!(Response { - id: dev_id, - boot: true, - uuid - }))) + let _ = send_packet( + bind_addr, + &device.broadcast_addr, + &create_buffer(&device.mac.to_string())?, + )?; + let dev_id = device.id.clone(); + let uuid = if payload.ping.is_some_and(|ping| ping) { + Some(setup_ping(state, device)) } else { - Err(Error::Generic) - } + None + }; + Ok(Json(json!(Response { + id: dev_id, + boot: true, + uuid + }))) } fn setup_ping(state: Arc, device: Device) -> String { -- cgit v1.2.3