Skip to content

Commit 9601478

Browse files
committed
Refactor OIDC authentication flow and error handling
1 parent bd3d0d3 commit 9601478

1 file changed

Lines changed: 14 additions & 16 deletions

File tree

src/webserver/oidc.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use actix_web::{
1616
};
1717
use anyhow::{anyhow, Context};
1818
use awc::Client;
19-
use base64::write;
2019
use chrono::Utc;
2120
use openidconnect::core::{
2221
CoreAuthDisplay, CoreAuthPrompt, CoreErrorResponseType, CoreGenderClaim, CoreJsonWebKey,
@@ -323,14 +322,15 @@ async fn handle_request(
323322
) -> actix_web::Result<MiddlewareResponse> {
324323
log::trace!("Started OIDC middleware request handling");
325324
oidc_state.refresh_if_expired(&request).await;
326-
let response = match get_authenticated_user_info(oidc_state, &request).await {
325+
match get_authenticated_user_info(oidc_state, &request).await {
327326
Ok(Some(claims)) => {
328327
if request.path() != SQLPAGE_REDIRECT_URI {
329328
log::trace!("Storing authenticated user info in request extensions: {claims:?}");
330329
request.extensions_mut().insert(claims);
331330
return Ok(MiddlewareResponse::Forward(request));
332331
}
333-
handle_authenticated_oidc_callback(request).await
332+
let response = handle_authenticated_oidc_callback(request);
333+
Ok(MiddlewareResponse::Respond(response))
334334
}
335335
Ok(None) => {
336336
log::trace!("No authenticated user found");
@@ -340,24 +340,28 @@ async fn handle_request(
340340
log::debug!("An auth cookie is present but could not be verified. Redirecting to OIDC provider to re-authenticate. {e:?}");
341341
handle_unauthenticated_request(oidc_state, request).await
342342
}
343-
};
344-
response.map(MiddlewareResponse::Respond)
343+
}
345344
}
346345

347346
async fn handle_unauthenticated_request(
348347
oidc_state: &OidcState,
349348
request: ServiceRequest,
350-
) -> Result<ServiceResponse<BoxBody>, Error> {
349+
) -> actix_web::Result<MiddlewareResponse> {
351350
log::debug!("Handling unauthenticated request to {}", request.path());
352351
if request.path() == SQLPAGE_REDIRECT_URI {
353352
log::debug!("The request is the OIDC callback");
354-
return handle_oidc_callback(oidc_state, request).await;
353+
let response = handle_oidc_callback(oidc_state, request).await?;
354+
return Ok(MiddlewareResponse::Respond(response));
355+
}
356+
357+
if oidc_state.config.is_public_path(request.path()) {
358+
return Ok(MiddlewareResponse::Forward(request));
355359
}
356360

357361
log::debug!("Redirecting to OIDC provider");
358362

359363
let response = build_auth_provider_redirect_response(oidc_state, &request).await;
360-
Ok(request.into_response(response))
364+
Ok(MiddlewareResponse::Respond(request.into_response(response)))
361365
}
362366

363367
async fn handle_oidc_callback(
@@ -376,16 +380,13 @@ async fn handle_oidc_callback(
376380
}
377381

378382
/// When an user has already authenticated (potentially in another tab), we ignore the callback and redirect to the initial URL.
379-
fn handle_authenticated_oidc_callback(
380-
request: ServiceRequest,
381-
) -> LocalBoxFuture<Result<ServiceResponse<BoxBody>, Error>> {
383+
fn handle_authenticated_oidc_callback(request: ServiceRequest) -> ServiceResponse {
382384
let redirect_url = match get_state_from_cookie(&request) {
383385
Ok(state) => state.initial_url,
384386
Err(_) => "/".to_string(),
385387
};
386388
log::debug!("OIDC callback received for authenticated user. Redirecting to {redirect_url}");
387-
let response = request.into_response(build_redirect_response(redirect_url));
388-
Box::pin(ready(Ok(response)))
389+
request.into_response(build_redirect_response(redirect_url))
389390
}
390391

391392
impl<S> Service<ServiceRequest> for OidcService<S>
@@ -400,9 +401,6 @@ where
400401
forward_ready!(service);
401402

402403
fn call(&self, request: ServiceRequest) -> Self::Future {
403-
if self.oidc_state.config.is_public_path(request.path()) {
404-
return Box::pin(self.service.call(request));
405-
}
406404
let srv = Rc::clone(&self.service);
407405
let oidc_state = Arc::clone(&self.oidc_state);
408406
Box::pin(async move {

0 commit comments

Comments
 (0)