Fetch upstream configuration
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -11,8 +11,10 @@ use base64::Engine as _;
|
||||
use crate::utils::err::Res;
|
||||
use crate::utils::string_utils::rand_str;
|
||||
|
||||
const JWK_USE_SIGN: &str = "sig";
|
||||
|
||||
/// Json Web Key <https://datatracker.ietf.org/doc/html/rfc7517>
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct JsonWebKey {
|
||||
#[serde(rename = "alg")]
|
||||
algorithm: String,
|
||||
@@ -24,6 +26,8 @@ pub struct JsonWebKey {
|
||||
modulus: String,
|
||||
#[serde(rename = "e")]
|
||||
public_exponent: String,
|
||||
#[serde(rename = "use", skip_serializing_if = "Option::is_none")]
|
||||
usage: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -44,6 +48,7 @@ impl JWTSigner {
|
||||
key_id: self.0.key_id().as_ref().unwrap().to_string(),
|
||||
public_exponent: BASE64_URL_URL_SAFE.encode(components.e),
|
||||
modulus: BASE64_URL_SAFE_NO_PAD.encode(components.n),
|
||||
usage: Some(JWK_USE_SIGN.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -12,6 +12,7 @@ pub mod login_redirect;
|
||||
pub mod open_id_user_info;
|
||||
pub mod openid_config;
|
||||
pub mod provider;
|
||||
pub mod provider_configuration;
|
||||
pub mod remote_ip;
|
||||
pub mod session_identity;
|
||||
pub mod totp_key;
|
||||
|
75
src/data/provider_configuration.rs
Normal file
75
src/data/provider_configuration.rs
Normal file
@@ -0,0 +1,75 @@
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::constants::OIDC_PROVIDERS_LIFETIME;
|
||||
use crate::data::jwt_signer::JsonWebKey;
|
||||
use crate::data::provider::Provider;
|
||||
use crate::utils::err::Res;
|
||||
use crate::utils::time::time;
|
||||
|
||||
#[derive(Debug, Clone, serde::Deserialize)]
|
||||
pub struct ProviderDiscovery {
|
||||
pub issuer: String,
|
||||
pub authorization_endpoint: String,
|
||||
pub token_endpoint: String,
|
||||
pub userinfo_endpoint: Option<String>,
|
||||
pub jwks_uri: String,
|
||||
pub claims_supported: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct ProviderJWKs {
|
||||
pub keys: Vec<JsonWebKey>,
|
||||
}
|
||||
|
||||
/// Provider configuration
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ProviderConfiguration {
|
||||
pub discovery: ProviderDiscovery,
|
||||
pub keys: ProviderJWKs,
|
||||
pub expire: u64,
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static THREAD_CACHE: RefCell<HashMap<String, ProviderConfiguration>> = RefCell::new(Default::default());
|
||||
}
|
||||
|
||||
pub struct ProviderConfigurationHelper {}
|
||||
|
||||
impl ProviderConfigurationHelper {
|
||||
/// Get or refresh the configuration for a provider
|
||||
pub async fn get_configuration(provider: &Provider) -> Res<ProviderConfiguration> {
|
||||
let config = THREAD_CACHE.with(|i| i.borrow().get(&provider.configuration_url).cloned());
|
||||
|
||||
// Refresh config cache if needed
|
||||
if config.is_none() || config.as_ref().unwrap().expire < time() {
|
||||
let conf = Self::fetch_configuration(provider).await?;
|
||||
|
||||
THREAD_CACHE.with(|i| {
|
||||
i.borrow_mut()
|
||||
.insert(provider.configuration_url.clone(), conf.clone())
|
||||
});
|
||||
|
||||
return Ok(conf);
|
||||
}
|
||||
|
||||
// We can return immediately previously extracted value
|
||||
Ok(config.unwrap())
|
||||
}
|
||||
|
||||
/// Get fresh configuration from provider
|
||||
async fn fetch_configuration(provider: &Provider) -> Res<ProviderConfiguration> {
|
||||
let discovery: ProviderDiscovery = reqwest::get(&provider.configuration_url)
|
||||
.await?
|
||||
.json()
|
||||
.await?;
|
||||
|
||||
let keys: ProviderJWKs = reqwest::get(&discovery.jwks_uri).await?.json().await?;
|
||||
|
||||
Ok(ProviderConfiguration {
|
||||
discovery,
|
||||
keys,
|
||||
expire: time() + OIDC_PROVIDERS_LIFETIME,
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user