Needs a lot of work
This commit is contained in:
63
src/server/auth/login.rs
Normal file
63
src/server/auth/login.rs
Normal file
@@ -0,0 +1,63 @@
|
||||
use crate::models::auth::{Claims, LoginInfo, LoginResponse};
|
||||
use axum::{
|
||||
extract::Json,
|
||||
http::{HeaderMap, StatusCode},
|
||||
};
|
||||
use jsonwebtoken::{decode, encode, DecodingKey, EncodingKey, Header, Validation};
|
||||
|
||||
pub fn is_valid_user(username: &str, password: &str) -> bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
pub async fn post_login_user(
|
||||
Json(login_info): Json<LoginInfo>,
|
||||
) -> Result<Json<LoginResponse>, StatusCode> {
|
||||
let user_authenticated = is_valid_user(&login_info.username, &login_info.password);
|
||||
|
||||
match user_authenticated {
|
||||
false => Err(StatusCode::UNAUTHORIZED),
|
||||
true => {
|
||||
let expires = match login_info.remember_me {
|
||||
true => chrono::Utc::now() + chrono::Duration::days(365),
|
||||
false => chrono::Utc::now() + chrono::Duration::days(1),
|
||||
};
|
||||
|
||||
let claims = Claims {
|
||||
sub: login_info.username.clone(),
|
||||
exp: expires.timestamp() as usize,
|
||||
};
|
||||
// @todo change secret
|
||||
let token = match encode(
|
||||
&Header::default(),
|
||||
&claims,
|
||||
&EncodingKey::from_secret("secret".as_ref()),
|
||||
) {
|
||||
Ok(token) => token,
|
||||
Err(_) => return Err(StatusCode::INTERNAL_SERVER_ERROR),
|
||||
};
|
||||
|
||||
let resp = LoginResponse { token, expires };
|
||||
Ok(Json(resp))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn post_test_login(header_map: HeaderMap) -> Result<Json<String>, StatusCode> {
|
||||
if let Some(auth_header) = header_map.get("Authorization") {
|
||||
if let Ok(auth_header_str) = auth_header.to_str() {
|
||||
if auth_header_str.starts_with("Bearer ") {
|
||||
let token = auth_header_str.trim_start_matches("Bearer ").to_string();
|
||||
// @todo change secret
|
||||
match decode::<Claims>(
|
||||
&token,
|
||||
&DecodingKey::from_secret("secret".as_ref()),
|
||||
&Validation::default(),
|
||||
) {
|
||||
Ok(_) => return Ok(Json("Logged in".to_owned())),
|
||||
Err(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(StatusCode::UNAUTHORIZED)
|
||||
}
|
||||
1
src/server/auth/mod.rs
Normal file
1
src/server/auth/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod login;
|
||||
@@ -1 +1,2 @@
|
||||
pub mod auth;
|
||||
pub mod routes;
|
||||
|
||||
@@ -1,24 +1,11 @@
|
||||
// (Server only) Routes
|
||||
use crate::{
|
||||
endpoints::{MATCH, USER},
|
||||
entity::{game, user},
|
||||
};
|
||||
use axum::{
|
||||
extract::Json,
|
||||
routing::{post, Router},
|
||||
};
|
||||
use crate::endpoints::{LOGIN, LOGIN_TEST};
|
||||
use axum::routing::{post, Router};
|
||||
|
||||
use super::auth::login::{post_login_user, post_test_login};
|
||||
|
||||
pub fn register_routes(app: Router) -> Router {
|
||||
let app = app.route(USER, post(post_user));
|
||||
app.route(MATCH, post(post_match))
|
||||
}
|
||||
|
||||
async fn post_user(_user: String) -> Json<user::Model> {
|
||||
// Update the store with the new match
|
||||
todo!()
|
||||
}
|
||||
|
||||
async fn post_match(_user: String) -> Json<game::Model> {
|
||||
// Update the store with the new match
|
||||
todo!()
|
||||
let app = app.route(LOGIN, post(post_login_user));
|
||||
let app = app.route(LOGIN_TEST, post(post_test_login));
|
||||
app
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user