diff options
author | Cara Salter <cara@devcara.com> | 2022-06-28 07:07:34 -0400 |
---|---|---|
committer | Cara Salter <cara@devcara.com> | 2022-06-28 07:07:34 -0400 |
commit | 8de1eae2b49d763dcac55b8a2a84673475a35e63 (patch) | |
tree | 3b2d385b590c4211cf311042122563b89dcc6e51 /src/main.rs | |
parent | f516a9616b7160be149ef4ba8726557e019bf621 (diff) | |
download | solard-8de1eae2b49d763dcac55b8a2a84673475a35e63.tar.gz solard-8de1eae2b49d763dcac55b8a2a84673475a35e63.zip |
auth: Scaffold auth/begin [WIP]
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 21 |
1 files changed, 21 insertions, 0 deletions
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::<tower::timeout::error::Elapsed>() { |