fastapi-docshield 0.1.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.
- fastapi_docshield-0.1.0/.github/workflows/publish-to-pypi.yml +31 -0
- fastapi_docshield-0.1.0/.github/workflows/python-tests.yml +60 -0
- fastapi_docshield-0.1.0/.gitignore +73 -0
- fastapi_docshield-0.1.0/LICENSE +21 -0
- fastapi_docshield-0.1.0/PKG-INFO +146 -0
- fastapi_docshield-0.1.0/README.md +88 -0
- fastapi_docshield-0.1.0/conftest.py +13 -0
- fastapi_docshield-0.1.0/demo.py +87 -0
- fastapi_docshield-0.1.0/examples/advanced_example.py +76 -0
- fastapi_docshield-0.1.0/examples/basic_example.py +38 -0
- fastapi_docshield-0.1.0/examples/manual_test.py +38 -0
- fastapi_docshield-0.1.0/fastapi_docshield/__init__.py +18 -0
- fastapi_docshield-0.1.0/fastapi_docshield/docshield.py +187 -0
- fastapi_docshield-0.1.0/pyproject.toml +54 -0
- fastapi_docshield-0.1.0/pytest.ini +20 -0
- fastapi_docshield-0.1.0/setup.py +5 -0
- fastapi_docshield-0.1.0/tests/__init__.py +0 -0
- fastapi_docshield-0.1.0/tests/test_docshield.py +172 -0
- fastapi_docshield-0.1.0/tests/test_integration.py +81 -0
- fastapi_docshield-0.1.0/uv.lock +1523 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [created]
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
deploy:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
steps:
|
|
11
|
+
- uses: actions/checkout@v3
|
|
12
|
+
- name: Set up Python
|
|
13
|
+
uses: actions/setup-python@v4
|
|
14
|
+
with:
|
|
15
|
+
python-version: '3.10'
|
|
16
|
+
|
|
17
|
+
- name: Install dependencies
|
|
18
|
+
run: |
|
|
19
|
+
python -m pip install --upgrade pip
|
|
20
|
+
pip install build twine
|
|
21
|
+
|
|
22
|
+
- name: Build and check package
|
|
23
|
+
run: |
|
|
24
|
+
python -m build
|
|
25
|
+
twine check dist/*
|
|
26
|
+
|
|
27
|
+
- name: Publish to PyPI
|
|
28
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
29
|
+
with:
|
|
30
|
+
user: __token__
|
|
31
|
+
password: ${{ secrets.PYPI_API_TOKEN }}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
name: Python Tests
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [ main ]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [ main ]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
test:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
strategy:
|
|
13
|
+
fail-fast: false
|
|
14
|
+
matrix:
|
|
15
|
+
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
|
|
16
|
+
|
|
17
|
+
steps:
|
|
18
|
+
- uses: actions/checkout@v3
|
|
19
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
20
|
+
uses: actions/setup-python@v4
|
|
21
|
+
with:
|
|
22
|
+
python-version: ${{ matrix.python-version }}
|
|
23
|
+
|
|
24
|
+
- name: Install dependencies
|
|
25
|
+
run: |
|
|
26
|
+
python -m pip install --upgrade pip
|
|
27
|
+
python -m pip install pytest pytest-cov httpx
|
|
28
|
+
python -m pip install -e .
|
|
29
|
+
|
|
30
|
+
- name: Test with pytest
|
|
31
|
+
run: |
|
|
32
|
+
pytest --cov=fastapi_docshield
|
|
33
|
+
|
|
34
|
+
test-with-uv:
|
|
35
|
+
runs-on: ubuntu-latest
|
|
36
|
+
strategy:
|
|
37
|
+
fail-fast: false
|
|
38
|
+
matrix:
|
|
39
|
+
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
|
|
40
|
+
|
|
41
|
+
steps:
|
|
42
|
+
- uses: actions/checkout@v3
|
|
43
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
44
|
+
uses: actions/setup-python@v4
|
|
45
|
+
with:
|
|
46
|
+
python-version: ${{ matrix.python-version }}
|
|
47
|
+
|
|
48
|
+
- name: Install uv
|
|
49
|
+
run: |
|
|
50
|
+
python -m pip install --upgrade pip
|
|
51
|
+
pip install uv
|
|
52
|
+
|
|
53
|
+
- name: Install dependencies with uv
|
|
54
|
+
run: |
|
|
55
|
+
uv pip install pytest pytest-cov httpx
|
|
56
|
+
uv pip install -e .
|
|
57
|
+
|
|
58
|
+
- name: Test with pytest
|
|
59
|
+
run: |
|
|
60
|
+
python -m pytest --cov=fastapi_docshield
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
*.egg-info/
|
|
24
|
+
.installed.cfg
|
|
25
|
+
*.egg
|
|
26
|
+
MANIFEST
|
|
27
|
+
|
|
28
|
+
# PyInstaller
|
|
29
|
+
# Usually these files are written by a python script from a template
|
|
30
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
31
|
+
*.manifest
|
|
32
|
+
*.spec
|
|
33
|
+
|
|
34
|
+
# Installer logs
|
|
35
|
+
pip-log.txt
|
|
36
|
+
pip-delete-this-directory.txt
|
|
37
|
+
|
|
38
|
+
# Unit test / coverage reports
|
|
39
|
+
htmlcov/
|
|
40
|
+
.tox/
|
|
41
|
+
.nox/
|
|
42
|
+
.coverage
|
|
43
|
+
.coverage.*
|
|
44
|
+
.cache
|
|
45
|
+
nosetests.xml
|
|
46
|
+
coverage.xml
|
|
47
|
+
*.cover
|
|
48
|
+
.hypothesis/
|
|
49
|
+
.pytest_cache/
|
|
50
|
+
|
|
51
|
+
# Environments
|
|
52
|
+
.env
|
|
53
|
+
.venv
|
|
54
|
+
env/
|
|
55
|
+
venv/
|
|
56
|
+
ENV/
|
|
57
|
+
env.bak/
|
|
58
|
+
venv.bak/
|
|
59
|
+
|
|
60
|
+
# mypy
|
|
61
|
+
.mypy_cache/
|
|
62
|
+
.dmypy.json
|
|
63
|
+
dmypy.json
|
|
64
|
+
|
|
65
|
+
# IDE files
|
|
66
|
+
.idea/
|
|
67
|
+
.vscode/
|
|
68
|
+
*.swp
|
|
69
|
+
*.swo
|
|
70
|
+
|
|
71
|
+
# OS specific files
|
|
72
|
+
.DS_Store
|
|
73
|
+
**/.claude/settings.local.json
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 George Khananaev
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fastapi-docshield
|
|
3
|
+
Version: 0.1.0
|
|
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: requests>=2.28.0; extra == 'dev'
|
|
56
|
+
Requires-Dist: uvicorn>=0.18.0; extra == 'dev'
|
|
57
|
+
Description-Content-Type: text/markdown
|
|
58
|
+
|
|
59
|
+
# FastAPI DocShield
|
|
60
|
+
|
|
61
|
+
A simple FastAPI integration to protect documentation endpoints with HTTP Basic Authentication.
|
|
62
|
+
|
|
63
|
+
[](https://badge.fury.io/py/fastapi-docshield)
|
|
64
|
+
[](https://opensource.org/licenses/MIT)
|
|
65
|
+
[](https://github.com/georgekhananaev/fastapi-docshield)
|
|
66
|
+
[](https://github.com/georgekhananaev/fastapi-docshield)
|
|
67
|
+
[](https://github.com/astral-sh/uv)
|
|
68
|
+
|
|
69
|
+
## About
|
|
70
|
+
|
|
71
|
+
Protect FastAPI's `/docs`, `/redoc`, and `/openapi.json` endpoints with HTTP Basic Authentication.
|
|
72
|
+
|
|
73
|
+
## Installation
|
|
74
|
+
|
|
75
|
+
### From PyPI
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
# Install with pip
|
|
79
|
+
pip install fastapi-docshield
|
|
80
|
+
|
|
81
|
+
# Or with uv
|
|
82
|
+
uv pip install fastapi-docshield
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### From Source
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
git clone https://github.com/georgekhananaev/fastapi-docshield.git
|
|
89
|
+
cd fastapi-docshield
|
|
90
|
+
pip install -e .
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Quick Usage
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
from fastapi import FastAPI
|
|
97
|
+
from fastapi_docshield import DocShield
|
|
98
|
+
|
|
99
|
+
app = FastAPI()
|
|
100
|
+
|
|
101
|
+
@app.get("/")
|
|
102
|
+
def read_root():
|
|
103
|
+
return {"Hello": "World"}
|
|
104
|
+
|
|
105
|
+
# Add protection to docs
|
|
106
|
+
DocShield(
|
|
107
|
+
app=app,
|
|
108
|
+
credentials={"admin": "password123"}
|
|
109
|
+
)
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Running Demo
|
|
113
|
+
|
|
114
|
+
```bash
|
|
115
|
+
# Run the demo app
|
|
116
|
+
python demo.py
|
|
117
|
+
|
|
118
|
+
# Visit http://localhost:8000/docs
|
|
119
|
+
# Username: admin
|
|
120
|
+
# Password: password123
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## Running Tests
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
# Install test dependencies
|
|
127
|
+
pip install pytest httpx
|
|
128
|
+
|
|
129
|
+
# Run all tests
|
|
130
|
+
pytest
|
|
131
|
+
|
|
132
|
+
# Run with coverage
|
|
133
|
+
pytest --cov=fastapi_docshield
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Features
|
|
137
|
+
|
|
138
|
+
- Protect Swagger UI, ReDoc, and OpenAPI JSON endpoints
|
|
139
|
+
- Customizable endpoint URLs
|
|
140
|
+
- Multiple username/password combinations
|
|
141
|
+
- Tested on Python 3.7-3.13
|
|
142
|
+
- Compatible with uv package manager
|
|
143
|
+
|
|
144
|
+
## License
|
|
145
|
+
|
|
146
|
+
MIT License - Copyright (c) 2025 George Khananaev
|
|
@@ -0,0 +1,88 @@
|
|
|
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
|
+
```python
|
|
38
|
+
from fastapi import FastAPI
|
|
39
|
+
from fastapi_docshield import DocShield
|
|
40
|
+
|
|
41
|
+
app = FastAPI()
|
|
42
|
+
|
|
43
|
+
@app.get("/")
|
|
44
|
+
def read_root():
|
|
45
|
+
return {"Hello": "World"}
|
|
46
|
+
|
|
47
|
+
# Add protection to docs
|
|
48
|
+
DocShield(
|
|
49
|
+
app=app,
|
|
50
|
+
credentials={"admin": "password123"}
|
|
51
|
+
)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Running Demo
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
# Run the demo app
|
|
58
|
+
python demo.py
|
|
59
|
+
|
|
60
|
+
# Visit http://localhost:8000/docs
|
|
61
|
+
# Username: admin
|
|
62
|
+
# Password: password123
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Running Tests
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
# Install test dependencies
|
|
69
|
+
pip install pytest httpx
|
|
70
|
+
|
|
71
|
+
# Run all tests
|
|
72
|
+
pytest
|
|
73
|
+
|
|
74
|
+
# Run with coverage
|
|
75
|
+
pytest --cov=fastapi_docshield
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Features
|
|
79
|
+
|
|
80
|
+
- Protect Swagger UI, ReDoc, and OpenAPI JSON endpoints
|
|
81
|
+
- Customizable endpoint URLs
|
|
82
|
+
- Multiple username/password combinations
|
|
83
|
+
- Tested on Python 3.7-3.13
|
|
84
|
+
- Compatible with uv package manager
|
|
85
|
+
|
|
86
|
+
## License
|
|
87
|
+
|
|
88
|
+
MIT License - Copyright (c) 2025 George Khananaev
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
from fastapi import FastAPI
|
|
3
|
+
from fastapi.testclient import TestClient
|
|
4
|
+
|
|
5
|
+
@pytest.fixture
|
|
6
|
+
def app():
|
|
7
|
+
"""Create a fresh FastAPI app for each test."""
|
|
8
|
+
return FastAPI()
|
|
9
|
+
|
|
10
|
+
@pytest.fixture
|
|
11
|
+
def client(app):
|
|
12
|
+
"""Create a test client using the test app."""
|
|
13
|
+
return TestClient(app)
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"""
|
|
2
|
+
DocShield Demo Script
|
|
3
|
+
|
|
4
|
+
This script runs a simple FastAPI application with DocShield protection
|
|
5
|
+
on documentation endpoints. Use it to quickly verify that DocShield is
|
|
6
|
+
working correctly.
|
|
7
|
+
|
|
8
|
+
To run the demo:
|
|
9
|
+
python demo.py
|
|
10
|
+
|
|
11
|
+
Then access:
|
|
12
|
+
http://localhost:8000/docs
|
|
13
|
+
http://localhost:8000/redoc
|
|
14
|
+
|
|
15
|
+
When prompted for credentials, use:
|
|
16
|
+
Username: admin
|
|
17
|
+
Password: password123
|
|
18
|
+
|
|
19
|
+
or
|
|
20
|
+
|
|
21
|
+
Username: user
|
|
22
|
+
Password: user456
|
|
23
|
+
"""
|
|
24
|
+
import uvicorn
|
|
25
|
+
from fastapi import FastAPI
|
|
26
|
+
from fastapi_docshield import DocShield
|
|
27
|
+
|
|
28
|
+
def main():
|
|
29
|
+
# Create a FastAPI app
|
|
30
|
+
app = FastAPI(
|
|
31
|
+
title="DocShield Demo API",
|
|
32
|
+
description="A demonstration of DocShield for protecting FastAPI documentation",
|
|
33
|
+
version="1.0.0"
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
# Add some API routes
|
|
37
|
+
@app.get("/")
|
|
38
|
+
def read_root():
|
|
39
|
+
"""Return a welcome message."""
|
|
40
|
+
return {
|
|
41
|
+
"message": "Welcome to DocShield Demo API",
|
|
42
|
+
"docs": "Visit /docs or /redoc (protected with authentication)"
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
@app.get("/users")
|
|
46
|
+
def get_users():
|
|
47
|
+
"""Get a list of sample users."""
|
|
48
|
+
return [
|
|
49
|
+
{"id": 1, "name": "John Doe", "email": "john@example.com"},
|
|
50
|
+
{"id": 2, "name": "Jane Smith", "email": "jane@example.com"},
|
|
51
|
+
{"id": 3, "name": "Bob Johnson", "email": "bob@example.com"}
|
|
52
|
+
]
|
|
53
|
+
|
|
54
|
+
@app.get("/users/{user_id}")
|
|
55
|
+
def get_user(user_id: int):
|
|
56
|
+
"""Get a specific user by ID."""
|
|
57
|
+
return {"id": user_id, "name": f"User {user_id}", "email": f"user{user_id}@example.com"}
|
|
58
|
+
|
|
59
|
+
# Apply DocShield to protect documentation
|
|
60
|
+
DocShield(
|
|
61
|
+
app=app,
|
|
62
|
+
credentials={
|
|
63
|
+
"admin": "password123",
|
|
64
|
+
"user": "user456"
|
|
65
|
+
}
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
# Display information
|
|
69
|
+
print("=" * 70)
|
|
70
|
+
print("DocShield Demo Server")
|
|
71
|
+
print("=" * 70)
|
|
72
|
+
print("API running at: http://localhost:8000")
|
|
73
|
+
print()
|
|
74
|
+
print("Documentation URLs (protected with HTTP Basic Auth):")
|
|
75
|
+
print("- Swagger UI: http://localhost:8000/docs")
|
|
76
|
+
print("- ReDoc: http://localhost:8000/redoc")
|
|
77
|
+
print()
|
|
78
|
+
print("Authentication credentials:")
|
|
79
|
+
print("- Username: admin, Password: password123")
|
|
80
|
+
print("- Username: user, Password: user456")
|
|
81
|
+
print("=" * 70)
|
|
82
|
+
|
|
83
|
+
# Run the server
|
|
84
|
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
85
|
+
|
|
86
|
+
if __name__ == "__main__":
|
|
87
|
+
main()
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
from fastapi import FastAPI, Depends, HTTPException
|
|
2
|
+
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
|
|
3
|
+
from pydantic import BaseModel
|
|
4
|
+
from fastapi_docshield import DocShield
|
|
5
|
+
|
|
6
|
+
# Create FastAPI app
|
|
7
|
+
app = FastAPI(
|
|
8
|
+
title="Advanced Protected API",
|
|
9
|
+
description="An API with protected documentation and custom paths",
|
|
10
|
+
version="1.0.0"
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
# Simple user database for the API routes (not related to DocShield)
|
|
14
|
+
fake_users_db = {
|
|
15
|
+
"user1": {
|
|
16
|
+
"username": "user1",
|
|
17
|
+
"email": "user1@example.com",
|
|
18
|
+
"password": "secret1",
|
|
19
|
+
},
|
|
20
|
+
"user2": {
|
|
21
|
+
"username": "user2",
|
|
22
|
+
"email": "user2@example.com",
|
|
23
|
+
"password": "secret2",
|
|
24
|
+
},
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
# OAuth2 token endpoint for the API (not related to DocShield)
|
|
28
|
+
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
|
29
|
+
|
|
30
|
+
class User(BaseModel):
|
|
31
|
+
username: str
|
|
32
|
+
email: str
|
|
33
|
+
|
|
34
|
+
class UserInDB(User):
|
|
35
|
+
password: str
|
|
36
|
+
|
|
37
|
+
# API routes (not related to DocShield)
|
|
38
|
+
@app.post("/token")
|
|
39
|
+
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
|
|
40
|
+
user = fake_users_db.get(form_data.username)
|
|
41
|
+
if not user:
|
|
42
|
+
raise HTTPException(status_code=400, detail="Incorrect username or password")
|
|
43
|
+
|
|
44
|
+
if user["password"] != form_data.password:
|
|
45
|
+
raise HTTPException(status_code=400, detail="Incorrect username or password")
|
|
46
|
+
|
|
47
|
+
return {"access_token": user["username"], "token_type": "bearer"}
|
|
48
|
+
|
|
49
|
+
@app.get("/users/me", response_model=User)
|
|
50
|
+
async def read_users_me(token: str = Depends(oauth2_scheme)):
|
|
51
|
+
if token not in fake_users_db:
|
|
52
|
+
raise HTTPException(status_code=401, detail="Invalid authentication credentials")
|
|
53
|
+
|
|
54
|
+
user = fake_users_db[token]
|
|
55
|
+
return user
|
|
56
|
+
|
|
57
|
+
# Protect the docs with DocShield - using custom URLs and UI resources
|
|
58
|
+
DocShield(
|
|
59
|
+
app=app,
|
|
60
|
+
credentials={
|
|
61
|
+
"admin": "admin123", # Credentials for accessing the docs
|
|
62
|
+
},
|
|
63
|
+
docs_url="/api/docs", # Custom docs URL
|
|
64
|
+
redoc_url="/api/redoc", # Custom redoc URL
|
|
65
|
+
openapi_url="/api/openapi.json", # Custom OpenAPI JSON URL
|
|
66
|
+
|
|
67
|
+
# Optional: custom Swagger UI and ReDoc resources
|
|
68
|
+
swagger_js_url="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui-bundle.js",
|
|
69
|
+
swagger_css_url="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui.css",
|
|
70
|
+
redoc_js_url="https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js"
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
# Run with: uvicorn advanced_example:app --reload
|
|
74
|
+
# Then try accessing:
|
|
75
|
+
# - http://localhost:8000/api/docs (protected with DocShield credentials)
|
|
76
|
+
# - http://localhost:8000/api/redoc (protected with DocShield credentials)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
from fastapi import FastAPI
|
|
2
|
+
from fastapi_docshield import DocShield
|
|
3
|
+
|
|
4
|
+
# Create FastAPI app
|
|
5
|
+
app = FastAPI(
|
|
6
|
+
title="Protected API",
|
|
7
|
+
description="An API with protected documentation",
|
|
8
|
+
version="1.0.0"
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
# Add some routes
|
|
12
|
+
@app.get("/")
|
|
13
|
+
def read_root():
|
|
14
|
+
"""Return a welcome message."""
|
|
15
|
+
return {"message": "Welcome to the API! Try accessing /docs with the credentials."}
|
|
16
|
+
|
|
17
|
+
@app.get("/users")
|
|
18
|
+
def get_users():
|
|
19
|
+
"""Get a list of sample users."""
|
|
20
|
+
return [
|
|
21
|
+
{"id": 1, "name": "User 1"},
|
|
22
|
+
{"id": 2, "name": "User 2"},
|
|
23
|
+
{"id": 3, "name": "User 3"}
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
# Protect the docs with DocShield
|
|
27
|
+
DocShield(
|
|
28
|
+
app=app,
|
|
29
|
+
credentials={
|
|
30
|
+
"admin": "password123", # Add your credentials here
|
|
31
|
+
"developer": "dev456" # You can add multiple credential pairs
|
|
32
|
+
}
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
# Run with: uvicorn basic_example:app --reload
|
|
36
|
+
# Then try accessing:
|
|
37
|
+
# - http://localhost:8000/docs (will require authentication)
|
|
38
|
+
# - http://localhost:8000/redoc (will require authentication)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Simple test script to manually verify DocShield functionality.
|
|
3
|
+
Run with:
|
|
4
|
+
uvicorn manual_test:app --reload
|
|
5
|
+
"""
|
|
6
|
+
from fastapi import FastAPI, Depends, HTTPException
|
|
7
|
+
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
|
|
8
|
+
from fastapi_docshield import DocShield
|
|
9
|
+
|
|
10
|
+
app = FastAPI(
|
|
11
|
+
title="DocShield Test App",
|
|
12
|
+
description="Testing API for DocShield",
|
|
13
|
+
version="0.1.0",
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
# Add a simple route
|
|
17
|
+
@app.get("/")
|
|
18
|
+
def read_root():
|
|
19
|
+
return {"message": "Welcome to the test API. Try accessing /docs with credentials."}
|
|
20
|
+
|
|
21
|
+
# Protect docs with DocShield
|
|
22
|
+
shield = DocShield(
|
|
23
|
+
app=app,
|
|
24
|
+
credentials={
|
|
25
|
+
"admin": "password123",
|
|
26
|
+
"user": "user456"
|
|
27
|
+
}
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
# To test:
|
|
31
|
+
# 1. Run the server: uvicorn manual_test:app --reload
|
|
32
|
+
# 2. Try accessing the docs endpoints without credentials
|
|
33
|
+
# - http://localhost:8000/docs
|
|
34
|
+
# - http://localhost:8000/redoc
|
|
35
|
+
# - http://localhost:8000/openapi.json
|
|
36
|
+
# All should prompt for Basic Auth
|
|
37
|
+
# 3. Enter correct credentials (admin/password123 or user/user456)
|
|
38
|
+
# All should show the respective documentation
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""
|
|
2
|
+
FastAPI DocShield - A simple package to protect FastAPI documentation endpoints with authentication.
|
|
3
|
+
|
|
4
|
+
This package provides an easy way to secure FastAPI's built-in documentation endpoints
|
|
5
|
+
(/docs, /redoc, /openapi.json) with HTTP Basic Authentication.
|
|
6
|
+
|
|
7
|
+
Author: George Khananaev
|
|
8
|
+
License: MIT License
|
|
9
|
+
Copyright (c) 2025 George Khananaev
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from .docshield import DocShield
|
|
13
|
+
|
|
14
|
+
__version__ = "0.1.0"
|
|
15
|
+
__author__ = "George Khananaev"
|
|
16
|
+
__license__ = "MIT"
|
|
17
|
+
__copyright__ = "Copyright (c) 2025 George Khananaev"
|
|
18
|
+
__all__ = ["DocShield"]
|