diff options
author | Cara Salter <cara@devcara.com> | 2022-06-28 09:42:00 -0400 |
---|---|---|
committer | Cara Salter <cara@devcara.com> | 2022-06-28 09:42:00 -0400 |
commit | a739b00bc366758be9c452979cc4b4c831dd8a75 (patch) | |
tree | 99d068468127c8f185da4eb9c0056a991c37c3c7 /src/handlers | |
parent | 2dea604581cafbfbb73443ec2449cda391d12ab7 (diff) | |
download | solard-a739b00bc366758be9c452979cc4b4c831dd8a75.tar.gz solard-a739b00bc366758be9c452979cc4b4c831dd8a75.zip |
tool: FMT
Diffstat (limited to 'src/handlers')
-rw-r--r-- | src/handlers/auth.rs | 54 | ||||
-rw-r--r-- | src/handlers/mod.rs | 2 | ||||
-rw-r--r-- | src/handlers/planets.rs | 47 |
3 files changed, 70 insertions, 33 deletions
diff --git a/src/handlers/auth.rs b/src/handlers/auth.rs index 8d65b05..cafaeb8 100644 --- a/src/handlers/auth.rs +++ b/src/handlers/auth.rs @@ -1,15 +1,22 @@ -use std::{collections::HashMap, fs::{self, File}}; +use std::{ + collections::HashMap, + fs::{self, File}, + sync::Arc, +}; -use axum::{extract::Query, Extension}; +use axum::{extract::Query, middleware::Next, response::Response, Extension}; use axum_macros::debug_handler; -use chrono::{Utc, TimeZone, Datelike}; +use chrono::{Datelike, TimeZone, Utc}; +use hyper::Request; use ring::{rand::SystemRandom, signature::Ed25519KeyPair}; use uuid::Uuid; use std::io::Write; -use crate::{errors::{NoneResult, ServiceError, StringResult, TokenResult}, State}; - +use crate::{ + errors::{NoneResult, ServiceError, StringResult, TokenResult}, + State, +}; /** * Takes in a request to create a new token with a secret key that gets printed @@ -17,7 +24,10 @@ use crate::{errors::{NoneResult, ServiceError, StringResult, TokenResult}, State * for future authentication */ #[debug_handler] -pub async fn begin(Query(params): Query<HashMap<String, String>>, Extension(state): Extension<State>) -> TokenResult { +pub async fn begin( + Query(params): Query<HashMap<String, String>>, + Extension(state): Extension<Arc<State>>, +) -> TokenResult { if let Some(k) = params.get("key") { if k == &state.gen_key { let dt = Utc::now(); @@ -33,15 +43,17 @@ pub async fn begin(Query(params): Query<HashMap<String, String>>, Extension(stat .set_issuer("solard") .set_audience("solard") .set_not_before(&Utc::now()) - .build() { - Ok(token) => token, - Err(_) => { - return Err(ServiceError::Generic(String::from("could not generate paseto key"))); - } - }; + .build() + { + Ok(token) => token, + Err(_) => { + return Err(ServiceError::Generic(String::from( + "could not generate paseto key", + ))); + } + }; return Ok(token.to_string()); - } else { return Err(ServiceError::NotAuthorized); } @@ -50,6 +62,17 @@ pub async fn begin(Query(params): Query<HashMap<String, String>>, Extension(stat } } +pub async fn requires_auth<B>(req: Request<B>, next: Next<B>) -> Result<Response, ServiceError> { + let auth_header = req + .headers() + .get(axum::http::header::AUTHORIZATION) + .and_then(|h| h.to_str().ok()); + + match auth_header { + Some(h) => Ok(next.run(req).await), + None => Err(ServiceError::NotAuthorized), + } +} fn load_or_gen_keypair() -> Result<Ed25519KeyPair, ServiceError> { let kp: Ed25519KeyPair; @@ -59,7 +82,10 @@ fn load_or_gen_keypair() -> Result<Ed25519KeyPair, ServiceError> { let srand = SystemRandom::new(); let pkcs8 = Ed25519KeyPair::generate_pkcs8(&srand)?; - let mut file = File::open(".keypair").unwrap(); + let mut file = match File::open(".keypair") { + Ok(f) => f, + Err(_) => File::create(".keypair").unwrap(), + }; file.write(pkcs8.as_ref()); kp = Ed25519KeyPair::from_pkcs8(pkcs8.as_ref())?; diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index 8f8224e..ddd4006 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -1,2 +1,2 @@ -pub mod planets; pub mod auth; +pub mod planets; diff --git a/src/handlers/planets.rs b/src/handlers/planets.rs index 2c66324..8593026 100644 --- a/src/handlers/planets.rs +++ b/src/handlers/planets.rs @@ -1,14 +1,14 @@ use axum::Extension; +use axum::{extract::Path, response::IntoResponse, Json}; use axum_macros::debug_handler; -use axum::{response::IntoResponse, Json, extract::Path}; use solarlib::ship::{DbShip, Ship}; use tokio::process::Command; use tracing::{error, instrument}; -use solarlib::star::{Star, NewPlanet}; -use solarlib::planet::Planet; use solarlib::errors::Error as SolarlibError; +use solarlib::planet::Planet; +use solarlib::star::{NewPlanet, Star}; use crate::{errors::*, get_star, State}; use std::sync::Arc; @@ -16,14 +16,13 @@ use std::sync::Arc; pub async fn list() -> JsonResult<Json<Vec<Planet>>> { let con_url = std::env::var("QEMU_URL").unwrap_or("qemu:///system".to_string()); let mut star = Star::new(con_url)?; - + let inhabitants = star.inhabitants()?; Ok(Json(inhabitants)) } pub async fn get(Path(uuid): Path<String>) -> JsonResult<Json<Planet>> { - let con_url = std::env::var("QEMU_URL").unwrap_or("qemu:///system".to_string()); let mut star = Star::new(con_url)?; @@ -79,7 +78,7 @@ pub async fn reboot(Path(uuid): Path<String>) -> NoneResult { } else { return Err(ServiceError::NotFound); } - + Ok(()) } @@ -113,7 +112,10 @@ pub async fn new_planet(Json(new_planet): Json<NewPlanet>) -> JsonResult<Json<Pl let ship_shasum = new_planet.clone().ship; - let res: DbShip = reqwest::get(format!("http://{}/ships/get/{}", hw_url, ship_shasum)).await?.json().await?; + let res: DbShip = reqwest::get(format!("http://{}/ships/get/{}", hw_url, ship_shasum)) + .await? + .json() + .await?; let ship: Ship = res.into(); let mut s = get_star()?; @@ -122,17 +124,27 @@ pub async fn new_planet(Json(new_planet): Json<NewPlanet>) -> JsonResult<Json<Pl // Try to create right away, if the Ship already exists on the system, it'll go through. If // not, we can download it by using the shasum - let r = s.planet(new_planet.clone().name, new_planet.max_mem, new_planet.max_cpus, new_planet.disk_size_mb, ship.clone()); + let r = s.planet( + new_planet.clone().name, + new_planet.max_mem, + new_planet.max_cpus, + new_planet.disk_size_mb, + ship.clone(), + ); match r { - Err(e) => { - match e { - SolarlibError::MissingImage(_) => { - ship.download(&s)?; - return Ok(Json(s.planet(new_planet.name, new_planet.max_mem, new_planet.max_cpus, new_planet.disk_size_mb, ship.clone())?)); - }, - _ => { - return Err(ServiceError::Solarlib(e)); - } + Err(e) => match e { + SolarlibError::MissingImage(_) => { + ship.download(&s)?; + return Ok(Json(s.planet( + new_planet.name, + new_planet.max_mem, + new_planet.max_cpus, + new_planet.disk_size_mb, + ship.clone(), + )?)); + } + _ => { + return Err(ServiceError::Solarlib(e)); } }, Ok(r) => { @@ -142,7 +154,6 @@ pub async fn new_planet(Json(new_planet): Json<NewPlanet>) -> JsonResult<Json<Pl }; } - pub async fn no_planet(Path(uuid): Path<String>) -> NoneResult { let mut s = get_star()?; |