use { std::{ sync::Arc, net::SocketAddr, collections::HashMap, process::exit, }, chrono::{ DateTime, }, http_body_util::{ Full, BodyExt, }, hyper::{ StatusCode, Request, Response, Method, HeaderMap, header::HeaderValue, body::{ Bytes, Incoming, }, server::conn::http1 as Server, service::service_fn, }, hyper_util::{ rt::TokioIo, }, tokio::{ net::TcpListener, }, rsa::{ pkcs1::{ EncodeRsaPublicKey, EncodeRsaPrivateKey, }, RsaPrivateKey, RsaPublicKey, }, jsonwebtoken as jwt, jwt::{ Header, Algorithm, TokenData, Validation, EncodingKey, DecodingKey, encode as jwt_encode, decode as jwt_decode, }, serde_json::{ Value as Json, Map as JsonMap, json, }, skytable::{ query, Config, pool::{ self, ConnectionMgrTcp }, error::Error as SkyError, }, bb8::{ Pool, }, urlencoding::{ encode as url_encode, decode as url_decode, }, crate::{ types::{ users::Users, sites::Sites, }, html::*, funcs::*, url::{ api, login, }, }, }; type Res = std::result::Result; type Result = std::result::Result>; type DBPool = Arc>; type FullBytes = Result>>; pub async fn login(req: Request, pool: DBPool, headers: &mut HeaderMap) -> Result<(String, StatusCode, HeaderValue)> { let mut body = build_html(LOGIN_HTML); let mut status = StatusCode::OK; let restype: HeaderValue = "text/html".parse().unwrap(); if *req.method() == Method::POST { let r = double_split(req.uri().query().or(Some("")).unwrap().to_owned(), "&", "="); let post = get_body_from_request(req).await?; let post = String::from_utf8(post).unwrap(); let post = double_split(post, "&", "="); let (access, refresh) = login_user(pool.clone(), post).await?; set_cookie(headers, "token", &access); set_cookie(headers, "refresh", &refresh); let r = r.get("q"); match r.is_some() { true => { status = StatusCode::FOUND; body = "".to_owned(); set_location(headers, format!("{}", url_decode(r.unwrap())?).as_str()); }, _ => {} } } Ok((body, status, restype)) }