From ffe3aa52324c1314fc3415a12f29548197c429ff Mon Sep 17 00:00:00 2001 From: Xiretza Date: Sat, 10 Feb 2024 18:34:07 +0000 Subject: [PATCH] server: pass state to handlers --- src/main.rs | 8 +++++++- src/server.rs | 46 ++++++++++++++++++++++++++++++++++++---------- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index f65bbb1..c84cb0e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -91,7 +91,13 @@ async fn main() -> Result<()> { Arc::clone(&padlock_generator), )); - tokio::spawn(server::run(config.port)).await??; + tokio::spawn(server::run( + config.port, + user_authenticator, + padlock_generator, + user_server_key_generator, + )) + .await??; Ok(()) } diff --git a/src/server.rs b/src/server.rs index ffbcd43..dcef0eb 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,7 +1,7 @@ -use std::net::Ipv6Addr; +use std::{net::Ipv6Addr, sync::Arc}; use axum::{ - extract::Query, + extract::{Query, State}, http::StatusCode, response::IntoResponse, routing::{get, post}, @@ -11,18 +11,38 @@ use secrecy::ExposeSecret; use serde::{Deserialize, Serialize}; use tracing::{event, instrument, Level}; -use crate::auth::{AuthenticationError, UserServerKeyGenerator}; +use crate::auth::{ + AuthenticationError, ServerPadlockGenerator, UserAuthenticator, UserServerKeyGenerator, +}; use crate::secrets::{Password, ServerHash, UserToken}; +#[derive(Debug)] +struct AppState { + user_authenticator: Arc, + server_padlock_generator: Arc, + user_server_key_generator: Arc, +} + #[instrument] -pub async fn run(port: u16) -> color_eyre::Result<()> { +pub async fn run( + port: u16, + user_authenticator: Arc, + server_padlock_generator: Arc, + user_server_key_generator: Arc, +) -> color_eyre::Result<()> { + let app_state = Arc::new(AppState { + user_authenticator, + server_padlock_generator, + user_server_key_generator, + }); let app = Router::new() .route("/tls-check/success", get(|| async { "OK" })) .route("/api-login", post(api_login)) .route( "/generate-user-server-key-2", post(generate_user_server_key_2), - ); + ) + .with_state(app_state); let listener = tokio::net::TcpListener::bind((Ipv6Addr::UNSPECIFIED, port)).await?; axum::serve(listener, app).await?; @@ -74,11 +94,16 @@ struct LoginResponse { #[instrument] async fn api_login( + State(state): State>, Query(ApiVersion { api_version }): Query, Form(LoginRequest { username, password }): Form, ) -> ApiResult> { - event!(Level::WARN, "Creating dummy token"); - let user_token = UserToken("invalid".to_owned().into()); + event!(Level::INFO, "Generating user key"); + + let user_token = state + .user_authenticator + .create_user_token(&username, &password) + .await?; Ok(Json(LoginResponse { username, @@ -101,6 +126,7 @@ struct UserServerKeyResponse { #[instrument] async fn generate_user_server_key_2( + State(state): State>, Query(ApiVersion { api_version }): Query, Form(UserServerKeyRequest { username, @@ -108,10 +134,10 @@ async fn generate_user_server_key_2( server_hash, }): Form, ) -> ApiResult> { - event!(Level::WARN, "Creating dummy user_server_key"); + event!(Level::INFO, "Creating user_server_key"); - let generator: UserServerKeyGenerator = todo!(); - let (server_key, server_key_timestamp) = generator + let (server_key, server_key_timestamp) = state + .user_server_key_generator .generate_user_server_key(&username, &token, &server_hash) .await?;