Use light-openid to reduce code base size (#4)
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Refactor code to publicly share "redundant" code Reviewed-on: #4
This commit is contained in:
27
src/main.rs
27
src/main.rs
@ -1,11 +1,11 @@
|
||||
use actix_web::middleware::Logger;
|
||||
use actix_web::{get, web, App, HttpResponse, HttpServer};
|
||||
use askama::Template;
|
||||
use light_openid::basic_state_manager::BasicStateManager;
|
||||
use light_openid::primitives::OpenIDConfig;
|
||||
|
||||
use oidc_test_client::app_config::AppConfig;
|
||||
use oidc_test_client::openid_primitives::OpenIDConfig;
|
||||
use oidc_test_client::remote_ip::RemoteIP;
|
||||
use oidc_test_client::state_manager::StateManager;
|
||||
|
||||
#[get("/assets/bootstrap.min.css")]
|
||||
async fn bootstrap() -> HttpResponse {
|
||||
@ -60,8 +60,8 @@ async fn home() -> HttpResponse {
|
||||
}
|
||||
|
||||
#[get("/start")]
|
||||
async fn start(remote_ip: RemoteIP) -> HttpResponse {
|
||||
let config = match OpenIDConfig::load_from(&AppConfig::get().configuration_url).await {
|
||||
async fn start(remote_ip: RemoteIP, state_manager: web::Data<BasicStateManager>) -> HttpResponse {
|
||||
let config = match OpenIDConfig::load_from_url(&AppConfig::get().configuration_url).await {
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
log::error!("Failed to load OpenID configuration! {e}");
|
||||
@ -69,7 +69,7 @@ async fn start(remote_ip: RemoteIP) -> HttpResponse {
|
||||
}
|
||||
};
|
||||
|
||||
let state = match StateManager::gen_state(&remote_ip) {
|
||||
let state = match state_manager.gen_state(remote_ip.0) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
log::error!("Failed to generate state! {:?}", e);
|
||||
@ -77,7 +77,7 @@ async fn start(remote_ip: RemoteIP) -> HttpResponse {
|
||||
}
|
||||
};
|
||||
|
||||
let authorization_url = config.authorization_url(
|
||||
let authorization_url = config.gen_authorization_url(
|
||||
&AppConfig::get().client_id,
|
||||
&state,
|
||||
&AppConfig::get().redirect_url(),
|
||||
@ -95,15 +95,19 @@ struct RedirectQuery {
|
||||
}
|
||||
|
||||
#[get("/redirect")]
|
||||
async fn redirect(remote_ip: RemoteIP, query: web::Query<RedirectQuery>) -> HttpResponse {
|
||||
async fn redirect(
|
||||
remote_ip: RemoteIP,
|
||||
query: web::Query<RedirectQuery>,
|
||||
state_manager: web::Data<BasicStateManager>,
|
||||
) -> HttpResponse {
|
||||
// First, validate state
|
||||
if let Err(e) = StateManager::validate_state(&remote_ip, &query.state) {
|
||||
if let Err(e) = state_manager.validate_state(remote_ip.0, &query.state) {
|
||||
log::error!("Failed to validate state {}: {:?}", query.state, e);
|
||||
return ErrorTemplate::build("State could not be validated!");
|
||||
}
|
||||
|
||||
// Then, load OpenID configuration
|
||||
let config = match OpenIDConfig::load_from(&AppConfig::get().configuration_url).await {
|
||||
let config = match OpenIDConfig::load_from_url(&AppConfig::get().configuration_url).await {
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
log::error!("Failed to load OpenID configuration! {e}");
|
||||
@ -158,13 +162,14 @@ async fn main() -> std::io::Result<()> {
|
||||
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
||||
|
||||
log::info!("Init state manager");
|
||||
StateManager::init();
|
||||
let state_manager = web::Data::new(BasicStateManager::new());
|
||||
|
||||
log::info!("Will listen on {}", AppConfig::get().listen_addr);
|
||||
|
||||
HttpServer::new(|| {
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.wrap(Logger::default())
|
||||
.app_data(state_manager.clone())
|
||||
.service(bootstrap)
|
||||
.service(cover)
|
||||
.service(home)
|
||||
|
Reference in New Issue
Block a user