chore: add app_auth_handlers
This commit is contained in:
parent
e9836e77ba
commit
fc0a3684af
5 changed files with 268 additions and 0 deletions
186
app_auth_handlers/src/lib.rs
Normal file
186
app_auth_handlers/src/lib.rs
Normal file
|
|
@ -0,0 +1,186 @@
|
|||
use app_auth::{AppAuthDBs, AuthError, UserCtx, WebResult, LoginRequest, CheckinRequest };
|
||||
// use uuid::Uuid;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use reqtasks::ReqTasks;
|
||||
use app_env::profile::Profile;
|
||||
|
||||
use warp::{
|
||||
http::{method::Method, HeaderMap, HeaderValue, StatusCode},
|
||||
Reply, Rejection // , reject,
|
||||
};
|
||||
|
||||
#[allow(clippy::missing_errors_doc)]
|
||||
pub async fn member_handler(user_ctx: UserCtx) -> WebResult<impl Reply> {
|
||||
Ok(format!("Member with id {}", user_ctx.user_id))
|
||||
}
|
||||
|
||||
#[allow(clippy::missing_errors_doc)]
|
||||
pub async fn admin_handler(user_ctx: UserCtx) -> WebResult<impl Reply> {
|
||||
Ok(format!("Admin with id {}", user_ctx.user_id))
|
||||
}
|
||||
|
||||
#[allow(clippy::missing_errors_doc)]
|
||||
pub async fn login_handler (
|
||||
header: HeaderMap<HeaderValue>,
|
||||
method: Method,
|
||||
db: AppAuthDBs
|
||||
) -> Result<impl Reply, Rejection> {
|
||||
let reqenv = ReqTasks::new(db.app, db.auth, header, method, "", "login_header", "auth",);
|
||||
let lang = reqenv.lang();
|
||||
let mut ctx = reqenv.ctx();
|
||||
let req_module = "";
|
||||
let app_module: &str;
|
||||
if ! req_module.is_empty() && req_module != reqenv.config().default_module.as_str() {
|
||||
app_module = req_module;
|
||||
} else {
|
||||
app_module = "";
|
||||
}
|
||||
let mut data_hash: HashMap<String, String> = HashMap::new();
|
||||
data_hash.insert("lang".to_string(), lang.to_owned());
|
||||
// let allow_origin = reqenv.config().allow_origin;
|
||||
// let mut res = String::from("");
|
||||
// let res = if let Some(name) = query.get("name") {
|
||||
// submitted form
|
||||
match reqenv
|
||||
.render_page(
|
||||
&mut ctx,
|
||||
reqenv.config().templates_path.as_str(),
|
||||
"login/index.html",
|
||||
"index.html",
|
||||
format!("login/{}.toml", lang.to_owned())
|
||||
.to_owned()
|
||||
.as_str(),
|
||||
&mut data_hash,
|
||||
app_module,
|
||||
)
|
||||
.await {
|
||||
Ok(page) =>
|
||||
Ok(warp::http::Response::builder()
|
||||
.body(page)
|
||||
.into_response()),
|
||||
/*
|
||||
Ok(warp::reply::with_header(
|
||||
warp::http::Response::new(page),
|
||||
// "Access-Control-Allow-Origin",
|
||||
// &allow_origin)),
|
||||
*/
|
||||
Err(err) =>
|
||||
Ok(warp::http::Response::builder()
|
||||
.body(err.to_string())
|
||||
.into_response()),
|
||||
/*
|
||||
Ok(warp::reply::with_header(
|
||||
warp::http::Response::new(err.to_string()),
|
||||
"Access-Control-Allow-Origin",
|
||||
&allow_origin)),
|
||||
*/
|
||||
}
|
||||
}
|
||||
#[allow(clippy::missing_errors_doc)]
|
||||
pub async fn loginin_handler(
|
||||
body: LoginRequest,
|
||||
header: HeaderMap<HeaderValue>,
|
||||
method: Method,
|
||||
db: AppAuthDBs,
|
||||
) -> Result<impl Reply, Rejection> {
|
||||
let reqenv = ReqTasks::new(db.app, db.auth, header, method, "/loginin", "loginin_header", "auth",);
|
||||
let prfx = "ui";
|
||||
// ) -> WebResult<impl Reply> {
|
||||
// let name = body.name;
|
||||
// println!("{}", &name);
|
||||
// dbg!(&reqenv.auth_store.users.read().await);
|
||||
// let allow_origin = reqenv.config().allow_origin;
|
||||
match &reqenv.token_session(&body).await {
|
||||
Ok(token) => // Ok(token.to_string()),o
|
||||
match reqenv.check_authentication(token.to_owned()).await {
|
||||
Ok(usrctx) => {
|
||||
let mut path = format!("{}/profiles/{}/{}/defs.yaml",reqenv.config().resources_path,&body.mapkey,&usrctx.user_id);
|
||||
if ! std::path::Path::new(&path).exists() {
|
||||
path = format!("{}/profiles/{}/defs.yaml",reqenv.config().resources_path,&prfx);
|
||||
}
|
||||
let content = Profile::load_fs_content(path.into());
|
||||
// let lang = opts.lang.unwrap_or_else(|| String::from("es"));
|
||||
// let section = opts.section.unwrap_or_else(|| String::from(""));
|
||||
// let lang_items = LangItems::new("langs/ta",&lang,"yaml");
|
||||
// let result = lang_items.get_items_str(§ion);
|
||||
let res = Profile::to_yaml(content); // String::from("");
|
||||
let str_defs = serde_json::to_string(&res).unwrap_or_else(|_| String::from(""));
|
||||
Ok(warp::http::Response::builder()
|
||||
// .header("Access-Control-Allow-Origin",&allow_origin)
|
||||
.body(format!("{{ \"token\": \"{}\", \"defs\": {}}}",&token,&str_defs))
|
||||
.into_response())
|
||||
},
|
||||
Err(_e) => {
|
||||
Ok(warp::http::Response::builder()
|
||||
.status(StatusCode::NOT_FOUND)
|
||||
// .header("Access-Control-Allow-Origin",&allow_origin)
|
||||
.body(AuthError::UserNotFoundError.to_string())
|
||||
.into_response())
|
||||
}
|
||||
},
|
||||
Err(_) =>
|
||||
Ok(warp::http::Response::builder()
|
||||
.status(StatusCode::NOT_FOUND)
|
||||
// .header("Access-Control-Allow-Origin",&allow_origin)
|
||||
.body(AuthError::UserNotFoundError.to_string())
|
||||
.into_response())
|
||||
// Err(reject::custom(AuthError::UserNotFoundError))
|
||||
}
|
||||
}
|
||||
#[allow(clippy::missing_errors_doc)]
|
||||
pub async fn checkin_handler(
|
||||
body: CheckinRequest,
|
||||
header: HeaderMap<HeaderValue>,
|
||||
method: Method,
|
||||
db: AppAuthDBs,
|
||||
) -> Result<impl Reply, Rejection> {
|
||||
let reqenv = ReqTasks::new(db.app, db.auth, header, method, "/checkin", "checkin_header", "auth",);
|
||||
// ) -> WebResult<impl Reply> {
|
||||
// let name = &body.data;
|
||||
// println!("{}", &name);
|
||||
//dbg!("{}", &body);
|
||||
// dbg!(&reqenv.auth_store.users.read().await);
|
||||
// let allow_origin = reqenv.config().allow_origin;
|
||||
match &reqenv.check_authentication(body.data.to_owned()).await {
|
||||
Ok(usrctx) => { // Ok(token.to_string()),
|
||||
let mut path = format!("{}/profiles/{}/{}/defs.yaml",reqenv.config().resources_path,&body.mapkey,&usrctx.user_id);
|
||||
if ! std::path::Path::new(&path).exists() {
|
||||
path = format!("{}/profiles/{}/defs.yaml",reqenv.config().resources_path,&body.mapkey);
|
||||
}
|
||||
let content = Profile::load_fs_content(path.into());
|
||||
let res = Profile::to_yaml(content);
|
||||
let str_defs = serde_json::to_string(&res).unwrap_or_else(|_| String::from(""));
|
||||
Ok(warp::http::Response::builder()
|
||||
// .header("Access-Control-Allow-Origin",&allow_origin)
|
||||
.body(format!("{{ \"token\": \"{}\", \"defs\": {}}}",&body.data,&str_defs))
|
||||
.into_response())
|
||||
},
|
||||
Err(_) =>
|
||||
Ok(warp::http::Response::builder()
|
||||
.status(StatusCode::NOT_FOUND)
|
||||
//.header("Access-Control-Allow-Origin",&allow_origin)
|
||||
.body(AuthError::UserNotFoundError.to_string())
|
||||
.into_response())
|
||||
// Err(reject::custom(AuthError::UserNotFoundError))
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::missing_errors_doc,clippy::unnecessary_operation)]
|
||||
pub async fn logout_handler(
|
||||
user_ctx: UserCtx,
|
||||
header: HeaderMap<HeaderValue>,
|
||||
method: Method,
|
||||
db: AppAuthDBs,
|
||||
) -> Result<impl Reply, Rejection> {
|
||||
//-> WebResult<impl Reply> {
|
||||
let reqenv = ReqTasks::new(db.app, db.auth, header, method, "", "logout_header", "auth",);
|
||||
// let allow_origin = reqenv.config().allow_origin;
|
||||
&reqenv.auth_store.sessions.write().await.remove(&user_ctx.token);
|
||||
Ok(warp::http::Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
//.header("Access-Control-Allow-Origin",&allow_origin)
|
||||
.body("success".to_string())
|
||||
.into_response())
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue