server: pass state to handlers
This commit is contained in:
parent
7bd354429a
commit
ffe3aa5232
2 changed files with 43 additions and 11 deletions
|
@ -91,7 +91,13 @@ async fn main() -> Result<()> {
|
||||||
Arc::clone(&padlock_generator),
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use std::net::Ipv6Addr;
|
use std::{net::Ipv6Addr, sync::Arc};
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::Query,
|
extract::{Query, State},
|
||||||
http::StatusCode,
|
http::StatusCode,
|
||||||
response::IntoResponse,
|
response::IntoResponse,
|
||||||
routing::{get, post},
|
routing::{get, post},
|
||||||
|
@ -11,18 +11,38 @@ use secrecy::ExposeSecret;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tracing::{event, instrument, Level};
|
use tracing::{event, instrument, Level};
|
||||||
|
|
||||||
use crate::auth::{AuthenticationError, UserServerKeyGenerator};
|
use crate::auth::{
|
||||||
|
AuthenticationError, ServerPadlockGenerator, UserAuthenticator, UserServerKeyGenerator,
|
||||||
|
};
|
||||||
use crate::secrets::{Password, ServerHash, UserToken};
|
use crate::secrets::{Password, ServerHash, UserToken};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct AppState {
|
||||||
|
user_authenticator: Arc<UserAuthenticator>,
|
||||||
|
server_padlock_generator: Arc<ServerPadlockGenerator>,
|
||||||
|
user_server_key_generator: Arc<UserServerKeyGenerator>,
|
||||||
|
}
|
||||||
|
|
||||||
#[instrument]
|
#[instrument]
|
||||||
pub async fn run(port: u16) -> color_eyre::Result<()> {
|
pub async fn run(
|
||||||
|
port: u16,
|
||||||
|
user_authenticator: Arc<UserAuthenticator>,
|
||||||
|
server_padlock_generator: Arc<ServerPadlockGenerator>,
|
||||||
|
user_server_key_generator: Arc<UserServerKeyGenerator>,
|
||||||
|
) -> color_eyre::Result<()> {
|
||||||
|
let app_state = Arc::new(AppState {
|
||||||
|
user_authenticator,
|
||||||
|
server_padlock_generator,
|
||||||
|
user_server_key_generator,
|
||||||
|
});
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
.route("/tls-check/success", get(|| async { "OK" }))
|
.route("/tls-check/success", get(|| async { "OK" }))
|
||||||
.route("/api-login", post(api_login))
|
.route("/api-login", post(api_login))
|
||||||
.route(
|
.route(
|
||||||
"/generate-user-server-key-2",
|
"/generate-user-server-key-2",
|
||||||
post(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?;
|
let listener = tokio::net::TcpListener::bind((Ipv6Addr::UNSPECIFIED, port)).await?;
|
||||||
axum::serve(listener, app).await?;
|
axum::serve(listener, app).await?;
|
||||||
|
|
||||||
|
@ -74,11 +94,16 @@ struct LoginResponse {
|
||||||
|
|
||||||
#[instrument]
|
#[instrument]
|
||||||
async fn api_login(
|
async fn api_login(
|
||||||
|
State(state): State<Arc<AppState>>,
|
||||||
Query(ApiVersion { api_version }): Query<ApiVersion>,
|
Query(ApiVersion { api_version }): Query<ApiVersion>,
|
||||||
Form(LoginRequest { username, password }): Form<LoginRequest>,
|
Form(LoginRequest { username, password }): Form<LoginRequest>,
|
||||||
) -> ApiResult<Json<LoginResponse>> {
|
) -> ApiResult<Json<LoginResponse>> {
|
||||||
event!(Level::WARN, "Creating dummy token");
|
event!(Level::INFO, "Generating user key");
|
||||||
let user_token = UserToken("invalid".to_owned().into());
|
|
||||||
|
let user_token = state
|
||||||
|
.user_authenticator
|
||||||
|
.create_user_token(&username, &password)
|
||||||
|
.await?;
|
||||||
|
|
||||||
Ok(Json(LoginResponse {
|
Ok(Json(LoginResponse {
|
||||||
username,
|
username,
|
||||||
|
@ -101,6 +126,7 @@ struct UserServerKeyResponse {
|
||||||
|
|
||||||
#[instrument]
|
#[instrument]
|
||||||
async fn generate_user_server_key_2(
|
async fn generate_user_server_key_2(
|
||||||
|
State(state): State<Arc<AppState>>,
|
||||||
Query(ApiVersion { api_version }): Query<ApiVersion>,
|
Query(ApiVersion { api_version }): Query<ApiVersion>,
|
||||||
Form(UserServerKeyRequest {
|
Form(UserServerKeyRequest {
|
||||||
username,
|
username,
|
||||||
|
@ -108,10 +134,10 @@ async fn generate_user_server_key_2(
|
||||||
server_hash,
|
server_hash,
|
||||||
}): Form<UserServerKeyRequest>,
|
}): Form<UserServerKeyRequest>,
|
||||||
) -> ApiResult<Json<UserServerKeyResponse>> {
|
) -> ApiResult<Json<UserServerKeyResponse>> {
|
||||||
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) = state
|
||||||
let (server_key, server_key_timestamp) = generator
|
.user_server_key_generator
|
||||||
.generate_user_server_key(&username, &token, &server_hash)
|
.generate_user_server_key(&username, &token, &server_hash)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue