refactoring

This commit is contained in:
Lennart
2025-04-14 17:32:53 +02:00
parent 8258e95bb1
commit 154d0c80c0
4 changed files with 52 additions and 48 deletions

View File

@@ -5,7 +5,7 @@ fn default_enabled() -> bool {
true true
} }
#[derive(Deserialize, Serialize, Debug, Clone)] #[derive(Deserialize, Serialize, Clone)]
pub struct OidcConfig { pub struct OidcConfig {
pub name: String, pub name: String,
pub issuer: IssuerUrl, pub issuer: IssuerUrl,
@@ -15,7 +15,7 @@ pub struct OidcConfig {
pub allow_sign_up: bool, pub allow_sign_up: bool,
} }
#[derive(Deserialize, Serialize, Debug, Clone)] #[derive(Deserialize, Serialize, Clone)]
#[serde(deny_unknown_fields)] #[serde(deny_unknown_fields)]
pub struct FrontendConfig { pub struct FrontendConfig {
#[serde(serialize_with = "hex::serde::serialize")] #[serde(serialize_with = "hex::serde::serialize")]

View File

@@ -0,0 +1,42 @@
use actix_session::SessionInsertError;
use actix_web::{
HttpResponse, ResponseError, body::BoxBody, error::UrlGenerationError, http::StatusCode,
};
use openidconnect::{ClaimsVerificationError, ConfigurationError, url::ParseError};
#[derive(Debug, thiserror::Error)]
pub enum OidcError {
#[error("Cannot generate redirect url, something's not configured correctly")]
OidcParseError(#[from] ParseError),
#[error("Cannot generate redirect url, something's not configured correctly")]
ActixUrlGenerationError(#[from] UrlGenerationError),
#[error("RustiCal is not configured correctly for OIDC")]
IncorrectConfiguration,
#[error(transparent)]
OidcConfigurationError(#[from] ConfigurationError),
#[error(transparent)]
OidcClaimsVerificationError(#[from] ClaimsVerificationError),
#[error(transparent)]
SessionInsertError(#[from] SessionInsertError),
#[error(transparent)]
StoreError(#[from] rustical_store::Error),
#[error("{0}")]
Other(&'static str),
}
impl ResponseError for OidcError {
fn status_code(&self) -> StatusCode {
StatusCode::INTERNAL_SERVER_ERROR
}
fn error_response(&self) -> HttpResponse<BoxBody> {
HttpResponse::build(self.status_code()).body(self.to_string())
}
}

View File

@@ -1,59 +1,21 @@
use crate::{FrontendConfig, config::OidcConfig}; use crate::{FrontendConfig, config::OidcConfig};
use actix_session::{Session, SessionInsertError}; use actix_session::Session;
use actix_web::{ use actix_web::{
HttpRequest, HttpResponse, Responder, ResponseError, HttpRequest, HttpResponse, Responder,
body::BoxBody,
error::UrlGenerationError,
http::StatusCode, http::StatusCode,
web::{Data, Form, Query, Redirect}, web::{Data, Form, Query, Redirect},
}; };
use error::OidcError;
use openidconnect::{ use openidconnect::{
AuthenticationFlow, AuthorizationCode, ClaimsVerificationError, ConfigurationError, CsrfToken, AuthenticationFlow, AuthorizationCode, CsrfToken, EmptyAdditionalClaims, EndpointMaybeSet,
EmptyAdditionalClaims, EndpointMaybeSet, EndpointNotSet, EndpointSet, IssuerUrl, Nonce, EndpointNotSet, EndpointSet, IssuerUrl, Nonce, OAuth2TokenResponse, PkceCodeChallenge,
OAuth2TokenResponse, PkceCodeChallenge, PkceCodeVerifier, RedirectUrl, TokenResponse, PkceCodeVerifier, RedirectUrl, TokenResponse, UserInfoClaims,
UserInfoClaims,
core::{CoreClient, CoreGenderClaim, CoreProviderMetadata, CoreResponseType}, core::{CoreClient, CoreGenderClaim, CoreProviderMetadata, CoreResponseType},
url::ParseError,
}; };
use rustical_store::auth::{AuthenticationProvider, User, user::PrincipalType::Individual}; use rustical_store::auth::{AuthenticationProvider, User, user::PrincipalType::Individual};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Debug, thiserror::Error)] mod error;
pub enum OidcError {
#[error("Cannot generate redirect url, something's not configured correctly")]
OidcParseError(#[from] ParseError),
#[error("Cannot generate redirect url, something's not configured correctly")]
ActixUrlGenerationError(#[from] UrlGenerationError),
#[error("RustiCal is not configured correctly for OIDC")]
IncorrectConfiguration,
#[error(transparent)]
OidcConfigurationError(#[from] ConfigurationError),
#[error(transparent)]
OidcClaimsVerificationError(#[from] ClaimsVerificationError),
#[error(transparent)]
SessionInsertError(#[from] SessionInsertError),
#[error(transparent)]
StoreError(#[from] rustical_store::Error),
#[error("{0}")]
Other(&'static str),
}
impl ResponseError for OidcError {
fn status_code(&self) -> StatusCode {
StatusCode::INTERNAL_SERVER_ERROR
}
fn error_response(&self) -> HttpResponse<BoxBody> {
HttpResponse::build(self.status_code()).body(self.to_string())
}
}
pub(crate) struct OidcProviderData<'a> { pub(crate) struct OidcProviderData<'a> {
pub name: &'a str, pub name: &'a str,

View File

@@ -23,7 +23,7 @@ pub struct GetLoginQuery {
redirect_uri: Option<String>, redirect_uri: Option<String>,
} }
#[instrument(skip(req))] #[instrument(skip(req, config))]
pub async fn route_get_login( pub async fn route_get_login(
Query(GetLoginQuery { redirect_uri }): Query<GetLoginQuery>, Query(GetLoginQuery { redirect_uri }): Query<GetLoginQuery>,
req: HttpRequest, req: HttpRequest,