One more commit

This commit is contained in:
BitHeaven 2024-05-20 11:07:56 +05:00
parent 8a7ff047b6
commit 2a9ac271a7
6 changed files with 101 additions and 3 deletions

13
TODO
View File

@ -11,6 +11,15 @@ Auth using email
##### API v0
# Auth link (device check url with this session and get data)
/auth?session=<UNIQUE>
/v0/auth?session=<UNIQUE>&back_url=<OPTIONAL>
# Get auth data
/auth_finish?session=<UNIQUE>
/v0/auth_finish?session=<UNIQUE>
##### API v1
# Auth link
/v1/auth?session=<UNIQUE>&back_url=<OPTIONAL>
# Get token
/v1/auth_finish?session=<UNIQUE>
# Refresh token
/v1/refresh?token=<REFRESH>

View File

@ -1,7 +1,7 @@
mod funcs;
mod types;
mod html;
mod api;
mod url;
use {
std::{
@ -84,6 +84,9 @@ use {
},
html::*,
funcs::*,
url::{
api,
},
},
};

1
src/url.rs Normal file
View File

@ -0,0 +1 @@
pub mod api;

View File

@ -1,4 +1,5 @@
mod v0;
mod v1;
use {
hyper::{
@ -26,6 +27,7 @@ pub async fn endpoint(req: Request<Incoming>, pool: DBPool) -> (String, StatusCo
let res: Json = match &uri[4..uri.len()] {
"/test" => json!({"error": false, "msg": "test"}),
x if x.starts_with("/v0/") => v0::api(req, pool.clone()).await,
x if x.starts_with("/v1/") => v1::api(req, pool.clone()).await,
_ => json!({"error": true, "msg": "No endpoint"})
};

83
src/url/api/v1.rs Normal file
View File

@ -0,0 +1,83 @@
use {
hyper::{
Request,
body::{
Incoming,
},
},
serde_json::{
Value as Json,
json,
},
skytable::{
pool::ConnectionMgrTcp,
query,
},
bb8::Pool,
std::sync::Arc,
crate::{
time,
double_split,
},
};
type DBPool = Arc<Pool<ConnectionMgrTcp>>;
pub async fn api(req: Request<Incoming>, 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<Incoming>, _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<Incoming>, 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})
}
}