use { hyper::{ Request, body::Incoming, }, serde_json::{ Value as Json, json, }, skytable::query, crate::{ DBPool, time, double_split, }, }; pub async fn api(req: Request, pool: DBPool) -> Json { let uri: &str = req.uri().path().as_ref(); match &uri[7..uri.len()] { "/auth" => auth(req, pool.clone()).await, "/auth_get" => auth_get(req, pool.clone()).await, _ => json!({"error": true, "msg": "No endpoint"}) } } async fn auth(req: Request, _pool: DBPool) -> Json { let query = req.uri().query().or(Some("")).unwrap(); let query = double_split(query.to_string(), "&", "="); let sess = std::string::String::from(query .get("session") .or(Some(&"".to_string())) .unwrap()); match sess.as_str() { "" => json!({"error": true, "msg": "No session in request"}), x if x.len() > 128 => json!({"error": true, "msg": "Session len is too long"}), _ => json!({ "error": false, "link": format!("https://auth.bitheaven.ru/authorize?v=0&session={}", sess) }) } } async fn auth_get(req: Request, pool: DBPool) -> Json { let mut con = pool.get().await.unwrap(); let query = req.uri().query().or(Some("")).unwrap(); let query = double_split(query.to_string(), "&", "="); let session = match query.get("session").is_none() { false => query.get("session").unwrap(), _ => "" }; let res = con.query_parse::<(String, String, u32)>(&query!( "SELECT login, uuid, expire FROM bitauth.v0 WHERE session = ?", session )).await; let _ = con.query_parse::<()>(&query!( "DELETE FROM bitauth.v0 WHERE session = ?", session )).await; let (login, uuid, exp) = match res.is_ok() { false => ("".to_owned(), "".to_owned(), 0), _ => res.unwrap() }; if login.as_str() == "" || exp < time() { json!({"error": true, "msg": "Not auth yet"}) } else { json!({"error": false, "login": login, "uuid": uuid}) } }