2023-10-22 18:41:37 +02:00
|
|
|
use std::net::SocketAddr;
|
|
|
|
|
|
|
|
use axum::{
|
|
|
|
extract::{FromRef, State},
|
|
|
|
response::IntoResponse,
|
|
|
|
routing::get,
|
|
|
|
Router,
|
|
|
|
};
|
|
|
|
use nefarious_login::{
|
|
|
|
auth::AuthService,
|
|
|
|
axum::{AuthController, UserFromSession},
|
2023-10-22 22:33:47 +02:00
|
|
|
login::{
|
2023-11-01 21:51:09 +01:00
|
|
|
auth_clap::{AuthEngine, ZitadelClap},
|
|
|
|
config::ConfigClap,
|
2023-10-22 22:33:47 +02:00
|
|
|
AuthClap,
|
|
|
|
},
|
2023-10-22 22:44:12 +02:00
|
|
|
session::{PostgresqlSessionClap, SessionBackend},
|
2023-10-22 18:41:37 +02:00
|
|
|
};
|
2023-10-22 22:33:47 +02:00
|
|
|
use tracing_subscriber::EnvFilter;
|
2023-10-22 18:41:37 +02:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct AppState {
|
|
|
|
auth: AuthService,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> anyhow::Result<()> {
|
2023-10-22 22:33:47 +02:00
|
|
|
tracing_subscriber::fmt()
|
|
|
|
.with_env_filter(EnvFilter::from_default_env())
|
|
|
|
.init();
|
2023-10-22 18:41:37 +02:00
|
|
|
|
2023-10-22 22:33:47 +02:00
|
|
|
let auth = AuthClap {
|
|
|
|
engine: AuthEngine::Zitadel,
|
|
|
|
session_backend: SessionBackend::Postgresql,
|
|
|
|
zitadel: ZitadelClap {
|
|
|
|
authority_url: Some("https://personal-wxuujs.zitadel.cloud".into()),
|
|
|
|
client_id: Some("237412977047895154@nefarious-test".into()),
|
|
|
|
client_secret: Some(
|
|
|
|
"rWwDi8gjNOyuMFKoOjNSlhjcVZ1B25wDh6HsDL27f0g2Hb0xGbvEf0WXFY2akOlL".into(),
|
|
|
|
),
|
|
|
|
redirect_url: Some("http://localhost:3001/auth/authorized".into()),
|
|
|
|
},
|
|
|
|
session: nefarious_login::session::SessionClap {
|
|
|
|
postgresql: PostgresqlSessionClap {
|
2023-10-22 22:56:25 +02:00
|
|
|
conn: Some("postgres://nefarious-test:somenotverysecurepassword@localhost:5432/nefarious-test".into()),
|
2023-10-22 22:33:47 +02:00
|
|
|
},
|
|
|
|
},
|
2023-11-01 21:51:09 +01:00
|
|
|
config: ConfigClap { return_url: "http://localhost:3001/authed".into() }
|
2023-10-22 22:33:47 +02:00
|
|
|
};
|
|
|
|
|
2023-10-22 22:44:12 +02:00
|
|
|
let auth_service = AuthService::new(&auth).await?;
|
2023-10-22 18:41:37 +02:00
|
|
|
|
|
|
|
let state = AppState {
|
|
|
|
auth: auth_service.clone(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let app = Router::new()
|
|
|
|
.route("/unauthed", get(unauthed))
|
|
|
|
.route("/authed", get(authed))
|
|
|
|
.with_state(state)
|
|
|
|
.nest("/auth", AuthController::new_router(auth_service).await?);
|
|
|
|
|
|
|
|
let addr = SocketAddr::from(([127, 0, 0, 1], 3001));
|
|
|
|
println!("listening on: {addr}");
|
2023-10-22 22:33:47 +02:00
|
|
|
println!("open browser at: http://localhost:3001/auth/zitadel");
|
2023-10-22 18:41:37 +02:00
|
|
|
axum::Server::bind(&addr)
|
|
|
|
.serve(app.into_make_service())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromRef<AppState> for AuthService {
|
|
|
|
fn from_ref(input: &AppState) -> Self {
|
|
|
|
input.auth.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn unauthed() -> String {
|
|
|
|
"Hello Unauthorized User".into()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[axum::debug_handler()]
|
|
|
|
async fn authed(
|
|
|
|
user: UserFromSession,
|
|
|
|
State(_auth_service): State<AuthService>,
|
|
|
|
) -> impl IntoResponse {
|
|
|
|
format!("Hello authorized user: {:?}", user.user.id)
|
|
|
|
}
|