small clean-ups
remove one level of Arc in state
This commit is contained in:
parent
988afff71d
commit
79bf7f764b
3 changed files with 20 additions and 15 deletions
|
@ -16,7 +16,7 @@ use tracing::{event, instrument, Level};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
config::AuthBackendConfig,
|
config::AuthBackendConfig,
|
||||||
db::{/* Database, */ Database, SqliteDatabase, UserTokenEntry},
|
db::{Database, SqliteDatabase, UserTokenEntry},
|
||||||
secrets::{
|
secrets::{
|
||||||
PadlockGenerationSecret, Password, ServerHash, ServerPadlock, UserServerKey, UserToken,
|
PadlockGenerationSecret, Password, ServerHash, ServerPadlock, UserServerKey, UserToken,
|
||||||
},
|
},
|
||||||
|
|
|
@ -86,7 +86,6 @@ impl Database for SqliteDatabase {
|
||||||
ORDER BY created DESC",
|
ORDER BY created DESC",
|
||||||
username
|
username
|
||||||
)
|
)
|
||||||
// .bind(username)
|
|
||||||
.fetch_optional(&mut self.conn)
|
.fetch_optional(&mut self.conn)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@ use crate::auth::{
|
||||||
};
|
};
|
||||||
use crate::secrets::{Password, ServerHash, UserToken};
|
use crate::secrets::{Password, ServerHash, UserToken};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
struct AppState {
|
struct AppState {
|
||||||
user_authenticator: Arc<UserAuthenticator>,
|
user_authenticator: Arc<UserAuthenticator>,
|
||||||
server_padlock_generator: Arc<ServerPadlockGenerator>,
|
server_padlock_generator: Arc<ServerPadlockGenerator>,
|
||||||
|
@ -30,11 +30,12 @@ pub async fn run(
|
||||||
server_padlock_generator: Arc<ServerPadlockGenerator>,
|
server_padlock_generator: Arc<ServerPadlockGenerator>,
|
||||||
user_server_key_generator: Arc<UserServerKeyGenerator>,
|
user_server_key_generator: Arc<UserServerKeyGenerator>,
|
||||||
) -> color_eyre::Result<()> {
|
) -> color_eyre::Result<()> {
|
||||||
let app_state = Arc::new(AppState {
|
let app_state = AppState {
|
||||||
user_authenticator,
|
user_authenticator,
|
||||||
server_padlock_generator,
|
server_padlock_generator,
|
||||||
user_server_key_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))
|
||||||
|
@ -47,6 +48,7 @@ pub async fn run(
|
||||||
post(generate_server_padlock_2),
|
post(generate_server_padlock_2),
|
||||||
)
|
)
|
||||||
.with_state(app_state);
|
.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?;
|
||||||
|
|
||||||
|
@ -103,14 +105,15 @@ struct LoginResponse {
|
||||||
|
|
||||||
#[instrument]
|
#[instrument]
|
||||||
async fn api_login(
|
async fn api_login(
|
||||||
State(state): State<Arc<AppState>>,
|
State(AppState {
|
||||||
|
user_authenticator, ..
|
||||||
|
}): State<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::INFO, "Generating user key");
|
event!(Level::INFO, "Generating user key");
|
||||||
|
|
||||||
let (username, user_token) = state
|
let (username, user_token) = user_authenticator
|
||||||
.user_authenticator
|
|
||||||
.create_user_token(&username, &password)
|
.create_user_token(&username, &password)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
@ -135,7 +138,10 @@ struct UserServerKeyResponse {
|
||||||
|
|
||||||
#[instrument]
|
#[instrument]
|
||||||
async fn generate_user_server_key_2(
|
async fn generate_user_server_key_2(
|
||||||
State(state): State<Arc<AppState>>,
|
State(AppState {
|
||||||
|
user_server_key_generator,
|
||||||
|
..
|
||||||
|
}): State<AppState>,
|
||||||
Query(ApiVersion { api_version }): Query<ApiVersion>,
|
Query(ApiVersion { api_version }): Query<ApiVersion>,
|
||||||
Form(UserServerKeyRequest {
|
Form(UserServerKeyRequest {
|
||||||
username,
|
username,
|
||||||
|
@ -145,8 +151,7 @@ async fn generate_user_server_key_2(
|
||||||
) -> ApiResult<Json<UserServerKeyResponse>> {
|
) -> ApiResult<Json<UserServerKeyResponse>> {
|
||||||
event!(Level::INFO, "Creating user_server_key");
|
event!(Level::INFO, "Creating user_server_key");
|
||||||
|
|
||||||
let (server_key, server_key_timestamp) = state
|
let (server_key, server_key_timestamp) = user_server_key_generator
|
||||||
.user_server_key_generator
|
|
||||||
.generate_user_server_key(&username, &token, &server_hash)
|
.generate_user_server_key(&username, &token, &server_hash)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
@ -164,15 +169,16 @@ struct ServerPadlockResponse {
|
||||||
|
|
||||||
#[instrument]
|
#[instrument]
|
||||||
async fn generate_server_padlock_2(
|
async fn generate_server_padlock_2(
|
||||||
State(state): State<Arc<AppState>>,
|
State(AppState {
|
||||||
|
server_padlock_generator,
|
||||||
|
..
|
||||||
|
}): State<AppState>,
|
||||||
Query(ApiVersion { api_version }): Query<ApiVersion>,
|
Query(ApiVersion { api_version }): Query<ApiVersion>,
|
||||||
) -> ApiResult<Json<ServerPadlockResponse>> {
|
) -> ApiResult<Json<ServerPadlockResponse>> {
|
||||||
event!(Level::INFO, "Creating server padlock");
|
event!(Level::INFO, "Creating server padlock");
|
||||||
|
|
||||||
let server_hash = ServerPadlockGenerator::generate_hash();
|
let server_hash = ServerPadlockGenerator::generate_hash();
|
||||||
let server_padlock = state
|
let server_padlock = server_padlock_generator.generate_padlock(&server_hash);
|
||||||
.server_padlock_generator
|
|
||||||
.generate_padlock(&server_hash);
|
|
||||||
|
|
||||||
Ok(Json(ServerPadlockResponse {
|
Ok(Json(ServerPadlockResponse {
|
||||||
server_hash,
|
server_hash,
|
||||||
|
|
Loading…
Reference in a new issue