From 8de1eae2b49d763dcac55b8a2a84673475a35e63 Mon Sep 17 00:00:00 2001 From: Cara Salter Date: Tue, 28 Jun 2022 07:07:34 -0400 Subject: auth: Scaffold auth/begin [WIP] --- src/main.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 4dc78a6..3f889cd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ use axum::{ Json, Router, Extension }; +use rand::{thread_rng, Rng, distributions::Alphanumeric}; use serde::{Deserialize, Serialize}; use solarlib::star::Star; use std::{net::SocketAddr, time::Duration, str::FromStr, sync::Arc}; @@ -20,8 +21,11 @@ mod errors; mod handlers; +#[derive(Clone)] pub struct State { pub hw_url: String, + pub secret_key: String, + pub gen_key: String, } #[tokio::main] @@ -36,10 +40,25 @@ async fn main() { .with(tracing_subscriber::fmt::layer()) .init(); + let rand_key: String = thread_rng() + .sample_iter(&Alphanumeric) + .take(30) + .map(char::from) + .collect(); + let shared_state = Arc::new(State { hw_url: std::env::var("HOMEWORLD_URL").expect("No Homeworld URL set"), + secret_key: std::env::var("SECRET_KEY").unwrap_or("bad-key".to_string()), + gen_key: rand_key, }); + if shared_state.secret_key == "bad-key" { + tracing::warn!("No secret key set! This is a bad idea."); + tracing::warn!("Using default of `bad-key`"); + } + + tracing::info!("Random Key: {}", shared_state.gen_key); + let app = Router::new() .route("/health", get(health_check)) .route("/planets/list", get(handlers::planets::list)) @@ -52,6 +71,8 @@ async fn main() { .route("/planets/:uuid/reboot", post(handlers::planets::reboot)) .route("/planets/:uuid/reboot/hard", post(handlers::planets::force_reboot)) .route("/planets/:uuid/destroy", post(handlers::planets::no_planet)) + // Authentication + .route("/auth/begin", post(handlers::auth::begin)) .layer( ServiceBuilder::new() .layer(HandleErrorLayer::new(|error: BoxError| async move { if error.is::() { -- cgit v1.2.3