skyplatform-iam 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,262 @@
1
+ Metadata-Version: 2.4
2
+ Name: skyplatform-iam
3
+ Version: 1.0.0
4
+ Summary: SkyPlatform IAM认证SDK,提供FastAPI中间件和认证路由
5
+ Project-URL: Homepage, https://github.com/xinmayoujiang12621/agenterra_iam
6
+ Project-URL: Documentation, https://skyplatform-iam.readthedocs.io/
7
+ Project-URL: Repository, https://github.com/xinmayoujiang12621/agenterra_iam.git
8
+ Project-URL: Issues, https://github.com/xinmayoujiang12621/agenterra_iam/issues
9
+ Project-URL: Changelog, https://github.com/xinmayoujiang12621/agenterra_iam/blob/main/CHANGELOG.md
10
+ Author-email: x9 <xuanxienanxunmobao@gmail.com>
11
+ License: MIT
12
+ Keywords: authentication,fastapi,iam,middleware,skyplatform
13
+ Classifier: Development Status :: 5 - Production/Stable
14
+ Classifier: Framework :: FastAPI
15
+ Classifier: Intended Audience :: Developers
16
+ Classifier: License :: OSI Approved :: MIT License
17
+ Classifier: Operating System :: OS Independent
18
+ Classifier: Programming Language :: Python :: 3
19
+ Classifier: Programming Language :: Python :: 3.8
20
+ Classifier: Programming Language :: Python :: 3.9
21
+ Classifier: Programming Language :: Python :: 3.10
22
+ Classifier: Programming Language :: Python :: 3.11
23
+ Classifier: Programming Language :: Python :: 3.12
24
+ Classifier: Topic :: Internet :: WWW/HTTP :: HTTP Servers
25
+ Classifier: Topic :: Security
26
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
27
+ Requires-Python: >=3.8
28
+ Requires-Dist: fastapi>=0.68.0
29
+ Requires-Dist: pydantic>=1.8.0
30
+ Requires-Dist: python-dotenv>=0.19.0
31
+ Requires-Dist: requests>=2.25.0
32
+ Requires-Dist: starlette>=0.14.0
33
+ Provides-Extra: dev
34
+ Requires-Dist: black>=22.0.0; extra == 'dev'
35
+ Requires-Dist: flake8>=4.0.0; extra == 'dev'
36
+ Requires-Dist: httpx>=0.23.0; extra == 'dev'
37
+ Requires-Dist: isort>=5.10.0; extra == 'dev'
38
+ Requires-Dist: mypy>=0.950; extra == 'dev'
39
+ Requires-Dist: pytest-asyncio>=0.18.0; extra == 'dev'
40
+ Requires-Dist: pytest>=6.0.0; extra == 'dev'
41
+ Description-Content-Type: text/markdown
42
+
43
+ # SkyPlatform IAM SDK
44
+
45
+ SkyPlatform IAM认证SDK,提供FastAPI中间件和认证路由,简化第三方服务的认证集成。
46
+
47
+ ## 功能特性
48
+
49
+ - 🔐 **FastAPI中间件**: 自动拦截请求进行Token验证和权限检查
50
+ - 🚀 **认证路由**: 封装用户注册、登录、登出等认证接口
51
+ - ⚙️ **灵活配置**: 支持环境变量和代码配置
52
+ - 🛡️ **白名单机制**: 支持配置无需认证的路径
53
+ - 🔧 **完整兼容**: 基于现有ConnectAgenterraIam类,保持完全兼容
54
+ - 📝 **类型提示**: 完整的TypeScript风格类型提示
55
+ - 🚨 **异常处理**: 完善的错误处理和自定义异常
56
+
57
+ ## 快速开始
58
+
59
+ ### 安装
60
+
61
+ ```bash
62
+ pip install skyplatform-iam
63
+ ```
64
+
65
+ ### 环境变量配置
66
+
67
+ 创建 `.env` 文件或设置环境变量:
68
+
69
+ ```bash
70
+ AGENTERRA_IAM_HOST=https://your-iam-host.com
71
+ AGENTERRA_SERVER_NAME=your-server-name
72
+ AGENTERRA_ACCESS_KEY=your-access-key
73
+ ```
74
+
75
+ ### 基本使用
76
+
77
+ #### 方式1:一键设置(推荐)
78
+
79
+ ```python
80
+ from fastapi import FastAPI
81
+ from skyplatform_iam import setup_auth
82
+
83
+ app = FastAPI()
84
+
85
+ # 一键设置认证中间件和路由
86
+ setup_auth(app)
87
+
88
+ @app.get("/protected")
89
+ async def protected_endpoint(request):
90
+ # 获取用户信息(由中间件自动设置)
91
+ user = request.state.user
92
+ return {"message": "访问成功", "user": user}
93
+ ```
94
+
95
+ #### 方式2:手动设置
96
+
97
+ ```python
98
+ from fastapi import FastAPI
99
+ from skyplatform_iam import AuthConfig, AuthMiddleware, create_auth_router
100
+
101
+ app = FastAPI()
102
+
103
+ # 创建配置
104
+ config = AuthConfig.from_env()
105
+
106
+ # 添加认证中间件
107
+ app.add_middleware(AuthMiddleware, config=config)
108
+
109
+ # 添加认证路由
110
+ auth_router = create_auth_router(config=config, prefix="/auth")
111
+ app.include_router(auth_router)
112
+ ```
113
+
114
+ #### 方式3:自定义配置
115
+
116
+ ```python
117
+ from skyplatform_iam import AuthConfig, setup_auth
118
+
119
+ # 自定义配置
120
+ config = AuthConfig(
121
+ agenterra_iam_host="https://your-iam-host.com",
122
+ server_name="your-server-name",
123
+ access_key="your-access-key",
124
+ whitelist_paths=[
125
+ "/docs", "/redoc", "/openapi.json",
126
+ "/health", "/public",
127
+ "/auth/register", "/auth/login"
128
+ ],
129
+ enable_debug=True
130
+ )
131
+
132
+ setup_auth(app, config=config)
133
+ ```
134
+
135
+ ## API接口
136
+
137
+ SDK自动提供以下认证接口:
138
+
139
+ - `POST /auth/register` - 用户注册
140
+ - `POST /auth/login` - 用户登录
141
+ - `POST /auth/login_without_password` - 免密登录
142
+ - `POST /auth/logout` - 用户登出
143
+ - `POST /auth/reset_password` - 重置密码
144
+ - `POST /auth/refresh_token` - 刷新Token
145
+ - `POST /auth/assign_role` - 分配角色
146
+ - `POST /auth/user_info` - 获取用户信息
147
+
148
+ ## 中间件功能
149
+
150
+ ### 自动Token验证
151
+
152
+ 中间件会自动:
153
+ 1. 检查请求路径是否在白名单中
154
+ 2. 从请求头提取Authorization Token
155
+ 3. 调用IAM服务验证Token和权限
156
+ 4. 将用户信息设置到 `request.state.user`
157
+
158
+ ### 白名单配置
159
+
160
+ 默认白名单路径:
161
+ - `/docs`, `/redoc`, `/openapi.json` - API文档
162
+ - `/health` - 健康检查
163
+ - `/auth/*` - 认证相关接口
164
+
165
+ 添加自定义白名单:
166
+
167
+ ```python
168
+ config = AuthConfig.from_env()
169
+ config.add_whitelist_path("/public")
170
+ config.add_whitelist_path("/status")
171
+ ```
172
+
173
+ ### 获取用户信息
174
+
175
+ 在受保护的路由中获取用户信息:
176
+
177
+ ```python
178
+ @app.get("/user-profile")
179
+ async def get_user_profile(request):
180
+ if hasattr(request.state, 'user'):
181
+ user = request.state.user
182
+ return {
183
+ "user_id": user["user_id"],
184
+ "username": user["username"],
185
+ "session_id": user["session_id"]
186
+ }
187
+ else:
188
+ raise HTTPException(status_code=401, detail="未认证")
189
+ ```
190
+
191
+ ## 异常处理
192
+
193
+ SDK提供完整的异常处理:
194
+
195
+ ```python
196
+ from skyplatform_iam.exceptions import (
197
+ AuthenticationError, # 认证失败
198
+ AuthorizationError, # 权限不足
199
+ TokenExpiredError, # Token过期
200
+ TokenInvalidError, # Token无效
201
+ ConfigurationError, # 配置错误
202
+ IAMServiceError, # IAM服务错误
203
+ NetworkError # 网络错误
204
+ )
205
+ ```
206
+
207
+ ## 配置选项
208
+
209
+ ### AuthConfig参数
210
+
211
+ | 参数 | 类型 | 必填 | 说明 |
212
+ |------|------|------|------|
213
+ | `agenterra_iam_host` | str | ✓ | IAM服务地址 |
214
+ | `server_name` | str | ✓ | 服务名称 |
215
+ | `access_key` | str | ✓ | 访问密钥 |
216
+ | `whitelist_paths` | List[str] | ✗ | 白名单路径 |
217
+ | `token_header` | str | ✗ | Token请求头名称(默认:Authorization) |
218
+ | `token_prefix` | str | ✗ | Token前缀(默认:Bearer ) |
219
+ | `enable_debug` | bool | ✗ | 启用调试模式 |
220
+
221
+ ## 开发和测试
222
+
223
+ ### 运行测试
224
+
225
+ ```bash
226
+ # 安装开发依赖
227
+ pip install -e ".[dev]"
228
+
229
+ # 运行测试
230
+ python examples/test_sdk.py
231
+ ```
232
+
233
+ ### 运行示例
234
+
235
+ ```bash
236
+ # 启动示例应用
237
+ python examples/basic_usage.py
238
+
239
+ # 访问 http://localhost:8000/docs 查看API文档
240
+ ```
241
+
242
+ ## 兼容性
243
+
244
+ - Python 3.8+
245
+ - FastAPI 0.68.0+
246
+ - 完全兼容现有的 `ConnectAgenterraIam` 类
247
+
248
+ ## 许可证
249
+
250
+ MIT License
251
+
252
+ ## 贡献
253
+
254
+ 欢迎提交Issue和Pull Request!
255
+
256
+ ## 更新日志
257
+
258
+ ### v1.0.0
259
+ - 初始版本发布
260
+ - 提供FastAPI中间件和认证路由
261
+ - 支持完整的认证功能
262
+ - 兼容现有ConnectAgenterraIam类
@@ -0,0 +1,220 @@
1
+ # SkyPlatform IAM SDK
2
+
3
+ SkyPlatform IAM认证SDK,提供FastAPI中间件和认证路由,简化第三方服务的认证集成。
4
+
5
+ ## 功能特性
6
+
7
+ - 🔐 **FastAPI中间件**: 自动拦截请求进行Token验证和权限检查
8
+ - 🚀 **认证路由**: 封装用户注册、登录、登出等认证接口
9
+ - ⚙️ **灵活配置**: 支持环境变量和代码配置
10
+ - 🛡️ **白名单机制**: 支持配置无需认证的路径
11
+ - 🔧 **完整兼容**: 基于现有ConnectAgenterraIam类,保持完全兼容
12
+ - 📝 **类型提示**: 完整的TypeScript风格类型提示
13
+ - 🚨 **异常处理**: 完善的错误处理和自定义异常
14
+
15
+ ## 快速开始
16
+
17
+ ### 安装
18
+
19
+ ```bash
20
+ pip install skyplatform-iam
21
+ ```
22
+
23
+ ### 环境变量配置
24
+
25
+ 创建 `.env` 文件或设置环境变量:
26
+
27
+ ```bash
28
+ AGENTERRA_IAM_HOST=https://your-iam-host.com
29
+ AGENTERRA_SERVER_NAME=your-server-name
30
+ AGENTERRA_ACCESS_KEY=your-access-key
31
+ ```
32
+
33
+ ### 基本使用
34
+
35
+ #### 方式1:一键设置(推荐)
36
+
37
+ ```python
38
+ from fastapi import FastAPI
39
+ from skyplatform_iam import setup_auth
40
+
41
+ app = FastAPI()
42
+
43
+ # 一键设置认证中间件和路由
44
+ setup_auth(app)
45
+
46
+ @app.get("/protected")
47
+ async def protected_endpoint(request):
48
+ # 获取用户信息(由中间件自动设置)
49
+ user = request.state.user
50
+ return {"message": "访问成功", "user": user}
51
+ ```
52
+
53
+ #### 方式2:手动设置
54
+
55
+ ```python
56
+ from fastapi import FastAPI
57
+ from skyplatform_iam import AuthConfig, AuthMiddleware, create_auth_router
58
+
59
+ app = FastAPI()
60
+
61
+ # 创建配置
62
+ config = AuthConfig.from_env()
63
+
64
+ # 添加认证中间件
65
+ app.add_middleware(AuthMiddleware, config=config)
66
+
67
+ # 添加认证路由
68
+ auth_router = create_auth_router(config=config, prefix="/auth")
69
+ app.include_router(auth_router)
70
+ ```
71
+
72
+ #### 方式3:自定义配置
73
+
74
+ ```python
75
+ from skyplatform_iam import AuthConfig, setup_auth
76
+
77
+ # 自定义配置
78
+ config = AuthConfig(
79
+ agenterra_iam_host="https://your-iam-host.com",
80
+ server_name="your-server-name",
81
+ access_key="your-access-key",
82
+ whitelist_paths=[
83
+ "/docs", "/redoc", "/openapi.json",
84
+ "/health", "/public",
85
+ "/auth/register", "/auth/login"
86
+ ],
87
+ enable_debug=True
88
+ )
89
+
90
+ setup_auth(app, config=config)
91
+ ```
92
+
93
+ ## API接口
94
+
95
+ SDK自动提供以下认证接口:
96
+
97
+ - `POST /auth/register` - 用户注册
98
+ - `POST /auth/login` - 用户登录
99
+ - `POST /auth/login_without_password` - 免密登录
100
+ - `POST /auth/logout` - 用户登出
101
+ - `POST /auth/reset_password` - 重置密码
102
+ - `POST /auth/refresh_token` - 刷新Token
103
+ - `POST /auth/assign_role` - 分配角色
104
+ - `POST /auth/user_info` - 获取用户信息
105
+
106
+ ## 中间件功能
107
+
108
+ ### 自动Token验证
109
+
110
+ 中间件会自动:
111
+ 1. 检查请求路径是否在白名单中
112
+ 2. 从请求头提取Authorization Token
113
+ 3. 调用IAM服务验证Token和权限
114
+ 4. 将用户信息设置到 `request.state.user`
115
+
116
+ ### 白名单配置
117
+
118
+ 默认白名单路径:
119
+ - `/docs`, `/redoc`, `/openapi.json` - API文档
120
+ - `/health` - 健康检查
121
+ - `/auth/*` - 认证相关接口
122
+
123
+ 添加自定义白名单:
124
+
125
+ ```python
126
+ config = AuthConfig.from_env()
127
+ config.add_whitelist_path("/public")
128
+ config.add_whitelist_path("/status")
129
+ ```
130
+
131
+ ### 获取用户信息
132
+
133
+ 在受保护的路由中获取用户信息:
134
+
135
+ ```python
136
+ @app.get("/user-profile")
137
+ async def get_user_profile(request):
138
+ if hasattr(request.state, 'user'):
139
+ user = request.state.user
140
+ return {
141
+ "user_id": user["user_id"],
142
+ "username": user["username"],
143
+ "session_id": user["session_id"]
144
+ }
145
+ else:
146
+ raise HTTPException(status_code=401, detail="未认证")
147
+ ```
148
+
149
+ ## 异常处理
150
+
151
+ SDK提供完整的异常处理:
152
+
153
+ ```python
154
+ from skyplatform_iam.exceptions import (
155
+ AuthenticationError, # 认证失败
156
+ AuthorizationError, # 权限不足
157
+ TokenExpiredError, # Token过期
158
+ TokenInvalidError, # Token无效
159
+ ConfigurationError, # 配置错误
160
+ IAMServiceError, # IAM服务错误
161
+ NetworkError # 网络错误
162
+ )
163
+ ```
164
+
165
+ ## 配置选项
166
+
167
+ ### AuthConfig参数
168
+
169
+ | 参数 | 类型 | 必填 | 说明 |
170
+ |------|------|------|------|
171
+ | `agenterra_iam_host` | str | ✓ | IAM服务地址 |
172
+ | `server_name` | str | ✓ | 服务名称 |
173
+ | `access_key` | str | ✓ | 访问密钥 |
174
+ | `whitelist_paths` | List[str] | ✗ | 白名单路径 |
175
+ | `token_header` | str | ✗ | Token请求头名称(默认:Authorization) |
176
+ | `token_prefix` | str | ✗ | Token前缀(默认:Bearer ) |
177
+ | `enable_debug` | bool | ✗ | 启用调试模式 |
178
+
179
+ ## 开发和测试
180
+
181
+ ### 运行测试
182
+
183
+ ```bash
184
+ # 安装开发依赖
185
+ pip install -e ".[dev]"
186
+
187
+ # 运行测试
188
+ python examples/test_sdk.py
189
+ ```
190
+
191
+ ### 运行示例
192
+
193
+ ```bash
194
+ # 启动示例应用
195
+ python examples/basic_usage.py
196
+
197
+ # 访问 http://localhost:8000/docs 查看API文档
198
+ ```
199
+
200
+ ## 兼容性
201
+
202
+ - Python 3.8+
203
+ - FastAPI 0.68.0+
204
+ - 完全兼容现有的 `ConnectAgenterraIam` 类
205
+
206
+ ## 许可证
207
+
208
+ MIT License
209
+
210
+ ## 贡献
211
+
212
+ 欢迎提交Issue和Pull Request!
213
+
214
+ ## 更新日志
215
+
216
+ ### v1.0.0
217
+ - 初始版本发布
218
+ - 提供FastAPI中间件和认证路由
219
+ - 支持完整的认证功能
220
+ - 兼容现有ConnectAgenterraIam类
@@ -0,0 +1,118 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "skyplatform-iam"
7
+ version = "1.0.0"
8
+ authors = [
9
+ { name="x9", email="xuanxienanxunmobao@gmail.com" },
10
+ ]
11
+ description = "SkyPlatform IAM认证SDK,提供FastAPI中间件和认证路由"
12
+ readme = "README.md"
13
+ requires-python = ">=3.8"
14
+ license = { text = "MIT" }
15
+ keywords = ["fastapi", "authentication", "middleware", "iam", "skyplatform"]
16
+ classifiers = [
17
+ "Development Status :: 5 - Production/Stable",
18
+ "Intended Audience :: Developers",
19
+ "License :: OSI Approved :: MIT License",
20
+ "Operating System :: OS Independent",
21
+ "Programming Language :: Python :: 3",
22
+ "Programming Language :: Python :: 3.8",
23
+ "Programming Language :: Python :: 3.9",
24
+ "Programming Language :: Python :: 3.10",
25
+ "Programming Language :: Python :: 3.11",
26
+ "Programming Language :: Python :: 3.12",
27
+ "Framework :: FastAPI",
28
+ "Topic :: Internet :: WWW/HTTP :: HTTP Servers",
29
+ "Topic :: Security",
30
+ "Topic :: Software Development :: Libraries :: Python Modules",
31
+ ]
32
+
33
+ dependencies = [
34
+ "fastapi>=0.68.0",
35
+ "pydantic>=1.8.0",
36
+ "requests>=2.25.0",
37
+ "python-dotenv>=0.19.0",
38
+ "starlette>=0.14.0",
39
+ ]
40
+
41
+ [project.optional-dependencies]
42
+ dev = [
43
+ "pytest>=6.0.0",
44
+ "pytest-asyncio>=0.18.0",
45
+ "httpx>=0.23.0",
46
+ "black>=22.0.0",
47
+ "isort>=5.10.0",
48
+ "flake8>=4.0.0",
49
+ "mypy>=0.950",
50
+ ]
51
+
52
+ [project.urls]
53
+ Homepage = "https://github.com/xinmayoujiang12621/agenterra_iam"
54
+ Documentation = "https://skyplatform-iam.readthedocs.io/"
55
+ Repository = "https://github.com/xinmayoujiang12621/agenterra_iam.git"
56
+ Issues = "https://github.com/xinmayoujiang12621/agenterra_iam/issues"
57
+ Changelog = "https://github.com/xinmayoujiang12621/agenterra_iam/blob/main/CHANGELOG.md"
58
+
59
+ [tool.hatch.build.targets.wheel]
60
+ packages = ["skyplatform_iam"]
61
+
62
+ [tool.hatch.build.targets.sdist]
63
+ include = [
64
+ "/skyplatform_iam",
65
+ "/README.md",
66
+ "/LICENSE",
67
+ ]
68
+
69
+ [tool.black]
70
+ line-length = 88
71
+ target-version = ['py38']
72
+ include = '\.pyi?$'
73
+ extend-exclude = '''
74
+ /(
75
+ # directories
76
+ \.eggs
77
+ | \.git
78
+ | \.hg
79
+ | \.mypy_cache
80
+ | \.tox
81
+ | \.venv
82
+ | build
83
+ | dist
84
+ )/
85
+ '''
86
+
87
+ [tool.isort]
88
+ profile = "black"
89
+ multi_line_output = 3
90
+ line_length = 88
91
+ known_first_party = ["skyplatform_iam"]
92
+
93
+ [tool.mypy]
94
+ python_version = "3.8"
95
+ warn_return_any = true
96
+ warn_unused_configs = true
97
+ disallow_untyped_defs = true
98
+ disallow_incomplete_defs = true
99
+ check_untyped_defs = true
100
+ disallow_untyped_decorators = true
101
+ no_implicit_optional = true
102
+ warn_redundant_casts = true
103
+ warn_unused_ignores = true
104
+ warn_no_return = true
105
+ warn_unreachable = true
106
+ strict_equality = true
107
+
108
+ [[tool.mypy.overrides]]
109
+ module = "tests.*"
110
+ disallow_untyped_defs = false
111
+
112
+ [tool.pytest.ini_options]
113
+ testpaths = ["tests"]
114
+ python_files = ["test_*.py"]
115
+ python_classes = ["Test*"]
116
+ python_functions = ["test_*"]
117
+ addopts = "-v --tb=short"
118
+ asyncio_mode = "auto"
@@ -0,0 +1,95 @@
1
+ """
2
+ SkyPlatform IAM SDK
3
+ 提供FastAPI认证中间件和IAM服务连接功能
4
+ """
5
+
6
+ from .config import AuthConfig
7
+ from .middleware import AuthMiddleware
8
+ from .connect_agenterra_iam import ConnectAgenterraIam
9
+ from .exceptions import (
10
+ SkyPlatformAuthException,
11
+ AuthenticationError,
12
+ AuthorizationError,
13
+ TokenExpiredError,
14
+ TokenInvalidError,
15
+ ConfigurationError,
16
+ IAMServiceError,
17
+ NetworkError
18
+ )
19
+
20
+ __version__ = "1.0.0"
21
+ __author__ = "x9"
22
+ __description__ = "SkyPlatform IAM认证SDK,提供FastAPI中间件和IAM服务连接功能"
23
+
24
+ # 导出主要类和函数
25
+ __all__ = [
26
+ # 配置
27
+ "AuthConfig",
28
+
29
+ # 中间件
30
+ "AuthMiddleware",
31
+
32
+ # 客户端
33
+ "ConnectAgenterraIam",
34
+
35
+ # 异常
36
+ "SkyPlatformAuthException",
37
+ "AuthenticationError",
38
+ "AuthorizationError",
39
+ "TokenExpiredError",
40
+ "TokenInvalidError",
41
+ "ConfigurationError",
42
+ "IAMServiceError",
43
+ "NetworkError",
44
+
45
+ # 版本信息
46
+ "__version__",
47
+ "__author__",
48
+ "__description__"
49
+ ]
50
+
51
+
52
+ def create_auth_middleware(config: AuthConfig = None, **kwargs) -> AuthMiddleware:
53
+ """
54
+ 创建认证中间件的便捷函数
55
+
56
+ Args:
57
+ config: 认证配置,如果为None则从环境变量创建
58
+ **kwargs: 其他中间件参数
59
+
60
+ Returns:
61
+ AuthMiddleware: 认证中间件实例
62
+
63
+ Note:
64
+ 此函数用于创建中间件实例,用于请求拦截和鉴权。
65
+ 客户端应用需要自己实现具体的业务接口。
66
+ """
67
+ if config is None:
68
+ config = AuthConfig.from_env()
69
+
70
+ return AuthMiddleware(config=config, **kwargs)
71
+
72
+
73
+ def setup_auth(app, config: AuthConfig = None):
74
+ """
75
+ 一键设置认证中间件的便捷函数
76
+
77
+ Args:
78
+ app: FastAPI应用实例
79
+ config: 认证配置,如果为None则从环境变量创建
80
+
81
+ Returns:
82
+ AuthMiddleware: 认证中间件实例
83
+
84
+ Note:
85
+ 此函数只设置认证中间件,不包含预制路由。
86
+ 客户端应用需要根据业务需求自己实现认证相关的API接口。
87
+ """
88
+ if config is None:
89
+ config = AuthConfig.from_env()
90
+
91
+ # 添加中间件
92
+ middleware = AuthMiddleware(app=app, config=config)
93
+ app.add_middleware(AuthMiddleware, config=config)
94
+
95
+ return middleware