sz_rust_auth_facade/lib.rs
1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2024-2026 SZ-Rust Team
3//
4//! SZ-Rust Auth Facade
5//!
6//! 提取自 `sz-rust-core` 的认证与网关模块,提供微信、OAuth2、Gateway 三大能力。
7//!
8//! ## 模块结构
9//!
10//! | 模块 | 对齐 PHP | 说明 |
11//! |------|---------|------|
12//! | [`wechat`] | `overtrue/wechat` / `EasyWeChat` | 微信 SDK(公众号/小程序/开放平台/企业微信) |
13//! | [`oauth`] | Laravel Socialite | OAuth2 客户端(OAuth2Provider trait + GenericOAuth2Provider) |
14//! | [`gateway`] | `GatewayWorker\Gateway` | WebSocket 网关客户端管理(Gateway API 抽象) |
15//!
16//! ## 用法
17//!
18//! ```ignore
19//! use sz_rust_auth_facade::oauth::{OAuth2Provider, GenericOAuth2Provider};
20//! use sz_rust_auth_facade::wechat::WechatApp;
21//! use sz_rust_auth_facade::gateway::{GatewayApi, GatewayTransport};
22//! ```
23//!
24//! ## 与 sz-rust-core 的关系
25//!
26//! `sz-rust-core` 通过 `pub use sz_rust_auth_facade as auth;` 重导出本 crate,
27//! 因此 `sz_rust_core::auth::oauth` 等价于 `sz_rust_auth_facade::oauth`。
28//! 下游业务包推荐直接依赖 `sz-rust-auth-facade` 以减少编译耦合。
29
30#![deny(unsafe_code)]
31#![warn(missing_docs)]
32
33pub mod gateway;
34pub mod oauth;
35pub mod wechat;
36
37/// Refresh Token 双 Token 机制(SsoJwtCodec + Issuer + Verifier + Revoker + Store)
38pub mod refresh;
39
40/// SSO 认证中心(SsoService + axum 路由,需启用 `axum` feature)
41pub mod sso;
42
43/// Redis Gateway 集群广播(需启用 `redis-gateway` feature)
44///
45/// 提供 [`RedisGatewayTransport`],基于 Redis pub/sub 实现跨节点 WebSocket 消息广播。
46#[cfg(feature = "redis-gateway")]
47pub mod redis_gateway;
48
49/// Redis 存储后端(需启用 `redis-store` feature)
50///
51/// 提供 [`RedisRefreshTokenStore`] / [`RedisTokenBlacklist`] / [`RedisConfig`],
52/// 替换 Memory 实现,使 SSO 双 Token 机制在生产环境可持久化。
53#[cfg(feature = "redis-store")]
54pub mod redis_store;
55
56/// OAuth2 Token 存储模块(需启用 `redis-store` feature)
57///
58/// 提供 [`OAuth2TokenStore`] trait 和 [`RedisOAuth2TokenStore`] 实现,
59/// 用于持久化 OAuth2 token 响应。
60#[cfg(feature = "redis-store")]
61pub mod oauth_store;
62
63/// JWT Audience 校验模块
64///
65/// 提供 [`JwtAudienceValidator`],根据配置的期望 audience 列表校验 JWT `aud` 字段。
66pub mod jwt;
67/// OAuth2 回调中间件模块
68///
69/// 提供 [`OAuth2StateStore`] trait 和 axum 回调中间件(需 `axum` feature)。
70pub mod oauth_callback;
71
72/// P4-3: OAuth2 设备码流程(spec 5.13.1 规则 5-8)
73///
74/// 提供 [`DeviceCodeFlow`],颁发 device_code/user_code + 轮询状态机 + 过期 + slow_down。
75#[cfg(feature = "device-code")]
76pub mod oauth_device_flow;
77
78/// P4-3: OAuth2 授权码存储 + 令牌存储 + PKCE + scope 校验(spec 5.13.1 规则 1-4/9-11/13)
79///
80/// 提供 [`AuthorizationCodeStore`](一次性使用 + PKCE S256/Plain 校验)、
81/// [`TokenStore`](令牌存储 + 刷新)、[`ScopeValidator`](scope 超范围拒绝)。
82#[cfg(feature = "oauth-token-store")]
83pub mod oauth_token_store;
84
85pub use jwt::JwtAudienceValidator;
86// ============================================================================
87// v1.7.0 P4-4.1: RBAC 细粒度权限
88// ============================================================================
89#[cfg(feature = "rbac")]
90pub mod rbac;
91#[cfg(feature = "rbac")]
92pub use rbac::{
93 DataScope, NoCache, PermissionCache, PermissionDecision, RbacEngine, RbacError, Resource,
94 RoleId,
95};