fastapi-payloadshield 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.
Files changed (29) hide show
  1. fastapi_payloadshield-1.0.0/LICENSE +133 -0
  2. fastapi_payloadshield-1.0.0/MANIFEST.in +6 -0
  3. fastapi_payloadshield-1.0.0/PKG-INFO +364 -0
  4. fastapi_payloadshield-1.0.0/README.md +327 -0
  5. fastapi_payloadshield-1.0.0/document/COMPLETION_SUMMARY.md +426 -0
  6. fastapi_payloadshield-1.0.0/document/CUSTOM_HANDLERS.md +391 -0
  7. fastapi_payloadshield-1.0.0/document/DELIVERY_CHECKLIST.md +386 -0
  8. fastapi_payloadshield-1.0.0/document/DEVELOPMENT.md +240 -0
  9. fastapi_payloadshield-1.0.0/document/FINAL_SUMMARY.md +369 -0
  10. fastapi_payloadshield-1.0.0/document/PACKAGE_SUMMARY.md +361 -0
  11. fastapi_payloadshield-1.0.0/document/PUBLICATION_READY.md +337 -0
  12. fastapi_payloadshield-1.0.0/document/PUBLISHING_GUIDE.md +226 -0
  13. fastapi_payloadshield-1.0.0/document/QUICKSTART.md +212 -0
  14. fastapi_payloadshield-1.0.0/document/QUICK_REFERENCE.md +367 -0
  15. fastapi_payloadshield-1.0.0/document/REFACTORING_SUMMARY.md +306 -0
  16. fastapi_payloadshield-1.0.0/examples/__init__.py +5 -0
  17. fastapi_payloadshield-1.0.0/examples/example_app.py +233 -0
  18. fastapi_payloadshield-1.0.0/examples/test_client.py +221 -0
  19. fastapi_payloadshield-1.0.0/fastapi_payloadshield/__init__.py +43 -0
  20. fastapi_payloadshield-1.0.0/fastapi_payloadshield/crypto.py +206 -0
  21. fastapi_payloadshield-1.0.0/fastapi_payloadshield/decorators.py +229 -0
  22. fastapi_payloadshield-1.0.0/fastapi_payloadshield.egg-info/PKG-INFO +364 -0
  23. fastapi_payloadshield-1.0.0/fastapi_payloadshield.egg-info/SOURCES.txt +27 -0
  24. fastapi_payloadshield-1.0.0/fastapi_payloadshield.egg-info/dependency_links.txt +1 -0
  25. fastapi_payloadshield-1.0.0/fastapi_payloadshield.egg-info/requires.txt +6 -0
  26. fastapi_payloadshield-1.0.0/fastapi_payloadshield.egg-info/top_level.txt +1 -0
  27. fastapi_payloadshield-1.0.0/pyproject.toml +51 -0
  28. fastapi_payloadshield-1.0.0/setup.cfg +4 -0
  29. fastapi_payloadshield-1.0.0/setup.py +46 -0
@@ -0,0 +1,133 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+
4
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
5
+
6
+ 1. Definitions.
7
+
8
+ "License" shall mean the terms and conditions for use, reproduction,
9
+ and distribution as defined in Sections 1 through 9 of this document.
10
+
11
+ "Licensor" shall mean the copyright owner or entity authorized by
12
+ the copyright owner that is granting the License.
13
+
14
+ "Legal Entity" shall mean the union of the acting entity and all
15
+ other entities that control, are controlled by, or are under common
16
+ control with that entity. For the purposes of this definition,
17
+ "control" means (i) the power, direct or indirect, to cause the
18
+ direction or management of such entity, whether by contract or
19
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
20
+ outstanding shares, or (iii) beneficial ownership of such entity.
21
+
22
+ "You" (or "Your") shall mean an individual or Legal Entity exercising
23
+ permissions granted by this License.
24
+
25
+ "Source" form shall mean the preferred form for making modifications,
26
+ including but not limited to software source code, documentation
27
+ source, and configuration files.
28
+
29
+ "Object" form shall mean any form resulting from mechanical
30
+ transformation or translation of a Source form, including but
31
+ not limited to compiled object code, generated documentation,
32
+ and conversions to other media types.
33
+
34
+ "Work" shall mean the work of authorship, whether in Source or Object
35
+ form, made available under the License, including but not limited to
36
+ the software source code, documentation source, and configuration files.
37
+
38
+ "Derivative Works" shall mean any work, whether in Source or Object
39
+ form, that is based on (or derived from) the Work and for which the
40
+ editorial revisions, annotations, elaborations, or other modifications
41
+ represent, as a whole, an original work of authorship.
42
+
43
+ "Contribution" shall mean any work of authorship, including
44
+ the original version of the Work and any modifications thereof
45
+ or derivative works thereof.
46
+
47
+ "Contributor" shall mean Licensor and any individual or Legal Entity
48
+ on behalf of whom a Contribution has been received by Licensor and
49
+ subsequently incorporated within the Work.
50
+
51
+ "Licensed Patents" shall mean the patent claims licensable by Licensor
52
+ that are necessarily infringed by the use or sale of either the Work
53
+ or Contribution alone or by combination with the Work.
54
+
55
+ 2. Grant of Copyright License. Subject to the terms and conditions of
56
+ this License, each Contributor hereby grants to You a perpetual,
57
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
58
+ copyright license to reproduce, prepare Derivative Works of,
59
+ publicly display, publicly perform, sublicense, and distribute the
60
+ Work and such Derivative Works in Source or Object form.
61
+
62
+ 3. Grant of Patent License. Subject to the terms and conditions of
63
+ this License, each Contributor hereby grants to You a perpetual,
64
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
65
+ (except as stated in the License) patent license to make, have made,
66
+ use, offer to sell, sell, import, and otherwise transfer the Work.
67
+
68
+ 4. Redistribution. You may reproduce and distribute copies of the
69
+ Work or Derivative Works thereof in any medium, with or without
70
+ modifications, and in Source or Object form, provided that You
71
+ meet the following conditions:
72
+
73
+ (a) You must give any other recipients of the Work or
74
+ Derivative Works a copy of this License; and
75
+
76
+ (b) You must cause any modified files to carry prominent notices
77
+ stating that You changed the files; and
78
+
79
+ (c) You must retain, in the Source form of any Derivative Works
80
+ that You distribute, all copyright, patent, trademark, and
81
+ attribution notices from the Source form of the Work; and
82
+
83
+ (d) If the Work includes a "NOTICE" text file, then any
84
+ Derivative Works that You distribute must include a readable
85
+ copy of the attribution notices contained within such NOTICE file.
86
+
87
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
88
+ any Contribution intentionally submitted for inclusion in the Work
89
+ by You to Licensor shall be under the terms and conditions of
90
+ this License, without any additional terms or conditions.
91
+
92
+ 6. Trademarks. This License does not grant permission to use the trade
93
+ names, trademarks, service marks, or product names of the Licensor,
94
+ except as required for reasonable and customary use in describing the
95
+ origin of the Work and reproducing the content of the NOTICE file.
96
+
97
+ 7. Disclaimer of Warranty. Unless required by applicable law or
98
+ agreed to in writing, Licensor provides the Work (and each
99
+ Contributor provides its Contributions) on an "AS IS" BASIS,
100
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
101
+ or implied, including, without limitation, any warranties or
102
+ conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or
103
+ FITNESS FOR A PARTICULAR PURPOSE.
104
+
105
+ 8. Limitation of Liability. In no event and under no legal theory,
106
+ whether in tort (including negligence), contract, or otherwise,
107
+ unless required by applicable law (such as deliberate and grossly
108
+ negligent acts) or agreed to in writing, shall any Contributor be
109
+ liable to You for damages, including any direct, indirect, special,
110
+ incidental, or consequential damages of any character arising as a
111
+ result of this License or out of the use or inability to use the
112
+ Work.
113
+
114
+ 9. Accepting Warranty or Additional Liability. While redistributing
115
+ the Work or Derivative Works thereof, You may choose to offer,
116
+ and charge a fee for, acceptance of support, warranty, indemnity,
117
+ or other liability obligations and/or rights consistent with this License.
118
+
119
+ END OF TERMS AND CONDITIONS
120
+
121
+ Copyright 2024 FastAPI Payload Shield Contributors
122
+
123
+ Licensed under the Apache License, Version 2.0 (the "License");
124
+ you may not use this file except in compliance with the License.
125
+ You may obtain a copy of the License at
126
+
127
+ http://www.apache.org/licenses/LICENSE-2.0
128
+
129
+ Unless required by applicable law or agreed to in writing, software
130
+ distributed under the License is distributed on an "AS IS" BASIS,
131
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132
+ See the License for the specific language governing permissions and
133
+ limitations under the License.
@@ -0,0 +1,6 @@
1
+ include README.md
2
+ include LICENSE
3
+ include pyproject.toml
4
+ recursive-include examples *.py
5
+ recursive-include tests *.py
6
+ recursive-include document *.md
@@ -0,0 +1,364 @@
1
+ Metadata-Version: 2.4
2
+ Name: fastapi_payloadshield
3
+ Version: 1.0.0
4
+ Summary: FastAPI decorators for automatic base64 encryption/decryption of request/response payloads
5
+ Home-page: https://github.com/PayloadShield/FastAPIPS
6
+ Author: Ganesh Kandu
7
+ Author-email: Ganesh Kandu <kanduganesh@gmail.com>
8
+ License: Apache-2.0
9
+ Project-URL: Homepage, https://github.com/PayloadShield/FastAPIPS
10
+ Project-URL: Repository, https://github.com/PayloadShield/FastAPIPS.git
11
+ Project-URL: Issues, https://github.com/PayloadShield/FastAPIPS/issues
12
+ Keywords: fastapi,base64,crypto,encryption,decorator
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.7
15
+ Classifier: Programming Language :: Python :: 3.8
16
+ Classifier: Programming Language :: Python :: 3.9
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: License :: OSI Approved :: Apache Software License
20
+ Classifier: Operating System :: OS Independent
21
+ Classifier: Development Status :: 4 - Beta
22
+ Classifier: Intended Audience :: Developers
23
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
24
+ Classifier: Framework :: FastAPI
25
+ Requires-Python: >=3.7
26
+ Description-Content-Type: text/markdown
27
+ License-File: LICENSE
28
+ Requires-Dist: fastapi>=0.68.0
29
+ Requires-Dist: starlette>=0.19.0
30
+ Provides-Extra: dev
31
+ Requires-Dist: pytest>=6.0; extra == "dev"
32
+ Requires-Dist: pytest-asyncio>=0.18.0; extra == "dev"
33
+ Dynamic: author
34
+ Dynamic: home-page
35
+ Dynamic: license-file
36
+ Dynamic: requires-python
37
+
38
+ # FastAPI Payload Shield
39
+
40
+ Lightweight FastAPI decorators for automatic encryption/decryption of request and response payloads. **Supports pluggable encryption handlers** - easily add new encryption types like AES, Fernet, or custom algorithms!
41
+
42
+ ## 🎯 Key Features
43
+
44
+ - 🔒 **Flexible Encryption**: Multiple encryption types (base64, AES, Fernet, custom)
45
+ - 🔓 **Automatic Decryption**: Decrypt incoming requests automatically
46
+ - 📝 **JSON-Friendly**: Works seamlessly with JSON requests and responses
47
+ - 🎯 **Route-Agnostic**: No changes needed to your existing route logic
48
+ - ⚡ **Lightweight**: Minimal dependencies and overhead
49
+ - 🚀 **Easy Integration**: Just add decorators to your routes
50
+ - 🧩 **Pluggable**: Create custom encryption handlers easily
51
+
52
+ ## Installation
53
+
54
+ ### From Local Development
55
+ ```bash
56
+ cd FastAPIPS
57
+ pip install -e .
58
+ ```
59
+
60
+ ### From PyPI (when published)
61
+ ```bash
62
+ pip install fastapi_payloadshield
63
+ ```
64
+
65
+ ## Quick Start
66
+
67
+ ### Basic Usage with Base64
68
+
69
+ ```python
70
+ from fastapi import FastAPI
71
+ from fastapi_payloadshield import PayloadShieldEnc, PayloadShieldDec, PayloadShield
72
+
73
+ app = FastAPI()
74
+
75
+ # Encrypt response only
76
+ @app.get("/api/data")
77
+ @PayloadShieldEnc("base64")
78
+ async def get_data():
79
+ return {"message": "hello", "data": "world"}
80
+
81
+ # Decrypt request only
82
+ @app.post("/api/process")
83
+ @PayloadShieldDec("base64")
84
+ async def process_data(data: dict):
85
+ return {"received": data, "status": "success"}
86
+
87
+ @app.post("/api/secure")
88
+ @PayloadShield("base64")
89
+ async def secure_endpoint(data: dict):
90
+ return {"processed": data}
91
+ ```
92
+
93
+ ## Decorators
94
+
95
+ ### `@PayloadShieldEnc(encryption_type)`
96
+
97
+ Encrypts the response payload.
98
+
99
+ ```python
100
+ @app.get("/api/users")
101
+ @PayloadShieldEnc("base64")
102
+ async def get_users():
103
+ return [{"id": 1, "name": "Alice"}]
104
+
105
+ # Response: {"encrypted": "W3siaWQiOiAxLCAibmFtZSI6ICJBbGljZSJ9XQ=="}
106
+ ```
107
+
108
+ ### `@PayloadShieldDec(encryption_type)`
109
+
110
+ Decrypts the request payload.
111
+
112
+ ```python
113
+ @app.post("/api/login")
114
+ @PayloadShieldDec("base64")
115
+ async def login(credentials: dict):
116
+ return {"status": "success"}
117
+
118
+ # Expects: {"encrypted": "base64_encoded_json"}
119
+ ```
120
+
121
+ ### `@PayloadShield(encryption_type)`
122
+
123
+ Combined encryption and decryption
124
+
125
+ ```python
126
+ @app.post("/api/secure")
127
+ @PayloadShield("base64")
128
+ async def secure_endpoint(data: dict):
129
+ return {"processed": data}
130
+
131
+ # Expects: {"encrypted": "encrypted_data"}
132
+ # Returns: {"encrypted": "encrypted_data"}
133
+ ```
134
+
135
+ ## Advanced Example: Multiple Encryption Types
136
+
137
+ ```python
138
+ from fastapi import FastAPI
139
+ from fastapi_payloadshield import PayloadShield, register_handler, EncryptionHandler
140
+ from cryptography.fernet import Fernet
141
+ import json
142
+
143
+ app = FastAPI()
144
+
145
+ # Create Fernet handler
146
+ class FernetHandler(EncryptionHandler):
147
+ def __init__(self, key):
148
+ self.cipher = Fernet(key)
149
+
150
+ def encode(self, data):
151
+ return self.cipher.encrypt(json.dumps(data).encode()).decode()
152
+
153
+ def decode(self, encoded_data):
154
+ return json.loads(self.cipher.decrypt(encoded_data.encode()))
155
+
156
+ # Register
157
+ key = Fernet.generate_key()
158
+ register_handler("fernet", FernetHandler(key))
159
+
160
+ # Use different encryption for different endpoints
161
+ @app.post("/api/public")
162
+ @PayloadShield("base64") # Light encryption
163
+ async def public_endpoint(data: dict):
164
+ return data
165
+
166
+ @app.post("/api/private")
167
+ @PayloadShield("fernet") # Strong encryption
168
+ async def private_endpoint(data: dict):
169
+ return data
170
+ ```
171
+
172
+ ## How It Works
173
+
174
+ ### Request Decryption Flow
175
+ 1. Client sends: `{"encrypted": "encrypted_data"}`
176
+ 2. `@PayloadShieldDec` decorator intercepts
177
+ 3. Decrypts using specified handler
178
+ 4. Route receives: `{"key": "value"}` (normal dict)
179
+
180
+ ### Response Encryption Flow
181
+ 1. Route returns: `{"key": "value"}`
182
+ 2. `@PayloadShieldEnc` decorator intercepts
183
+ 3. Encrypts using specified handler
184
+ 4. Client receives: `{"encrypted": "encrypted_data"}`
185
+
186
+ ## Testing
187
+
188
+ ### Run Example Application
189
+ ```bash
190
+ python examples/example_app.py
191
+ ```
192
+
193
+ ### Run Test Client
194
+ ```bash
195
+ python examples/test_client.py
196
+ ```
197
+
198
+ ### Manual Test with cURL
199
+
200
+ ```bash
201
+ # Encrypt test data
202
+ echo '{"username":"admin"}' | base64
203
+ # eyJ1c2VybmFtZSI6ImFkbWluIn0=
204
+
205
+ # Send encrypted request
206
+ curl -X POST http://localhost:8000/api/login \
207
+ -H "Content-Type: application/json" \
208
+ -d '{"encrypted":"eyJ1c2VybmFtZSI6ImFkbWluIn0="}'
209
+ ```
210
+
211
+ ### Testing with Python
212
+ ```python
213
+ import requests
214
+ import json
215
+ import base64
216
+
217
+ # Encode request
218
+ data = {"username": "admin", "password": "secret"}
219
+ json_str = json.dumps(data)
220
+ encrypted = base64.b64encode(json_str.encode()).decode()
221
+
222
+ # Send request
223
+ response = requests.post(
224
+ "http://localhost:8000/api/login",
225
+ json={"encrypted": encrypted}
226
+ )
227
+
228
+ # Decode response
229
+ encrypted_response = response.json()["encrypted"]
230
+ decrypted = json.loads(base64.b64decode(encrypted_response).decode())
231
+ print(decrypted)
232
+ # {'status': 'success', 'token': 'abc123'}
233
+ ```
234
+
235
+ ## Creating Custom Encryption Handlers
236
+
237
+ See [CUSTOM_HANDLERS.md](CUSTOM_HANDLERS.md) for detailed guide on:
238
+
239
+ - Creating custom handlers
240
+ - Fernet encryption example
241
+ - AES encryption example
242
+ - Best practices
243
+ - Performance tips
244
+ - Security considerations
245
+
246
+ ## Backward Compatibility
247
+
248
+ Old decorator names still work:
249
+
250
+ ```python
251
+ from fastapi_payloadshield import encrypt_response, decrypt_request, crypto_middleware
252
+
253
+ # These are equivalent to:
254
+ # PayloadShieldEnc("base64")
255
+ # PayloadShieldDec("base64")
256
+ # PayloadShield("base64")
257
+
258
+ @app.get("/api/data")
259
+ @encrypt_response
260
+ async def get_data():
261
+ return {"data": "value"}
262
+ ```
263
+
264
+ ## API Reference
265
+
266
+ ### Decorators
267
+
268
+ | Decorator | Purpose |
269
+ |-----------|---------|
270
+ | `PayloadShieldEnc(type)` | Encrypt response |
271
+ | `PayloadShieldDec(type)` | Decrypt request |
272
+ | `PayloadShield(type)` | Both encrypt & decrypt |
273
+
274
+ ### Functions
275
+
276
+ | Function | Purpose |
277
+ |----------|---------|
278
+ | `register_handler(name, handler)` | Register custom encryption handler |
279
+ | `get_handler(name)` | Get handler by name |
280
+ | `EncryptionHandler` | Base class for handlers |
281
+
282
+ ### Built-in Handlers
283
+
284
+ | Handler | Type | Security | Use Case |
285
+ |---------|------|----------|----------|
286
+ | `base64` | Encoding | None | Obfuscation, development |
287
+
288
+ ## Error Handling
289
+
290
+ The decorators include built-in error handling:
291
+
292
+ ```python
293
+ # Invalid encrypted data
294
+ # Response: {"error": "Failed to decrypt request: ..."}
295
+
296
+ # Missing encryption handler
297
+ # Response: ValueError: Encryption handler 'xyz' not found. Available: base64, fernet
298
+ ```
299
+
300
+ ## Performance Considerations
301
+
302
+ - **Caching**: Handler instances are cached
303
+ - **Compression**: Consider compressing before encryption for large payloads
304
+ - **Async**: All operations are async-friendly
305
+
306
+ ## Requirements
307
+
308
+ - Python 3.7+
309
+ - FastAPI 0.68+
310
+ - Starlette 0.19+
311
+
312
+ ## Files Included
313
+
314
+ - `fastapi_payloadshield/` - Main package
315
+ - `__init__.py` - Exports decorators and handlers
316
+ - `crypto.py` - Encryption handlers
317
+ - `decorators.py` - FastAPI decorators
318
+ - `examples/` - Working examples
319
+ - `example_app.py` - Full-featured demo
320
+ - `test_client.py` - Test/client script
321
+ - `README.md` - This file
322
+ - `QUICKSTART.md` - Quick start guide
323
+ - `CUSTOM_HANDLERS.md` - Creating custom handlers
324
+ - `DEVELOPMENT.md` - Development guide
325
+
326
+ ## License
327
+
328
+ Apache-2.0 - See LICENSE file for details
329
+
330
+ ## Contributing
331
+
332
+ Contributions welcome! Areas for contribution:
333
+
334
+ 1. New encryption handlers (AES, Fernet, etc.)
335
+ 2. Performance optimizations
336
+ 3. Documentation improvements
337
+ 4. Test coverage
338
+ 5. Examples
339
+
340
+ ## Support
341
+
342
+ - 📖 Full guide: [README.md](README.md)
343
+ - ⚡ Quick start: [QUICKSTART.md](QUICKSTART.md)
344
+ - 🧩 Custom handlers: [CUSTOM_HANDLERS.md](CUSTOM_HANDLERS.md)
345
+ - 🛠️ Development: [DEVELOPMENT.md](DEVELOPMENT.md)
346
+
347
+ ---
348
+
349
+ **Happy encrypting!** 🔒
350
+
351
+ ## Why Payload Shield?
352
+
353
+ This package was designed with extensibility in mind. Unlike static encryption libraries, Payload Shield lets you:
354
+
355
+ - Mix and match encryption types in the same app
356
+ - Add new encryption types without touching core code
357
+ - Keep route logic clean and simple
358
+ - Support multiple security levels
359
+
360
+ Perfect for:
361
+ - Building multi-tier security APIs
362
+ - Migrating from one encryption to another
363
+ - Testing different encryption strategies
364
+ - Production systems requiring flexible crypto