diff --git a/Cargo.toml b/Cargo.toml index c2e0401..f27a9a3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ futures = "0.3" tower = "0.4" tokio = { version = "1", features = [ "full" ] } hyper = { version = "0.14", features = [ "full" ] } -axum = { version = "0.6", features = [ "headers", "http2", "macros", "tracing" ] } +axum = { version = "0.6", features = [ "headers", "http2", "macros", "tracing", "json" ] } clap = { version = "4.3", features = [ "derive" ] } snafu = "0.7" log = "0.4" @@ -20,7 +20,7 @@ env_logger = "0.10" ring = "0.16" hex = "0.4" chrono = { version = "0.4", features = [ "serde" ] } -yunohost-api = { path = "yunohost-api" } +yunohost-api = { path = "yunohost-api", features = [ "axum" ] } axum_typed_multipart = "0.8" async-trait = "0.1" serde = { version = "1", features = [ "derive" ] } diff --git a/src/routes/login.rs b/src/routes/login.rs index c7d018a..e255c2e 100644 --- a/src/routes/login.rs +++ b/src/routes/login.rs @@ -1,16 +1,19 @@ use axum::{ - extract::{FromRequest, Form, Json, Query}, + extract::{FromRequest, Form, Json, Query, State}, http::{self, Request, StatusCode}, response::{IntoResponse, Response}, RequestExt, }; use axum_typed_multipart::{TryFromMultipart, TypedMultipart}; +use yunohost_api::{Username, Password}; + +use crate::state::RoutableAppState; #[derive(Debug, TryFromMultipart, Deserialize)] pub struct LoginForm { - username: String, + username: Username, #[allow(dead_code)] - password: String, + password: Password, } #[async_trait] @@ -52,6 +55,10 @@ where } #[debug_handler] -pub async fn route(form: LoginForm) -> String { - format!("Welcome {}", form.username) +pub async fn route(state: State, form: LoginForm) -> String { + if state.check_login(&form.username, &form.password).await.unwrap() { + format!("Welcome {}", &form.username) + } else { + format!("Invalid login for {}", &form.username) + } } diff --git a/src/routes/mod.rs b/src/routes/mod.rs index a8eb98b..1a63e07 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -13,7 +13,7 @@ mod login; pub fn router(subpath: Option, state: RoutableAppState) -> Router { let app = Router::new() .route("/", get(index::route)) - .route("/login/", get(login::route)) + .route("/login/", post(login::route)) .with_state(state); if let Some(p) = subpath { Router::new() diff --git a/src/state/mod.rs b/src/state/mod.rs index 8e712eb..4e28dfe 100644 --- a/src/state/mod.rs +++ b/src/state/mod.rs @@ -1,5 +1,5 @@ use snafu::prelude::*; -use yunohost_api::YunohostUsers; +use yunohost_api::{YunohostUsers, Username, Password}; use std::sync::Arc; @@ -23,4 +23,8 @@ impl AppState { users: YunohostUsers::new(500).await.context(YunohostSnafu)?, }) } + + pub async fn check_login(&self, username: &Username, password: &Password) -> Result { + self.users.check_credentials(username, password).await.context(YunohostSnafu) + } }