fix(web): allow explicitly trusted extra OIDC audiences (#2873)

This commit is contained in:
fanyang
2026-11-24 20:28:58 +08:00
committed by GitHub
parent 1415628969
commit c97f36b8d4
2 changed files with 100 additions and 5 deletions
+3
View File
@@ -67,6 +67,9 @@ cli:
oidc_scopes:
en: "OIDC scopes to request during login. Supports comma-separated values or repeated --oidc-scopes flags, default: openid,profile"
zh-CN: "登录时请求的 OIDC scopes。支持逗号分隔或多次指定 --oidc-scopes,默认: openid,profile"
oidc_extra_audiences:
en: "Comma-separated list of trusted extra ID token audiences, such as a ZITADEL project ID. Default: none. The token must still include this application's client ID."
zh-CN: "ID Token 中受信任的额外 audience,多个值用逗号分隔,例如 ZITADEL 项目 ID。默认不信任额外 audience;令牌仍须包含本应用的客户端 ID。"
oidc_redirect_url:
en: "The OIDC redirect URL (callback URL), must match exactly what is registered with your Identity Provider. Required when using OIDC. Example: http://your-domain.com:11211/api/v1/auth/oidc/callback"
zh-CN: "OIDC 重定向 URL(回调 URL),必须与身份提供商注册的地址完全一致。使用 OIDC 时必须提供。示例: http://your-domain.com:11211/api/v1/auth/oidc/callback"
+97 -5
View File
@@ -8,9 +8,10 @@ use subtle::ConstantTimeEq;
use axum::Router;
use axum::routing::get;
use openidconnect::core::{
CoreAuthDisplay, CoreAuthPrompt, CoreErrorResponseType, CoreGenderClaim, CoreJsonWebKey,
CoreJweContentEncryptionAlgorithm, CoreJwsSigningAlgorithm, CoreProviderMetadata,
CoreRevocableToken, CoreRevocationErrorResponse, CoreTokenIntrospectionResponse, CoreTokenType,
CoreAuthDisplay, CoreAuthPrompt, CoreErrorResponseType, CoreGenderClaim, CoreIdTokenVerifier,
CoreJsonWebKey, CoreJweContentEncryptionAlgorithm, CoreJwsSigningAlgorithm,
CoreProviderMetadata, CoreRevocableToken, CoreRevocationErrorResponse,
CoreTokenIntrospectionResponse, CoreTokenType,
};
use openidconnect::{
Client, ClientId, ClientSecret, EmptyExtraTokenFields, EndpointMaybeSet, EndpointNotSet,
@@ -146,6 +147,14 @@ pub struct OidcOptions {
)]
pub oidc_scopes: Vec<String>,
#[arg(
long,
value_delimiter = ',',
value_parser = clap::builder::NonEmptyStringValueParser::new(),
help = t!("cli.oidc_extra_audiences").to_string()
)]
pub oidc_extra_audiences: Vec<String>,
#[arg(long, help = t!("cli.oidc_redirect_url").to_string())]
pub oidc_redirect_url: Option<String>,
@@ -165,6 +174,7 @@ impl OidcOptions {
|| self.oidc_frontend_base_url.is_some()
|| self.oidc_username_claim != "preferred_username"
|| self.oidc_scopes != DEFAULT_OIDC_SCOPES
|| !self.oidc_extra_audiences.is_empty()
|| self.oidc_disable_pkce
}
}
@@ -178,6 +188,7 @@ pub struct OidcConfig {
pub redirect_url: Option<RedirectUrl>,
pub username_claim: String,
pub scopes: Vec<String>,
extra_audiences: Vec<String>,
pub pkce_enabled: bool,
pub frontend_base_url: Option<String>,
pub http_client: Option<ReqwestClient>,
@@ -197,6 +208,7 @@ impl OidcConfig {
.iter()
.map(|scope| scope.to_string())
.collect(),
extra_audiences: Vec::new(),
pkce_enabled: false,
frontend_base_url: None,
http_client: None,
@@ -211,6 +223,7 @@ impl OidcConfig {
oidc_client_secret,
oidc_username_claim,
oidc_scopes,
oidc_extra_audiences,
oidc_redirect_url,
oidc_disable_pkce,
oidc_frontend_base_url,
@@ -269,6 +282,7 @@ impl OidcConfig {
redirect_url: Some(redirect_url),
username_claim: oidc_username_claim,
scopes: normalize_oidc_scopes(&oidc_scopes),
extra_audiences: oidc_extra_audiences,
pkce_enabled: !oidc_disable_pkce,
frontend_base_url: oidc_frontend_base_url,
http_client: Some(http_client),
@@ -281,6 +295,15 @@ impl OidcConfig {
}
}
fn with_extra_audiences<'a>(
verifier: CoreIdTokenVerifier<'a>,
extra_audiences: &'a [String],
) -> CoreIdTokenVerifier<'a> {
verifier.set_other_audience_verifier_fn(move |aud| {
extra_audiences.iter().any(|id| id == aud.as_str())
})
}
pub fn router() -> Router<AppStateInner> {
Router::new()
.route("/api/v1/auth/oidc/config", get(self::route::oidc_config))
@@ -576,7 +599,9 @@ mod route {
}
};
let claims = match id_token.claims(&client.id_token_verifier(), &Nonce::new(stored_nonce)) {
let id_token_verifier =
super::with_extra_audiences(client.id_token_verifier(), &oidc.extra_audiences);
let claims = match id_token.claims(&id_token_verifier, &Nonce::new(stored_nonce)) {
Ok(c) => c,
Err(e) => {
tracing::error!("Failed to verify ID token: {:?}", e);
@@ -589,7 +614,6 @@ mod route {
};
if let Some(expected_at_hash) = claims.access_token_hash() {
let id_token_verifier = client.id_token_verifier();
let (Ok(signing_alg), Ok(signing_key)) = (
id_token.signing_alg(),
id_token.signing_key(&id_token_verifier),
@@ -777,6 +801,7 @@ mod tests {
oidc_client_secret: Some("test-secret".to_owned()),
oidc_username_claim: "preferred_username".to_owned(),
oidc_scopes: vec!["openid".to_owned()],
oidc_extra_audiences: vec![],
oidc_redirect_url: Some("http://localhost/callback".to_owned()),
oidc_disable_pkce: false,
oidc_frontend_base_url: None,
@@ -816,6 +841,73 @@ mod tests {
assert_eq!(redirect_hits.load(Ordering::SeqCst), 0);
}
#[test]
fn extra_audiences_require_client_id_and_trust_for_every_other_audience() {
use chrono::Utc;
use clap::Parser;
use openidconnect::core::{CoreHmacKey, CoreIdToken, CoreIdTokenClaims, CoreJsonWebKeySet};
use openidconnect::{
Audience, ClaimsVerificationError, Nonce, StandardClaims, SubjectIdentifier,
};
let issuer = IssuerUrl::new("https://issuer.example".into()).unwrap();
let alg = CoreJwsSigningAlgorithm::HmacSha256;
let verifier = CoreIdTokenVerifier::new_confidential_client(
ClientId::new("client".into()),
ClientSecret::new("test-secret".into()),
issuer.clone(),
CoreJsonWebKeySet::default(),
)
.set_allowed_algs([alg.clone()]);
let nonce = Nonce::new_random();
let now = Utc::now();
let default = crate::Cli::try_parse_from(["easytier-web"]).unwrap().oidc;
let configured = crate::Cli::try_parse_from([
"easytier-web",
"--oidc-extra-audiences",
"project,other-client",
])
.unwrap()
.oidc;
for (trusted, audiences, accept) in [
(&default, vec!["client"], true),
(&default, vec!["client", "project"], false),
(&configured, vec!["client", "project", "other-client"], true),
(&configured, vec!["project"], false),
(&configured, vec!["client", "project", "unknown"], false),
] {
let token = CoreIdToken::new(
CoreIdTokenClaims::new(
issuer.clone(),
audiences
.iter()
.map(|&id| Audience::new(id.into()))
.collect(),
now + chrono::Duration::minutes(5),
now,
StandardClaims::new(SubjectIdentifier::new("user".into())),
Default::default(),
)
.set_nonce(Some(nonce.clone())),
&CoreHmacKey::new("test-secret"),
alg.clone(),
None,
None,
)
.unwrap();
let verifier = with_extra_audiences(verifier.clone(), &trusted.oidc_extra_audiences);
let result = token.claims(&verifier, &nonce);
if accept {
result.unwrap();
} else {
assert!(
matches!(result, Err(ClaimsVerificationError::InvalidAudience(_))),
"{audiences:?}: {result:?}"
);
}
}
}
#[test]
fn test_dot_path_to_json_pointer() {
use serde_json::json;