Start work on login form

This commit is contained in:
selfhoster selfhoster 2023-08-22 17:00:55 +02:00
parent 4eb3ac6350
commit 1667c3295b
4 changed files with 20 additions and 9 deletions

View File

@ -10,7 +10,7 @@ futures = "0.3"
tower = "0.4" tower = "0.4"
tokio = { version = "1", features = [ "full" ] } tokio = { version = "1", features = [ "full" ] }
hyper = { version = "0.14", 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" ] } clap = { version = "4.3", features = [ "derive" ] }
snafu = "0.7" snafu = "0.7"
log = "0.4" log = "0.4"
@ -20,7 +20,7 @@ env_logger = "0.10"
ring = "0.16" ring = "0.16"
hex = "0.4" hex = "0.4"
chrono = { version = "0.4", features = [ "serde" ] } chrono = { version = "0.4", features = [ "serde" ] }
yunohost-api = { path = "yunohost-api" } yunohost-api = { path = "yunohost-api", features = [ "axum" ] }
axum_typed_multipart = "0.8" axum_typed_multipart = "0.8"
async-trait = "0.1" async-trait = "0.1"
serde = { version = "1", features = [ "derive" ] } serde = { version = "1", features = [ "derive" ] }

View File

@ -1,16 +1,19 @@
use axum::{ use axum::{
extract::{FromRequest, Form, Json, Query}, extract::{FromRequest, Form, Json, Query, State},
http::{self, Request, StatusCode}, http::{self, Request, StatusCode},
response::{IntoResponse, Response}, response::{IntoResponse, Response},
RequestExt, RequestExt,
}; };
use axum_typed_multipart::{TryFromMultipart, TypedMultipart}; use axum_typed_multipart::{TryFromMultipart, TypedMultipart};
use yunohost_api::{Username, Password};
use crate::state::RoutableAppState;
#[derive(Debug, TryFromMultipart, Deserialize)] #[derive(Debug, TryFromMultipart, Deserialize)]
pub struct LoginForm { pub struct LoginForm {
username: String, username: Username,
#[allow(dead_code)] #[allow(dead_code)]
password: String, password: Password,
} }
#[async_trait] #[async_trait]
@ -52,6 +55,10 @@ where
} }
#[debug_handler] #[debug_handler]
pub async fn route(form: LoginForm) -> String { pub async fn route(state: State<RoutableAppState>, form: LoginForm) -> String {
format!("Welcome {}", form.username) if state.check_login(&form.username, &form.password).await.unwrap() {
format!("Welcome {}", &form.username)
} else {
format!("Invalid login for {}", &form.username)
}
} }

View File

@ -13,7 +13,7 @@ mod login;
pub fn router(subpath: Option<String>, state: RoutableAppState) -> Router { pub fn router(subpath: Option<String>, state: RoutableAppState) -> Router {
let app = Router::new() let app = Router::new()
.route("/", get(index::route)) .route("/", get(index::route))
.route("/login/", get(login::route)) .route("/login/", post(login::route))
.with_state(state); .with_state(state);
if let Some(p) = subpath { if let Some(p) = subpath {
Router::new() Router::new()

View File

@ -1,5 +1,5 @@
use snafu::prelude::*; use snafu::prelude::*;
use yunohost_api::YunohostUsers; use yunohost_api::{YunohostUsers, Username, Password};
use std::sync::Arc; use std::sync::Arc;
@ -23,4 +23,8 @@ impl AppState {
users: YunohostUsers::new(500).await.context(YunohostSnafu)?, users: YunohostUsers::new(500).await.context(YunohostSnafu)?,
}) })
} }
pub async fn check_login(&self, username: &Username, password: &Password) -> Result<bool, Error> {
self.users.check_credentials(username, password).await.context(YunohostSnafu)
}
} }