taolib.testing.auth.fastapi.dependencies#
FastAPI 依赖注入工厂。
提供认证和授权相关的 FastAPI 依赖注入函数。
Attributes#
Functions#
|
创建认证依赖注入函数。 |
|
从 Authorization 头部提取 API Key。 |
|
角色检查依赖工厂。 |
|
权限检查依赖工厂。 |
|
作用域检查依赖工厂。 |
Module Contents#
- taolib.testing.auth.fastapi.dependencies.AuthDependency#
- taolib.testing.auth.fastapi.dependencies.create_auth_dependency(jwt_service: taolib.testing.auth.tokens.JWTService, blacklist: taolib.testing.auth.blacklist.TokenBlacklistProtocol | None = None, api_key_lookup: taolib.testing.auth.api_key.APIKeyLookupProtocol | None = None, oauth2_scheme: fastapi.security.OAuth2PasswordBearer | None = None, api_key_header: fastapi.security.APIKeyHeader | None = None) AuthDependency#
创建认证依赖注入函数。
返回一个可直接用于
Depends()的异步函数,支持 JWT Bearer 和 API Key 双通道认证。- 参数:
jwt_service -- JWT 令牌服务
blacklist -- 令牌黑名单实现(默认 NullTokenBlacklist)
api_key_lookup -- API 密钥查找实现(None 表示不启用 API Key 认证)
oauth2_scheme -- OAuth2 安全方案(None 使用默认配置)
api_key_header -- API Key Header 安全方案(None 使用默认配置)
- 返回:
FastAPI 依赖注入函数
- taolib.testing.auth.fastapi.dependencies._extract_api_key_from_auth_header(request: fastapi.Request) str | None#
从 Authorization 头部提取 API Key。
支持格式:
Authorization: ApiKey <key>- 参数:
request -- FastAPI 请求对象
- 返回:
API Key 字符串,未找到返回 None
- taolib.testing.auth.fastapi.dependencies.require_roles(*roles: str, auth_dependency: AuthDependency | None = None) collections.abc.Callable[Ellipsis, collections.abc.Coroutine[Any, Any, taolib.testing.auth.models.AuthenticatedUser]]#
角色检查依赖工厂。
要求用户至少拥有指定角色之一。
- 参数:
roles -- 允许的角色名称
auth_dependency -- 认证依赖(None 时需要在路由中单独注入)
- 返回:
FastAPI 依赖注入函数
- taolib.testing.auth.fastapi.dependencies.require_permissions(resource: str, action: str, rbac_policy: taolib.testing.auth.rbac.RBACPolicy, auth_dependency: AuthDependency | None = None) collections.abc.Callable[Ellipsis, collections.abc.Coroutine[Any, Any, taolib.testing.auth.models.AuthenticatedUser]]#
权限检查依赖工厂。
通过 RBAC 策略检查用户是否有指定资源的操作权限。
- 参数:
resource -- 资源类型
action -- 操作类型
rbac_policy -- RBAC 策略引擎
auth_dependency -- 认证依赖
- 返回:
FastAPI 依赖注入函数
- taolib.testing.auth.fastapi.dependencies.require_scope(scope_type: str, scope_value: str, rbac_policy: taolib.testing.auth.rbac.RBACPolicy, auth_dependency: AuthDependency | None = None) collections.abc.Callable[Ellipsis, collections.abc.Coroutine[Any, Any, taolib.testing.auth.models.AuthenticatedUser]]#
作用域检查依赖工厂。
通过 RBAC 策略检查用户角色是否在指定作用域内。
- 参数:
scope_type -- 作用域类型
scope_value -- 作用域值
rbac_policy -- RBAC 策略引擎
auth_dependency -- 认证依赖
- 返回:
FastAPI 依赖注入函数