fastapi-docshield 0.1.0__tar.gz → 0.2.1__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_docshield-0.1.0 → fastapi_docshield-0.2.1}/.gitignore +3 -0
- fastapi_docshield-0.2.1/PKG-INFO +322 -0
- fastapi_docshield-0.2.1/README.md +263 -0
- {fastapi_docshield-0.1.0 → fastapi_docshield-0.2.1}/fastapi_docshield/__init__.py +2 -2
- fastapi_docshield-0.2.1/fastapi_docshield/docshield.py +422 -0
- fastapi_docshield-0.2.1/fastapi_docshield/static/redoc/redoc.standalone.js +1782 -0
- fastapi_docshield-0.2.1/fastapi_docshield/static/swagger/swagger-ui-bundle.js +2 -0
- fastapi_docshield-0.2.1/fastapi_docshield/static/swagger/swagger-ui.css +3 -0
- fastapi_docshield-0.2.1/fastapi_docshield/static_handler.py +200 -0
- {fastapi_docshield-0.1.0 → fastapi_docshield-0.2.1}/pyproject.toml +11 -1
- fastapi_docshield-0.1.0/.github/workflows/publish-to-pypi.yml +0 -31
- fastapi_docshield-0.1.0/.github/workflows/python-tests.yml +0 -60
- fastapi_docshield-0.1.0/PKG-INFO +0 -146
- fastapi_docshield-0.1.0/README.md +0 -88
- fastapi_docshield-0.1.0/conftest.py +0 -13
- fastapi_docshield-0.1.0/demo.py +0 -87
- fastapi_docshield-0.1.0/examples/advanced_example.py +0 -76
- fastapi_docshield-0.1.0/examples/basic_example.py +0 -38
- fastapi_docshield-0.1.0/examples/manual_test.py +0 -38
- fastapi_docshield-0.1.0/fastapi_docshield/docshield.py +0 -187
- fastapi_docshield-0.1.0/pytest.ini +0 -20
- fastapi_docshield-0.1.0/setup.py +0 -5
- fastapi_docshield-0.1.0/tests/__init__.py +0 -0
- fastapi_docshield-0.1.0/tests/test_docshield.py +0 -172
- fastapi_docshield-0.1.0/tests/test_integration.py +0 -81
- fastapi_docshield-0.1.0/uv.lock +0 -1523
- {fastapi_docshield-0.1.0 → fastapi_docshield-0.2.1}/LICENSE +0 -0
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fastapi-docshield
|
|
3
|
+
Version: 0.2.1
|
|
4
|
+
Summary: A simple FastAPI integration to protect documentation endpoints with authentication
|
|
5
|
+
Project-URL: Homepage, https://github.com/georgekhananaev/fastapi-docshield
|
|
6
|
+
Project-URL: Bug Tracker, https://github.com/georgekhananaev/fastapi-docshield/issues
|
|
7
|
+
Project-URL: Documentation, https://github.com/georgekhananaev/fastapi-docshield#readme
|
|
8
|
+
Project-URL: Source Code, https://github.com/georgekhananaev/fastapi-docshield
|
|
9
|
+
Author-email: George Khananaev <georgekhananaev@gmail.com>
|
|
10
|
+
License: MIT License
|
|
11
|
+
|
|
12
|
+
Copyright (c) 2025 George Khananaev
|
|
13
|
+
|
|
14
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
15
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
16
|
+
in the Software without restriction, including without limitation the rights
|
|
17
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
18
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
19
|
+
furnished to do so, subject to the following conditions:
|
|
20
|
+
|
|
21
|
+
The above copyright notice and this permission notice shall be included in all
|
|
22
|
+
copies or substantial portions of the Software.
|
|
23
|
+
|
|
24
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
25
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
26
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
27
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
28
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
29
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
30
|
+
SOFTWARE.
|
|
31
|
+
License-File: LICENSE
|
|
32
|
+
Keywords: authentication,documentation,fastapi,security
|
|
33
|
+
Classifier: Development Status :: 4 - Beta
|
|
34
|
+
Classifier: Intended Audience :: Developers
|
|
35
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
36
|
+
Classifier: Operating System :: OS Independent
|
|
37
|
+
Classifier: Programming Language :: Python :: 3
|
|
38
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
39
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
40
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
41
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
42
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
43
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
44
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
45
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: HTTP Servers
|
|
46
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
47
|
+
Requires-Python: >=3.7
|
|
48
|
+
Requires-Dist: fastapi>=0.68.0
|
|
49
|
+
Requires-Dist: requests>=2.31.0
|
|
50
|
+
Requires-Dist: uvicorn>=0.22.0
|
|
51
|
+
Provides-Extra: dev
|
|
52
|
+
Requires-Dist: httpx>=0.23.0; extra == 'dev'
|
|
53
|
+
Requires-Dist: pytest-cov>=4.0.0; extra == 'dev'
|
|
54
|
+
Requires-Dist: pytest>=7.0.0; extra == 'dev'
|
|
55
|
+
Requires-Dist: python-multipart>=0.0.5; extra == 'dev'
|
|
56
|
+
Requires-Dist: requests>=2.28.0; extra == 'dev'
|
|
57
|
+
Requires-Dist: uvicorn>=0.18.0; extra == 'dev'
|
|
58
|
+
Description-Content-Type: text/markdown
|
|
59
|
+
|
|
60
|
+
# FastAPI DocShield
|
|
61
|
+
|
|
62
|
+
A simple FastAPI integration to protect documentation endpoints with HTTP Basic Authentication.
|
|
63
|
+
|
|
64
|
+
[](https://badge.fury.io/py/fastapi-docshield)
|
|
65
|
+
[](https://opensource.org/licenses/MIT)
|
|
66
|
+
[](https://github.com/georgekhananaev/fastapi-docshield)
|
|
67
|
+
[](https://github.com/georgekhananaev/fastapi-docshield)
|
|
68
|
+
[](https://github.com/astral-sh/uv)
|
|
69
|
+
|
|
70
|
+
## About
|
|
71
|
+
|
|
72
|
+
Protect FastAPI's `/docs`, `/redoc`, and `/openapi.json` endpoints with HTTP Basic Authentication.
|
|
73
|
+
|
|
74
|
+
## Installation
|
|
75
|
+
|
|
76
|
+
### From PyPI
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
# Install with pip
|
|
80
|
+
pip install fastapi-docshield
|
|
81
|
+
|
|
82
|
+
# Or with uv
|
|
83
|
+
uv pip install fastapi-docshield
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### From Source
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
git clone https://github.com/georgekhananaev/fastapi-docshield.git
|
|
90
|
+
cd fastapi-docshield
|
|
91
|
+
pip install -e .
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Quick Usage
|
|
95
|
+
|
|
96
|
+
### Single User
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
from fastapi import FastAPI
|
|
100
|
+
from fastapi_docshield import DocShield
|
|
101
|
+
|
|
102
|
+
app = FastAPI()
|
|
103
|
+
|
|
104
|
+
@app.get("/")
|
|
105
|
+
def read_root():
|
|
106
|
+
return {"Hello": "World"}
|
|
107
|
+
|
|
108
|
+
# Add protection to docs with a single user
|
|
109
|
+
DocShield(
|
|
110
|
+
app=app,
|
|
111
|
+
credentials={"admin": "password123"}
|
|
112
|
+
)
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Multiple Users
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
from fastapi import FastAPI
|
|
119
|
+
from fastapi_docshield import DocShield
|
|
120
|
+
|
|
121
|
+
app = FastAPI()
|
|
122
|
+
|
|
123
|
+
@app.get("/")
|
|
124
|
+
def read_root():
|
|
125
|
+
return {"Hello": "World"}
|
|
126
|
+
|
|
127
|
+
# Add protection to docs with multiple users
|
|
128
|
+
DocShield(
|
|
129
|
+
app=app,
|
|
130
|
+
credentials={
|
|
131
|
+
"admin": "admin_password",
|
|
132
|
+
"developer": "dev_password",
|
|
133
|
+
"viewer": "viewer_password"
|
|
134
|
+
}
|
|
135
|
+
)
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### CDN Fallback Mode (Default)
|
|
139
|
+
|
|
140
|
+
```python
|
|
141
|
+
from fastapi import FastAPI
|
|
142
|
+
from fastapi_docshield import DocShield
|
|
143
|
+
|
|
144
|
+
app = FastAPI()
|
|
145
|
+
|
|
146
|
+
# Default mode: Use CDN with automatic fallback to local files
|
|
147
|
+
DocShield(
|
|
148
|
+
app=app,
|
|
149
|
+
credentials={"admin": "password123"},
|
|
150
|
+
use_cdn_fallback=True # Default - automatically falls back to local if CDN fails
|
|
151
|
+
)
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Prefer Local Files
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
from fastapi import FastAPI
|
|
158
|
+
from fastapi_docshield import DocShield
|
|
159
|
+
|
|
160
|
+
app = FastAPI()
|
|
161
|
+
|
|
162
|
+
# Always use local files instead of CDN
|
|
163
|
+
DocShield(
|
|
164
|
+
app=app,
|
|
165
|
+
credentials={"admin": "password123"},
|
|
166
|
+
prefer_local=True # Serve documentation from bundled static files
|
|
167
|
+
)
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### CDN Only (No Fallback)
|
|
171
|
+
|
|
172
|
+
```python
|
|
173
|
+
from fastapi import FastAPI
|
|
174
|
+
from fastapi_docshield import DocShield
|
|
175
|
+
|
|
176
|
+
app = FastAPI()
|
|
177
|
+
|
|
178
|
+
# Use CDN without fallback (original behavior)
|
|
179
|
+
DocShield(
|
|
180
|
+
app=app,
|
|
181
|
+
credentials={"admin": "password123"},
|
|
182
|
+
use_cdn_fallback=False # Disable fallback, CDN only
|
|
183
|
+
)
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### Custom CSS and JavaScript
|
|
187
|
+
|
|
188
|
+
```python
|
|
189
|
+
from fastapi import FastAPI
|
|
190
|
+
from fastapi_docshield import DocShield
|
|
191
|
+
import requests
|
|
192
|
+
|
|
193
|
+
app = FastAPI()
|
|
194
|
+
|
|
195
|
+
# Load dark theme CSS from external source
|
|
196
|
+
# You can use https://github.com/georgekhananaev/fastapi-swagger-dark
|
|
197
|
+
dark_theme_url = "https://raw.githubusercontent.com/georgekhananaev/fastapi-swagger-dark/main/src/fastapi_swagger_dark/swagger_ui_dark.min.css"
|
|
198
|
+
custom_css = requests.get(dark_theme_url).text
|
|
199
|
+
|
|
200
|
+
# Custom JavaScript for analytics
|
|
201
|
+
custom_js = """
|
|
202
|
+
console.log('📊 Documentation accessed at:', new Date().toISOString());
|
|
203
|
+
document.addEventListener('DOMContentLoaded', function() {
|
|
204
|
+
console.log('Dark theme loaded!');
|
|
205
|
+
});
|
|
206
|
+
"""
|
|
207
|
+
|
|
208
|
+
# Apply with custom styling
|
|
209
|
+
DocShield(
|
|
210
|
+
app=app,
|
|
211
|
+
credentials={"admin": "password123"},
|
|
212
|
+
custom_css=custom_css,
|
|
213
|
+
custom_js=custom_js
|
|
214
|
+
)
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
#### Using with fastapi-swagger-dark
|
|
218
|
+
|
|
219
|
+
For a complete dark theme solution, you can use the [fastapi-swagger-dark](https://github.com/georgekhananaev/fastapi-swagger-dark) package:
|
|
220
|
+
|
|
221
|
+
```python
|
|
222
|
+
from fastapi import FastAPI
|
|
223
|
+
from fastapi_docshield import DocShield
|
|
224
|
+
import requests
|
|
225
|
+
|
|
226
|
+
app = FastAPI()
|
|
227
|
+
|
|
228
|
+
# Fetch dark theme CSS
|
|
229
|
+
response = requests.get(
|
|
230
|
+
"https://raw.githubusercontent.com/georgekhananaev/fastapi-swagger-dark/main/src/fastapi_swagger_dark/swagger_ui_dark.min.css"
|
|
231
|
+
)
|
|
232
|
+
dark_css = response.text
|
|
233
|
+
|
|
234
|
+
DocShield(
|
|
235
|
+
app=app,
|
|
236
|
+
credentials={"admin": "password123"},
|
|
237
|
+
custom_css=dark_css # Apply dark theme
|
|
238
|
+
)
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
See [examples/custom_styling.py](examples/custom_styling.py) for more customization examples including:
|
|
242
|
+
- ✨ Minimal clean theme
|
|
243
|
+
- 🏢 Corporate theme with analytics
|
|
244
|
+
- 📖 ReDoc customization
|
|
245
|
+
- 🎨 Custom branding
|
|
246
|
+
|
|
247
|
+
## Running Demo
|
|
248
|
+
|
|
249
|
+
```bash
|
|
250
|
+
# Run the demo app
|
|
251
|
+
python demo.py
|
|
252
|
+
|
|
253
|
+
# Visit http://localhost:8000/docs
|
|
254
|
+
# Username: admin
|
|
255
|
+
# Password: password123
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
## Running Tests
|
|
259
|
+
|
|
260
|
+
```bash
|
|
261
|
+
# Install test dependencies
|
|
262
|
+
pip install pytest httpx
|
|
263
|
+
|
|
264
|
+
# Run all tests
|
|
265
|
+
pytest
|
|
266
|
+
|
|
267
|
+
# Run with coverage
|
|
268
|
+
pytest --cov=fastapi_docshield
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
## Features
|
|
272
|
+
|
|
273
|
+
- Protect Swagger UI, ReDoc, and OpenAPI JSON endpoints
|
|
274
|
+
- Customizable endpoint URLs
|
|
275
|
+
- Multiple username/password combinations
|
|
276
|
+
- **Automatic CDN fallback** - Falls back to local files if CDN is unavailable
|
|
277
|
+
- **Local file preference option** - Serve documentation from local files for better reliability
|
|
278
|
+
- **Custom CSS and JavaScript injection** - Fully customize the look and behavior of documentation
|
|
279
|
+
- **Resilient documentation** - Works even when CDN is down or blocked
|
|
280
|
+
- Tested on Python 3.7-3.13
|
|
281
|
+
- Compatible with uv package manager
|
|
282
|
+
|
|
283
|
+
## Changelog
|
|
284
|
+
|
|
285
|
+
### Version 0.2.1 (2025-08-17)
|
|
286
|
+
- **Fixed**: Blank page issue after authentication for some users
|
|
287
|
+
- Improved handling of custom URL parameters by storing them as instance variables
|
|
288
|
+
- Simplified `_setup_routes()` method for better maintainability
|
|
289
|
+
- Applied fix from PR #2 for more robust URL parameter handling
|
|
290
|
+
- **Fixed**: Route removal logic now correctly removes all default documentation routes
|
|
291
|
+
- Properly removes `/docs`, `/redoc`, and `/openapi.json` endpoints
|
|
292
|
+
- Prevents 500 errors when accessing old endpoints
|
|
293
|
+
- **Improved**: Example files and documentation
|
|
294
|
+
- Fixed `custom_styling.py` to work with uvicorn by adding default app variable
|
|
295
|
+
- Standardized credentials across all custom styling examples
|
|
296
|
+
- Added `python-multipart` to dev dependencies for form data handling
|
|
297
|
+
- Added clear run instructions in example files
|
|
298
|
+
|
|
299
|
+
### Version 0.2.0 (2025-08-17)
|
|
300
|
+
- **Added**: Custom CSS and JavaScript injection support
|
|
301
|
+
- New `custom_css` parameter to inject custom styles into documentation pages
|
|
302
|
+
- New `custom_js` parameter to inject custom JavaScript for enhanced functionality
|
|
303
|
+
- Complete customization examples for dark theme, minimal theme, corporate branding, and analytics
|
|
304
|
+
- Support for both Swagger UI and ReDoc customization
|
|
305
|
+
- **Added**: Automatic CDN fallback to local files for better reliability
|
|
306
|
+
- Documentation now automatically falls back to bundled static files if CDN is unavailable
|
|
307
|
+
- New `prefer_local` option to always serve from local files
|
|
308
|
+
- New `use_cdn_fallback` option to control fallback behavior
|
|
309
|
+
- Bundled Swagger UI and ReDoc static files for offline capability
|
|
310
|
+
- **Fixed**: Static file URL bug that caused blank documentation pages
|
|
311
|
+
- Previously, when no custom CDN URLs were provided, the package would pass `None` values to FastAPI's documentation functions
|
|
312
|
+
- This resulted in HTML with `href="None"` and `src="None"`, causing white/blank pages
|
|
313
|
+
- Now properly handles default CDN URLs when custom URLs are not specified
|
|
314
|
+
|
|
315
|
+
### Version 0.1.0 (2025-01-15)
|
|
316
|
+
- Initial release
|
|
317
|
+
- Basic HTTP authentication for FastAPI documentation endpoints
|
|
318
|
+
- Support for multiple users
|
|
319
|
+
|
|
320
|
+
## License
|
|
321
|
+
|
|
322
|
+
MIT License - Copyright (c) 2025 George Khananaev
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
# FastAPI DocShield
|
|
2
|
+
|
|
3
|
+
A simple FastAPI integration to protect documentation endpoints with HTTP Basic Authentication.
|
|
4
|
+
|
|
5
|
+
[](https://badge.fury.io/py/fastapi-docshield)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
[](https://github.com/georgekhananaev/fastapi-docshield)
|
|
8
|
+
[](https://github.com/georgekhananaev/fastapi-docshield)
|
|
9
|
+
[](https://github.com/astral-sh/uv)
|
|
10
|
+
|
|
11
|
+
## About
|
|
12
|
+
|
|
13
|
+
Protect FastAPI's `/docs`, `/redoc`, and `/openapi.json` endpoints with HTTP Basic Authentication.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
### From PyPI
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
# Install with pip
|
|
21
|
+
pip install fastapi-docshield
|
|
22
|
+
|
|
23
|
+
# Or with uv
|
|
24
|
+
uv pip install fastapi-docshield
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### From Source
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
git clone https://github.com/georgekhananaev/fastapi-docshield.git
|
|
31
|
+
cd fastapi-docshield
|
|
32
|
+
pip install -e .
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Quick Usage
|
|
36
|
+
|
|
37
|
+
### Single User
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
from fastapi import FastAPI
|
|
41
|
+
from fastapi_docshield import DocShield
|
|
42
|
+
|
|
43
|
+
app = FastAPI()
|
|
44
|
+
|
|
45
|
+
@app.get("/")
|
|
46
|
+
def read_root():
|
|
47
|
+
return {"Hello": "World"}
|
|
48
|
+
|
|
49
|
+
# Add protection to docs with a single user
|
|
50
|
+
DocShield(
|
|
51
|
+
app=app,
|
|
52
|
+
credentials={"admin": "password123"}
|
|
53
|
+
)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Multiple Users
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
from fastapi import FastAPI
|
|
60
|
+
from fastapi_docshield import DocShield
|
|
61
|
+
|
|
62
|
+
app = FastAPI()
|
|
63
|
+
|
|
64
|
+
@app.get("/")
|
|
65
|
+
def read_root():
|
|
66
|
+
return {"Hello": "World"}
|
|
67
|
+
|
|
68
|
+
# Add protection to docs with multiple users
|
|
69
|
+
DocShield(
|
|
70
|
+
app=app,
|
|
71
|
+
credentials={
|
|
72
|
+
"admin": "admin_password",
|
|
73
|
+
"developer": "dev_password",
|
|
74
|
+
"viewer": "viewer_password"
|
|
75
|
+
}
|
|
76
|
+
)
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### CDN Fallback Mode (Default)
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
from fastapi import FastAPI
|
|
83
|
+
from fastapi_docshield import DocShield
|
|
84
|
+
|
|
85
|
+
app = FastAPI()
|
|
86
|
+
|
|
87
|
+
# Default mode: Use CDN with automatic fallback to local files
|
|
88
|
+
DocShield(
|
|
89
|
+
app=app,
|
|
90
|
+
credentials={"admin": "password123"},
|
|
91
|
+
use_cdn_fallback=True # Default - automatically falls back to local if CDN fails
|
|
92
|
+
)
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Prefer Local Files
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
from fastapi import FastAPI
|
|
99
|
+
from fastapi_docshield import DocShield
|
|
100
|
+
|
|
101
|
+
app = FastAPI()
|
|
102
|
+
|
|
103
|
+
# Always use local files instead of CDN
|
|
104
|
+
DocShield(
|
|
105
|
+
app=app,
|
|
106
|
+
credentials={"admin": "password123"},
|
|
107
|
+
prefer_local=True # Serve documentation from bundled static files
|
|
108
|
+
)
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### CDN Only (No Fallback)
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
from fastapi import FastAPI
|
|
115
|
+
from fastapi_docshield import DocShield
|
|
116
|
+
|
|
117
|
+
app = FastAPI()
|
|
118
|
+
|
|
119
|
+
# Use CDN without fallback (original behavior)
|
|
120
|
+
DocShield(
|
|
121
|
+
app=app,
|
|
122
|
+
credentials={"admin": "password123"},
|
|
123
|
+
use_cdn_fallback=False # Disable fallback, CDN only
|
|
124
|
+
)
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Custom CSS and JavaScript
|
|
128
|
+
|
|
129
|
+
```python
|
|
130
|
+
from fastapi import FastAPI
|
|
131
|
+
from fastapi_docshield import DocShield
|
|
132
|
+
import requests
|
|
133
|
+
|
|
134
|
+
app = FastAPI()
|
|
135
|
+
|
|
136
|
+
# Load dark theme CSS from external source
|
|
137
|
+
# You can use https://github.com/georgekhananaev/fastapi-swagger-dark
|
|
138
|
+
dark_theme_url = "https://raw.githubusercontent.com/georgekhananaev/fastapi-swagger-dark/main/src/fastapi_swagger_dark/swagger_ui_dark.min.css"
|
|
139
|
+
custom_css = requests.get(dark_theme_url).text
|
|
140
|
+
|
|
141
|
+
# Custom JavaScript for analytics
|
|
142
|
+
custom_js = """
|
|
143
|
+
console.log('📊 Documentation accessed at:', new Date().toISOString());
|
|
144
|
+
document.addEventListener('DOMContentLoaded', function() {
|
|
145
|
+
console.log('Dark theme loaded!');
|
|
146
|
+
});
|
|
147
|
+
"""
|
|
148
|
+
|
|
149
|
+
# Apply with custom styling
|
|
150
|
+
DocShield(
|
|
151
|
+
app=app,
|
|
152
|
+
credentials={"admin": "password123"},
|
|
153
|
+
custom_css=custom_css,
|
|
154
|
+
custom_js=custom_js
|
|
155
|
+
)
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
#### Using with fastapi-swagger-dark
|
|
159
|
+
|
|
160
|
+
For a complete dark theme solution, you can use the [fastapi-swagger-dark](https://github.com/georgekhananaev/fastapi-swagger-dark) package:
|
|
161
|
+
|
|
162
|
+
```python
|
|
163
|
+
from fastapi import FastAPI
|
|
164
|
+
from fastapi_docshield import DocShield
|
|
165
|
+
import requests
|
|
166
|
+
|
|
167
|
+
app = FastAPI()
|
|
168
|
+
|
|
169
|
+
# Fetch dark theme CSS
|
|
170
|
+
response = requests.get(
|
|
171
|
+
"https://raw.githubusercontent.com/georgekhananaev/fastapi-swagger-dark/main/src/fastapi_swagger_dark/swagger_ui_dark.min.css"
|
|
172
|
+
)
|
|
173
|
+
dark_css = response.text
|
|
174
|
+
|
|
175
|
+
DocShield(
|
|
176
|
+
app=app,
|
|
177
|
+
credentials={"admin": "password123"},
|
|
178
|
+
custom_css=dark_css # Apply dark theme
|
|
179
|
+
)
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
See [examples/custom_styling.py](examples/custom_styling.py) for more customization examples including:
|
|
183
|
+
- ✨ Minimal clean theme
|
|
184
|
+
- 🏢 Corporate theme with analytics
|
|
185
|
+
- 📖 ReDoc customization
|
|
186
|
+
- 🎨 Custom branding
|
|
187
|
+
|
|
188
|
+
## Running Demo
|
|
189
|
+
|
|
190
|
+
```bash
|
|
191
|
+
# Run the demo app
|
|
192
|
+
python demo.py
|
|
193
|
+
|
|
194
|
+
# Visit http://localhost:8000/docs
|
|
195
|
+
# Username: admin
|
|
196
|
+
# Password: password123
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
## Running Tests
|
|
200
|
+
|
|
201
|
+
```bash
|
|
202
|
+
# Install test dependencies
|
|
203
|
+
pip install pytest httpx
|
|
204
|
+
|
|
205
|
+
# Run all tests
|
|
206
|
+
pytest
|
|
207
|
+
|
|
208
|
+
# Run with coverage
|
|
209
|
+
pytest --cov=fastapi_docshield
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
## Features
|
|
213
|
+
|
|
214
|
+
- Protect Swagger UI, ReDoc, and OpenAPI JSON endpoints
|
|
215
|
+
- Customizable endpoint URLs
|
|
216
|
+
- Multiple username/password combinations
|
|
217
|
+
- **Automatic CDN fallback** - Falls back to local files if CDN is unavailable
|
|
218
|
+
- **Local file preference option** - Serve documentation from local files for better reliability
|
|
219
|
+
- **Custom CSS and JavaScript injection** - Fully customize the look and behavior of documentation
|
|
220
|
+
- **Resilient documentation** - Works even when CDN is down or blocked
|
|
221
|
+
- Tested on Python 3.7-3.13
|
|
222
|
+
- Compatible with uv package manager
|
|
223
|
+
|
|
224
|
+
## Changelog
|
|
225
|
+
|
|
226
|
+
### Version 0.2.1 (2025-08-17)
|
|
227
|
+
- **Fixed**: Blank page issue after authentication for some users
|
|
228
|
+
- Improved handling of custom URL parameters by storing them as instance variables
|
|
229
|
+
- Simplified `_setup_routes()` method for better maintainability
|
|
230
|
+
- Applied fix from PR #2 for more robust URL parameter handling
|
|
231
|
+
- **Fixed**: Route removal logic now correctly removes all default documentation routes
|
|
232
|
+
- Properly removes `/docs`, `/redoc`, and `/openapi.json` endpoints
|
|
233
|
+
- Prevents 500 errors when accessing old endpoints
|
|
234
|
+
- **Improved**: Example files and documentation
|
|
235
|
+
- Fixed `custom_styling.py` to work with uvicorn by adding default app variable
|
|
236
|
+
- Standardized credentials across all custom styling examples
|
|
237
|
+
- Added `python-multipart` to dev dependencies for form data handling
|
|
238
|
+
- Added clear run instructions in example files
|
|
239
|
+
|
|
240
|
+
### Version 0.2.0 (2025-08-17)
|
|
241
|
+
- **Added**: Custom CSS and JavaScript injection support
|
|
242
|
+
- New `custom_css` parameter to inject custom styles into documentation pages
|
|
243
|
+
- New `custom_js` parameter to inject custom JavaScript for enhanced functionality
|
|
244
|
+
- Complete customization examples for dark theme, minimal theme, corporate branding, and analytics
|
|
245
|
+
- Support for both Swagger UI and ReDoc customization
|
|
246
|
+
- **Added**: Automatic CDN fallback to local files for better reliability
|
|
247
|
+
- Documentation now automatically falls back to bundled static files if CDN is unavailable
|
|
248
|
+
- New `prefer_local` option to always serve from local files
|
|
249
|
+
- New `use_cdn_fallback` option to control fallback behavior
|
|
250
|
+
- Bundled Swagger UI and ReDoc static files for offline capability
|
|
251
|
+
- **Fixed**: Static file URL bug that caused blank documentation pages
|
|
252
|
+
- Previously, when no custom CDN URLs were provided, the package would pass `None` values to FastAPI's documentation functions
|
|
253
|
+
- This resulted in HTML with `href="None"` and `src="None"`, causing white/blank pages
|
|
254
|
+
- Now properly handles default CDN URLs when custom URLs are not specified
|
|
255
|
+
|
|
256
|
+
### Version 0.1.0 (2025-01-15)
|
|
257
|
+
- Initial release
|
|
258
|
+
- Basic HTTP authentication for FastAPI documentation endpoints
|
|
259
|
+
- Support for multiple users
|
|
260
|
+
|
|
261
|
+
## License
|
|
262
|
+
|
|
263
|
+
MIT License - Copyright (c) 2025 George Khananaev
|
|
@@ -9,9 +9,9 @@ License: MIT License
|
|
|
9
9
|
Copyright (c) 2025 George Khananaev
|
|
10
10
|
"""
|
|
11
11
|
|
|
12
|
-
from .docshield import DocShield
|
|
12
|
+
from .docshield import DocShield, __version__
|
|
13
13
|
|
|
14
|
-
__version__ =
|
|
14
|
+
__version__ = __version__
|
|
15
15
|
__author__ = "George Khananaev"
|
|
16
16
|
__license__ = "MIT"
|
|
17
17
|
__copyright__ = "Copyright (c) 2025 George Khananaev"
|