Skip to content

Commit

Permalink
fix: oidc: Wrap oidc cookie errors with enum
Browse files Browse the repository at this point in the history
So they can be extracted later on if needed
  • Loading branch information
philipcristiano committed Oct 3, 2024
1 parent 9e05322 commit 3785b3b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ axum-core = {version = "0.4.3", optional = true}
async-trait = {version = "0.1.80", optional = true}
redacted = {version = "0.2.0", optional = true}
chrono = {version = "0.4.38", optional = true}
thiserror = "1.0.64"

[features]
all = ["tracing", "oidc", "tracing-http"]
Expand Down
58 changes: 42 additions & 16 deletions src/oidc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,29 @@ fn uic_name_to_name(
None
}

use thiserror::Error;
#[derive(Error, Debug)]
pub enum OIDCUserError {
#[error("Error loading cookies")]
CookieLoadError,
#[error("Missing Cookie")]
MissingCookie,
#[error("Problem during cookie Deserialize {0}")]
CookieDeserializeError(serde_json::Error),
}
impl axum::response::IntoResponse for OIDCUserError {
fn into_response(self) -> Response {
let r = match self {
OIDCUserError::CookieLoadError => (StatusCode::BAD_REQUEST, "Error loading cookies"),
OIDCUserError::MissingCookie => (StatusCode::BAD_REQUEST, "Missing User Cookie"),
OIDCUserError::CookieDeserializeError(_) => {
(StatusCode::BAD_REQUEST, "Problem with user cookie")
}
};
r.into_response()
}
}

use async_trait::async_trait;
use axum_core::extract::FromRequestParts;
use http::request::Parts;
Expand All @@ -89,25 +112,28 @@ impl<S> FromRequestParts<S> for OIDCUser
where
S: Send + Sync,
{
type Rejection = (http::StatusCode, &'static str);
type Rejection = OIDCUserError;

async fn from_request_parts(req: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
let cookies = Cookies::from_request_parts(req, state).await?;
let key = KEY.get().unwrap();
let private_cookies = cookies.private(key);

let cookie = match private_cookies.get(USER_COOKIE_NAME) {
Some(c) => c,
_ => return Err((StatusCode::BAD_REQUEST, "User Cookie problem a")),
};
let oidc_user = serde_json::from_str(&cookie.value());

match oidc_user {
Err(e) => {
tracing::error!("User Cookie problem {:?}", e);
Err((StatusCode::BAD_REQUEST, "User Cookie problem"))
if let Ok(cookies) = Cookies::from_request_parts(req, state).await {
let key = KEY.get().unwrap();
let private_cookies = cookies.private(key);

let cookie = match private_cookies.get(USER_COOKIE_NAME) {
Some(c) => c,
_ => return Err(OIDCUserError::MissingCookie),
};
let oidc_user = serde_json::from_str(&cookie.value());

match oidc_user {
Err(e) => {
tracing::error!("User Cookie problem {:?}", e);
Err(OIDCUserError::CookieDeserializeError(e))
}
Ok(ou) => Ok(ou),
}
Ok(ou) => Ok(ou),
} else {
Err(OIDCUserError::CookieLoadError)
}
}
}
Expand Down

0 comments on commit 3785b3b

Please sign in to comment.