#![cfg(feature = "oauth2-server")]
use std::sync::Arc;
use axum::extract::{Extension, Query, State};
use axum::http::{HeaderMap, HeaderName, HeaderValue, StatusCode};
use axum::response::{IntoResponse, Response};
use axum::routing::{get, post};
use axum::{Json, Router};
use serde_json::json;
use crate::context::GarrisonPrincipal;
use crate::dao::GarrisonDao;
use crate::error::{GarrisonError, GarrisonResult};
use crate::loc;
use crate::oauth2_server::authorize::{AuthorizeHandler, AuthorizeRequest, AuthorizeResponse};
use crate::oauth2_server::client::OAuth2ClientStore;
use crate::oauth2_server::introspect::{IntrospectHandler, IntrospectRequest};
use crate::oauth2_server::revoke::{RevokeHandler, RevokeRequest};
use crate::oauth2_server::token::{
PasswordRateLimiter, TokenHandler, TokenRateLimiter, TokenRequest,
};
pub struct OAuth2State {
pub authorize_handler: Arc<AuthorizeHandler>,
pub token_handler: Arc<TokenHandler>,
pub revoke_handler: Arc<RevokeHandler>,
pub introspect_handler: Arc<IntrospectHandler>,
pub client_store: Arc<dyn OAuth2ClientStore>,
pub jwks_source: Option<(String, String)>,
pub issuer: Option<String>,
pub jwks_keystore: Option<Arc<crate::oauth2_server::jwks::JwksKeystore>>,
pub oidc_discovery_enabled: bool,
pub password_grant_advertised: bool,
pub jwks_retention_secs: u64,
#[doc(hidden)]
pub scopes_cache:
tokio::sync::RwLock<Option<(std::time::Instant, std::sync::Arc<Vec<String>>)>>,
#[doc(hidden)]
pub jwks_doc_cache: tokio::sync::Mutex<Option<JwksDocCacheEntry>>,
}
#[doc(hidden)]
pub struct JwksDocCacheEntry {
pub state_hash: u64,
pub now_bucket: i64,
pub retention_secs: u64,
pub doc: std::sync::Arc<crate::oauth2_server::jwks::JwkSet>,
}
impl OAuth2State {
pub fn new(
store: Arc<dyn OAuth2ClientStore>,
dao: Arc<dyn GarrisonDao>,
login_url: String,
) -> Self {
let authorize_handler =
Arc::new(AuthorizeHandler::new(store.clone(), dao.clone(), login_url));
let token_handler = Arc::new(TokenHandler::new(
store.clone(),
dao.clone(),
authorize_handler.clone(),
Arc::new(PasswordRateLimiter::new(5, 300)),
Arc::new(TokenRateLimiter::new()),
));
let revoke_handler = Arc::new(RevokeHandler::new(store.clone(), token_handler.clone()));
let introspect_handler =
Arc::new(IntrospectHandler::new(store.clone(), token_handler.clone()));
#[cfg(feature = "db-sqlite")]
if !token_handler.has_refresh_rotation() {
tracing::warn!(
"{}",
crate::i18n::translate_detail(
"logs-server-oauth2-state-without-refresh-rotation",
&[]
)
);
}
Self {
authorize_handler,
token_handler,
revoke_handler,
introspect_handler,
client_store: store,
jwks_source: None,
issuer: None,
jwks_keystore: None,
oidc_discovery_enabled: cfg!(feature = "protocol-oidc"),
password_grant_advertised: false,
jwks_retention_secs: crate::config::DEFAULT_JWKS_RETENTION_SECS,
scopes_cache: tokio::sync::RwLock::new(None),
jwks_doc_cache: tokio::sync::Mutex::new(None),
}
}
pub fn with_jwks_source(mut self, jwt_algorithm: &str, private_key_pem: &str) -> Self {
self.jwks_source = Some((jwt_algorithm.to_string(), private_key_pem.to_string()));
self
}
pub fn with_issuer(mut self, issuer: impl Into<String>) -> Self {
self.issuer = Some(issuer.into());
self
}
pub fn with_jwks_keystore(
mut self,
keystore: Arc<crate::oauth2_server::jwks::JwksKeystore>,
) -> Self {
self.jwks_keystore = Some(keystore);
self
}
pub fn with_oidc_discovery_enabled(mut self, enabled: bool) -> Self {
self.oidc_discovery_enabled = enabled;
self
}
pub fn with_password_grant_advertised(mut self, advertised: bool) -> Self {
self.password_grant_advertised = advertised;
self
}
pub fn with_jwks_retention_secs(mut self, retention_secs: u64) -> Self {
if retention_secs == 0 {
let secs = crate::config::DEFAULT_JWKS_RETENTION_SECS.to_string();
tracing::warn!(
"{}",
crate::i18n::translate_detail(
"logs-server-jwks-retention-zero-fallback",
&[("secs", secs.as_str())]
)
);
self.jwks_retention_secs = crate::config::DEFAULT_JWKS_RETENTION_SECS;
return self;
}
self.jwks_retention_secs = retention_secs;
self
}
}
pub fn oauth2_external_router(state: Arc<OAuth2State>) -> Router {
Router::new()
.route("/oauth2/authorize", get(authorize_endpoint))
.route("/oauth2/authorize/resume", get(authorize_resume_endpoint))
.route("/oauth2/token", post(token_endpoint))
.route("/oauth2/revoke", post(revoke_endpoint))
.route("/oauth2/jwks.json", get(jwks_endpoint))
.route(
"/.well-known/openid-configuration",
get(openid_configuration_endpoint),
)
.route(
"/.well-known/oauth-authorization-server",
get(oauth_authorization_server_endpoint),
)
.with_state(state)
}
const DISCOVERY_CACHE_CONTROL: &str = "public, max-age=300";
async fn openid_configuration_endpoint(State(state): State<Arc<OAuth2State>>) -> Response {
if !state.oidc_discovery_enabled {
return StatusCode::NOT_FOUND.into_response();
}
discovery_response(&state, DiscoveryFlavor::Oidc).await
}
async fn oauth_authorization_server_endpoint(State(state): State<Arc<OAuth2State>>) -> Response {
discovery_response(&state, DiscoveryFlavor::Rfc8414).await
}
enum DiscoveryFlavor {
Rfc8414,
Oidc,
}
async fn discovery_response(state: &OAuth2State, flavor: DiscoveryFlavor) -> Response {
let Some(issuer) = state.issuer.as_deref() else {
return StatusCode::NOT_FOUND.into_response();
};
let mut grant_types = vec![
"authorization_code".to_string(),
"refresh_token".to_string(),
"client_credentials".to_string(),
];
if state.password_grant_advertised {
grant_types.push("password".to_string());
}
let scopes_arc = {
let cached = state.scopes_cache.read().await.clone();
match cached {
Some((at, scopes)) if at.elapsed() < std::time::Duration::from_secs(300) => scopes,
_ => match derive_scopes_supported(state).await {
Ok(scopes) => scopes,
Err(resp) => return resp,
},
}
};
let mut scopes = (*scopes_arc).clone();
if scopes.is_empty() {
scopes.push("openid".to_string());
}
let jwks_algorithms = match published_jwk_algorithms(state).await {
Ok(algs) => algs,
Err(e) => {
tracing::error!(
error = %e,
"{}",
crate::i18n::translate_detail(
"logs-server-discovery-jwks-algorithms-failed",
&[]
)
);
return StatusCode::INTERNAL_SERVER_ERROR.into_response();
},
};
let mut metadata = serde_json::json!({
"issuer": issuer,
"authorization_endpoint": format!("{issuer}/oauth2/authorize"),
"token_endpoint": format!("{issuer}/oauth2/token"),
"revocation_endpoint": format!("{issuer}/oauth2/revoke"),
"grant_types_supported": grant_types,
"response_types_supported": ["code"],
"scopes_supported": scopes,
"token_endpoint_auth_methods_supported": ["client_secret_basic", "client_secret_post"],
"code_challenge_methods_supported": ["S256"],
});
if let Some(algs) = jwks_algorithms {
metadata["jwks_uri"] = serde_json::Value::String(format!("{issuer}/oauth2/jwks.json"));
metadata["jwks_algorithms"] =
serde_json::Value::Array(algs.into_iter().map(serde_json::Value::String).collect());
}
if matches!(flavor, DiscoveryFlavor::Oidc) {
metadata["subject_types_supported"] = serde_json::json!(["public"]);
}
(
StatusCode::OK,
[(axum::http::header::CACHE_CONTROL, DISCOVERY_CACHE_CONTROL)],
axum::Json(metadata),
)
.into_response()
}
#[allow(clippy::result_large_err)]
async fn derive_scopes_supported(
state: &OAuth2State,
) -> Result<std::sync::Arc<Vec<String>>, Response> {
match state.client_store.list().await {
Ok(clients) => {
let mut scopes: Vec<String> = clients.into_iter().flat_map(|c| c.scopes).collect();
scopes.sort();
scopes.dedup();
let scopes = std::sync::Arc::new(scopes);
let mut cache = state.scopes_cache.write().await;
*cache = Some((std::time::Instant::now(), std::sync::Arc::clone(&scopes)));
Ok(scopes)
},
Err(e) => {
tracing::error!(
error = %e,
"{}",
crate::i18n::translate_detail("logs-server-discovery-scopes-failed", &[])
);
Err(StatusCode::INTERNAL_SERVER_ERROR.into_response())
},
}
}
async fn published_jwk_algorithms(state: &OAuth2State) -> GarrisonResult<Option<Vec<String>>> {
if let Some(keystore) = &state.jwks_keystore {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_err(|e| GarrisonError::Internal(format!("jwks-clock-invalid::{e}")))?;
let doc = keystore_jwks_document_cached(state, keystore, now.as_secs() as i64).await?;
let Some(doc) = doc else {
return Ok(None);
};
if doc.keys.is_empty() {
return Ok(None);
}
let mut algs: Vec<String> = doc.keys.iter().map(|k| k.alg.clone()).collect();
algs.sort();
algs.dedup();
return Ok(Some(algs));
}
Ok(state
.jwks_source
.as_ref()
.map(|(algorithm, _)| vec![algorithm.clone()]))
}
fn state_content_hash(raw: Option<&str>) -> u64 {
use std::hash::{Hash, Hasher};
let mut hasher = std::collections::hash_map::DefaultHasher::new();
raw.hash(&mut hasher);
hasher.finish()
}
async fn keystore_jwks_document_cached(
state: &OAuth2State,
keystore: &crate::oauth2_server::jwks::JwksKeystore,
now: i64,
) -> GarrisonResult<Option<std::sync::Arc<crate::oauth2_server::jwks::JwkSet>>> {
let now_bucket = now.div_euclid(60);
let raw = keystore.state_raw().await?;
let hash = state_content_hash(raw.as_deref());
{
let cache = state.jwks_doc_cache.lock().await;
if let Some(entry) = cache.as_ref() {
if entry.state_hash == hash
&& entry.now_bucket == now_bucket
&& entry.retention_secs == state.jwks_retention_secs
{
return Ok(Some(std::sync::Arc::clone(&entry.doc)));
}
}
}
let doc = std::sync::Arc::new(
keystore
.jwks_document(now, state.jwks_retention_secs)
.await?,
);
{
let mut cache = state.jwks_doc_cache.lock().await;
*cache = Some(JwksDocCacheEntry {
state_hash: hash,
now_bucket,
retention_secs: state.jwks_retention_secs,
doc: std::sync::Arc::clone(&doc),
});
}
Ok(Some(doc))
}
async fn jwks_endpoint(State(state): State<Arc<OAuth2State>>) -> Response {
let max_age = jwks_cache_max_age_secs(state.jwks_retention_secs);
let cache_control = format!("public, max-age={max_age}");
if let Some(keystore) = &state.jwks_keystore {
let now = match std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH) {
Ok(d) => d.as_secs() as i64,
Err(_) => return StatusCode::NOT_FOUND.into_response(),
};
return match keystore_jwks_document_cached(&state, keystore, now).await {
Ok(Some(doc)) if !doc.keys.is_empty() => (
StatusCode::OK,
[(axum::http::header::CACHE_CONTROL, cache_control)],
axum::Json((*doc).clone()),
)
.into_response(),
Ok(_) => StatusCode::NOT_FOUND.into_response(),
Err(e) => {
tracing::error!(
error = %e,
"{}",
crate::i18n::translate_detail("logs-server-jwks-document-build-failed", &[])
);
StatusCode::NOT_FOUND.into_response()
},
};
}
match &state.jwks_source {
Some((algorithm, pem)) => match crate::oauth2_server::jwks::build_jwk_set(algorithm, pem) {
Ok(jwk_set) => (
StatusCode::OK,
[(axum::http::header::CACHE_CONTROL, cache_control)],
axum::Json(jwk_set),
)
.into_response(),
Err(_) => StatusCode::NOT_FOUND.into_response(),
},
None => StatusCode::NOT_FOUND.into_response(),
}
}
fn jwks_cache_max_age_secs(retention_secs: u64) -> u64 {
(retention_secs / 2).max(1)
}
pub fn oauth2_internal_router(state: Arc<OAuth2State>) -> Router {
Router::new()
.route("/oauth2/introspect", post(introspect_endpoint))
.with_state(state)
}
const OAUTH2_BODY_LIMIT: usize = 64 * 1024;
fn apply_no_store(mut resp: Response) -> Response {
let headers = resp.headers_mut();
headers.insert(
HeaderName::from_static("cache-control"),
HeaderValue::from_static("no-store"),
);
headers.insert(
HeaderName::from_static("pragma"),
HeaderValue::from_static("no-cache"),
);
resp
}
fn form_percent_decode(input: &[u8]) -> String {
let mut out = Vec::with_capacity(input.len());
let mut i = 0;
while i < input.len() {
match input[i] {
b'+' => {
out.push(b' ');
i += 1;
},
b'%' if i + 2 < input.len() => {
let hex = std::str::from_utf8(&input[i + 1..i + 3]).ok();
match hex.and_then(|h| u8::from_str_radix(h, 16).ok()) {
Some(b) => {
out.push(b);
i += 3;
},
None => {
out.push(b'%');
i += 1;
},
}
},
b => {
out.push(b);
i += 1;
},
}
}
String::from_utf8_lossy(&out).into_owned()
}
fn parse_form_body(body: &[u8]) -> Result<serde_json::Value, String> {
let mut map = serde_json::Map::new();
for pair in body.split(|&b| b == b'&') {
if pair.is_empty() {
continue;
}
let mut it = pair.splitn(2, |&b| b == b'=');
let key = form_percent_decode(it.next().unwrap_or_default());
let value = form_percent_decode(it.next().unwrap_or_default());
if key.is_empty() {
return Err("empty form field name".to_string());
}
map.insert(key, serde_json::Value::String(value));
}
Ok(serde_json::Value::Object(map))
}
fn extract_oauth2_request<T: serde::de::DeserializeOwned>(
content_type: Option<&str>,
body: &[u8],
) -> Result<T, Box<Response>> {
let is_form = content_type
.map(|ct| ct.starts_with("application/x-www-form-urlencoded"))
.unwrap_or(false);
if !is_form {
return Err(Box::new(apply_no_store(
(
StatusCode::UNSUPPORTED_MEDIA_TYPE,
Json(json!({
"error": "invalid_request",
"message": "Content-Type must be application/x-www-form-urlencoded"
})),
)
.into_response(),
)));
}
match parse_form_body(body).and_then(|v| serde_json::from_value(v).map_err(|e| e.to_string())) {
Ok(req) => Ok(req),
Err(e) => Err(Box::new(apply_no_store(
(
StatusCode::BAD_REQUEST,
Json(json!({ "error": "invalid_request", "message": e })),
)
.into_response(),
))),
}
}
fn bridge_rate_limited_error(
e: crate::error::GarrisonError,
retry_hint_secs: u64,
) -> crate::error::GarrisonError {
match e {
crate::error::GarrisonError::OAuth2(msg) if msg.starts_with("rate_limited") => {
crate::error::GarrisonError::RateLimited {
retry_after_secs: retry_hint_secs,
}
},
other => other,
}
}
fn with_retry_after(
mut response: axum::response::Response,
e: &crate::error::GarrisonError,
) -> axum::response::Response {
if let Some(secs) = e.retry_after_secs() {
response.headers_mut().insert(
axum::http::header::HeaderName::from_static("retry-after"),
axum::http::HeaderValue::from(secs),
);
}
response
}
async fn authorize_endpoint(
State(state): State<Arc<OAuth2State>>,
Query(req): Query<AuthorizeRequest>,
principal: Option<Extension<GarrisonPrincipal>>,
) -> Response {
let user_id: Option<i64> = principal.and_then(|ext| ext.0.login_id.parse::<i64>().ok());
let response = if req.prompt.is_some() {
state
.authorize_handler
.authorize_with_prompt(&req, user_id, req.prompt_hint())
.await
} else {
state.authorize_handler.authorize(&req, user_id).await
};
match response {
Ok(AuthorizeResponse::Redirect { location }) => {
(StatusCode::FOUND, [("Location", location)]).into_response()
},
Ok(AuthorizeResponse::LoginRequired { login_url }) => {
(StatusCode::FOUND, [("Location", login_url)]).into_response()
},
Err(e) => {
let (_, error_code, message, _) = e.response_parts_i18n();
with_retry_after(
(
StatusCode::BAD_REQUEST,
Json(json!({ "error": error_code, "message": message })),
)
.into_response(),
&e,
)
},
}
}
async fn authorize_resume_endpoint(
State(state): State<Arc<OAuth2State>>,
Query(params): Query<std::collections::HashMap<String, String>>,
principal: Option<Extension<GarrisonPrincipal>>,
) -> Response {
let Some(ticket) = params.get("ticket").cloned() else {
return (
StatusCode::BAD_REQUEST,
Json(json!({
"error": "invalid_request",
"message": loc!("oauth2-resume-ticket-missing", "missing ticket".to_string())
})),
)
.into_response();
};
let user_id: Option<i64> = principal.and_then(|ext| ext.0.login_id.parse::<i64>().ok());
match state.authorize_handler.resume(&ticket, user_id, &[]).await {
Ok(AuthorizeResponse::Redirect { location }) => {
(StatusCode::FOUND, [("Location", location)]).into_response()
},
Ok(AuthorizeResponse::LoginRequired { login_url }) => {
(StatusCode::FOUND, [("Location", login_url)]).into_response()
},
Err(e) => {
let (_, error_code, message, _) = e.response_parts_i18n();
with_retry_after(
(
StatusCode::BAD_REQUEST,
Json(json!({ "error": error_code, "message": message })),
)
.into_response(),
&e,
)
},
}
}
async fn token_endpoint(
State(state): State<Arc<OAuth2State>>,
headers: HeaderMap,
body: axum::body::Bytes,
) -> Response {
let authorization = headers
.get(axum::http::header::AUTHORIZATION)
.and_then(|v| v.to_str().ok())
.map(|s| s.to_string());
let content_type = headers
.get(axum::http::header::CONTENT_TYPE)
.and_then(|v| v.to_str().ok());
if body.len() > OAUTH2_BODY_LIMIT {
return apply_no_store(
(
StatusCode::BAD_REQUEST,
Json(json!({ "error": "invalid_request", "message": "request body too large" })),
)
.into_response(),
);
}
let req: TokenRequest = match extract_oauth2_request(content_type, &body) {
Ok(req) => req,
Err(resp) => return *resp,
};
match state
.token_handler
.handle_with_authorization(&req, authorization.as_deref())
.await
{
Ok(resp) => {
apply_no_store((StatusCode::OK, Json(resp)).into_response())
},
Err(e) => {
let e = bridge_rate_limited_error(
e,
state.token_handler.rate_limit_window_upper_bound_secs(),
);
let (_, error_code, message, _) = e.response_parts_i18n();
let is_rate_limited = matches!(e, crate::error::GarrisonError::RateLimited { .. });
let status = if is_rate_limited {
StatusCode::TOO_MANY_REQUESTS
} else {
StatusCode::BAD_REQUEST
};
let body_error: &str = if is_rate_limited {
"RATE_LIMIT_EXCEEDED"
} else {
error_code
};
apply_no_store(with_retry_after(
(
status,
Json(json!({ "error": body_error, "message": message })),
)
.into_response(),
&e,
))
},
}
}
async fn revoke_endpoint(
State(state): State<Arc<OAuth2State>>,
headers: HeaderMap,
body: axum::body::Bytes,
) -> Response {
let content_type = headers
.get(axum::http::header::CONTENT_TYPE)
.and_then(|v| v.to_str().ok());
let req: RevokeRequest = match extract_oauth2_request(content_type, &body) {
Ok(req) => req,
Err(resp) => return *resp,
};
match state.revoke_handler.handle(&req).await {
Ok(()) => apply_no_store(StatusCode::NO_CONTENT.into_response()),
Err(e) => {
let (_, error_code, message, _) = e.response_parts_i18n();
apply_no_store(with_retry_after(
(
StatusCode::BAD_REQUEST,
Json(json!({ "error": error_code, "message": message })),
)
.into_response(),
&e,
))
},
}
}
async fn introspect_endpoint(
State(state): State<Arc<OAuth2State>>,
headers: HeaderMap,
body: axum::body::Bytes,
) -> Response {
let content_type = headers
.get(axum::http::header::CONTENT_TYPE)
.and_then(|v| v.to_str().ok());
let req: IntrospectRequest = match extract_oauth2_request(content_type, &body) {
Ok(req) => req,
Err(resp) => return *resp,
};
match state.introspect_handler.handle(&req).await {
Ok(resp) => apply_no_store((StatusCode::OK, Json(resp)).into_response()),
Err(e) => {
let (_, error_code, message, _) = e.response_parts_i18n();
apply_no_store(with_retry_after(
(
StatusCode::BAD_REQUEST,
Json(json!({ "error": error_code, "message": message })),
)
.into_response(),
&e,
))
},
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::dao::{GarrisonDao, InMemoryDao};
use crate::oauth2_server::client::{
DaoOAuth2ClientStore, GrantType, OAuth2Client, OAuth2ClientStore,
};
use axum::body::Body;
use axum::http::{Request, StatusCode};
use http_body_util::BodyExt;
use tower::ServiceExt;
fn make_state() -> (Arc<OAuth2State>, Arc<dyn OAuth2ClientStore>) {
let dao: Arc<dyn GarrisonDao> = Arc::new(InMemoryDao::new());
let store: Arc<dyn OAuth2ClientStore> = Arc::new(DaoOAuth2ClientStore::new(dao.clone()));
let state = Arc::new(OAuth2State::new(
store.clone(),
dao,
"https://auth.example.com/login".to_string(),
));
(state, store)
}
#[cfg(feature = "db-sqlite")]
#[derive(Clone)]
struct LogCapture(std::sync::Arc<std::sync::Mutex<Vec<String>>>);
#[cfg(feature = "db-sqlite")]
impl<'a> tracing_subscriber::fmt::MakeWriter<'a> for LogCapture {
type Writer = LogCaptureWriter;
fn make_writer(&'a self) -> Self::Writer {
LogCaptureWriter(self.0.clone())
}
}
#[cfg(feature = "db-sqlite")]
struct LogCaptureWriter(std::sync::Arc<std::sync::Mutex<Vec<String>>>);
#[cfg(feature = "db-sqlite")]
impl std::io::Write for LogCaptureWriter {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.0
.lock()
.expect("日志缓冲锁应可用")
.push(String::from_utf8_lossy(buf).to_string());
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
#[cfg(feature = "db-sqlite")]
#[test]
#[serial_test::serial]
fn oauth2_state_new_without_rotation_warns_at_construction() {
let _en = crate::i18n::set_locale(crate::i18n::GarrisonLocale::En);
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
let capture = LogCapture(std::sync::Arc::new(std::sync::Mutex::new(Vec::new())));
let subscriber = tracing_subscriber::registry().with(
tracing_subscriber::fmt::layer()
.with_ansi(false)
.with_writer(capture.clone()),
);
let _guard = subscriber.set_default();
tracing::callsite::rebuild_interest_cache();
let mut logs: Vec<String> = Vec::new();
for _ in 0..3 {
let (state, _store) = make_state();
assert!(
!state.token_handler.has_refresh_rotation(),
"OAuth2State::new 默认不注入 rotation,探针应为 false"
);
logs = capture.0.lock().expect("日志缓冲锁应可用").clone();
if logs.iter().any(|l| {
l.contains("without RefreshTokenRotation") && l.contains("reuse detection")
}) {
break;
}
tracing::callsite::rebuild_interest_cache();
}
assert!(
logs.iter().any(|l| {
l.contains("without RefreshTokenRotation") && l.contains("reuse detection")
}),
"构造期应输出结构化退化 warn(含 reuse detection 不可用后果),日志: {:?}",
logs
);
}
fn make_test_client(id: &str) -> OAuth2Client {
OAuth2Client::new(
id,
"secret-123",
vec!["https://app.example.com/cb".into()],
vec![GrantType::AuthorizationCode, GrantType::ClientCredentials],
vec!["read".into()],
)
.unwrap()
}
#[test]
fn test_oauth2_state_construction() {
let (state, _) = make_state();
assert_eq!(Arc::strong_count(&state.authorize_handler), 2);
assert_eq!(Arc::strong_count(&state.token_handler), 3);
assert_eq!(Arc::strong_count(&state.revoke_handler), 1);
assert_eq!(Arc::strong_count(&state.introspect_handler), 1);
}
#[tokio::test]
async fn test_oauth2_external_router_has_authorize_route() {
let (state, store) = make_state();
store.create(make_test_client("route-auth")).await.unwrap();
let app = oauth2_external_router(state);
let resp = app
.oneshot(
Request::builder()
.method("GET")
.uri("/oauth2/authorize")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_ne!(resp.status(), StatusCode::NOT_FOUND);
}
#[tokio::test]
async fn test_oauth2_external_router_has_token_route() {
let (state, _) = make_state();
let app = oauth2_external_router(state);
let resp = app
.oneshot(
Request::builder()
.method("POST")
.uri("/oauth2/token")
.header("content-type", "application/x-www-form-urlencoded")
.body(Body::from("grant_type=client_credentials"))
.unwrap(),
)
.await
.unwrap();
assert_ne!(resp.status(), StatusCode::NOT_FOUND);
}
#[tokio::test]
async fn test_oauth2_external_router_has_revoke_route() {
let (state, _) = make_state();
let app = oauth2_external_router(state);
let resp = app
.oneshot(
Request::builder()
.method("POST")
.uri("/oauth2/revoke")
.header("content-type", "application/x-www-form-urlencoded")
.body(Body::from("token=x&client_id=c&client_secret=s"))
.unwrap(),
)
.await
.unwrap();
assert_ne!(resp.status(), StatusCode::NOT_FOUND);
}
#[tokio::test]
async fn test_oauth2_internal_router_has_introspect_route() {
let (state, store) = make_state();
store.create(make_test_client("route-int")).await.unwrap();
let app = oauth2_internal_router(state);
let body = "token=nonexistent&client_id=route-int&client_secret=secret-123";
let resp = app
.oneshot(
Request::builder()
.method("POST")
.uri("/oauth2/introspect")
.header("content-type", "application/x-www-form-urlencoded")
.body(Body::from(body.to_string()))
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
}
#[tokio::test]
async fn test_authorize_resume_missing_ticket_message_is_localized() {
let (state, _) = make_state();
let app = oauth2_external_router(state);
let _guard = crate::i18n::set_locale(crate::i18n::GarrisonLocale::Zh);
let resp = app
.oneshot(
Request::builder()
.method("GET")
.uri("/oauth2/authorize/resume")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
let bytes = resp.into_body().collect().await.unwrap().to_bytes();
let json: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
assert_eq!(json["error"], "invalid_request");
assert!(
json["message"].as_str().unwrap().contains("参数缺失"),
"zh locale 应返回 Fluent 翻译,实际: {}",
json["message"]
);
}
#[tokio::test]
async fn test_authorize_endpoint_redirects_when_not_logged_in() {
let (state, store) = make_state();
store.create(make_test_client("auth-redir")).await.unwrap();
let app = oauth2_external_router(state);
let uri = "/oauth2/authorize?response_type=code&client_id=auth-redir&redirect_uri=https://app.example.com/cb&code_challenge=test-challenge&code_challenge_method=S256";
let resp = app
.oneshot(
Request::builder()
.method("GET")
.uri(uri)
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::FOUND);
let location = resp
.headers()
.get("Location")
.expect("Location header 必须存在")
.to_str()
.unwrap();
assert!(
location.starts_with("https://auth.example.com/login"),
"应重定向到登录页,实际: {location}"
);
}
#[tokio::test]
async fn test_authorize_endpoint_returns_redirect_with_code_when_principal_present() {
let (state, store) = make_state();
store
.create(make_test_client("auth-principal"))
.await
.unwrap();
let app = oauth2_external_router(state).layer(Extension(GarrisonPrincipal {
login_id: "1001".to_string(),
}));
let uri = "/oauth2/authorize?response_type=code&client_id=auth-principal&redirect_uri=https://app.example.com/cb&code_challenge=test-challenge&code_challenge_method=S256";
let resp = app
.oneshot(
Request::builder()
.method("GET")
.uri(uri)
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::FOUND);
let location = resp
.headers()
.get("Location")
.expect("Location header 必须存在")
.to_str()
.unwrap();
assert!(
location.starts_with("https://app.example.com/cb?code="),
"有 principal 时应重定向到 redirect_uri 含 code,实际: {location}"
);
}
#[tokio::test]
async fn test_authorize_endpoint_returns_login_required_when_no_principal() {
let (state, store) = make_state();
store
.create(make_test_client("auth-no-principal"))
.await
.unwrap();
let app = oauth2_external_router(state);
let uri = "/oauth2/authorize?response_type=code&client_id=auth-no-principal&redirect_uri=https://app.example.com/cb&code_challenge=test-challenge&code_challenge_method=S256";
let resp = app
.oneshot(
Request::builder()
.method("GET")
.uri(uri)
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::FOUND);
let location = resp
.headers()
.get("Location")
.expect("Location header 必须存在")
.to_str()
.unwrap();
assert!(
location.starts_with("https://auth.example.com/login"),
"无 principal 应重定向到登录页,实际: {location}"
);
}
#[tokio::test]
async fn test_token_endpoint_returns_bad_request_on_invalid_client() {
let (state, _) = make_state();
let app = oauth2_external_router(state);
let body = "grant_type=client_credentials&client_id=no-such-client&client_secret=secret";
let resp = app
.oneshot(
Request::builder()
.method("POST")
.uri("/oauth2/token")
.header("content-type", "application/x-www-form-urlencoded")
.body(Body::from(body.to_string()))
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
}
#[tokio::test]
async fn test_token_endpoint_returns_cache_control_no_store_header() {
let (state, store) = make_state();
store.create(make_test_client("cc-cid")).await.unwrap();
let app = oauth2_external_router(state);
let body = "grant_type=client_credentials&client_id=cc-cid&client_secret=secret-123";
let resp = app
.oneshot(
Request::builder()
.method("POST")
.uri("/oauth2/token")
.header("content-type", "application/x-www-form-urlencoded")
.body(Body::from(body.to_string()))
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let cache_control = resp
.headers()
.get("cache-control")
.expect("Cache-Control 头必须存在");
assert_eq!(
cache_control.to_str().unwrap(),
"no-store",
"Cache-Control 必须为 no-store"
);
let pragma = resp.headers().get("pragma").expect("Pragma 头必须存在");
assert_eq!(
pragma.to_str().unwrap(),
"no-cache",
"Pragma 必须为 no-cache"
);
}
#[tokio::test]
async fn test_token_endpoint_accepts_basic_auth_header() {
use base64::engine::general_purpose::STANDARD;
use base64::Engine;
let (state, store) = make_state();
store.create(make_test_client("basic-cid")).await.unwrap();
let app = oauth2_external_router(state);
let credentials = STANDARD.encode("basic-cid:secret-123");
let auth_header = format!("Basic {}", credentials);
let body = "grant_type=client_credentials";
let resp = app
.oneshot(
Request::builder()
.method("POST")
.uri("/oauth2/token")
.header("content-type", "application/x-www-form-urlencoded")
.header("authorization", &auth_header)
.body(Body::from(body.to_string()))
.unwrap(),
)
.await
.unwrap();
assert_eq!(
resp.status(),
StatusCode::OK,
"Basic Auth 认证应成功,body 留空"
);
}
#[tokio::test]
async fn test_revoke_endpoint_returns_no_content_on_success() {
let (state, store) = make_state();
store.create(make_test_client("rev-ok")).await.unwrap();
let app = oauth2_external_router(state);
let issue_body = "grant_type=client_credentials&client_id=rev-ok&client_secret=secret-123";
let resp = app
.clone()
.oneshot(
Request::builder()
.method("POST")
.uri("/oauth2/token")
.header("content-type", "application/x-www-form-urlencoded")
.body(Body::from(issue_body.to_string()))
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let bytes = resp.into_body().collect().await.unwrap().to_bytes();
let token_resp: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
let token = token_resp["access_token"].as_str().expect("access_token");
let revoke_body = format!("token={}&client_id=rev-ok&client_secret=secret-123", token);
let resp = app
.oneshot(
Request::builder()
.method("POST")
.uri("/oauth2/revoke")
.header("content-type", "application/x-www-form-urlencoded")
.body(Body::from(revoke_body.to_string()))
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::NO_CONTENT);
}
#[tokio::test]
async fn test_token_endpoint_returns_429_on_rate_limit_exceeded() {
let dao: Arc<dyn GarrisonDao> = Arc::new(InMemoryDao::new());
let store: Arc<dyn OAuth2ClientStore> = Arc::new(DaoOAuth2ClientStore::new(dao.clone()));
let authorize_handler = Arc::new(AuthorizeHandler::new(
store.clone(),
dao.clone(),
"https://auth.example.com/login".to_string(),
));
let token_handler = Arc::new(TokenHandler::new(
store.clone(),
dao.clone(),
authorize_handler.clone(),
Arc::new(PasswordRateLimiter::new(1000, 300)),
Arc::new(TokenRateLimiter::with_limits(1, 60, 100, 60)),
));
let revoke_handler = Arc::new(RevokeHandler::new(store.clone(), token_handler.clone()));
let introspect_handler =
Arc::new(IntrospectHandler::new(store.clone(), token_handler.clone()));
let state = Arc::new(OAuth2State {
authorize_handler,
token_handler,
revoke_handler,
introspect_handler,
client_store: store.clone(),
jwks_source: None,
issuer: None,
jwks_keystore: None,
oidc_discovery_enabled: false,
password_grant_advertised: false,
scopes_cache: tokio::sync::RwLock::new(None),
jwks_doc_cache: tokio::sync::Mutex::new(None),
jwks_retention_secs: crate::config::DEFAULT_JWKS_RETENTION_SECS,
});
store.create(make_test_client("rl-429")).await.unwrap();
let app = oauth2_external_router(state);
let body = "grant_type=client_credentials&client_id=rl-429&client_secret=secret-123";
let resp = app
.clone()
.oneshot(
Request::builder()
.method("POST")
.uri("/oauth2/token")
.header("content-type", "application/x-www-form-urlencoded")
.body(Body::from(body.to_string()))
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let resp = app
.oneshot(
Request::builder()
.method("POST")
.uri("/oauth2/token")
.header("content-type", "application/x-www-form-urlencoded")
.body(Body::from(body.to_string()))
.unwrap(),
)
.await
.unwrap();
assert_eq!(
resp.status(),
StatusCode::TOO_MANY_REQUESTS,
"超速率限制应返回 429"
);
assert_eq!(
resp.headers()
.get("retry-after")
.and_then(|v| v.to_str().ok()),
Some("300"),
"429 响应必须携带 Retry-After 头"
);
let bytes = resp.into_body().collect().await.unwrap().to_bytes();
let resp_json: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
assert_eq!(
resp_json["error"].as_str().unwrap(),
"RATE_LIMIT_EXCEEDED",
"error code 应为 RATE_LIMIT_EXCEEDED"
);
}
#[tokio::test]
async fn test_token_endpoint_returns_400_on_non_rate_limited_error() {
let (state, store) = make_state();
store.create(make_test_client("nrl-400")).await.unwrap();
let app = oauth2_external_router(state);
let body = "grant_type=client_credentials&client_id=nrl-400&client_secret=wrong-secret";
let resp = app
.oneshot(
Request::builder()
.method("POST")
.uri("/oauth2/token")
.header("content-type", "application/x-www-form-urlencoded")
.body(Body::from(body.to_string()))
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
}
#[test]
fn bridge_rate_limited_error_maps_prefix_and_preserves_others() {
use crate::error::GarrisonError;
let bridged = bridge_rate_limited_error(
GarrisonError::OAuth2("rate_limited: client requests too frequent".to_string()),
300,
);
assert!(
matches!(bridged, GarrisonError::RateLimited { .. }),
"rate_limited 前缀错误应桥接为 RateLimited,实际: {bridged:?}"
);
assert_eq!(
bridged.retry_after_secs(),
Some(300),
"桥接后 Retry-After 秒数必须可达"
);
let passthrough = bridge_rate_limited_error(
GarrisonError::OAuth2("invalid_client: nope".to_string()),
300,
);
assert!(
matches!(passthrough, GarrisonError::OAuth2(_)),
"非 rate_limited 前缀错误不得被改写: {passthrough:?}"
);
assert_eq!(passthrough.retry_after_secs(), None);
}
#[tokio::test]
async fn test_token_endpoint_accepts_form_urlencoded() {
let (state, store) = make_state();
store.create(make_test_client("form-tok")).await.unwrap();
let app = oauth2_external_router(state);
let body = "grant_type=client_credentials&client_id=form-tok&client_secret=secret-123";
let resp = app
.oneshot(
Request::builder()
.method("POST")
.uri("/oauth2/token")
.header(
"content-type",
"application/x-www-form-urlencoded;charset=UTF-8",
)
.body(Body::from(body.to_string()))
.unwrap(),
)
.await
.unwrap();
assert_eq!(
resp.status(),
StatusCode::OK,
"表单编码的 token 请求应被接受(RFC 6749 §3.2)"
);
let bytes = resp.into_body().collect().await.unwrap().to_bytes();
let resp_json: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
assert!(resp_json["access_token"].as_str().is_some());
}
#[test]
fn parse_form_body_decodes_percent_and_plus() {
let value =
parse_form_body(b"grant_type=password&username=a%40b.com+cd%26x&code=%E4%B8%AD")
.unwrap();
assert_eq!(value["username"], "a@b.com cd&x");
assert_eq!(value["code"], "中");
}
#[tokio::test]
async fn test_token_endpoint_malformed_body_returns_400_invalid_request() {
let (state, _) = make_state();
let app = oauth2_external_router(state);
let resp = app
.oneshot(
Request::builder()
.method("POST")
.uri("/oauth2/token")
.header("content-type", "application/x-www-form-urlencoded")
.body(Body::from("grant_type=a&=b"))
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
let cache_control = resp
.headers()
.get("cache-control")
.and_then(|v| v.to_str().ok())
.map(|s| s.to_string());
let pragma = resp
.headers()
.get("pragma")
.and_then(|v| v.to_str().ok())
.map(|s| s.to_string());
let bytes = resp.into_body().collect().await.unwrap().to_bytes();
let resp_json: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
assert_eq!(resp_json["error"], "invalid_request");
assert_eq!(cache_control.as_deref(), Some("no-store"));
assert_eq!(pragma.as_deref(), Some("no-cache"));
}
#[tokio::test]
async fn test_token_endpoint_missing_content_type_returns_415() {
let (state, _) = make_state();
let app = oauth2_external_router(state);
let resp = app
.oneshot(
Request::builder()
.method("POST")
.uri("/oauth2/token")
.body(Body::from(
"grant_type=client_credentials&client_id=x&client_secret=y",
))
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::UNSUPPORTED_MEDIA_TYPE);
assert_eq!(
resp.headers()
.get("cache-control")
.and_then(|v| v.to_str().ok()),
Some("no-store")
);
}
#[tokio::test]
async fn test_token_endpoint_json_content_type_returns_415() {
let (state, _) = make_state();
let app = oauth2_external_router(state);
let resp = app
.oneshot(
Request::builder()
.method("POST")
.uri("/oauth2/token")
.header("content-type", "application/json")
.body(Body::from(r#"{"grant_type":"client_credentials"}"#))
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::UNSUPPORTED_MEDIA_TYPE);
let bytes = resp.into_body().collect().await.unwrap().to_bytes();
let resp_json: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
assert_eq!(resp_json["error"], "invalid_request");
}
}
#[cfg(test)]
mod jwks_tests {
use super::*;
use crate::dao::{GarrisonDao, InMemoryDao};
use crate::oauth2_server::client::{DaoOAuth2ClientStore, OAuth2ClientStore};
use axum::body::Body;
use axum::http::{Request, StatusCode};
use http_body_util::BodyExt;
use tower::ServiceExt;
const JWKS_TEST_RSA_PEM: &str = "-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDMQoXOvmvs4kpj
nYshns5CYyNziLt/xBQBZtlkzY3KUuHtJMz9zK0TTz0DbhCnDCWF8tpWqxHBTtON
pMnnC6bTN4Wg/PWDn67hub23b4xAKq5qH45RmWn4a0TGTUyQktebjlCiWBlMCo43
j7FLyvGrOx3SmyUUaI5vh02Pf5Swg2CCQ69ht3L58jM6lp+jILJ4gEjNC2hmaWgH
bbYhTX5qCaPgZTndfgPXX9wkDG8jhpgRpfwmSuJ4aRiRrjvgycWuPccryX7aXiBU
c5H8rdKVgnjPTzSL8h3P/2tpZkNj5OFBZKTgmdPuwF87Mjlbr7Oi2xpCIpNpnyF1
L8G/mIIRAgMBAAECggEAY2vnzIOEbceRtN4cuC8fr1GpElXWCfELWclRfI7O+tGP
9YlpnAmxnsn9ZTuAMIcphoL4QqI+4Kw5LeMtgV/7AikuyncGG9ywV1+819ocVqlP
vwkAEXjOi2PPFITQhThsaOODHRorqgcjRSkUf9NXAWUjdX0dtcrUtbWSi4vqeGWC
O0Ni/9iWJYFjAgHu+XJgYXZtn7sJGe30PuIFmiKHfHuuM9alo6SubQ3UU80B+hQm
N4VVuYN+dYmGsekp+Ofj65mq2+WAyvHE2V9OB92L+yVY0uKZ428eXdN9ydp8kVqh
yOW2yLTq6sBNBOi/F/DMim6QrIzn6zpp0hXyc97d1wKBgQD+2qQOQ+urTE1ymdXT
C/os5kMrjsYd/rfcMaBbl4XmDE/PjlJg1Z6yVBGMmvM/shjTslcV8kgKyKf+aHm1
b3ckgu9PLtoYGX0yjeHbYCidwkjJPz8sy5pIFHslY/bGhV5RqsSPJo1aX2En0c4R
3cJHGGGn2YguQuWSOU94VTKgowKBgQDNLaS9Q08j/tiozEY23oH9i5p5iSwxc3It
hS/UwLYNFad6byRiDgVlvx+sVHZfLBsmNMlHEY+oMGocctqGAC8+a0pvtaLnU12/
GI61axWfAlCJrYs3CBOKwOH3hBM04mqPUb2ySHdlzPawpIr55jMkohuXXyw/22je
pK7iIPHZuwKBgQC+/Gi/TAUbjQXpIQHNtAcaiMDDrq4nolB00jfjC81LVeSlnXl8
mfngmAHCxggOrs/OLbL3fmagtji2/eJfppW5penjBDBqqQda0Fr2xLwLZaKYNi6I
ylfnNnoGzkAMC7xgJUJCKNj7ZcjwR1lPqElEcDAW0n0sdfOGvi4g9nAHUwKBgGIB
E1dz9zFyYXr/V+qNjfnV3QuAgiN8yWUE4Tv2cP7/AOhyfiZ4HAvlpvNhxMjhAHbX
b+0KblwgBA9irQ6kt+xQw1VopU9perX0vPXbGJDDQkUBKCY5LVxxlX3tEF+KZuve
V4X5J07xAESP0/JaCsPMyvEa/L/jxcvTTdWlduBRAoGBAPx2MMqUGXa+UZ+a0cyF
tW0xGglEWCX+e2vSNf31v4FyoGxNi6h2ap2OWEddpGttS+UbOzO9BlzcCtYJvPwW
lRVGEQXEoVyslCUwTlV8LmtrJS6Xl9YwRHmmajJMH6GJTk/CToOLIvj2bOMxHW5A
dzWfBsm+KAfTJuqbV7VnJL3G
-----END PRIVATE KEY-----
";
const JWKS_TEST_RSA_PEM_ALT: &str = "-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC751S0Pf/oKnCq
AvE+iD2TB3fvkUXTiHfGgVHXfuFrVnam2DV9rPAWtzj78N/n4SzwIojGGzPFZtpd
Kw+VMWOEf0y5kTwOYEIAGJel6MPZBm8r2BqkgiDUw4usrI9BCRy9zRuDighMWoo0
r23RtOglWjwh6YbEXkg+dlaaFzuwZbtd7zOokVcd7MlXV51PRGT3HHbLekMJqKjM
+L+cN7NSoR6CsWHSqx8HlEJ8VQyNizsO1wKFo6gUSzGPeMH1uNMVxguI2OOzM8NS
8iEJmvMtVbcUxdYgIgFCol8ibBXYBsRQ02/arulEts3DoNJ/dOqt3uWaHv/+SVpc
CjdCgxdXAgMBAAECggEABSHWYG3pFXBDT4FxEWIrPF7R2cs/+v0ZOGTD1XzzrzjX
WMtC+sHEdPpgJhF4LB8sWQq4baDEkzmx8SWB8XM94pqPf+oFl+btJo+FZNSstLrG
Qo5Oe/vJ5cXJhNfZuc8D5/M4MymL/HnkmHfKKhYk2RBT4CE+uxJQKtSUnPTRfonc
v74IXU9mgrEWyhRnRLDCQTDS+MiFx+ca+Q2j3A3VXb6mLngi7+MlCTHesOrNi/h4
Krh6gXQDE3xw4oWlSy59zRXswrThFeNePbtN5suqHrGfhAJqvcVTuJRNPBL1OhYL
z9Fw+i5ypa0IFiiWmzIdTf6Z8fWAIRa16Gi7HofgYQKBgQDyasNqMmH2SJZ0X5Sk
RNsrHXlCVHdofP7B/w1mRXdXQN7bEdt+TkgHBOqO+005kHqcpjnm8GfKwaTVo9UK
cpYYDg11mBTULFn8BFKMH+MEzYTjKrHjA62DlM9PfdmKjPDXqJceWG1bp6Z2d+Rd
U0TfkTQLKmWihLYTUn2+c/gIWQKBgQDGbqAv9KU/7SKiUNVfSMMw/YYBm2jjEdQb
++IQuugzg0/cTwm+5sVbdzwqqt0jihO+2KWDbEHLqRndEeRjA5hvk+qp316MaeV8
x/dySeNG/lCnobiY+9ZjF14Zmaz1jGe+oAwmqGF4beoL5hkF77tFKUQtBRzkZgFC
A7yXzdQnLwKBgQCUu/Cd7b+xLiQxzpsSlrSqJXFKwyxoTZi5SlXcU+6++CxD2RcE
zd7ff6Kyi3l8QisYhdys1v+3pUwPUG/b8yYoKCcV6XOOIpArUjObiczuG3LXNlDi
alVBkEIKEbsxiPwUNXpSwgqG27wEn9bbc8WkLiDyYNbu+eIExO4ltl2OMQKBgHYk
KTVEGBruab9wFwmq/aOuXdmZGKKQ29NpbRf+3/7DgImveSLyrLAfVnAk2JKvQ8BN
poWPr8C8xkxLucmFu306+Oz4s4cwCVT4jYe7HBkJkyWq8IgM8ICAyiK9zy9G0AG7
smBVwep8rms1LNLO/5VW02NmduQ5IyiVpvROtLA7AoGBALycmaxr5wjzgTiw0oyh
/SSBunDWL23NnXrjjnLZ9BsmglkQUvhtkaA9AquuGJA4RiVxznoQQ6iXgZV80+Ui
j3MTO7dWRoAlfHi/NMU1BNA7zXLA2xPP/ekmHfb3PwCt5AhZTl43dEL+8V3xNlxp
HxmxsMlpmvvbydfhRl3OoqMR
-----END PRIVATE KEY-----
";
fn make_state() -> OAuth2State {
let dao: Arc<dyn GarrisonDao> = Arc::new(InMemoryDao::new());
let store: Arc<dyn OAuth2ClientStore> = Arc::new(DaoOAuth2ClientStore::new(dao.clone()));
OAuth2State::new(store, dao, "http://localhost/login".into())
}
#[tokio::test]
async fn jwks_endpoint_returns_404_without_source() {
let app = oauth2_external_router(Arc::new(make_state()));
let resp = app
.oneshot(
Request::builder()
.method("GET")
.uri("/oauth2/jwks.json")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
}
#[tokio::test]
async fn jwks_endpoint_serves_keystore_with_multiple_kids() {
let dao: Arc<dyn GarrisonDao> = Arc::new(InMemoryDao::new());
let keystore = Arc::new(crate::oauth2_server::jwks::JwksKeystore::new(dao.clone()));
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs() as i64;
keystore
.rotate("RS256", JWKS_TEST_RSA_PEM, now - 5_000, 10_000)
.await
.unwrap();
keystore
.rotate("RS256", JWKS_TEST_RSA_PEM_ALT, now - 1_000, 10_000)
.await
.unwrap();
let state = make_state()
.with_issuer("https://auth.example.com")
.with_jwks_keystore(keystore)
.with_jwks_retention_secs(10_000);
let app = oauth2_external_router(Arc::new(state));
let resp = app
.oneshot(
Request::builder()
.method("GET")
.uri("/oauth2/jwks.json")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let body = BodyExt::collect(resp.into_body()).await.unwrap().to_bytes();
let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
let keys = json["keys"].as_array().unwrap();
assert_eq!(keys.len(), 2, "Active + Passive 两把钥都必须发布");
assert_ne!(keys[0]["kid"], keys[1]["kid"]);
assert!(!json.to_string().contains("PRIVATE KEY"));
}
#[tokio::test]
async fn jwks_endpoint_excludes_retired_kid() {
let dao: Arc<dyn GarrisonDao> = Arc::new(InMemoryDao::new());
let keystore = Arc::new(crate::oauth2_server::jwks::JwksKeystore::new(dao.clone()));
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs() as i64;
let first = keystore
.rotate("RS256", JWKS_TEST_RSA_PEM, now - 30_000, 10_000)
.await
.unwrap();
keystore
.rotate("RS256", JWKS_TEST_RSA_PEM_ALT, now - 15_000, 10_000)
.await
.unwrap();
let retired = keystore.retire_expired(now, 10_000).await.unwrap();
assert_eq!(
retired,
vec![first.activated_kid.clone()],
"超期 Passive 必须落库退役"
);
let state = make_state()
.with_issuer("https://auth.example.com")
.with_jwks_keystore(keystore)
.with_jwks_retention_secs(10_000);
let app = oauth2_external_router(Arc::new(state));
let resp = app
.oneshot(
Request::builder()
.method("GET")
.uri("/oauth2/jwks.json")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let body = BodyExt::collect(resp.into_body()).await.unwrap().to_bytes();
let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert_eq!(
json["keys"].as_array().unwrap().len(),
1,
"退役钥必须移出 JWKS"
);
}
#[tokio::test]
async fn jwks_endpoint_cache_control_derives_from_retention() {
let build = |retention: u64| async move {
let dao: Arc<dyn GarrisonDao> = Arc::new(InMemoryDao::new());
let keystore = Arc::new(crate::oauth2_server::jwks::JwksKeystore::new(dao.clone()));
keystore
.rotate("RS256", JWKS_TEST_RSA_PEM, 1_000, retention)
.await
.unwrap();
let state = make_state()
.with_issuer("https://auth.example.com")
.with_jwks_keystore(keystore)
.with_jwks_retention_secs(retention);
oauth2_external_router(Arc::new(state))
};
let app = build(7_200).await;
let resp = app
.oneshot(
Request::builder()
.method("GET")
.uri("/oauth2/jwks.json")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(
resp.headers()
.get("cache-control")
.and_then(|v| v.to_str().ok()),
Some("public, max-age=3600"),
"retention 7200 → max-age 3600"
);
let app = build(3_600).await;
let resp = app
.oneshot(
Request::builder()
.method("GET")
.uri("/oauth2/jwks.json")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(
resp.headers()
.get("cache-control")
.and_then(|v| v.to_str().ok()),
Some("public, max-age=1800"),
"retention 3600 → max-age 1800(动态派生非静态值)"
);
}
#[tokio::test]
async fn jwks_endpoint_returns_200_with_rsa_source() {
let app = oauth2_external_router(Arc::new(
make_state().with_jwks_source("RS256", JWKS_TEST_RSA_PEM),
));
let resp = app
.oneshot(
Request::builder()
.method("GET")
.uri("/oauth2/jwks.json")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let body = BodyExt::collect(resp.into_body()).await.unwrap().to_bytes();
let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
let keys = json["keys"].as_array().unwrap();
assert_eq!(keys.len(), 1);
assert_eq!(keys[0]["kty"], "RSA");
assert_eq!(keys[0]["use"], "sig");
assert!(keys[0]["kid"].as_str().unwrap().len() > 20);
assert!(keys[0]["n"].as_str().is_some() && keys[0]["e"].as_str().is_some());
assert!(!json.to_string().contains("PRIVATE KEY"));
}
}
#[cfg(test)]
mod discovery_tests {
use super::*;
use crate::dao::{GarrisonDao, InMemoryDao};
use crate::oauth2_server::client::{
DaoOAuth2ClientStore, GrantType, OAuth2Client, OAuth2ClientStore,
};
use axum::body::Body;
use axum::http::{Request, StatusCode};
use http_body_util::BodyExt;
use tower::ServiceExt;
async fn make_discovery_state(register_client: bool) -> Arc<OAuth2State> {
let dao: Arc<dyn GarrisonDao> = Arc::new(InMemoryDao::new());
let store: Arc<dyn OAuth2ClientStore> = Arc::new(DaoOAuth2ClientStore::new(dao.clone()));
if register_client {
let client = OAuth2Client::new(
"disc-client",
"secret-123",
vec!["https://app.example.com/cb".into()],
vec![GrantType::AuthorizationCode, GrantType::ClientCredentials],
vec!["read".into(), "profile".into()],
)
.unwrap();
store.create(client).await.unwrap();
}
Arc::new(
OAuth2State::new(store, dao, "https://auth.example.com/login".to_string())
.with_issuer("https://auth.example.com")
.with_oidc_discovery_enabled(true),
)
}
async fn get(app: Router, uri: &str) -> axum::response::Response {
app.oneshot(
Request::builder()
.method("GET")
.uri(uri)
.body(Body::empty())
.unwrap(),
)
.await
.unwrap()
}
#[tokio::test]
async fn openid_configuration_returns_200_with_derived_metadata() {
let app = oauth2_external_router(make_discovery_state(true).await);
let resp = get(app, "/.well-known/openid-configuration").await;
assert_eq!(resp.status(), StatusCode::OK);
let body = BodyExt::collect(resp.into_body()).await.unwrap().to_bytes();
let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert_eq!(json["issuer"], "https://auth.example.com");
assert_eq!(
json["authorization_endpoint"],
"https://auth.example.com/oauth2/authorize"
);
assert_eq!(
json["token_endpoint"],
"https://auth.example.com/oauth2/token"
);
assert_eq!(
json["revocation_endpoint"],
"https://auth.example.com/oauth2/revoke"
);
assert!(json.get("introspection_endpoint").is_none());
let grants: Vec<&str> = json["grant_types_supported"]
.as_array()
.unwrap()
.iter()
.map(|v| v.as_str().unwrap())
.collect();
assert!(grants.contains(&"authorization_code"));
assert!(grants.contains(&"refresh_token"));
assert!(grants.contains(&"client_credentials"));
assert!(
!grants.contains(&"password"),
"未宣告的 password grant 不得出现"
);
assert_eq!(
json["response_types_supported"],
serde_json::json!(["code"])
);
let scopes: Vec<&str> = json["scopes_supported"]
.as_array()
.unwrap()
.iter()
.map(|v| v.as_str().unwrap())
.collect();
assert!(scopes.contains(&"read") && scopes.contains(&"profile"));
assert_eq!(
json["code_challenge_methods_supported"],
serde_json::json!(["S256"])
);
assert!(json.get("jwks_uri").is_none());
}
#[tokio::test]
async fn openid_configuration_password_grant_follows_advertised_flag() {
let dao: Arc<dyn GarrisonDao> = Arc::new(InMemoryDao::new());
let store: Arc<dyn OAuth2ClientStore> = Arc::new(DaoOAuth2ClientStore::new(dao.clone()));
let state = Arc::new(
OAuth2State::new(store, dao, "https://auth.example.com/login".to_string())
.with_issuer("https://auth.example.com")
.with_oidc_discovery_enabled(true)
.with_password_grant_advertised(true),
);
let app = oauth2_external_router(state);
let resp = get(app, "/.well-known/openid-configuration").await;
let body = BodyExt::collect(resp.into_body()).await.unwrap().to_bytes();
let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
let grants: Vec<&str> = json["grant_types_supported"]
.as_array()
.unwrap()
.iter()
.map(|v| v.as_str().unwrap())
.collect();
assert!(
grants.contains(&"password"),
"宣告后 password grant 必须出现在元数据"
);
}
#[tokio::test]
async fn openid_configuration_returns_404_when_oidc_disabled() {
let dao: Arc<dyn GarrisonDao> = Arc::new(InMemoryDao::new());
let store: Arc<dyn OAuth2ClientStore> = Arc::new(DaoOAuth2ClientStore::new(dao.clone()));
let state = Arc::new(
OAuth2State::new(store, dao, "https://auth.example.com/login".to_string())
.with_issuer("https://auth.example.com")
.with_oidc_discovery_enabled(false),
);
let app = oauth2_external_router(state);
let resp = get(app, "/.well-known/openid-configuration").await;
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
}
#[tokio::test]
async fn discovery_returns_404_without_issuer() {
let dao: Arc<dyn GarrisonDao> = Arc::new(InMemoryDao::new());
let store: Arc<dyn OAuth2ClientStore> = Arc::new(DaoOAuth2ClientStore::new(dao.clone()));
let state = Arc::new(OAuth2State::new(
store,
dao,
"https://auth.example.com/login".to_string(),
));
let app = oauth2_external_router(state);
let resp = get(app.clone(), "/.well-known/openid-configuration").await;
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
let resp = get(app, "/.well-known/oauth-authorization-server").await;
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
}
#[tokio::test]
async fn rfc8414_metadata_returns_200_independent_of_oidc_flag() {
let dao: Arc<dyn GarrisonDao> = Arc::new(InMemoryDao::new());
let store: Arc<dyn OAuth2ClientStore> = Arc::new(DaoOAuth2ClientStore::new(dao.clone()));
let state = Arc::new(
OAuth2State::new(store, dao, "https://auth.example.com/login".to_string())
.with_issuer("https://auth.example.com")
.with_oidc_discovery_enabled(false),
);
let app = oauth2_external_router(state);
let resp = get(app, "/.well-known/oauth-authorization-server").await;
assert_eq!(resp.status(), StatusCode::OK);
let body = BodyExt::collect(resp.into_body()).await.unwrap().to_bytes();
let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert_eq!(json["issuer"], "https://auth.example.com");
assert_eq!(
json["response_types_supported"],
serde_json::json!(["code"])
);
let grants: Vec<&str> = json["grant_types_supported"]
.as_array()
.unwrap()
.iter()
.map(|v| v.as_str().unwrap())
.collect();
assert!(grants.contains(&"authorization_code"));
let auth_methods: Vec<&str> = json["token_endpoint_auth_methods_supported"]
.as_array()
.unwrap()
.iter()
.map(|v| v.as_str().unwrap())
.collect();
assert!(auth_methods.contains(&"client_secret_basic"));
assert!(auth_methods.contains(&"client_secret_post"));
}
}