factoriauth/src/server.rs

59 lines
1.3 KiB
Rust
Raw Normal View History

2024-02-10 11:44:31 +01:00
use axum::{
extract::Query,
routing::{get, post},
Form, Json, Router,
};
use serde::{Deserialize, Serialize};
use tracing::{event, instrument, Level};
use crate::auth::Password;
2024-02-10 11:13:11 +01:00
#[instrument]
2024-02-10 11:44:31 +01:00
pub async fn run() -> color_eyre::Result<()> {
let app = Router::new()
.route("/tls-check/success", get(|| async { "OK" }))
.route("/api-login", post(api_login));
2024-02-10 11:13:11 +01:00
let listener = tokio::net::TcpListener::bind("[::]:8080").await?;
axum::serve(listener, app).await?;
Ok(())
}
2024-02-10 11:44:31 +01:00
2024-02-10 11:49:36 +01:00
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
struct ApiError {
error: String,
message: String,
}
type ApiResult<T> = Result<T, Json<ApiError>>;
2024-02-10 11:44:31 +01:00
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
struct ApiVersion {
api_version: String,
}
#[derive(Debug, Clone, Deserialize)]
struct LoginRequest {
username: String,
password: Password,
}
#[derive(Debug, Clone, Serialize)]
struct LoginResponse {
username: String,
token: String,
}
#[instrument]
async fn api_login(
Query(ApiVersion { api_version }): Query<ApiVersion>,
Form(LoginRequest { username, password }): Form<LoginRequest>,
2024-02-10 11:49:36 +01:00
) -> ApiResult<Json<LoginResponse>> {
2024-02-10 11:44:31 +01:00
event!(Level::WARN, "Creating dummy token");
Ok(Json(LoginResponse {
username,
token: "invalid".to_string(),
}))
}