feat: add auth middleware
This commit is contained in:
+52
-14
@@ -1,34 +1,72 @@
|
||||
mod auth;
|
||||
|
||||
use crate::auth::{keycloak, jwt};
|
||||
use crate::auth::middleware::auth_middleware;
|
||||
use crate::auth::jwt::Claims;
|
||||
|
||||
use dotenvy::dotenv;
|
||||
use axum::extract::Extension;
|
||||
use axum::{
|
||||
extract::Request,
|
||||
http::{HeaderMap, StatusCode},
|
||||
routing::get,
|
||||
Router,
|
||||
middleware,
|
||||
};
|
||||
use dotenvy::dotenv;
|
||||
use std::env;
|
||||
use std::net::SocketAddr;
|
||||
|
||||
pub async fn protected_route(
|
||||
Extension(claims): Extension<Claims>,
|
||||
) -> String {
|
||||
format!(
|
||||
"Hello {}, your user id is {}",
|
||||
claims
|
||||
.preferred_username
|
||||
.unwrap_or("unknown".to_string()),
|
||||
claims.sub
|
||||
)
|
||||
}
|
||||
|
||||
pub async fn public_route() -> &'static str {
|
||||
println!("Public route hit");
|
||||
"Public endpoint: no authentication required"
|
||||
}
|
||||
|
||||
pub fn app() -> Router {
|
||||
let public_routes = Router::new()
|
||||
.route("/", get(public_route));
|
||||
|
||||
let protected_routes = Router::new()
|
||||
.route("/protected", get(protected_route))
|
||||
.layer(middleware::from_fn(auth_middleware));
|
||||
|
||||
Router::new()
|
||||
.merge(public_routes)
|
||||
.merge(protected_routes)
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
dotenv().ok();
|
||||
|
||||
let jwks = keycloak::get_jwks().await;
|
||||
// let jwks = keycloak::get_jwks()
|
||||
// .await
|
||||
// .expect("Failed to fetch JWKS");
|
||||
|
||||
println!("{:?}", jwks);
|
||||
// // println!("{:?}", jwks);
|
||||
|
||||
// let app = Router::new().route("/protected", get(protected_route))
|
||||
// .route("/", get(dumb));
|
||||
// let token = "eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJublpLek04TkZHVmpWbGFPRXZpMUtFSTVHQWRwaGlsYjh3RHRLeG5JOENZIn0.eyJleHAiOjE3NzU3Mzg2MDUsImlhdCI6MTc3NTczODMwNSwianRpIjoiNjk2OTY4NzQtZWMwNi00NGFkLTg0MDYtYmY3YWM4MjI5MjkxIiwiaXNzIjoiaHR0cHM6Ly9hdXRoLmljZWJlcmcuYmxhY2svcmVhbG1zL2ljZWJlcmciLCJzdWIiOiJmZGRiN2FjZC1kMmE5LTRmMTctOWIxNi1kZjVlN2EzNDI4YjciLCJ0eXAiOiJCZWFyZXIiLCJhenAiOiJjaGF0LWFwaSIsInNjb3BlIjoiIiwiY2xpZW50SG9zdCI6Ijg2LjIxMi44NC4xOTEiLCJjbGllbnRBZGRyZXNzIjoiODYuMjEyLjg0LjE5MSIsImNsaWVudF9pZCI6ImNoYXQtYXBpIn0.BS7ohLWiMDxAUz_Q-Qi2UoLYbNn8AUrYeSWeO-602SQ-AYBW3gfYxXOSeRgWyn4VfObpVfK7QfqQBUxorXxi1JVld-4fGXL8NXQNyq5Ip_JHNG1p02Z39Pe9MmC9MXOwA_GQF2PIkLIdOJ_W_guXVhl2ptEWPPSiXM5Z5CNg8lyOiKPI0g2JWV6FBRG-HMXzqnxAb1j8wGUpC9JzGwAU3sjWBGhT1AAovs-XLmm5hZEPxI-Ia3SmUnF-QjFMmebPVxLdxL7OszzVEhKipsZRiwQxjY6eJhJFFa8uycBigHPSzu_HqqkK6AjNlyExvR0EGvl9zUWdOfMPDiVX2Sg92g";
|
||||
// match jwt::validate_token(token, &jwks) {
|
||||
// Ok(claims) => {
|
||||
// println!("Valid token for user: {:?}", claims);
|
||||
// }
|
||||
// Err(err) => {
|
||||
// println!("Invalid token: {}", err);
|
||||
// }
|
||||
// }
|
||||
|
||||
// let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
||||
// println!("Server running on {}", addr);
|
||||
let app = app();
|
||||
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
||||
println!("Server running on {}", addr);
|
||||
|
||||
// axum::serve(tokio::net::TcpListener::bind(addr).await.unwrap(), app)
|
||||
// .await
|
||||
// .unwrap();
|
||||
axum::serve(tokio::net::TcpListener::bind(addr).await.unwrap(), app)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
Reference in New Issue
Block a user