diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/auth.rs | 6 | ||||
-rw-r--r-- | src/main.rs | 6 | ||||
-rw-r--r-- | src/routes/start.rs | 32 |
3 files changed, 37 insertions, 7 deletions
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( | |||
23 | match auth.method { | 23 | match auth.method { |
24 | Methods::Key => { | 24 | Methods::Key => { |
25 | if let Some(secret) = headers.get("authorization") { | 25 | if let Some(secret) = headers.get("authorization") { |
26 | if !(auth.secret.as_str() == secret) { return Err(StatusCode::UNAUTHORIZED); }; | 26 | if auth.secret.as_str() != secret { |
27 | return Err(StatusCode::UNAUTHORIZED); | ||
28 | }; | ||
27 | let response = next.run(request).await; | 29 | let response = next.run(request).await; |
28 | Ok(response) | 30 | Ok(response) |
29 | } else { | 31 | } else { |
30 | return Err(StatusCode::UNAUTHORIZED); | 32 | Err(StatusCode::UNAUTHORIZED) |
31 | } | 33 | } |
32 | } | 34 | } |
33 | Methods::None => Ok(next.run(request).await), | 35 | 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; | |||
37 | #[derive(OpenApi)] | 37 | #[derive(OpenApi)] |
38 | #[openapi( | 38 | #[openapi( |
39 | paths( | 39 | paths( |
40 | start::start, | 40 | start::post, |
41 | start::get, | ||
41 | start::start_payload, | 42 | start::start_payload, |
42 | device::get, | 43 | device::get, |
43 | device::get_payload, | 44 | device::get_payload, |
@@ -119,13 +120,14 @@ async fn main() -> color_eyre::eyre::Result<()> { | |||
119 | 120 | ||
120 | let app = Router::new() | 121 | let app = Router::new() |
121 | .route("/start", post(start::start_payload)) | 122 | .route("/start", post(start::start_payload)) |
122 | .route("/start/:id", post(start::start)) | 123 | .route("/start/:id", post(start::post).get(start::get)) |
123 | .route( | 124 | .route( |
124 | "/device", | 125 | "/device", |
125 | post(device::post).get(device::get_payload).put(device::put), | 126 | post(device::post).get(device::get_payload).put(device::put), |
126 | ) | 127 | ) |
127 | .route("/device/:id", get(device::get)) | 128 | .route("/device/:id", get(device::get)) |
128 | .route("/status", get(status::status)) | 129 | .route("/status", get(status::status)) |
130 | // TODO: Don't load on `None` Auth | ||
129 | .route_layer(from_fn_with_state(shared_state.clone(), auth::auth)) | 131 | .route_layer(from_fn_with_state(shared_state.clone(), auth::auth)) |
130 | .merge(SwaggerUi::new("/swagger-ui").url("/api-docs/openapi.json", ApiDoc::openapi())) | 132 | .merge(SwaggerUi::new("/swagger-ui").url("/api-docs/openapi.json", ApiDoc::openapi())) |
131 | .with_state(Arc::new(shared_state)); | 133 | .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; | |||
18 | responses( | 18 | responses( |
19 | (status = 200, description = "List matching todos by query", body = [Response]) | 19 | (status = 200, description = "List matching todos by query", body = [Response]) |
20 | ), | 20 | ), |
21 | security(("api_key" = [])) | 21 | security((), ("api_key" = [])) |
22 | )] | 22 | )] |
23 | #[deprecated] | 23 | #[deprecated] |
24 | pub async fn start_payload( | 24 | pub async fn start_payload( |
@@ -70,13 +70,39 @@ pub async fn start_payload( | |||
70 | params( | 70 | params( |
71 | ("id" = String, Path, description = "Device id") | 71 | ("id" = String, Path, description = "Device id") |
72 | ), | 72 | ), |
73 | security(("api_key" = [])) | 73 | security((), ("api_key" = [])) |
74 | )] | 74 | )] |
75 | pub async fn start( | 75 | pub async fn post( |
76 | State(state): State<Arc<crate::AppState>>, | 76 | State(state): State<Arc<crate::AppState>>, |
77 | Path(id): Path<String>, | 77 | Path(id): Path<String>, |
78 | payload: Option<Json<Payload>>, | 78 | payload: Option<Json<Payload>>, |
79 | ) -> Result<Json<Value>, Error> { | 79 | ) -> Result<Json<Value>, Error> { |
80 | send_wol(state, &id, payload).await | ||
81 | } | ||
82 | |||
83 | #[utoipa::path( | ||
84 | get, | ||
85 | path = "/start/{id}", | ||
86 | responses( | ||
87 | (status = 200, description = "Start the device with the given id", body = [Response]) | ||
88 | ), | ||
89 | params( | ||
90 | ("id" = String, Path, description = "Device id") | ||
91 | ), | ||
92 | security((), ("api_key" = [])) | ||
93 | )] | ||
94 | pub async fn get( | ||
95 | State(state): State<Arc<crate::AppState>>, | ||
96 | Path(id): Path<String>, | ||
97 | ) -> Result<Json<Value>, Error> { | ||
98 | send_wol(state, &id, None).await | ||
99 | } | ||
100 | |||
101 | async fn send_wol( | ||
102 | state: Arc<crate::AppState>, | ||
103 | id: &str, | ||
104 | payload: Option<Json<Payload>>, | ||
105 | ) -> Result<Json<Value>, Error> { | ||
80 | info!("Start request for {id}"); | 106 | info!("Start request for {id}"); |
81 | let device = sqlx::query_as!( | 107 | let device = sqlx::query_as!( |
82 | Device, | 108 | Device, |