Move a lot funcs...
This commit is contained in:
124
src/url/login.rs
Normal file
124
src/url/login.rs
Normal file
@@ -0,0 +1,124 @@
|
||||
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<T, E> = std::result::Result<T, E>;
|
||||
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
|
||||
type DBPool = Arc<Pool<ConnectionMgrTcp>>;
|
||||
type FullBytes = Result<Response<Full<Bytes>>>;
|
||||
|
||||
|
||||
pub async fn uri_login(req: Request<Incoming>, 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))
|
||||
}
|
||||
Reference in New Issue
Block a user