fastapi-authly 0.1.1__tar.gz → 0.1.2__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.
- fastapi_authly-0.1.2/CHANGELOG_SCALAR.md +84 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/PKG-INFO +29 -22
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/README.md +28 -21
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/README.zh.md +28 -0
- fastapi_authly-0.1.2/docs/SCALAR_DOCS_USAGE.md +121 -0
- fastapi_authly-0.1.2/docs//345/207/275/346/225/260/345/274/217/347/274/226/347/250/213/344/270/203/346/255/246/345/231/250.md +11 -0
- fastapi_authly-0.1.2/examples/complete_example.py +63 -0
- fastapi_authly-0.1.2/examples/use_scalar_docs.py +53 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/pyproject.toml +2 -1
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/src/fastapi_authly/__about__.py +1 -1
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/src/fastapi_authly/__init__.py +2 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/src/fastapi_authly/auth.py +12 -10
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/src/fastapi_authly/core/config.py +27 -8
- fastapi_authly-0.1.2/src/fastapi_authly/deps.py +121 -0
- fastapi_authly-0.1.2/src/fastapi_authly/docs.py +70 -0
- fastapi_authly-0.1.2/src/fastapi_authly/static/scalar/standalone.js +34213 -0
- fastapi_authly-0.1.2/src/fastapi_authly/static/scalar/style.css +1 -0
- fastapi_authly-0.1.2/test.py +177 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/.gitignore +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/LICENSE +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/coverage.xml +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/htmlcov/.gitignore +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/htmlcov/class_index.html +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/htmlcov/coverage_html_cb_dd2e7eb5.js +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/htmlcov/favicon_32_cb_c827f16f.png +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/htmlcov/function_index.html +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/htmlcov/index.html +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/htmlcov/keybd_closed_cb_900cfef5.png +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/htmlcov/status.json +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/htmlcov/style_cb_9ff733b0.css +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/htmlcov/z_af1bec017750c6fc___init___py.html +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/htmlcov/z_af1bec017750c6fc_user_py.html +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/htmlcov/z_b9d93864b1b0ad6e___about___py.html +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/htmlcov/z_b9d93864b1b0ad6e___init___py.html +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/htmlcov/z_b9d93864b1b0ad6e_auth_py.html +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/htmlcov/z_b9d93864b1b0ad6e_interfaces_py.html +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/htmlcov/z_d015ea9b27b0258e___init___py.html +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/htmlcov/z_d015ea9b27b0258e_config_py.html +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/htmlcov/z_d015ea9b27b0258e_security_py.html +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/htmlcov/z_ddd01122054512b0___init___py.html +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/htmlcov/z_ddd01122054512b0_tortoise_pg_py.html +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/htmlcov/z_feee2d9ae7f7fd96___init___py.html +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/htmlcov/z_feee2d9ae7f7fd96_user_py.html +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/src/fastapi_authly/contrib/__init__.py +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/src/fastapi_authly/contrib/tortoise_pg.py +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/src/fastapi_authly/core/__init__.py +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/src/fastapi_authly/core/security.py +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/src/fastapi_authly/interfaces.py +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/src/fastapi_authly/models/__init__.py +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/src/fastapi_authly/models/user.py +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/src/fastapi_authly/schemas/__init__.py +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/src/fastapi_authly/schemas/user.py +0 -0
- {fastapi_authly-0.1.1 → fastapi_authly-0.1.2}/uv.lock +0 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# Scalar 文档功能集成说明
|
|
2
|
+
|
|
3
|
+
## 改动概述
|
|
4
|
+
|
|
5
|
+
已将 Scalar API 文档功能集成到 `fastapi-authly` 包中,包含所有必要的静态资源文件。用户只需导入并使用 `setup_scalar_docs` 函数即可启用文档功能,无需手动管理静态文件。
|
|
6
|
+
|
|
7
|
+
## 新增文件
|
|
8
|
+
|
|
9
|
+
1. **`src/fastapi_authly/docs.py`**: 文档功能模块
|
|
10
|
+
- `setup_scalar_docs()`: 主要函数,用于设置 Scalar 文档
|
|
11
|
+
- `get_static_dir()`: 获取包内静态文件目录路径
|
|
12
|
+
|
|
13
|
+
2. **`src/fastapi_authly/static/scalar/`**: 静态资源目录
|
|
14
|
+
- `standalone.js`: Scalar JavaScript 文件
|
|
15
|
+
- `style.css`: Scalar CSS 样式文件
|
|
16
|
+
|
|
17
|
+
3. **示例文件**:
|
|
18
|
+
- `examples/use_scalar_docs.py`: 基本使用示例
|
|
19
|
+
- `examples/complete_example.py`: 完整使用示例
|
|
20
|
+
- `docs/SCALAR_DOCS_USAGE.md`: 详细使用文档
|
|
21
|
+
|
|
22
|
+
## 修改文件
|
|
23
|
+
|
|
24
|
+
1. **`src/fastapi_authly/__init__.py`**:
|
|
25
|
+
- 添加 `setup_scalar_docs` 的导入和导出
|
|
26
|
+
|
|
27
|
+
2. **`README.md` 和 `README.zh.md`**:
|
|
28
|
+
- 添加 Scalar 文档功能的使用说明
|
|
29
|
+
|
|
30
|
+
## 使用方法
|
|
31
|
+
|
|
32
|
+
### 基本使用
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
from fastapi import FastAPI
|
|
36
|
+
from fastapi_authly import setup_scalar_docs
|
|
37
|
+
|
|
38
|
+
app = FastAPI(title="My API")
|
|
39
|
+
setup_scalar_docs(app)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### 自定义配置
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
setup_scalar_docs(
|
|
46
|
+
app,
|
|
47
|
+
docs_url="/api-docs",
|
|
48
|
+
static_url="/assets",
|
|
49
|
+
title="Custom API Docs",
|
|
50
|
+
openapi_url="/openapi.json"
|
|
51
|
+
)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## 优势
|
|
55
|
+
|
|
56
|
+
1. **零配置**: 无需手动复制静态文件
|
|
57
|
+
2. **可移植**: 安装包后即可使用
|
|
58
|
+
3. **简单**: 一行代码启用
|
|
59
|
+
4. **灵活**: 支持自定义 URL 和配置
|
|
60
|
+
|
|
61
|
+
## 构建和发布
|
|
62
|
+
|
|
63
|
+
静态文件会自动包含在构建的包中,因为它们在 `src/fastapi_authly/static/` 目录下,属于包的一部分。
|
|
64
|
+
|
|
65
|
+
构建和发布流程不变:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
uv build
|
|
69
|
+
uv publish --token your-token
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## 测试
|
|
73
|
+
|
|
74
|
+
安装包后,可以这样测试:
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
from fastapi import FastAPI
|
|
78
|
+
from fastapi_authly import setup_scalar_docs
|
|
79
|
+
|
|
80
|
+
app = FastAPI(title="Test API")
|
|
81
|
+
setup_scalar_docs(app)
|
|
82
|
+
|
|
83
|
+
# 运行应用后,访问 http://localhost:8000/docs 即可看到文档
|
|
84
|
+
```
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: fastapi-authly
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.2
|
|
4
4
|
Summary: A modular authentication system for FastAPI with OAuth2, JWT, and password recovery
|
|
5
5
|
Project-URL: Homepage, https://github.com/yourusername/fastapi-auth-module
|
|
6
6
|
Project-URL: Documentation, https://yourusername.github.io/fastapi-auth-module/
|
|
@@ -109,6 +109,10 @@ deps = AuthDependencyConfig(user_repository=TortoiseUserRepository())
|
|
|
109
109
|
|
|
110
110
|
auth_router = create_auth_router(config=config, dependencies=deps)
|
|
111
111
|
app.include_router(auth_router)
|
|
112
|
+
|
|
113
|
+
# Optional: Setup Scalar API documentation (static resources included, no manual setup needed)
|
|
114
|
+
from fastapi_authly import setup_scalar_docs
|
|
115
|
+
setup_scalar_docs(app, docs_url="/docs", static_url="/static")
|
|
112
116
|
```
|
|
113
117
|
|
|
114
118
|
### Advanced Usage (custom implementations)
|
|
@@ -135,6 +139,30 @@ deps = AuthDependencyConfig(
|
|
|
135
139
|
auth_router = create_auth_router(config=config, dependencies=deps)
|
|
136
140
|
```
|
|
137
141
|
|
|
142
|
+
## 📚 API Documentation
|
|
143
|
+
|
|
144
|
+
`fastapi-authly` includes built-in Scalar API documentation support with all necessary static resources:
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
from fastapi import FastAPI
|
|
148
|
+
from fastapi_authly import setup_scalar_docs
|
|
149
|
+
|
|
150
|
+
app = FastAPI(title="My API")
|
|
151
|
+
|
|
152
|
+
# One line to enable Scalar documentation
|
|
153
|
+
# Automatically mounts static files to /static and creates docs page at /docs
|
|
154
|
+
setup_scalar_docs(app)
|
|
155
|
+
|
|
156
|
+
# Custom configuration
|
|
157
|
+
setup_scalar_docs(
|
|
158
|
+
app,
|
|
159
|
+
docs_url="/api-docs", # Custom docs URL
|
|
160
|
+
static_url="/assets", # Custom static files prefix
|
|
161
|
+
title="Custom API Docs", # Custom title
|
|
162
|
+
openapi_url="/openapi.json" # Custom OpenAPI schema URL
|
|
163
|
+
)
|
|
164
|
+
```
|
|
165
|
+
|
|
138
166
|
## 📋 API Endpoints
|
|
139
167
|
|
|
140
168
|
### Authentication
|
|
@@ -216,26 +244,6 @@ deps = AuthDependencyConfig(user_repository=TortoiseUserRepository())
|
|
|
216
244
|
app.include_router(create_auth_router(config=config, dependencies=deps))
|
|
217
245
|
```
|
|
218
246
|
|
|
219
|
-
## 🧪 Testing
|
|
220
|
-
|
|
221
|
-
```bash
|
|
222
|
-
# Install test dependencies
|
|
223
|
-
uv pip install -e ".[test]"
|
|
224
|
-
|
|
225
|
-
# Run tests
|
|
226
|
-
uv run pytest
|
|
227
|
-
```
|
|
228
|
-
|
|
229
|
-
## 📦 Build & Publish
|
|
230
|
-
|
|
231
|
-
```bash
|
|
232
|
-
# Build
|
|
233
|
-
uv build
|
|
234
|
-
|
|
235
|
-
# Publish to PyPI (set UV_PUBLISH_TOKEN or pass --token)
|
|
236
|
-
uv publish --token pypi-xxxxx
|
|
237
|
-
```
|
|
238
|
-
|
|
239
247
|
## 🤝 Contributing
|
|
240
248
|
|
|
241
249
|
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
@@ -261,6 +269,5 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
|
|
|
261
269
|
|
|
262
270
|
If you have any questions or need help:
|
|
263
271
|
|
|
264
|
-
- 📧 Email: your.email@example.com
|
|
265
272
|
- 💬 GitHub Issues: [Create an issue](https://github.com/yourusername/fastapi-authly/issues)
|
|
266
273
|
- 📖 Documentation: [Read the docs](https://yourusername.github.io/fastapi-authly/)
|
|
@@ -57,6 +57,10 @@ deps = AuthDependencyConfig(user_repository=TortoiseUserRepository())
|
|
|
57
57
|
|
|
58
58
|
auth_router = create_auth_router(config=config, dependencies=deps)
|
|
59
59
|
app.include_router(auth_router)
|
|
60
|
+
|
|
61
|
+
# Optional: Setup Scalar API documentation (static resources included, no manual setup needed)
|
|
62
|
+
from fastapi_authly import setup_scalar_docs
|
|
63
|
+
setup_scalar_docs(app, docs_url="/docs", static_url="/static")
|
|
60
64
|
```
|
|
61
65
|
|
|
62
66
|
### Advanced Usage (custom implementations)
|
|
@@ -83,6 +87,30 @@ deps = AuthDependencyConfig(
|
|
|
83
87
|
auth_router = create_auth_router(config=config, dependencies=deps)
|
|
84
88
|
```
|
|
85
89
|
|
|
90
|
+
## 📚 API Documentation
|
|
91
|
+
|
|
92
|
+
`fastapi-authly` includes built-in Scalar API documentation support with all necessary static resources:
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
from fastapi import FastAPI
|
|
96
|
+
from fastapi_authly import setup_scalar_docs
|
|
97
|
+
|
|
98
|
+
app = FastAPI(title="My API")
|
|
99
|
+
|
|
100
|
+
# One line to enable Scalar documentation
|
|
101
|
+
# Automatically mounts static files to /static and creates docs page at /docs
|
|
102
|
+
setup_scalar_docs(app)
|
|
103
|
+
|
|
104
|
+
# Custom configuration
|
|
105
|
+
setup_scalar_docs(
|
|
106
|
+
app,
|
|
107
|
+
docs_url="/api-docs", # Custom docs URL
|
|
108
|
+
static_url="/assets", # Custom static files prefix
|
|
109
|
+
title="Custom API Docs", # Custom title
|
|
110
|
+
openapi_url="/openapi.json" # Custom OpenAPI schema URL
|
|
111
|
+
)
|
|
112
|
+
```
|
|
113
|
+
|
|
86
114
|
## 📋 API Endpoints
|
|
87
115
|
|
|
88
116
|
### Authentication
|
|
@@ -164,26 +192,6 @@ deps = AuthDependencyConfig(user_repository=TortoiseUserRepository())
|
|
|
164
192
|
app.include_router(create_auth_router(config=config, dependencies=deps))
|
|
165
193
|
```
|
|
166
194
|
|
|
167
|
-
## 🧪 Testing
|
|
168
|
-
|
|
169
|
-
```bash
|
|
170
|
-
# Install test dependencies
|
|
171
|
-
uv pip install -e ".[test]"
|
|
172
|
-
|
|
173
|
-
# Run tests
|
|
174
|
-
uv run pytest
|
|
175
|
-
```
|
|
176
|
-
|
|
177
|
-
## 📦 Build & Publish
|
|
178
|
-
|
|
179
|
-
```bash
|
|
180
|
-
# Build
|
|
181
|
-
uv build
|
|
182
|
-
|
|
183
|
-
# Publish to PyPI (set UV_PUBLISH_TOKEN or pass --token)
|
|
184
|
-
uv publish --token pypi-xxxxx
|
|
185
|
-
```
|
|
186
|
-
|
|
187
195
|
## 🤝 Contributing
|
|
188
196
|
|
|
189
197
|
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
@@ -209,6 +217,5 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
|
|
|
209
217
|
|
|
210
218
|
If you have any questions or need help:
|
|
211
219
|
|
|
212
|
-
- 📧 Email: your.email@example.com
|
|
213
220
|
- 💬 GitHub Issues: [Create an issue](https://github.com/yourusername/fastapi-authly/issues)
|
|
214
221
|
- 📖 Documentation: [Read the docs](https://yourusername.github.io/fastapi-authly/)
|
|
@@ -41,6 +41,10 @@ register_tortoise(
|
|
|
41
41
|
config = AuthConfig(token_url="login")
|
|
42
42
|
deps = AuthDependencyConfig(user_repository=TortoiseUserRepository())
|
|
43
43
|
app.include_router(create_auth_router(config=config, dependencies=deps))
|
|
44
|
+
|
|
45
|
+
# 可选:设置 Scalar API 文档(内置静态资源,无需手动配置)
|
|
46
|
+
from fastapi_authly import setup_scalar_docs
|
|
47
|
+
setup_scalar_docs(app, docs_url="/docs", static_url="/static")
|
|
44
48
|
```
|
|
45
49
|
|
|
46
50
|
## 🔌 自定义实现示例
|
|
@@ -65,6 +69,30 @@ deps = AuthDependencyConfig(user_repository=MyRepo(), mailer=MyMailer())
|
|
|
65
69
|
router = create_auth_router(config=config, dependencies=deps)
|
|
66
70
|
```
|
|
67
71
|
|
|
72
|
+
## 📚 API 文档功能
|
|
73
|
+
|
|
74
|
+
`fastapi-authly` 内置了 Scalar API 文档支持,包含所有必要的静态资源,无需手动配置:
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
from fastapi import FastAPI
|
|
78
|
+
from fastapi_authly import setup_scalar_docs
|
|
79
|
+
|
|
80
|
+
app = FastAPI(title="My API")
|
|
81
|
+
|
|
82
|
+
# 一行代码启用 Scalar 文档
|
|
83
|
+
# 自动挂载静态文件到 /static,创建文档页面到 /docs
|
|
84
|
+
setup_scalar_docs(app)
|
|
85
|
+
|
|
86
|
+
# 自定义配置
|
|
87
|
+
setup_scalar_docs(
|
|
88
|
+
app,
|
|
89
|
+
docs_url="/api-docs", # 自定义文档 URL
|
|
90
|
+
static_url="/assets", # 自定义静态文件前缀
|
|
91
|
+
title="Custom API Docs", # 自定义标题
|
|
92
|
+
openapi_url="/openapi.json" # 自定义 OpenAPI schema URL
|
|
93
|
+
)
|
|
94
|
+
```
|
|
95
|
+
|
|
68
96
|
## 📋 主要接口
|
|
69
97
|
|
|
70
98
|
- `POST /auth/login`:登录,返回 access_token(可选 refresh_token)
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# Scalar API 文档功能使用指南
|
|
2
|
+
|
|
3
|
+
## 概述
|
|
4
|
+
|
|
5
|
+
`fastapi-authly` 现在内置了 Scalar API 文档支持,包含所有必要的静态资源文件。你无需手动复制静态文件,只需一行代码即可启用美观的 API 文档。
|
|
6
|
+
|
|
7
|
+
## 快速开始
|
|
8
|
+
|
|
9
|
+
### 基本使用
|
|
10
|
+
|
|
11
|
+
```python
|
|
12
|
+
from fastapi import FastAPI
|
|
13
|
+
from fastapi_authly import setup_scalar_docs
|
|
14
|
+
|
|
15
|
+
app = FastAPI(title="My API")
|
|
16
|
+
|
|
17
|
+
# 一行代码启用 Scalar 文档
|
|
18
|
+
setup_scalar_docs(app)
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
这将会:
|
|
22
|
+
- 自动挂载静态文件到 `/static`
|
|
23
|
+
- 创建文档页面到 `/docs`
|
|
24
|
+
- 使用应用的 `title` 和 `openapi_url`
|
|
25
|
+
|
|
26
|
+
### 完整示例
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
from fastapi import FastAPI
|
|
30
|
+
from fastapi_authly import setup_scalar_docs, create_auth_router, AuthConfig, AuthDependencyConfig
|
|
31
|
+
from fastapi_authly.contrib.tortoise_pg import TortoiseUserRepository
|
|
32
|
+
from tortoise.contrib.fastapi import register_tortoise
|
|
33
|
+
|
|
34
|
+
app = FastAPI(
|
|
35
|
+
title="Complaint API",
|
|
36
|
+
description="Complaint Data Streaming API",
|
|
37
|
+
version="2.0.1",
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
# 初始化数据库
|
|
41
|
+
register_tortoise(
|
|
42
|
+
app,
|
|
43
|
+
db_url="postgres://user:password@localhost:5432/mydb",
|
|
44
|
+
modules={"models": ["fastapi_authly.models.user"]},
|
|
45
|
+
generate_schemas=True,
|
|
46
|
+
add_exception_handlers=True,
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
# 配置认证路由
|
|
50
|
+
config = AuthConfig()
|
|
51
|
+
deps = AuthDependencyConfig(user_repository=TortoiseUserRepository())
|
|
52
|
+
auth_router = create_auth_router(config=config, dependencies=deps)
|
|
53
|
+
app.include_router(auth_router)
|
|
54
|
+
|
|
55
|
+
# 设置 Scalar 文档
|
|
56
|
+
setup_scalar_docs(app, docs_url="/docs", static_url="/static")
|
|
57
|
+
|
|
58
|
+
if __name__ == "__main__":
|
|
59
|
+
import uvicorn
|
|
60
|
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## 自定义配置
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
setup_scalar_docs(
|
|
67
|
+
app,
|
|
68
|
+
docs_url="/api-docs", # 自定义文档 URL
|
|
69
|
+
static_url="/assets", # 自定义静态文件前缀
|
|
70
|
+
title="Custom API Docs", # 自定义标题
|
|
71
|
+
openapi_url="/openapi.json" # 自定义 OpenAPI schema URL
|
|
72
|
+
)
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## 参数说明
|
|
76
|
+
|
|
77
|
+
- `app` (必需): FastAPI 应用实例
|
|
78
|
+
- `docs_url` (可选): 文档页面 URL,默认为 `"/docs"`
|
|
79
|
+
- `static_url` (可选): 静态文件 URL 前缀,默认为 `"/static"`
|
|
80
|
+
- `title` (可选): 文档标题,默认使用 `app.title`
|
|
81
|
+
- `openapi_url` (可选): OpenAPI schema URL,默认使用 `app.openapi_url`
|
|
82
|
+
|
|
83
|
+
## 注意事项
|
|
84
|
+
|
|
85
|
+
1. **静态文件位置**: 静态文件已经打包在 `fastapi_authly` 包内,无需手动复制
|
|
86
|
+
2. **URL 冲突**: 确保 `docs_url` 和 `static_url` 不与现有路由冲突
|
|
87
|
+
3. **OpenAPI Schema**: 确保 FastAPI 应用已启用 OpenAPI schema(默认启用)
|
|
88
|
+
|
|
89
|
+
## 迁移指南
|
|
90
|
+
|
|
91
|
+
如果你之前手动配置了 Scalar 文档,可以按以下步骤迁移:
|
|
92
|
+
|
|
93
|
+
### 之前的方式
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
97
|
+
|
|
98
|
+
@app.get("/docs", include_in_schema=False)
|
|
99
|
+
async def scalar_html(_app):
|
|
100
|
+
html_content = f"""
|
|
101
|
+
<!DOCTYPE html>
|
|
102
|
+
...
|
|
103
|
+
"""
|
|
104
|
+
return HTMLResponse(content=html_content)
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### 现在的方式
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
from fastapi_authly import setup_scalar_docs
|
|
111
|
+
|
|
112
|
+
# 一行代码替代上面的所有配置
|
|
113
|
+
setup_scalar_docs(app, docs_url="/docs", static_url="/static")
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## 优势
|
|
117
|
+
|
|
118
|
+
1. **无需手动管理静态文件**: 所有静态资源已打包在包内
|
|
119
|
+
2. **简单易用**: 一行代码即可启用
|
|
120
|
+
3. **可移植**: 其他项目安装包后即可使用,无需额外配置
|
|
121
|
+
4. **可定制**: 支持自定义 URL 和标题
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"""
|
|
2
|
+
完整示例:展示如何像原项目一样使用 fastapi-authly 的文档功能
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from fastapi import FastAPI
|
|
6
|
+
from fastapi_authly import setup_scalar_docs, create_auth_router, AuthConfig, AuthDependencyConfig
|
|
7
|
+
from fastapi_authly.contrib.tortoise_pg import TortoiseUserRepository
|
|
8
|
+
from tortoise.contrib.fastapi import register_tortoise, tortoise_exception_handlers
|
|
9
|
+
from contextlib import asynccontextmanager
|
|
10
|
+
|
|
11
|
+
# 生命周期管理
|
|
12
|
+
@asynccontextmanager
|
|
13
|
+
async def lifespan(app: FastAPI):
|
|
14
|
+
# 启动时执行
|
|
15
|
+
yield
|
|
16
|
+
# 关闭时执行
|
|
17
|
+
|
|
18
|
+
# 创建 FastAPI 应用
|
|
19
|
+
app = FastAPI(
|
|
20
|
+
title="Complaint API",
|
|
21
|
+
description="Complaint Data Streaming API",
|
|
22
|
+
version="2.0.1",
|
|
23
|
+
exception_handlers=tortoise_exception_handlers(),
|
|
24
|
+
lifespan=lifespan,
|
|
25
|
+
docs_url="/old_docs", # 保留旧的 docs,但使用 Scalar
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
# 初始化数据库
|
|
29
|
+
register_tortoise(
|
|
30
|
+
app,
|
|
31
|
+
db_url="postgres://user:password@localhost:5432/mydb",
|
|
32
|
+
modules={"models": ["fastapi_authly.models.user"]},
|
|
33
|
+
generate_schemas=True,
|
|
34
|
+
add_exception_handlers=True,
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
# 配置认证路由
|
|
38
|
+
config = AuthConfig()
|
|
39
|
+
deps = AuthDependencyConfig(user_repository=TortoiseUserRepository())
|
|
40
|
+
auth_router = create_auth_router(config=config, dependencies=deps)
|
|
41
|
+
app.include_router(auth_router)
|
|
42
|
+
|
|
43
|
+
# 设置 Scalar 文档
|
|
44
|
+
# 这会自动挂载静态文件并创建文档页面
|
|
45
|
+
setup_scalar_docs(
|
|
46
|
+
app,
|
|
47
|
+
docs_url="/docs", # 文档页面 URL
|
|
48
|
+
static_url="/static", # 静态文件 URL 前缀(与原来的保持一致)
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
# 其他 API 路由
|
|
52
|
+
@app.get("/")
|
|
53
|
+
async def root():
|
|
54
|
+
return {"message": "API is running"}
|
|
55
|
+
|
|
56
|
+
if __name__ == "__main__":
|
|
57
|
+
import uvicorn
|
|
58
|
+
uvicorn.run(
|
|
59
|
+
app,
|
|
60
|
+
host="0.0.0.0",
|
|
61
|
+
port=3040,
|
|
62
|
+
loop="uvloop",
|
|
63
|
+
)
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"""
|
|
2
|
+
使用示例:如何在项目中使用 fastapi-authly 的 Scalar 文档功能
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from fastapi import FastAPI
|
|
6
|
+
from fastapi_authly import setup_scalar_docs, create_auth_router, AuthConfig, AuthDependencyConfig
|
|
7
|
+
from fastapi_authly.contrib.tortoise_pg import TortoiseUserRepository
|
|
8
|
+
from tortoise.contrib.fastapi import register_tortoise
|
|
9
|
+
|
|
10
|
+
# 创建 FastAPI 应用
|
|
11
|
+
app = FastAPI(
|
|
12
|
+
title="My API",
|
|
13
|
+
description="My API with Scalar Documentation",
|
|
14
|
+
version="1.0.0",
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
# 配置认证路由(示例)
|
|
18
|
+
config = AuthConfig()
|
|
19
|
+
deps = AuthDependencyConfig(user_repository=TortoiseUserRepository())
|
|
20
|
+
auth_router = create_auth_router(config=config, dependencies=deps)
|
|
21
|
+
app.include_router(auth_router)
|
|
22
|
+
|
|
23
|
+
# 设置 Scalar 文档
|
|
24
|
+
# 这会自动:
|
|
25
|
+
# 1. 挂载静态文件到 /static
|
|
26
|
+
# 2. 创建文档页面到 /docs
|
|
27
|
+
setup_scalar_docs(
|
|
28
|
+
app,
|
|
29
|
+
docs_url="/docs", # 文档页面 URL
|
|
30
|
+
static_url="/static", # 静态文件 URL 前缀
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
# 或者自定义配置
|
|
34
|
+
# setup_scalar_docs(
|
|
35
|
+
# app,
|
|
36
|
+
# docs_url="/api-docs", # 自定义文档 URL
|
|
37
|
+
# static_url="/assets", # 自定义静态文件前缀
|
|
38
|
+
# title="Custom API Docs", # 自定义标题
|
|
39
|
+
# openapi_url="/openapi.json", # 自定义 OpenAPI schema URL
|
|
40
|
+
# )
|
|
41
|
+
|
|
42
|
+
# 初始化数据库(示例)
|
|
43
|
+
register_tortoise(
|
|
44
|
+
app,
|
|
45
|
+
db_url="postgres://user:password@localhost:5432/mydb",
|
|
46
|
+
modules={"models": ["fastapi_authly.models.user"]},
|
|
47
|
+
generate_schemas=True,
|
|
48
|
+
add_exception_handlers=True,
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
if __name__ == "__main__":
|
|
52
|
+
import uvicorn
|
|
53
|
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "fastapi-authly"
|
|
7
|
-
version = "0.1.
|
|
7
|
+
version = "0.1.2"
|
|
8
8
|
description = "A modular authentication system for FastAPI with OAuth2, JWT, and password recovery"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = {text = "MIT"}
|
|
@@ -75,6 +75,7 @@ path = "src/fastapi_authly/__about__.py"
|
|
|
75
75
|
|
|
76
76
|
[tool.hatch.build.targets.wheel]
|
|
77
77
|
packages = ["src/fastapi_authly"]
|
|
78
|
+
# 静态文件在包目录内,会自动包含,无需额外配置
|
|
78
79
|
|
|
79
80
|
[tool.hatch.build.targets.sdist]
|
|
80
81
|
exclude = [
|
|
@@ -11,6 +11,7 @@ This package provides a complete authentication solution with:
|
|
|
11
11
|
|
|
12
12
|
from .auth import AuthModule, create_auth_router
|
|
13
13
|
from .core import AuthConfig, AuthDependencyConfig, BcryptPasswordHasher, JWTTokenService
|
|
14
|
+
from .docs import setup_scalar_docs
|
|
14
15
|
from .interfaces import Mailer, PasswordHasher, TokenService, UserRepository
|
|
15
16
|
from .schemas.user import UserBase, UserCreate, UserUpdate, UserPublic, Token, TokenData
|
|
16
17
|
|
|
@@ -24,6 +25,7 @@ __all__ = [
|
|
|
24
25
|
|
|
25
26
|
# Core functions
|
|
26
27
|
"create_auth_router",
|
|
28
|
+
"setup_scalar_docs",
|
|
27
29
|
|
|
28
30
|
# Security utilities
|
|
29
31
|
"BcryptPasswordHasher",
|
|
@@ -7,7 +7,7 @@ from fastapi.security import OAuth2PasswordBearer
|
|
|
7
7
|
from jose import JWTError
|
|
8
8
|
from pydantic import EmailStr
|
|
9
9
|
|
|
10
|
-
from .core.config import AuthConfig, AuthDependencyConfig
|
|
10
|
+
from .core.config import AuthConfig, AuthDependencyConfig, _config
|
|
11
11
|
from .core.security import BcryptPasswordHasher, JWTTokenService
|
|
12
12
|
from .interfaces import Mailer, PasswordHasher, TokenService, UserRepository
|
|
13
13
|
from .schemas.user import (
|
|
@@ -24,11 +24,9 @@ from fastapi_authly.contrib.tortoise_pg import TortoiseUserRepository
|
|
|
24
24
|
|
|
25
25
|
def _merge_config(config: Optional[AuthConfig]) -> AuthConfig:
|
|
26
26
|
"""Environment-driven config, overridden by user-supplied config when provided."""
|
|
27
|
-
env_config = AuthConfig()
|
|
28
27
|
if config is None:
|
|
29
|
-
return
|
|
30
|
-
|
|
31
|
-
return env_config.model_copy(update=config.model_dump(exclude_none=True))
|
|
28
|
+
return _config
|
|
29
|
+
return _config.model_copy(update=config.model_dump(exclude_none=True))
|
|
32
30
|
|
|
33
31
|
|
|
34
32
|
class AuthModule:
|
|
@@ -64,9 +62,13 @@ class AuthModule:
|
|
|
64
62
|
self.router = APIRouter(
|
|
65
63
|
prefix=self.config.router_prefix, tags=self.config.router_tags
|
|
66
64
|
)
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
65
|
+
# token 提取依赖:默认使用 OAuth2PasswordBearer,可通过 AuthDependencyConfig.token_dependency 覆盖
|
|
66
|
+
self.token_dependency = (
|
|
67
|
+
self.dependencies.token_dependency
|
|
68
|
+
or OAuth2PasswordBearer(
|
|
69
|
+
tokenUrl=f"{self.config.router_prefix.rstrip('/')}/{self.config.token_url}".strip(
|
|
70
|
+
"/"
|
|
71
|
+
)
|
|
70
72
|
)
|
|
71
73
|
)
|
|
72
74
|
|
|
@@ -120,7 +122,7 @@ class AuthModule:
|
|
|
120
122
|
return Token(access_token=access_token, refresh_token=refresh_token)
|
|
121
123
|
|
|
122
124
|
@self.router.post("/token/verify")
|
|
123
|
-
async def verify_token(token: str = Depends(self.
|
|
125
|
+
async def verify_token(token: str = Depends(self.token_dependency)) -> Dict[str, Any]:
|
|
124
126
|
try:
|
|
125
127
|
payload = self.token_service.decode_token(token)
|
|
126
128
|
token_data = TokenData(**payload)
|
|
@@ -142,7 +144,7 @@ class AuthModule:
|
|
|
142
144
|
return await repo.to_public(created)
|
|
143
145
|
|
|
144
146
|
@self.router.get("/me", response_model=UserPublic)
|
|
145
|
-
async def get_current_user(token: str = Depends(self.
|
|
147
|
+
async def get_current_user(token: str = Depends(self.token_dependency)) -> UserPublic:
|
|
146
148
|
repo = self._ensure_user_repo()
|
|
147
149
|
try:
|
|
148
150
|
payload = self.token_service.decode_token(token, verify_type="access")
|