Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: oidc: Wrap oidc cookie errors with enum #129

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading