#![warn(missing_docs)]
#![allow(clippy::result_large_err)]
mod handler;
pub mod observe;
mod state;
pub use handler::{allow, authenticate, authorization, error_response, handle};
pub use state::State;
use postrust_auth::jwks::{Fetch, JwksBuilder};
use postrust_auth::{Jwks, JwtConfig, JwtKeyError, JwtKeys};
use postrust_core::AppConfig;
pub fn jwt_config(config: &AppConfig) -> Result<JwtConfig, JwtKeyError> {
let keys = JwtKeys::from_config(config.jwt_secret.as_deref(), config.jwt_secret_is_base64)?;
Ok(jwt_config_with_keys(config, keys))
}
pub fn jwt_config_with_keys(config: &AppConfig, keys: Option<JwtKeys>) -> JwtConfig {
let mut jwt = JwtConfig::default();
jwt.keys = keys;
jwt.audience = config.jwt_aud.clone();
jwt.issuer = config.jwt_iss.clone();
jwt.role_claim_key = config.jwt_role_claim_key.clone();
jwt.anon_role = config.db_anon_role.clone();
jwt
}
pub fn jwks_builder(
config: &AppConfig,
keys: Option<&JwtKeys>,
fetch: impl Fetch + 'static,
) -> Option<JwksBuilder> {
let url = config.jwt_jwks_url.as_deref()?;
Some(
Jwks::builder(url, fetch)
.fixed(keys)
.refresh(std::time::Duration::from_secs(config.jwt_jwks_refresh)),
)
}