xjp_oidc/
client.rs

1//! High-level client for convenient SDK usage
2
3use crate::{
4    cache::{Cache, MokaCacheImpl},
5    errors::Result,
6    http::{HttpClient, ReqwestHttpClient},
7    jwks::Jwks,
8    types::OidcProviderMetadata,
9};
10use std::sync::Arc;
11
12/// Default OIDC client with built-in HTTP and cache implementations
13///
14/// This provides a convenient way to use the SDK without worrying about
15/// the generic parameters.
16#[cfg(all(not(target_arch = "wasm32"), feature = "http-reqwest", feature = "moka"))]
17pub struct OidcClient {
18    /// HTTP client
19    pub http: Arc<ReqwestHttpClient>,
20    /// Discovery metadata cache
21    pub discovery_cache: Arc<MokaCacheImpl<String, OidcProviderMetadata>>,
22    /// JWKS cache
23    pub jwks_cache: Arc<MokaCacheImpl<String, Jwks>>,
24}
25
26#[cfg(all(not(target_arch = "wasm32"), feature = "http-reqwest", feature = "moka"))]
27impl OidcClient {
28    /// Create a new OIDC client with default settings
29    pub fn new() -> Result<Self> {
30        let http = Arc::new(ReqwestHttpClient::default());
31        let discovery_cache = Arc::new(MokaCacheImpl::new(100));
32        let jwks_cache = Arc::new(MokaCacheImpl::new(100));
33
34        Ok(Self { http, discovery_cache, jwks_cache })
35    }
36
37    /// Get a reference to the HTTP client
38    pub fn http(&self) -> &dyn HttpClient {
39        self.http.as_ref()
40    }
41
42    /// Get a reference to the discovery cache
43    pub fn discovery_cache(&self) -> &dyn Cache<String, OidcProviderMetadata> {
44        self.discovery_cache.as_ref()
45    }
46
47    /// Get a reference to the JWKS cache
48    pub fn jwks_cache(&self) -> &dyn Cache<String, Jwks> {
49        self.jwks_cache.as_ref()
50    }
51}
52
53#[cfg(all(not(target_arch = "wasm32"), feature = "http-reqwest", feature = "moka"))]
54impl Default for OidcClient {
55    fn default() -> Self {
56        Self::new().expect("Failed to create default OIDC client")
57    }
58}