ts_token/jwks/
mod.rs

1//! JSON web key set
2//!
3
4mod cache;
5#[cfg(feature = "http-jwks-provider")]
6pub mod http_jwks_provider;
7
8use alloc::vec::Vec;
9use core::convert::Infallible;
10
11use serde::{Deserialize, Serialize};
12
13use crate::jwk::JsonWebKey;
14
15pub use cache::JsonWebKeyCache;
16
17/// Trait to wrap fetching a JSON web key set.
18pub trait JsonWebKeySetProvider: Clone + Send + Sync + 'static {
19    /// The error type from the fetch operation.
20    type Error: core::error::Error + 'static;
21
22    /// Fetch from the JSON web key set.
23    fn fetch(&self) -> impl Future<Output = Result<JsonWebKeySet, Self::Error>> + Send + Sync;
24}
25
26/// A JSON web key set.
27#[derive(Deserialize, Serialize, Debug, Clone)]
28#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
29pub struct JsonWebKeySet {
30    /// The keys.
31    pub keys: Vec<JsonWebKey>,
32}
33
34/// A static JSON web key set provider.
35#[derive(Clone, Debug)]
36pub struct StaticJsonWebKeySet {
37    /// The JSON web key set.
38    jwks: JsonWebKeySet,
39}
40impl StaticJsonWebKeySet {
41    /// Create a new static JSON web key set provider
42    pub fn new(jwks: JsonWebKeySet) -> Self {
43        Self { jwks }
44    }
45}
46impl JsonWebKeySetProvider for StaticJsonWebKeySet {
47    type Error = Infallible;
48
49    async fn fetch(&self) -> Result<JsonWebKeySet, Self::Error> {
50        Ok(self.jwks.clone())
51    }
52}