use super::{
ActionContext, ActorContext, ApiResponse, SystemTenantCapability, TenantContext, TenantId, User,
};
use crate::error::BaseError;
use crate::router::{Middleware, MiddlewareRole, Next};
use async_trait::async_trait;
pub const TENANT_ID_HEADER: &str = "x-tenant-id";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TenantResolution {
Tenant(TenantContext),
System(SystemTenantCapability),
}
impl TenantResolution {
pub fn system_for(user: &User) -> Result<Self, BaseError> {
if !user.has_role("system") {
return Err(BaseError::PermissionDenied(
"只有已认证 system 角色可获授系统租户 capability".to_string(),
));
}
Ok(Self::System(SystemTenantCapability::issue(
ActorContext::new(user.id),
)))
}
}
impl From<TenantContext> for TenantResolution {
fn from(value: TenantContext) -> Self {
Self::Tenant(value)
}
}
#[async_trait]
pub trait TenantResolver: Send + Sync + 'static {
async fn resolve(
&self,
context: &ActionContext,
requested: Option<TenantId>,
) -> Result<TenantResolution, BaseError>;
async fn resolve_with_context(
&self,
context: &mut ActionContext,
requested: Option<TenantId>,
) -> Result<TenantResolution, BaseError> {
self.resolve(context, requested).await
}
}
pub struct TenantResolverMiddleware<R> {
resolver: R,
}
impl<R> TenantResolverMiddleware<R>
where
R: TenantResolver,
{
pub fn new(resolver: R) -> Self {
Self { resolver }
}
}
#[async_trait]
impl<R> Middleware for TenantResolverMiddleware<R>
where
R: TenantResolver,
{
fn role(&self) -> MiddlewareRole {
MiddlewareRole::TenantResolution
}
async fn handle(
&self,
mut context: ActionContext,
next: Next<'_>,
) -> Result<ApiResponse, BaseError> {
let requested = requested_tenant(&context)?;
let resolution = self
.resolver
.resolve_with_context(&mut context, requested)
.await?;
let context = match resolution {
TenantResolution::Tenant(tenant) => {
tracing::Span::current().record("tenant_scope", "tenant");
tracing::Span::current().record("tenant_id", tenant.id().get());
context.with_tenant(tenant)
}
TenantResolution::System(capability) => {
tracing::Span::current().record("tenant_scope", "system");
context.with_system_tenant(capability)
}
};
next.run(context).await
}
}
fn requested_tenant(context: &ActionContext) -> Result<Option<TenantId>, BaseError> {
let Some(raw) = context.request.get_header(TENANT_ID_HEADER) else {
return Ok(None);
};
let value = raw.parse::<i64>().map_err(|_| {
BaseError::ParamInvalid(
TENANT_ID_HEADER.to_string(),
"租户 ID 必须是正整数".to_string(),
)
})?;
if value <= 0 {
return Err(BaseError::ParamInvalid(
TENANT_ID_HEADER.to_string(),
"租户 ID 必须是正整数".to_string(),
));
}
Ok(Some(TenantId::new(value)))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::action::{ContextKey, Request, TypedHandler, User};
use crate::definition::{
ActionName, ActionRef, ActionSpec, AddonName, AddonSpec, AppBuilder, HttpMethod,
ModuleName, ModuleSpec, RouteSpec,
};
use crate::tools::ToolsBuilder;
use async_trait::async_trait;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use yang_base_derive::Action;
#[derive(Debug, Default, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
struct EmptyInput {}
#[derive(Debug, Serialize, JsonSchema)]
struct TenantProbeOutput {
tenant_id: Option<i64>,
system_actor_id: Option<i64>,
resolver_evidence: Option<i64>,
}
const RESOLVER_EVIDENCE: ContextKey<i64> = ContextKey::new("resolver_evidence");
#[derive(Action)]
#[action(name = "tenant_probe", display_name = "租户探针")]
struct TenantProbe;
#[async_trait]
impl TypedHandler for TenantProbe {
type Input = EmptyInput;
type Output = TenantProbeOutput;
async fn handle(
&self,
mut context: ActionContext,
_input: Self::Input,
) -> Result<Self::Output, BaseError> {
let resolver_evidence = context.request_context().get(RESOLVER_EVIDENCE).copied();
match context.tenant() {
Ok(tenant) => Ok(TenantProbeOutput {
tenant_id: Some(tenant.id().get()),
system_actor_id: None,
resolver_evidence,
}),
Err(_) => Ok(TenantProbeOutput {
tenant_id: None,
system_actor_id: Some(context.system_tenant()?.actor().user_id()),
resolver_evidence,
}),
}
}
}
struct MembershipResolver;
#[async_trait]
impl TenantResolver for MembershipResolver {
async fn resolve(
&self,
context: &ActionContext,
requested: Option<TenantId>,
) -> Result<TenantResolution, BaseError> {
let user = context
.authenticated_user()
.ok_or_else(|| BaseError::Unauthorized("租户解析需要已认证用户".to_string()))?;
if user.has_role("system") {
return TenantResolution::system_for(user);
}
let tenant = requested.unwrap_or_else(|| TenantId::new(10));
if user.id == 7 && tenant == TenantId::new(10) {
Ok(TenantContext::new(tenant).into())
} else {
Err(BaseError::PermissionDenied(format!(
"用户无权访问租户 {}",
tenant.get()
)))
}
}
async fn resolve_with_context(
&self,
context: &mut ActionContext,
requested: Option<TenantId>,
) -> Result<TenantResolution, BaseError> {
let resolution = self.resolve(context, requested).await?;
if matches!(resolution, TenantResolution::Tenant(_)) {
context.request_context().insert(RESOLVER_EVIDENCE, 42);
}
Ok(resolution)
}
}
fn test_app() -> crate::definition::BuiltApp {
let module_name = ModuleName::new("org.tenant").expect("测试 Module 名称应有效");
let module = ModuleSpec::new(module_name)
.middleware(TenantResolverMiddleware::new(MembershipResolver))
.action(
ActionSpec::new(
ActionName::new("tenant_probe").expect("测试 Action 名称应有效"),
RouteSpec::new(
HttpMethod::Get,
"/api/v1/org/tenant/probe",
"org.tenant.tenant_probe",
),
),
TenantProbe,
);
let tools = Arc::new(ToolsBuilder::new().build().expect("测试 Tools 应构建成功"));
AppBuilder::new()
.addon(
AddonSpec::new(AddonName::new("org").expect("测试 Addon 名称应有效"))
.module(module),
)
.build(tools)
.expect("租户解析测试应用应构建成功")
}
fn probe_handle(app: &crate::definition::BuiltApp) -> crate::definition::ActionHandle {
app.registry()
.resolve(&ActionRef::new(
ModuleName::new("org.tenant").expect("测试 Module 名称应有效"),
ActionName::new("tenant_probe").expect("测试 Action 名称应有效"),
))
.expect("租户探针应已注册")
}
fn tenant_response(response: ApiResponse) -> (Option<i64>, Option<i64>) {
let data = response.data.expect("租户探针应返回 JSON data");
(
data.get("tenant_id").and_then(serde_json::Value::as_i64),
data.get("system_actor_id")
.and_then(serde_json::Value::as_i64),
)
}
async fn dispatch_as(
app: &crate::definition::BuiltApp,
request: Request,
user: Option<User>,
) -> Result<ApiResponse, BaseError> {
let mut context = app.context(request);
if let Some(user) = user {
context = context.with_user(user);
}
app.dispatch_context(probe_handle(app), context).await
}
#[tokio::test]
async fn resolver_accepts_authorized_candidate_and_safe_default() {
let app = test_app();
let user = User::new(7, "member");
let selected = dispatch_as(
&app,
Request::new(serde_json::json!({})).header(TENANT_ID_HEADER, "10"),
Some(user.clone()),
)
.await
.expect("成员应能选择所属租户");
assert_eq!(
selected
.data
.as_ref()
.and_then(|data| data.get("resolver_evidence"))
.and_then(serde_json::Value::as_i64),
Some(42),
"resolver 同一次事实查询写入的请求 capability 必须到达后续 Action"
);
assert_eq!(tenant_response(selected), (Some(10), None));
let defaulted = dispatch_as(&app, Request::new(serde_json::json!({})), Some(user))
.await
.expect("resolver 应能安全选择服务端默认租户");
assert_eq!(tenant_response(defaulted), (Some(10), None));
}
#[tokio::test]
async fn resolver_rejects_invalid_cross_tenant_and_unauthenticated_requests() {
let app = test_app();
let user = User::new(7, "member");
for invalid in ["", "system", "0", "-1"] {
let result = dispatch_as(
&app,
Request::new(serde_json::json!({})).header(TENANT_ID_HEADER, invalid),
Some(user.clone()),
)
.await;
assert!(matches!(
result,
Err(BaseError::ParamInvalid(field, _)) if field == TENANT_ID_HEADER
));
}
let cross_tenant = dispatch_as(
&app,
Request::new(serde_json::json!({})).header(TENANT_ID_HEADER, "20"),
Some(user),
)
.await;
assert!(matches!(
cross_tenant,
Err(BaseError::PermissionDenied(message)) if message.contains("租户 20")
));
let unauthenticated = dispatch_as(
&app,
Request::new(serde_json::json!({})).header(TENANT_ID_HEADER, "10"),
None,
)
.await;
assert!(matches!(unauthenticated, Err(BaseError::Unauthorized(_))));
}
#[tokio::test]
async fn system_context_can_only_come_from_trusted_resolver() {
let app = test_app();
let system_user = User::new(1, "root").with_roles(["system"]);
assert!(matches!(
TenantResolution::system_for(&User::new(7, "member")),
Err(BaseError::PermissionDenied(_))
));
let response = dispatch_as(&app, Request::new(serde_json::json!({})), Some(system_user))
.await
.expect("系统角色应由可信 resolver 映射为 system 上下文");
assert_eq!(tenant_response(response), (None, Some(1)));
let forged = dispatch_as(
&app,
Request::new(serde_json::json!({})).header(TENANT_ID_HEADER, "system"),
Some(User::new(7, "member")),
)
.await;
assert!(matches!(
forged,
Err(BaseError::ParamInvalid(field, _)) if field == TENANT_ID_HEADER
));
}
#[cfg(feature = "token")]
mod token_auth_chain {
use super::*;
use crate::action::TokenAuthMiddleware;
fn chain_tools() -> Arc<crate::tools::Tools> {
Arc::new(
ToolsBuilder::new()
.token(
crate::token::TokenManager::new_symmetric(
"tenant-chain-test-secretxxxxxxxx",
jsonwebtoken::Algorithm::HS256,
"test-issuer".to_string(),
"test-audience".to_string(),
3600,
7200,
)
.expect("测试 TokenManager 应构建成功"),
)
.build()
.expect("测试 Tools 应构建成功"),
)
}
fn chain_handle(
app: &crate::definition::BuiltApp,
module: &str,
action: &str,
) -> crate::definition::ActionHandle {
app.registry()
.resolve(&ActionRef::new(
ModuleName::new(module).expect("测试 Module 名称应有效"),
ActionName::new(action).expect("测试 Action 名称应有效"),
))
.expect("探针 Action 应已注册")
}
fn chain_access_token(app: &crate::definition::BuiltApp) -> String {
app.tools()
.token()
.expect("测试应用应配置 TokenManager")
.generate_access_token("user-7", serde_json::json!({}))
.expect("测试 Access Token 应生成成功")
}
fn protected_chain_app() -> crate::definition::BuiltApp {
let module = ModuleSpec::new(
ModuleName::new("org.tenant_chain").expect("测试 Module 名称应有效"),
)
.middleware(TokenAuthMiddleware::new(|claims| {
User::new(7, claims.sub.clone())
}))
.middleware(TenantResolverMiddleware::new(MembershipResolver))
.action(
ActionSpec::new(
ActionName::new("tenant_probe").expect("测试 Action 名称应有效"),
RouteSpec::new(
HttpMethod::Get,
"/api/v1/org/tenant-chain/probe",
"org.tenant_chain.tenant_probe",
),
),
TenantProbe,
);
AppBuilder::new()
.addon(
AddonSpec::new(AddonName::new("org").expect("测试 Addon 名称应有效"))
.module(module),
)
.build(chain_tools())
.expect("链路测试应用应构建成功")
}
#[test]
fn build_rejects_tenant_before_token_authentication() {
let module = ModuleSpec::new(
ModuleName::new("org.tenant_bad_order").expect("测试 Module 名称应有效"),
)
.middleware(TenantResolverMiddleware::new(MembershipResolver))
.middleware(TokenAuthMiddleware::new(|claims| {
User::new(7, claims.sub.clone())
}))
.action(
ActionSpec::new(
ActionName::new("tenant_probe").expect("测试 Action 名称应有效"),
RouteSpec::new(
HttpMethod::Get,
"/api/v1/org/tenant-bad-order/probe",
"org.tenant_bad_order.tenant_probe",
),
),
TenantProbe,
);
let error = AppBuilder::new()
.addon(
AddonSpec::new(AddonName::new("org").expect("测试 Addon 名称应有效"))
.module(module),
)
.build(chain_tools())
.expect_err("租户解析先于认证必须在构建期失败");
assert!(matches!(
error,
crate::definition::BuildError::InvalidReference {
kind: "Middleware order",
..
}
));
}
#[tokio::test]
async fn token_auth_runs_before_tenant_resolver_and_fails_closed() {
let app = protected_chain_app();
let handle = chain_handle(&app, "org.tenant_chain", "tenant_probe");
let missing = app
.dispatch(
handle,
Request::new(serde_json::json!({})).header(TENANT_ID_HEADER, "10"),
)
.await;
assert!(matches!(
missing,
Err(BaseError::Unauthorized(message)) if message.contains("Authorization Bearer Token")
));
let invalid = app
.dispatch(
handle,
Request::new(serde_json::json!({}))
.header("Authorization", "Bearer invalid-token")
.header(TENANT_ID_HEADER, "10"),
)
.await;
assert!(matches!(invalid, Err(BaseError::TokenVerifyFailed(_))));
let token = chain_access_token(&app);
let no_store = app
.dispatch(
handle,
Request::new(serde_json::json!({}))
.header("Authorization", format!("Bearer {token}"))
.header(TENANT_ID_HEADER, "10"),
)
.await;
assert!(
matches!(no_store, Err(BaseError::RedisNotInitialized)),
"无撤销存储时不得降级放行: {no_store:?}"
);
}
#[derive(Debug, Serialize, JsonSchema)]
struct TenantAuthProbeOutput {
tenant_id: i64,
authenticated: bool,
}
#[derive(Action)]
#[action(name = "tenant_auth_probe", display_name = "租户认证探针", public)]
struct TenantAuthProbe;
#[async_trait]
impl TypedHandler for TenantAuthProbe {
type Input = EmptyInput;
type Output = TenantAuthProbeOutput;
async fn handle(
&self,
context: ActionContext,
_input: Self::Input,
) -> Result<Self::Output, BaseError> {
let tenant = context.tenant()?;
Ok(TenantAuthProbeOutput {
tenant_id: tenant.id().get(),
authenticated: context.authenticated_user().is_some(),
})
}
}
struct GuestOrMemberResolver;
#[async_trait]
impl TenantResolver for GuestOrMemberResolver {
async fn resolve(
&self,
context: &ActionContext,
requested: Option<TenantId>,
) -> Result<TenantResolution, BaseError> {
let Some(user) = context.authenticated_user() else {
return Ok(TenantContext::new(TenantId::new(1)).into());
};
let tenant = requested.unwrap_or_else(|| TenantId::new(10));
if user.id == 7 && tenant == TenantId::new(10) {
Ok(TenantContext::new(tenant).into())
} else {
Err(BaseError::PermissionDenied(format!(
"用户无权访问租户 {}",
tenant.get()
)))
}
}
}
fn public_chain_app() -> crate::definition::BuiltApp {
let module = ModuleSpec::new(
ModuleName::new("org.tenant_public").expect("测试 Module 名称应有效"),
)
.middleware(
TokenAuthMiddleware::new(|claims| User::new(7, claims.sub.clone()))
.authenticate_public_actions(),
)
.middleware(TenantResolverMiddleware::new(GuestOrMemberResolver))
.action(
ActionSpec::new(
ActionName::new("tenant_auth_probe").expect("测试 Action 名称应有效"),
RouteSpec::new(
HttpMethod::Get,
"/api/v1/org/tenant-public/probe",
"org.tenant_public.tenant_auth_probe",
),
)
.public(true),
TenantAuthProbe,
);
AppBuilder::new()
.addon(
AddonSpec::new(AddonName::new("org").expect("测试 Addon 名称应有效"))
.module(module),
)
.build(chain_tools())
.expect("三方组合测试应用应构建成功")
}
fn probe_observation(response: ApiResponse) -> (i64, bool) {
let data = response.data.expect("探针应返回 JSON data");
(
data.get("tenant_id")
.and_then(serde_json::Value::as_i64)
.expect("探针应返回 tenant_id"),
data.get("authenticated")
.and_then(serde_json::Value::as_bool)
.expect("探针应返回 authenticated 布尔值"),
)
}
#[tokio::test]
async fn public_action_with_optional_auth_and_tenant_middleware_compose() {
let app = public_chain_app();
let handle = chain_handle(&app, "org.tenant_public", "tenant_auth_probe");
let anonymous = app
.dispatch(
handle,
Request::new(serde_json::json!({})).header(TENANT_ID_HEADER, "10"),
)
.await
.expect("匿名请求应经可选认证放行并获得访客租户上下文");
assert_eq!(probe_observation(anonymous), (1, false));
let wrong_scheme = app
.dispatch(
handle,
Request::new(serde_json::json!({}))
.header("Authorization", "Basic credentials"),
)
.await;
assert!(matches!(
wrong_scheme,
Err(BaseError::Unauthorized(message)) if message.contains("Authorization Bearer Token")
));
let invalid = app
.dispatch(
handle,
Request::new(serde_json::json!({}))
.header("Authorization", "Bearer invalid-token"),
)
.await;
assert!(matches!(invalid, Err(BaseError::TokenVerifyFailed(_))));
let token = chain_access_token(&app);
let no_store = app
.dispatch(
handle,
Request::new(serde_json::json!({}))
.header("Authorization", format!("Bearer {token}")),
)
.await;
assert!(
matches!(no_store, Err(BaseError::RedisNotInitialized)),
"无撤销存储时不得降级为匿名: {no_store:?}"
);
}
}
}