mirror of
https://github.com/speatzle/nfsense.git
synced 2025-09-13 15:19:08 +00:00
wip auth
This commit is contained in:
parent
2a7c13d612
commit
ed6d1fa3f0
9 changed files with 221 additions and 27 deletions
|
@ -5,7 +5,7 @@ use tracing_subscriber;
|
|||
|
||||
mod config_manager;
|
||||
mod definitions;
|
||||
mod router;
|
||||
mod web;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
|
@ -14,11 +14,11 @@ async fn main() {
|
|||
|
||||
// let mut config_manager = config_manager::new_config_manager().unwrap();
|
||||
|
||||
let app = router::get_router();
|
||||
let main_router = web::router::get_router();
|
||||
|
||||
info!("Server started successfully");
|
||||
axum::Server::bind(&"[::]:8080".parse().unwrap())
|
||||
.serve(app.await.into_make_service())
|
||||
.serve(main_router.await.into_make_service())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
|
|
|
@ -1,23 +0,0 @@
|
|||
use axum::{
|
||||
response::IntoResponse,
|
||||
routing::{get, post},
|
||||
Json, Router,
|
||||
};
|
||||
|
||||
use tracing::info;
|
||||
|
||||
pub async fn get_router() -> Router {
|
||||
Router::new().route("/health", get(health_checker_handler))
|
||||
}
|
||||
|
||||
pub async fn health_checker_handler() -> impl IntoResponse {
|
||||
info!("health hit");
|
||||
const MESSAGE: &str = "Hello there";
|
||||
|
||||
let json_response = serde_json::json!({
|
||||
"status": "success",
|
||||
"message": MESSAGE
|
||||
});
|
||||
|
||||
Json(json_response)
|
||||
}
|
97
src/web/auth.rs
Normal file
97
src/web/auth.rs
Normal file
|
@ -0,0 +1,97 @@
|
|||
use std::collections::HashMap;
|
||||
use std::hash::Hash;
|
||||
|
||||
use axum::routing::post;
|
||||
use axum::{Json, Router};
|
||||
use serde::Deserialize;
|
||||
use tower_cookies::Cookies;
|
||||
|
||||
use axum::{
|
||||
extract::Extension,
|
||||
http::{Request, StatusCode},
|
||||
middleware::{self, Next},
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
|
||||
const SESSION_COOKIE: &str = "session";
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct SessionState {
|
||||
sessions: HashMap<String, Session>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct Session {
|
||||
username: String,
|
||||
//expires: time,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
struct LoginParameters {
|
||||
username: String,
|
||||
password: String,
|
||||
}
|
||||
|
||||
pub fn routes() -> Router {
|
||||
let state = SessionState {
|
||||
sessions: HashMap::new(),
|
||||
};
|
||||
|
||||
Router::new()
|
||||
.route("/session", post(session_handler))
|
||||
.route("/login", post(login_handler))
|
||||
.route("/logout", post(logout_handler))
|
||||
}
|
||||
|
||||
async fn session_handler() -> impl IntoResponse {
|
||||
//return Err(StatusCode::UNAUTHORIZED);
|
||||
todo!()
|
||||
}
|
||||
|
||||
async fn login_handler(
|
||||
cookies: Cookies,
|
||||
Json(payload): Json<LoginParameters>,
|
||||
// mut session_state: SessionState,
|
||||
) -> impl IntoResponse {
|
||||
//cookies.add(Cookie::new());
|
||||
todo!()
|
||||
}
|
||||
|
||||
async fn logout_handler(
|
||||
cookies: Cookies,
|
||||
// mut session_state: SessionState
|
||||
) -> impl IntoResponse {
|
||||
/*
|
||||
if let Some(session_cookie) = cookies.get(SESSION_COOKIE) {
|
||||
let session_id = session_cookie.value();
|
||||
|
||||
// TODO check that sessions_id is a valid uuidv4
|
||||
|
||||
session_state.sessions.remove(session_id);
|
||||
cookies.remove(session_cookie);
|
||||
}
|
||||
*/
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub async fn mw_auth<B>(
|
||||
cookies: Cookies,
|
||||
mut req: Request<B>,
|
||||
next: Next<B>,
|
||||
// session_state: SessionState,
|
||||
) -> Result<Response, StatusCode> {
|
||||
/*
|
||||
if let Some(session_cookie) = cookies.get(SESSION_COOKIE) {
|
||||
let session_id = session_cookie.value();
|
||||
|
||||
// TODO check that sessions_id is a valid uuidv4
|
||||
|
||||
if let Some(session) = session_state.sessions.get(session_id) {
|
||||
req.extensions_mut().insert(session.clone());
|
||||
return Ok(next.run(req).await);
|
||||
}
|
||||
}
|
||||
return Err(StatusCode::UNAUTHORIZED);
|
||||
*/
|
||||
todo!()
|
||||
}
|
3
src/web/mod.rs
Normal file
3
src/web/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
pub mod auth;
|
||||
pub mod router;
|
||||
pub mod rpc;
|
15
src/web/router.rs
Normal file
15
src/web/router.rs
Normal file
|
@ -0,0 +1,15 @@
|
|||
use axum::{middleware, Router};
|
||||
|
||||
use tower_cookies::CookieManagerLayer;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct tmp {}
|
||||
|
||||
pub async fn get_router() -> Router {
|
||||
Router::new()
|
||||
.merge(super::auth::routes())
|
||||
.nest("/api", super::rpc::routes())
|
||||
.layer(middleware::from_fn_with_state(tmp {}, super::auth::mw_auth))
|
||||
.layer(CookieManagerLayer::new())
|
||||
// .fallback_service(service)
|
||||
}
|
5
src/web/rpc.rs
Normal file
5
src/web/rpc.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
use axum::Router;
|
||||
|
||||
pub fn routes() -> Router {
|
||||
Router::new()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue