Ready to initiate OpenID login
This commit is contained in:
42
moneymgr_backend/src/controllers/auth_controller.rs
Normal file
42
moneymgr_backend/src/controllers/auth_controller.rs
Normal file
@ -0,0 +1,42 @@
|
||||
use crate::app_config::AppConfig;
|
||||
use crate::controllers::HttpResult;
|
||||
use crate::extractors::money_session::MoneySession;
|
||||
use actix_web::HttpResponse;
|
||||
use light_openid::primitives::OpenIDConfig;
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
struct StartOIDCResponse {
|
||||
url: String,
|
||||
}
|
||||
|
||||
/// Start OIDC authentication
|
||||
pub async fn start_oidc(session: MoneySession) -> HttpResult {
|
||||
let prov = AppConfig::get().openid_provider();
|
||||
|
||||
let conf = match OpenIDConfig::load_from_url(prov.configuration_url).await {
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
log::error!("Failed to fetch OpenID provider configuration! {e}");
|
||||
return Ok(HttpResponse::InternalServerError()
|
||||
.json("Failed to fetch OpenID provider configuration!"));
|
||||
}
|
||||
};
|
||||
|
||||
let state = match session.gen_oidc_state() {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
log::error!("Failed to generate auth state! {e}");
|
||||
return Ok(HttpResponse::InternalServerError().json("Failed to generate auth state!"));
|
||||
}
|
||||
};
|
||||
|
||||
Ok(HttpResponse::Ok().json(StartOIDCResponse {
|
||||
url: conf.gen_authorization_url(
|
||||
prov.client_id,
|
||||
&state,
|
||||
&AppConfig::get().oidc_redirect_url(),
|
||||
),
|
||||
}))
|
||||
}
|
||||
|
||||
// TODO : take from previous projects
|
@ -1 +1,42 @@
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::{HttpResponse, ResponseError};
|
||||
use std::error::Error;
|
||||
|
||||
pub mod auth_controller;
|
||||
pub mod server_controller;
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum HttpFailure {
|
||||
#[error("this resource requires higher privileges")]
|
||||
Forbidden,
|
||||
#[error("this resource was not found")]
|
||||
NotFound,
|
||||
#[error("Actix web error")]
|
||||
ActixError(#[from] actix_web::Error),
|
||||
#[error("an unhandled session insert error occurred")]
|
||||
SessionInsertError(#[from] actix_session::SessionInsertError),
|
||||
#[error("an unhandled session error occurred")]
|
||||
SessionError(#[from] actix_session::SessionGetError),
|
||||
#[error("an unspecified open id error occurred: {0}")]
|
||||
OpenID(Box<dyn Error>),
|
||||
#[error("an unspecified internal error occurred: {0}")]
|
||||
InternalError(#[from] anyhow::Error),
|
||||
#[error("a serde_json error occurred: {0}")]
|
||||
SerdeJsonError(#[from] serde_json::error::Error),
|
||||
}
|
||||
|
||||
impl ResponseError for HttpFailure {
|
||||
fn status_code(&self) -> StatusCode {
|
||||
match &self {
|
||||
Self::Forbidden => StatusCode::FORBIDDEN,
|
||||
Self::NotFound => StatusCode::NOT_FOUND,
|
||||
_ => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
}
|
||||
}
|
||||
|
||||
fn error_response(&self) -> HttpResponse {
|
||||
HttpResponse::build(self.status_code()).body(self.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
pub type HttpResult = Result<HttpResponse, HttpFailure>;
|
||||
|
Reference in New Issue
Block a user