use crate::discovery::openid_configuration_json;
use crate::jwks::jwks_json;
use crate::jwt::JwtSigner;
use multistore::route_handler::{ProxyResult, RequestInfo, RouteHandler, RouteHandlerFuture};
use multistore::router::Router;
struct OidcConfigHandler {
issuer: String,
jwks_uri: String,
}
impl RouteHandler for OidcConfigHandler {
fn handle<'a>(&'a self, req: &'a RequestInfo<'a>) -> RouteHandlerFuture<'a> {
if req.method.as_str() != "GET" {
return Box::pin(async { None });
}
let json = openid_configuration_json(&self.issuer, &self.jwks_uri);
Box::pin(async move { Some(ProxyResult::json(200, json)) })
}
}
struct OidcJwksHandler {
signers: Vec<JwtSigner>,
}
impl RouteHandler for OidcJwksHandler {
fn handle<'a>(&'a self, req: &'a RequestInfo<'a>) -> RouteHandlerFuture<'a> {
if req.method.as_str() != "GET" {
return Box::pin(async { None });
}
let keys: Vec<_> = self
.signers
.iter()
.map(|s| (s.public_key(), s.kid()))
.collect();
let json = jwks_json(&keys);
Box::pin(async move { Some(ProxyResult::json(200, json)) })
}
}
pub trait OidcRouterExt {
fn with_oidc_discovery(self, issuer: String, signers: Vec<JwtSigner>) -> Self;
}
impl OidcRouterExt for Router {
fn with_oidc_discovery(self, issuer: String, signers: Vec<JwtSigner>) -> Self {
let jwks_uri = format!("{}/.well-known/jwks.json", issuer);
self.route(
"/.well-known/openid-configuration",
OidcConfigHandler { issuer, jwks_uri },
)
.route("/.well-known/jwks.json", OidcJwksHandler { signers })
}
}