Skip to content

Commit 5ad010b

Browse files
committed
Enhance OIDC client error handling and refactor HTTP request types
- Added context to OIDC client creation error handling. - Updated HTTP request and response types for better integration with the openidconnect library. - Introduced AwcWrapperError for improved error management in HTTP calls.
1 parent 249a687 commit 5ad010b

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

src/webserver/oidc.rs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ impl<S> OidcService<S> {
173173
let issuer_url = config.issuer_url.clone();
174174
let http_client = AwcHttpClient::new(&app_state.config)?;
175175
let provider_metadata = discover_provider_metadata(&http_client, issuer_url).await?;
176-
let client: OidcClient = make_oidc_client(&config, provider_metadata)?;
176+
let client: OidcClient = make_oidc_client(&config, provider_metadata)
177+
.with_context(|| format!("Unable to create OIDC client with config: {config:?}"))?;
177178
Ok(Self {
178179
service,
179180
config,
@@ -388,24 +389,23 @@ impl AwcHttpClient {
388389
}
389390

390391
impl<'c> AsyncHttpClient<'c> for AwcHttpClient {
391-
type Error = StringError;
392-
type Future = Pin<
393-
Box<dyn Future<Output = Result<openidconnect::http::Response<Vec<u8>>, Self::Error>> + 'c>,
394-
>;
392+
type Error = AwcWrapperError;
393+
type Future =
394+
Pin<Box<dyn Future<Output = Result<openidconnect::HttpResponse, Self::Error>> + 'c>>;
395395

396-
fn call(&'c self, request: openidconnect::http::Request<Vec<u8>>) -> Self::Future {
396+
fn call(&'c self, request: openidconnect::HttpRequest) -> Self::Future {
397397
let client = self.client.clone();
398398
Box::pin(async move {
399399
execute_oidc_request_with_awc(client, request)
400400
.await
401-
.map_err(|err| StringError(format!("Failed to execute OIDC request: {err:?}")))
401+
.map_err(|err| AwcWrapperError(err))
402402
})
403403
}
404404
}
405405

406406
async fn execute_oidc_request_with_awc(
407407
client: Client,
408-
request: openidconnect::http::Request<Vec<u8>>,
408+
request: openidconnect::HttpRequest,
409409
) -> Result<openidconnect::http::Response<Vec<u8>>, anyhow::Error> {
410410
let awc_method = awc::http::Method::from_bytes(request.method().as_str().as_bytes())?;
411411
let awc_uri = awc::http::Uri::from_str(&request.uri().to_string())?;
@@ -414,30 +414,36 @@ async fn execute_oidc_request_with_awc(
414414
for (name, value) in request.headers() {
415415
req = req.insert_header((name.as_str(), value.to_str()?));
416416
}
417-
let mut response = req
418-
.send_body(request.into_body())
419-
.await
420-
.map_err(|e| anyhow!("{:?}", e))?;
417+
let (req_head, body) = request.into_parts();
418+
let mut response = req.send_body(body).await.map_err(|e| {
419+
anyhow!(e.to_string()).context(format!(
420+
"Failed to send request: {} {}",
421+
&req_head.method, &req_head.uri
422+
))
423+
})?;
421424
let head = response.headers();
422425
let mut resp_builder =
423426
openidconnect::http::Response::builder().status(response.status().as_u16());
424427
for (name, value) in head {
425428
resp_builder = resp_builder.header(name.as_str(), value.to_str()?);
426429
}
427-
let body = response.body().await?.to_vec();
430+
let body = response
431+
.body()
432+
.await
433+
.with_context(|| format!("Couldnt read from {}", &req_head.uri))?;
428434
log::debug!(
429435
"Received OIDC response with status {}: {}",
430436
response.status(),
431437
String::from_utf8_lossy(&body)
432438
);
433-
let resp = resp_builder.body(body)?;
439+
let resp = resp_builder.body(body.to_vec())?;
434440
Ok(resp)
435441
}
436442

437-
#[derive(Debug, PartialEq, Eq)]
438-
pub struct StringError(String);
443+
#[derive(Debug)]
444+
pub struct AwcWrapperError(anyhow::Error);
439445

440-
impl std::fmt::Display for StringError {
446+
impl std::fmt::Display for AwcWrapperError {
441447
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
442448
std::fmt::Display::fmt(&self.0, f)
443449
}
@@ -450,7 +456,11 @@ type OidcClient = openidconnect::core::CoreClient<
450456
EndpointMaybeSet,
451457
EndpointMaybeSet,
452458
>;
453-
impl std::error::Error for StringError {}
459+
impl std::error::Error for AwcWrapperError {
460+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
461+
self.0.source()
462+
}
463+
}
454464

455465
fn make_oidc_client(
456466
config: &Arc<OidcConfig>,

0 commit comments

Comments
 (0)