Skip to content

Commit

Permalink
Create new Open ID client if old client is older than 60s (#1226)
Browse files Browse the repository at this point in the history
This PR adds an explicit lifetime to the Open ID connection, and if the
connection has existed for more than 60 seconds, it will initialise a new
connection in order to pick up possible configuration changes at the provider.
  • Loading branch information
Koenvh1 authored Jul 29, 2024
1 parent afc1e78 commit 7ba7e22
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/daemon/auth/providers/openid_connect/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ use std::{
HashMap,
},
ops::Deref,
sync::Arc,
sync::Arc,
time::Instant,
};

use tokio::sync::{RwLock, RwLockReadGuard};
Expand Down Expand Up @@ -151,6 +152,7 @@ pub struct ProviderConnectionProperties {
client: FlexibleClient,
email_scope_supported: bool,
userinfo_endpoint_supported: bool,
time_established: Instant,
logout_mode: LogoutMode,
}

Expand Down Expand Up @@ -179,7 +181,11 @@ impl OpenIDConnectAuthProvider {
async fn initialize_connection_if_needed(&self) -> KrillResult<()> {
let mut conn_guard = self.conn.write().await;

if conn_guard.is_none() {
// If we don’t have a connection or it is older than 60 seconds,
// get a new one.
if conn_guard.as_ref().map(|c| {
c.time_established.elapsed().as_secs()
}).unwrap_or(60) >= 60 {
*conn_guard = Some(self.initialize_connection().await?);
}

Expand All @@ -194,10 +200,12 @@ impl OpenIDConnectAuthProvider {
let (email_scope_supported, userinfo_endpoint_supported, logout_mode) =
self.check_provider_capabilities(&meta)?;
let client = self.build_client(meta, &logout_mode)?;
let time_established = Instant::now();
let conn = ProviderConnectionProperties {
client,
email_scope_supported,
userinfo_endpoint_supported,
time_established,
logout_mode,
};
trace!("OpenID Connect: Provider connection initialized");
Expand Down

0 comments on commit 7ba7e22

Please sign in to comment.