2021-05-18 17:04:06 +00:00
|
|
|
use crate::cookies::set_cookie;
|
2021-05-13 17:33:57 +00:00
|
|
|
use anyhow::{anyhow, Result};
|
2021-05-09 11:26:50 +00:00
|
|
|
use lldap_model::*;
|
|
|
|
|
|
|
|
use yew::callback::Callback;
|
|
|
|
use yew::format::Json;
|
2021-05-13 17:33:57 +00:00
|
|
|
use yew::services::fetch::{Credentials, FetchOptions, FetchService, FetchTask, Request, Response};
|
2021-05-09 11:26:50 +00:00
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct HostService {}
|
|
|
|
|
2021-05-13 17:33:57 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2021-05-25 08:03:33 +00:00
|
|
|
fn create_handler<Resp, CallbackResult, F>(
|
|
|
|
callback: Callback<Result<CallbackResult>>,
|
|
|
|
handler: F,
|
|
|
|
) -> Callback<Response<Result<Resp>>>
|
|
|
|
where
|
|
|
|
F: Fn(http::StatusCode, Resp) -> Result<CallbackResult> + 'static,
|
|
|
|
Resp: std::fmt::Display,
|
|
|
|
CallbackResult: 'static,
|
|
|
|
{
|
|
|
|
Callback::once(move |response: Response<Result<Resp>>| {
|
|
|
|
let (meta, maybe_data) = response.into_parts();
|
|
|
|
let message = maybe_data
|
|
|
|
.map_err(|e| anyhow!("Could not reach server: {}", e))
|
|
|
|
.and_then(|data| handler(meta.status, data));
|
|
|
|
callback.emit(message)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-09 11:26:50 +00:00
|
|
|
impl HostService {
|
2021-05-10 08:21:24 +00:00
|
|
|
pub fn list_users(
|
|
|
|
request: ListUsersRequest,
|
|
|
|
callback: Callback<Result<Vec<User>>>,
|
|
|
|
) -> Result<FetchTask> {
|
2021-05-13 17:33:57 +00:00
|
|
|
let url = "/api/users";
|
|
|
|
let request = Request::post(url)
|
|
|
|
.header("Content-Type", "application/json")
|
|
|
|
.body(Json(&request))?;
|
2021-05-25 08:03:33 +00:00
|
|
|
let handler = create_handler(callback, |status, data: String| {
|
|
|
|
if status.is_success() {
|
|
|
|
serde_json::from_str(&data).map_err(|e| anyhow!("Could not parse response: {}", e))
|
|
|
|
} else {
|
|
|
|
Err(anyhow!("[{}]: {}", status, data))
|
|
|
|
}
|
|
|
|
});
|
|
|
|
FetchService::fetch_with_options(request, get_default_options(), handler)
|
2021-05-13 17:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn authenticate(
|
|
|
|
request: BindRequest,
|
|
|
|
callback: Callback<Result<String>>,
|
|
|
|
) -> Result<FetchTask> {
|
2021-05-20 15:41:30 +00:00
|
|
|
let url = "/auth";
|
2021-05-13 17:33:57 +00:00
|
|
|
let request = Request::post(url)
|
2021-05-10 08:21:24 +00:00
|
|
|
.header("Content-Type", "application/json")
|
2021-05-13 17:33:57 +00:00
|
|
|
.body(Json(&request))?;
|
2021-05-25 08:03:33 +00:00
|
|
|
let handler = create_handler(callback, |status, data: String| {
|
|
|
|
if status.is_success() {
|
|
|
|
get_claims_from_jwt(&data)
|
|
|
|
.map_err(|e| anyhow!("Could not parse response: {}", e))
|
|
|
|
.and_then(|jwt_claims| {
|
|
|
|
set_cookie("user_id", &jwt_claims.user, &jwt_claims.exp)
|
|
|
|
.map(|_| jwt_claims.user.clone())
|
|
|
|
.map_err(|e| anyhow!("Error clearing cookie: {}", e))
|
|
|
|
})
|
|
|
|
} else if status == 401 {
|
|
|
|
Err(anyhow!("Invalid username or password"))
|
|
|
|
} else {
|
|
|
|
Err(anyhow!("Could not authenticate: [{}]: {}", status, data))
|
|
|
|
}
|
|
|
|
});
|
|
|
|
FetchService::fetch_with_options(request, get_default_options(), handler)
|
2021-05-09 11:26:50 +00:00
|
|
|
}
|
2021-05-23 15:07:02 +00:00
|
|
|
|
|
|
|
pub fn logout(callback: Callback<Result<()>>) -> Result<FetchTask> {
|
|
|
|
let url = "/auth/logout";
|
|
|
|
let request = Request::post(url).body(yew::format::Nothing)?;
|
2021-05-25 08:03:33 +00:00
|
|
|
let handler = create_handler(callback, |status, data: String| {
|
|
|
|
if status.is_success() {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(anyhow!("Could not logout: [{}]: {}", status, data))
|
|
|
|
}
|
|
|
|
});
|
|
|
|
FetchService::fetch_with_options(request, get_default_options(), handler)
|
2021-05-23 15:07:02 +00:00
|
|
|
}
|
2021-06-01 15:30:57 +00:00
|
|
|
|
|
|
|
pub fn create_user(
|
|
|
|
request: CreateUserRequest,
|
|
|
|
callback: Callback<Result<()>>,
|
|
|
|
) -> Result<FetchTask> {
|
|
|
|
let url = "/api/users/create";
|
|
|
|
let request = Request::post(url)
|
|
|
|
.header("Content-Type", "application/json")
|
|
|
|
.body(Json(&request))?;
|
|
|
|
let handler = create_handler(callback, |status, data: String| {
|
|
|
|
if status.is_success() {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(anyhow!("Could not create a user: [{}]: {}", status, data))
|
|
|
|
}
|
|
|
|
});
|
|
|
|
FetchService::fetch_with_options(request, get_default_options(), handler)
|
|
|
|
}
|
2021-05-09 11:26:50 +00:00
|
|
|
}
|