mirror of
https://github.com/nitnelave/lldap.git
synced 2023-04-12 14:25:13 +00:00
app: implement login and authorization
This commit is contained in:
parent
e431c40475
commit
2de589d05c
@ -5,12 +5,16 @@ authors = ["Valentin Tolmer <valentin@tolmer.fr>", "Steve Barrau <steve.barrau@g
|
|||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
yew = "0.17.4"
|
yew = "0.17"
|
||||||
wasm-bindgen = "0.2.73"
|
wasm-bindgen = "0.2"
|
||||||
lldap_model = { path = "../model" }
|
lldap_model = { path = "../model" }
|
||||||
anyhow = "1.0.40"
|
anyhow = "1"
|
||||||
web-sys = { version = "0.3", features = [ "console" ] }
|
web-sys = { version = "0.3", features = [ "console", "Document", "Element", "HtmlDocument" ] }
|
||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
|
serde = "1"
|
||||||
|
http = "0.2.4"
|
||||||
|
jwt = "0.13"
|
||||||
|
chrono = "*"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
crate-type = ["cdylib"]
|
crate-type = ["cdylib"]
|
||||||
|
107
app/src/api.rs
107
app/src/api.rs
@ -1,40 +1,103 @@
|
|||||||
use anyhow::Result;
|
use anyhow::{anyhow, Result};
|
||||||
use lldap_model::*;
|
use lldap_model::*;
|
||||||
|
use wasm_bindgen::JsCast;
|
||||||
|
|
||||||
use yew::callback::Callback;
|
use yew::callback::Callback;
|
||||||
use yew::format::Json;
|
use yew::format::Json;
|
||||||
use yew::services::fetch::{FetchService, FetchTask, Request, Response};
|
use yew::services::fetch::{Credentials, FetchOptions, FetchService, FetchTask, Request, Response};
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct HostService {}
|
pub struct HostService {}
|
||||||
|
|
||||||
|
fn get_default_options() -> FetchOptions {
|
||||||
|
FetchOptions {
|
||||||
|
credentials: Some(Credentials::SameOrigin),
|
||||||
|
..FetchOptions::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_claims_from_jwt(jwt: &str) -> Result<JWTClaims> {
|
||||||
|
use jwt::*;
|
||||||
|
let token = Token::<header::Header, JWTClaims, token::Unverified>::parse_unverified(jwt)?;
|
||||||
|
Ok(token.claims().clone())
|
||||||
|
}
|
||||||
|
|
||||||
impl HostService {
|
impl HostService {
|
||||||
pub fn list_users(
|
pub fn list_users(
|
||||||
&mut self,
|
|
||||||
request: ListUsersRequest,
|
request: ListUsersRequest,
|
||||||
callback: Callback<Result<Vec<User>>>,
|
callback: Callback<Result<Vec<User>>>,
|
||||||
) -> Result<FetchTask> {
|
) -> Result<FetchTask> {
|
||||||
let url = format!("/api/users");
|
let url = "/api/users";
|
||||||
let handler =
|
let handler = move |response: Response<Result<String>>| {
|
||||||
move |response: Response<Result<String>>| {
|
let (meta, maybe_data) = response.into_parts();
|
||||||
let (meta, maybe_data) = response.into_parts();
|
match maybe_data {
|
||||||
match maybe_data {
|
Ok(data) => {
|
||||||
Ok(data) => {
|
if meta.status.is_success() {
|
||||||
if meta.status.is_success() {
|
callback.emit(
|
||||||
callback.emit(serde_json::from_str(&data).map_err(|e| {
|
serde_json::from_str(&data)
|
||||||
anyhow::format_err!("Could not parse response: {}", e)
|
.map_err(|e| anyhow!("Could not parse response: {}", e)),
|
||||||
}))
|
)
|
||||||
} else {
|
} else {
|
||||||
callback.emit(Err(anyhow::anyhow!("[{}]: {}", meta.status, data)))
|
callback.emit(Err(anyhow!("[{}]: {}", meta.status, data)))
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Err(e) => callback.emit(Err(anyhow::anyhow!("Could not fetch: {}", e))),
|
|
||||||
}
|
}
|
||||||
};
|
Err(e) => callback.emit(Err(anyhow!("Could not fetch: {}", e))),
|
||||||
let request = Request::post(url.as_str())
|
}
|
||||||
|
};
|
||||||
|
let request = Request::post(url)
|
||||||
.header("Content-Type", "application/json")
|
.header("Content-Type", "application/json")
|
||||||
.body(Json(&request))
|
.body(Json(&request))?;
|
||||||
.unwrap();
|
FetchService::fetch_with_options(request, get_default_options(), handler.into())
|
||||||
FetchService::fetch(request, handler.into())
|
}
|
||||||
|
|
||||||
|
pub fn authenticate(
|
||||||
|
request: BindRequest,
|
||||||
|
callback: Callback<Result<String>>,
|
||||||
|
) -> Result<FetchTask> {
|
||||||
|
let url = "/api/authorize";
|
||||||
|
let handler = move |response: Response<Result<String>>| {
|
||||||
|
let (meta, maybe_data) = response.into_parts();
|
||||||
|
match maybe_data {
|
||||||
|
Ok(data) => {
|
||||||
|
if meta.status.is_success() {
|
||||||
|
match get_claims_from_jwt(&data) {
|
||||||
|
Ok(jwt_claims) => {
|
||||||
|
let document = web_sys::window()
|
||||||
|
.unwrap()
|
||||||
|
.document()
|
||||||
|
.unwrap()
|
||||||
|
.dyn_into::<web_sys::HtmlDocument>()
|
||||||
|
.unwrap();
|
||||||
|
document
|
||||||
|
.set_cookie(&format!(
|
||||||
|
"user_id={}; expires={}",
|
||||||
|
&jwt_claims.user, &jwt_claims.exp
|
||||||
|
))
|
||||||
|
.unwrap();
|
||||||
|
callback.emit(Ok(jwt_claims.user.clone()))
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
callback.emit(Err(anyhow!("Could not parse response: {}", e)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if meta.status == 401 {
|
||||||
|
callback.emit(Err(anyhow!("Invalid username or password")))
|
||||||
|
} else {
|
||||||
|
callback.emit(Err(anyhow!(
|
||||||
|
"Could not authenticate: [{}]: {}",
|
||||||
|
meta.status,
|
||||||
|
data
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
callback.emit(Err(anyhow!("Could not reach authentication server: {}", e)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let request = Request::post(url)
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.body(Json(&request))?;
|
||||||
|
FetchService::fetch_with_options(request, get_default_options(), handler.into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,60 @@
|
|||||||
|
use crate::login::LoginForm;
|
||||||
use crate::user_table::UserTable;
|
use crate::user_table::UserTable;
|
||||||
|
use anyhow::{anyhow, Result};
|
||||||
|
use wasm_bindgen::JsCast;
|
||||||
use yew::prelude::*;
|
use yew::prelude::*;
|
||||||
|
|
||||||
pub struct App {}
|
pub struct App {
|
||||||
|
link: ComponentLink<Self>,
|
||||||
|
user_name: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
pub enum Msg {}
|
pub enum Msg {
|
||||||
|
Login(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
fn extract_user_id_cookie() -> Result<String> {
|
||||||
|
let document = web_sys::window()
|
||||||
|
.unwrap()
|
||||||
|
.document()
|
||||||
|
.unwrap()
|
||||||
|
.dyn_into::<web_sys::HtmlDocument>()
|
||||||
|
.unwrap();
|
||||||
|
let cookies = document.cookie().unwrap();
|
||||||
|
yew::services::ConsoleService::info(&cookies);
|
||||||
|
cookies
|
||||||
|
.split(";")
|
||||||
|
.filter_map(|c| c.split_once('='))
|
||||||
|
.map(|(name, value)| {
|
||||||
|
if name == "user_id" {
|
||||||
|
Ok(value.into())
|
||||||
|
} else {
|
||||||
|
Err(anyhow!("Wrong cookie"))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.filter(Result::is_ok)
|
||||||
|
.next()
|
||||||
|
.unwrap_or(Err(anyhow!("User ID cookie not found in response")))
|
||||||
|
}
|
||||||
|
|
||||||
impl Component for App {
|
impl Component for App {
|
||||||
type Message = Msg;
|
type Message = Msg;
|
||||||
type Properties = ();
|
type Properties = ();
|
||||||
|
|
||||||
fn create(_: Self::Properties, _link: ComponentLink<Self>) -> Self {
|
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
|
||||||
App {}
|
App {
|
||||||
|
link: link.clone(),
|
||||||
|
user_name: extract_user_id_cookie().ok(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(&mut self, _msg: Self::Message) -> ShouldRender {
|
fn update(&mut self, msg: Self::Message) -> ShouldRender {
|
||||||
false
|
match msg {
|
||||||
|
Msg::Login(user_name) => {
|
||||||
|
self.user_name = Some(user_name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
fn change(&mut self, _: Self::Properties) -> ShouldRender {
|
fn change(&mut self, _: Self::Properties) -> ShouldRender {
|
||||||
@ -25,7 +65,11 @@ impl Component for App {
|
|||||||
html! {
|
html! {
|
||||||
<div>
|
<div>
|
||||||
<h1>{ "LLDAP" }</h1>
|
<h1>{ "LLDAP" }</h1>
|
||||||
<UserTable />
|
{if self.user_name.is_some() {
|
||||||
|
html! {<UserTable />}
|
||||||
|
} else {
|
||||||
|
html! {<LoginForm on_logged_in=self.link.callback(|u| { Msg::Login(u) })/>}
|
||||||
|
}}
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#![recursion_limit = "256"]
|
#![recursion_limit = "256"]
|
||||||
mod api;
|
mod api;
|
||||||
mod app;
|
mod app;
|
||||||
|
mod login;
|
||||||
mod user_table;
|
mod user_table;
|
||||||
|
|
||||||
use wasm_bindgen::prelude::{wasm_bindgen, JsValue};
|
use wasm_bindgen::prelude::{wasm_bindgen, JsValue};
|
||||||
|
103
app/src/login.rs
Normal file
103
app/src/login.rs
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
use crate::api::HostService;
|
||||||
|
use anyhow::{anyhow, Result};
|
||||||
|
use lldap_model::*;
|
||||||
|
use wasm_bindgen::JsCast;
|
||||||
|
use yew::prelude::*;
|
||||||
|
use yew::services::fetch::FetchTask;
|
||||||
|
|
||||||
|
pub struct LoginForm {
|
||||||
|
link: ComponentLink<Self>,
|
||||||
|
on_logged_in: Callback<String>,
|
||||||
|
error: Option<anyhow::Error>,
|
||||||
|
node_ref: NodeRef,
|
||||||
|
task: Option<FetchTask>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, PartialEq, Properties)]
|
||||||
|
pub struct Props {
|
||||||
|
pub on_logged_in: Callback<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum Msg {
|
||||||
|
Submit,
|
||||||
|
AuthenticationResponse(Result<String>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Component for LoginForm {
|
||||||
|
type Message = Msg;
|
||||||
|
type Properties = Props;
|
||||||
|
|
||||||
|
fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
|
||||||
|
LoginForm {
|
||||||
|
link: link.clone(),
|
||||||
|
on_logged_in: props.on_logged_in,
|
||||||
|
error: None,
|
||||||
|
node_ref: NodeRef::default(),
|
||||||
|
task: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update(&mut self, msg: Self::Message) -> ShouldRender {
|
||||||
|
match msg {
|
||||||
|
Msg::Submit => {
|
||||||
|
let document = web_sys::window().unwrap().document().unwrap();
|
||||||
|
let username = document
|
||||||
|
.get_element_by_id("username")
|
||||||
|
.unwrap()
|
||||||
|
.dyn_into::<web_sys::HtmlInputElement>()
|
||||||
|
.unwrap()
|
||||||
|
.value();
|
||||||
|
let password = document
|
||||||
|
.get_element_by_id("password")
|
||||||
|
.unwrap()
|
||||||
|
.dyn_into::<web_sys::HtmlInputElement>()
|
||||||
|
.unwrap()
|
||||||
|
.value();
|
||||||
|
let req = BindRequest {
|
||||||
|
name: username.to_string(),
|
||||||
|
password: password.to_string(),
|
||||||
|
};
|
||||||
|
match HostService::authenticate(
|
||||||
|
req,
|
||||||
|
self.link.callback(Msg::AuthenticationResponse),
|
||||||
|
) {
|
||||||
|
Ok(task) => self.task = Some(task),
|
||||||
|
Err(e) => self.error = Some(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Msg::AuthenticationResponse(Ok(user_id)) => {
|
||||||
|
self.on_logged_in.emit(user_id);
|
||||||
|
}
|
||||||
|
Msg::AuthenticationResponse(Err(e)) => {
|
||||||
|
self.error = Some(anyhow!("Could not log in: {}", e));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn change(&mut self, _: Self::Properties) -> ShouldRender {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
fn view(&self) -> Html {
|
||||||
|
html! {
|
||||||
|
<form ref=self.node_ref.clone()>
|
||||||
|
<div>
|
||||||
|
<label for="username">{"User name:"}</label>
|
||||||
|
<input type="text" id="username" />
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label for="password">{"Password:"}</label>
|
||||||
|
<input type="password" id="password" />
|
||||||
|
</div>
|
||||||
|
<button type="button" onclick=self.link.callback(|_| { Msg::Submit })>{"Login"}</button>
|
||||||
|
<div>
|
||||||
|
{ if let Some(e) = &self.error {
|
||||||
|
html! { e.to_string() }
|
||||||
|
} else { html! {} }
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -7,7 +7,6 @@ use yew::services::{fetch::FetchTask, ConsoleService};
|
|||||||
|
|
||||||
pub struct UserTable {
|
pub struct UserTable {
|
||||||
link: ComponentLink<Self>,
|
link: ComponentLink<Self>,
|
||||||
ipservice: HostService,
|
|
||||||
task: Option<FetchTask>,
|
task: Option<FetchTask>,
|
||||||
users: Option<Result<Vec<User>>>,
|
users: Option<Result<Vec<User>>>,
|
||||||
}
|
}
|
||||||
@ -18,10 +17,7 @@ pub enum Msg {
|
|||||||
|
|
||||||
impl UserTable {
|
impl UserTable {
|
||||||
fn get_users(&mut self, req: ListUsersRequest) {
|
fn get_users(&mut self, req: ListUsersRequest) {
|
||||||
match self
|
match HostService::list_users(req, self.link.callback(Msg::ListUsersResponse)) {
|
||||||
.ipservice
|
|
||||||
.list_users(req, self.link.callback(Msg::ListUsersResponse))
|
|
||||||
{
|
|
||||||
Ok(task) => self.task = Some(task),
|
Ok(task) => self.task = Some(task),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
self.task = None;
|
self.task = None;
|
||||||
@ -38,7 +34,6 @@ impl Component for UserTable {
|
|||||||
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
|
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
|
||||||
let mut table = UserTable {
|
let mut table = UserTable {
|
||||||
link: link.clone(),
|
link: link.clone(),
|
||||||
ipservice: HostService::default(),
|
|
||||||
task: None,
|
task: None,
|
||||||
users: None,
|
users: None,
|
||||||
};
|
};
|
||||||
@ -54,7 +49,6 @@ impl Component for UserTable {
|
|||||||
true
|
true
|
||||||
}
|
}
|
||||||
Msg::ListUsersResponse(Err(e)) => {
|
Msg::ListUsersResponse(Err(e)) => {
|
||||||
self.task = None;
|
|
||||||
self.users = Some(Err(anyhow!("Error listing users: {}", e)));
|
self.users = Some(Err(anyhow!("Error listing users: {}", e)));
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user