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: log error responses returned by Zitadel #584

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
31 changes: 30 additions & 1 deletion src/oidc/introspection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ use custom_error::custom_error;
use openidconnect::http::Method;
use openidconnect::reqwest::async_http_client;
use openidconnect::url::{ParseError, Url};
use openidconnect::HttpResponse;
use openidconnect::{
core::CoreTokenType, ExtraTokenFields, HttpRequest, StandardTokenIntrospectionResponse,
};

use reqwest::header::{HeaderMap, ACCEPT, AUTHORIZATION, CONTENT_TYPE};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt::Debug;
use std::error::Error;
use std::fmt::{Debug, Display};

use crate::credentials::{Application, ApplicationError};

Expand All @@ -25,6 +27,7 @@ custom_error! {
ParseUrl{source: ParseError} = "could not parse url: {source}",
ParseResponse{source: serde_json::Error} = "could not parse introspection response: {source}",
DecodeResponse{source: base64::DecodeError} = "could not decode base64 metadata: {source}",
ResponseError{source: ZitadelResponseError} = "received error response from Zitadel: {source}",
}

/// Introspection response information that is returned by the ZITADEL
Expand Down Expand Up @@ -221,13 +224,39 @@ pub async fn introspect(
.await
.map_err(|source| IntrospectionError::RequestFailed { source })?;

if !response.status_code.is_success() {
return Err(IntrospectionError::ResponseError {
source: ZitadelResponseError::from_response(&response),
});
}

let mut response: ZitadelIntrospectionResponse =
serde_json::from_slice(response.body.as_slice())
.map_err(|source| IntrospectionError::ParseResponse { source })?;
decode_metadata(&mut response)?;
Ok(response)
}

#[derive(Debug)]
struct ZitadelResponseError {
status_code: String,
body: String,
}
impl ZitadelResponseError {
fn from_response(response: &HttpResponse) -> Self {
Self {
status_code: response.status_code.to_string(),
body: String::from_utf8_lossy(response.body.as_slice()).to_string(),
}
}
}
impl Display for ZitadelResponseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "status code: {}, body: {}", self.status_code, self.body)
}
}
impl Error for ZitadelResponseError {}

// Metadata values are base64 encoded.
fn decode_metadata(response: &mut ZitadelIntrospectionResponse) -> Result<(), IntrospectionError> {
if let Some(h) = &response.extra_fields().metadata {
Expand Down