From 016fa3a31f8847d3f52800941b7f8fe5ef872240 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Thu, 15 Feb 2024 19:29:48 +0100 Subject: Closes #15. No usable Error message right now --- src/routes/device.rs | 63 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 18 deletions(-) (limited to 'src/routes/device.rs') diff --git a/src/routes/device.rs b/src/routes/device.rs index aa52cf7..5ca574a 100644 --- a/src/routes/device.rs +++ b/src/routes/device.rs @@ -1,18 +1,23 @@ -use std::sync::Arc; +use crate::auth::auth; +use crate::db::Device; +use crate::error::Error; use axum::extract::State; -use axum::Json; use axum::http::HeaderMap; +use axum::Json; use serde::{Deserialize, Serialize}; use serde_json::{json, Value}; +use std::sync::Arc; use tracing::{debug, info}; -use crate::auth::auth; -use crate::db::Device; -use crate::error::Error; -pub async fn get(State(state): State>, headers: HeaderMap, Json(payload): Json) -> Result, Error> { +pub async fn get( + State(state): State>, + headers: HeaderMap, + Json(payload): Json, +) -> Result, Error> { info!("add device {}", payload.id); let secret = headers.get("authorization"); - if auth(&state.config, secret)? { + let authorized = matches!(auth(&state.config, secret)?, crate::auth::Response::Success); + if authorized { let device = sqlx::query_as!( Device, r#" @@ -21,7 +26,9 @@ pub async fn get(State(state): State>, headers: HeaderMap, WHERE id = $1; "#, payload.id - ).fetch_one(&state.db).await?; + ) + .fetch_one(&state.db) + .await?; debug!("got device {:?}", device); @@ -36,10 +43,18 @@ pub struct GetDevicePayload { id: String, } -pub async fn put(State(state): State>, headers: HeaderMap, Json(payload): Json) -> Result, Error> { - info!("add device {} ({}, {}, {})", payload.id, payload.mac, payload.broadcast_addr, payload.ip); +pub async fn put( + State(state): State>, + headers: HeaderMap, + Json(payload): Json, +) -> Result, Error> { + info!( + "add device {} ({}, {}, {})", + payload.id, payload.mac, payload.broadcast_addr, payload.ip + ); let secret = headers.get("authorization"); - if auth(&state.config, secret)? { + let authorized = matches!(auth(&state.config, secret)?, crate::auth::Response::Success); + if authorized { sqlx::query!( r#" INSERT INTO devices (id, mac, broadcast_addr, ip) @@ -49,7 +64,9 @@ pub async fn put(State(state): State>, headers: HeaderMap, payload.mac, payload.broadcast_addr, payload.ip - ).execute(&state.db).await?; + ) + .execute(&state.db) + .await?; Ok(Json(json!(PutDeviceResponse { success: true }))) } else { @@ -62,18 +79,26 @@ pub struct PutDevicePayload { id: String, mac: String, broadcast_addr: String, - ip: String + ip: String, } #[derive(Serialize)] pub struct PutDeviceResponse { - success: bool + success: bool, } -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); +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"); - if auth(&state.config, secret)? { + let authorized = matches!(auth(&state.config, secret)?, crate::auth::Response::Success); + if authorized { let device = sqlx::query_as!( Device, r#" @@ -85,7 +110,9 @@ pub async fn post(State(state): State>, headers: HeaderMap, payload.broadcast_addr, payload.ip, payload.id - ).fetch_one(&state.db).await?; + ) + .fetch_one(&state.db) + .await?; Ok(Json(json!(device))) } else { -- cgit v1.2.3