py-aide 0.0.3__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.
- py_aide-0.0.3/LICENSE +21 -0
- py_aide-0.0.3/PKG-INFO +118 -0
- py_aide-0.0.3/README.md +83 -0
- py_aide-0.0.3/pyproject.toml +51 -0
- py_aide-0.0.3/setup.cfg +4 -0
- py_aide-0.0.3/src/py_aide/__init__.py +41 -0
- py_aide-0.0.3/src/py_aide/auth/__init__.py +2 -0
- py_aide-0.0.3/src/py_aide/auth/legacy.py +95 -0
- py_aide-0.0.3/src/py_aide/auth/password.py +498 -0
- py_aide-0.0.3/src/py_aide/cache/__init__.py +1 -0
- py_aide-0.0.3/src/py_aide/cache/cacheConfig.py +62 -0
- py_aide-0.0.3/src/py_aide/cache/cacheEngine.py +99 -0
- py_aide-0.0.3/src/py_aide/cache/cacheTransaction.py +68 -0
- py_aide-0.0.3/src/py_aide/cache/core.py +658 -0
- py_aide-0.0.3/src/py_aide/cache/memory.py +41 -0
- py_aide-0.0.3/src/py_aide/cache/persistence.py +604 -0
- py_aide-0.0.3/src/py_aide/codes.py +54 -0
- py_aide-0.0.3/src/py_aide/customTypes.py +21 -0
- py_aide-0.0.3/src/py_aide/database.py +780 -0
- py_aide-0.0.3/src/py_aide/dates.py +213 -0
- py_aide-0.0.3/src/py_aide/encryption/__init__.py +2 -0
- py_aide-0.0.3/src/py_aide/encryption/codec.py +38 -0
- py_aide-0.0.3/src/py_aide/encryption/keygen.py +29 -0
- py_aide-0.0.3/src/py_aide/enforcer.py +120 -0
- py_aide-0.0.3/src/py_aide/figures.py +305 -0
- py_aide-0.0.3/src/py_aide/images.py +77 -0
- py_aide-0.0.3/src/py_aide/privacy.py +0 -0
- py_aide-0.0.3/src/py_aide/response.py +60 -0
- py_aide-0.0.3/src/py_aide/servers/__init__.py +16 -0
- py_aide-0.0.3/src/py_aide/servers/breaker.py +791 -0
- py_aide-0.0.3/src/py_aide/servers/fastApi.py +1283 -0
- py_aide-0.0.3/src/py_aide/servers/flask.py +1902 -0
- py_aide-0.0.3/src/py_aide/servers/gate.py +1352 -0
- py_aide-0.0.3/src/py_aide/servers/websockets/__init__.py +6 -0
- py_aide-0.0.3/src/py_aide/servers/websockets/config.py +613 -0
- py_aide-0.0.3/src/py_aide/servers/websockets/engine.py +1335 -0
- py_aide-0.0.3/src/py_aide/servers/websockets/limits.py +1435 -0
- py_aide-0.0.3/src/py_aide/servers/websockets/metrics.py +1750 -0
- py_aide-0.0.3/src/py_aide/servers/websockets/protocol.py +1531 -0
- py_aide-0.0.3/src/py_aide/servers/websockets/security.py +1200 -0
- py_aide-0.0.3/src/py_aide/sqlite.py +231 -0
- py_aide-0.0.3/src/py_aide/storage.py +149 -0
- py_aide-0.0.3/src/py_aide/structures.py +148 -0
- py_aide-0.0.3/src/py_aide/threading/__init__.py +2 -0
- py_aide-0.0.3/src/py_aide/threading/executor.py +206 -0
- py_aide-0.0.3/src/py_aide/threading/scheduler.py +138 -0
- py_aide-0.0.3/src/py_aide/tokens.py +113 -0
- py_aide-0.0.3/src/py_aide.egg-info/PKG-INFO +118 -0
- py_aide-0.0.3/src/py_aide.egg-info/SOURCES.txt +62 -0
- py_aide-0.0.3/src/py_aide.egg-info/dependency_links.txt +1 -0
- py_aide-0.0.3/src/py_aide.egg-info/requires.txt +15 -0
- py_aide-0.0.3/src/py_aide.egg-info/top_level.txt +1 -0
- py_aide-0.0.3/tests/test_codes.py +34 -0
- py_aide-0.0.3/tests/test_custom_types.py +47 -0
- py_aide-0.0.3/tests/test_dates.py +41 -0
- py_aide-0.0.3/tests/test_db.py +132 -0
- py_aide-0.0.3/tests/test_dsl.py +55 -0
- py_aide-0.0.3/tests/test_encryption.py +58 -0
- py_aide-0.0.3/tests/test_enforcer.py +37 -0
- py_aide-0.0.3/tests/test_executor.py +70 -0
- py_aide-0.0.3/tests/test_images.py +65 -0
- py_aide-0.0.3/tests/test_path.py +34 -0
- py_aide-0.0.3/tests/test_response.py +43 -0
- py_aide-0.0.3/tests/test_validator.py +60 -0
py_aide-0.0.3/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kakuru Douglas
|
|
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.
|
py_aide-0.0.3/PKG-INFO
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: py_aide
|
|
3
|
+
Version: 0.0.3
|
|
4
|
+
Summary: Modern Python 3.11+ Framework: Flask/FastAPI orchestration, and strict runtime enforcement.
|
|
5
|
+
Author-email: Kakuru Douglas <vicaniddouglas@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://gitlab.com/vicaniddouglas/py_aide
|
|
8
|
+
Project-URL: Bug Tracker, https://gitlab.com/vicaniddouglas/py_aide/-/issues
|
|
9
|
+
Keywords: framework,flask,fastapi,runtime-enforcement,threading,websockets
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
16
|
+
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
|
|
17
|
+
Requires-Python: >=3.11
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
License-File: LICENSE
|
|
20
|
+
Requires-Dist: flask>=3.0.0
|
|
21
|
+
Requires-Dist: flask-cors>=4.0.0
|
|
22
|
+
Requires-Dist: flask-socketio>=5.3.6
|
|
23
|
+
Requires-Dist: eventlet>=0.33.3
|
|
24
|
+
Requires-Dist: fastapi>=0.100.0
|
|
25
|
+
Requires-Dist: cryptography>=41.0.0
|
|
26
|
+
Requires-Dist: argon2-cffi>=23.1.0
|
|
27
|
+
Requires-Dist: pydantic>=1.10.0
|
|
28
|
+
Provides-Extra: dev
|
|
29
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
30
|
+
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
|
|
31
|
+
Requires-Dist: black>=23.0.0; extra == "dev"
|
|
32
|
+
Requires-Dist: isort>=5.12.0; extra == "dev"
|
|
33
|
+
Requires-Dist: mypy>=1.5.0; extra == "dev"
|
|
34
|
+
Dynamic: license-file
|
|
35
|
+
|
|
36
|
+
# py_aide
|
|
37
|
+
|
|
38
|
+
**Modern Python 3.11+ Framework: Flask/FastAPI Orchestration & Strict Runtime Enforcement**
|
|
39
|
+
|
|
40
|
+
`py_aide` is a robust, developer-centric framework designed to bring safety, structure, and consistency to Python web applications. It provides a unique "Strict Runtime Enforcement" layer that ensures your code remains clean, documented, and type-safe at execution time.
|
|
41
|
+
|
|
42
|
+
## 🚀 Key Features
|
|
43
|
+
|
|
44
|
+
- **Strict Runtime Enforcement**: A powerful decorator that enforces type hints, docstrings, and calling conventions (positional-only/keyword-only) at runtime.
|
|
45
|
+
- **Unified Server Portal**: Seamlessly orchestrate Flask and FastAPI applications with shared security gates and auto-discovery of routes.
|
|
46
|
+
- **WebSocket Excellence**: Built-in support for WebSockets (powered by Eventlet and Flask-SocketIO) with integrated security and metrics.
|
|
47
|
+
- **Thread-Safe SQL**: A thread-level multiton SQLite manager with automatic JSON serialization, transaction tracking, and schema management.
|
|
48
|
+
- **Modern Security**: First-class support for Bearer tokens, API keys, and Fernet-based encryption.
|
|
49
|
+
- **Rich Utilities**: Built-in handlers for images (base64/files), dates (aware/naive conversions), and custom data structures.
|
|
50
|
+
|
|
51
|
+
## 📦 Installation
|
|
52
|
+
```bash
|
|
53
|
+
pip install py_aide
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
> **Note**: `py_aide` requires Python 3.11+ and is currently optimized for Linux environments.
|
|
57
|
+
|
|
58
|
+
## ⚙️ Core Philosophy: Strict Enforcement
|
|
59
|
+
|
|
60
|
+
At the heart of `py_aide` is the `@enforce_requirements` decorator. It's designed to prevent "sloppy" code by failing early if:
|
|
61
|
+
|
|
62
|
+
- A function is missing a docstring.
|
|
63
|
+
- A parameter or return value is missing a type hint.
|
|
64
|
+
- A function uses more than 8 arguments (promoting better decomposition).
|
|
65
|
+
- Calling conventions (`/` or `*`) are not explicitly defined.
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
from py_aide.enforcer import enforce_requirements
|
|
69
|
+
|
|
70
|
+
@enforce_requirements
|
|
71
|
+
def create_user(name: str, age: int, /) -> dict:
|
|
72
|
+
"""Creates a new user dict."""
|
|
73
|
+
return {"name": name, "age": age}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## 🌐 Unified Server Example (Flask)
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
from py_aide.servers.flask import ServicePortal, GateConfig
|
|
80
|
+
|
|
81
|
+
portal = ServicePortal()
|
|
82
|
+
|
|
83
|
+
@portal.endpoint("/api/greet", gate=GateConfig(auth_required=False))
|
|
84
|
+
def greet(name: str, /) -> dict:
|
|
85
|
+
"""Returns a greeting message."""
|
|
86
|
+
return {"message": f"Hello, {name}!"}
|
|
87
|
+
|
|
88
|
+
if __name__ == "__main__":
|
|
89
|
+
portal.run(port=5000)
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## 🗄️ Database Management
|
|
93
|
+
|
|
94
|
+
`py_aide` provides a thread-local multiton pattern for SQLite, ensuring each thread has its own connection while sharing the same configuration.
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
from py_aide.database import Api
|
|
98
|
+
|
|
99
|
+
db_config = {
|
|
100
|
+
'users': 'id INTEGER PRIMARY KEY, name TEXT, meta JSON'
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
with Api(db_path="data.db", tables=db_config) as db:
|
|
104
|
+
db.insert(table="users", data=[(1, "Alice", {"role": "admin"})])
|
|
105
|
+
result = db.fetch(table="users", columns=["name", "meta::role as role"])
|
|
106
|
+
print(result.data) # [{'name': 'Alice', 'role': 'admin'}]
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## ⚠️ Important Note: Eventlet Monkey Patching
|
|
110
|
+
|
|
111
|
+
By default, importing `src.py_aide` (or the top-level package) **immediately** performs `eventlet.monkey_patch()`. This is required for reliable WebSocket support and some threading features. If you need to avoid this side-effect, ensure you understand the dependencies of your modules.
|
|
112
|
+
|
|
113
|
+
## 📄 License
|
|
114
|
+
This project is licensed under the **MIT License**.
|
|
115
|
+
See the [LICENSE](LICENSE) file for more details.
|
|
116
|
+
|
|
117
|
+
## 👥 Authors
|
|
118
|
+
- **Kakuru Douglas** - [vicaniddouglas@gmail.com](mailto:vicaniddouglas@gmail.com)
|
py_aide-0.0.3/README.md
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# py_aide
|
|
2
|
+
|
|
3
|
+
**Modern Python 3.11+ Framework: Flask/FastAPI Orchestration & Strict Runtime Enforcement**
|
|
4
|
+
|
|
5
|
+
`py_aide` is a robust, developer-centric framework designed to bring safety, structure, and consistency to Python web applications. It provides a unique "Strict Runtime Enforcement" layer that ensures your code remains clean, documented, and type-safe at execution time.
|
|
6
|
+
|
|
7
|
+
## 🚀 Key Features
|
|
8
|
+
|
|
9
|
+
- **Strict Runtime Enforcement**: A powerful decorator that enforces type hints, docstrings, and calling conventions (positional-only/keyword-only) at runtime.
|
|
10
|
+
- **Unified Server Portal**: Seamlessly orchestrate Flask and FastAPI applications with shared security gates and auto-discovery of routes.
|
|
11
|
+
- **WebSocket Excellence**: Built-in support for WebSockets (powered by Eventlet and Flask-SocketIO) with integrated security and metrics.
|
|
12
|
+
- **Thread-Safe SQL**: A thread-level multiton SQLite manager with automatic JSON serialization, transaction tracking, and schema management.
|
|
13
|
+
- **Modern Security**: First-class support for Bearer tokens, API keys, and Fernet-based encryption.
|
|
14
|
+
- **Rich Utilities**: Built-in handlers for images (base64/files), dates (aware/naive conversions), and custom data structures.
|
|
15
|
+
|
|
16
|
+
## 📦 Installation
|
|
17
|
+
```bash
|
|
18
|
+
pip install py_aide
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
> **Note**: `py_aide` requires Python 3.11+ and is currently optimized for Linux environments.
|
|
22
|
+
|
|
23
|
+
## ⚙️ Core Philosophy: Strict Enforcement
|
|
24
|
+
|
|
25
|
+
At the heart of `py_aide` is the `@enforce_requirements` decorator. It's designed to prevent "sloppy" code by failing early if:
|
|
26
|
+
|
|
27
|
+
- A function is missing a docstring.
|
|
28
|
+
- A parameter or return value is missing a type hint.
|
|
29
|
+
- A function uses more than 8 arguments (promoting better decomposition).
|
|
30
|
+
- Calling conventions (`/` or `*`) are not explicitly defined.
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
from py_aide.enforcer import enforce_requirements
|
|
34
|
+
|
|
35
|
+
@enforce_requirements
|
|
36
|
+
def create_user(name: str, age: int, /) -> dict:
|
|
37
|
+
"""Creates a new user dict."""
|
|
38
|
+
return {"name": name, "age": age}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## 🌐 Unified Server Example (Flask)
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from py_aide.servers.flask import ServicePortal, GateConfig
|
|
45
|
+
|
|
46
|
+
portal = ServicePortal()
|
|
47
|
+
|
|
48
|
+
@portal.endpoint("/api/greet", gate=GateConfig(auth_required=False))
|
|
49
|
+
def greet(name: str, /) -> dict:
|
|
50
|
+
"""Returns a greeting message."""
|
|
51
|
+
return {"message": f"Hello, {name}!"}
|
|
52
|
+
|
|
53
|
+
if __name__ == "__main__":
|
|
54
|
+
portal.run(port=5000)
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## 🗄️ Database Management
|
|
58
|
+
|
|
59
|
+
`py_aide` provides a thread-local multiton pattern for SQLite, ensuring each thread has its own connection while sharing the same configuration.
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
from py_aide.database import Api
|
|
63
|
+
|
|
64
|
+
db_config = {
|
|
65
|
+
'users': 'id INTEGER PRIMARY KEY, name TEXT, meta JSON'
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
with Api(db_path="data.db", tables=db_config) as db:
|
|
69
|
+
db.insert(table="users", data=[(1, "Alice", {"role": "admin"})])
|
|
70
|
+
result = db.fetch(table="users", columns=["name", "meta::role as role"])
|
|
71
|
+
print(result.data) # [{'name': 'Alice', 'role': 'admin'}]
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## ⚠️ Important Note: Eventlet Monkey Patching
|
|
75
|
+
|
|
76
|
+
By default, importing `src.py_aide` (or the top-level package) **immediately** performs `eventlet.monkey_patch()`. This is required for reliable WebSocket support and some threading features. If you need to avoid this side-effect, ensure you understand the dependencies of your modules.
|
|
77
|
+
|
|
78
|
+
## 📄 License
|
|
79
|
+
This project is licensed under the **MIT License**.
|
|
80
|
+
See the [LICENSE](LICENSE) file for more details.
|
|
81
|
+
|
|
82
|
+
## 👥 Authors
|
|
83
|
+
- **Kakuru Douglas** - [vicaniddouglas@gmail.com](mailto:vicaniddouglas@gmail.com)
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "py_aide"
|
|
7
|
+
version = "0.0.3"
|
|
8
|
+
authors = [
|
|
9
|
+
{ name="Kakuru Douglas", email="vicaniddouglas@gmail.com" },
|
|
10
|
+
]
|
|
11
|
+
description = "Modern Python 3.11+ Framework: Flask/FastAPI orchestration, and strict runtime enforcement."
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
requires-python = ">=3.11"
|
|
14
|
+
keywords = ["framework", "flask", "fastapi", "runtime-enforcement", "threading", "websockets"]
|
|
15
|
+
license = {text = "MIT"}
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 4 - Beta",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"Programming Language :: Python :: 3.11",
|
|
20
|
+
"Programming Language :: Python :: 3.12",
|
|
21
|
+
"License :: OSI Approved :: MIT License",
|
|
22
|
+
"Operating System :: POSIX :: Linux",
|
|
23
|
+
"Topic :: Software Development :: Libraries :: Application Frameworks",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
dependencies = [
|
|
27
|
+
"flask>=3.0.0",
|
|
28
|
+
"flask-cors>=4.0.0",
|
|
29
|
+
"flask-socketio>=5.3.6",
|
|
30
|
+
"eventlet>=0.33.3",
|
|
31
|
+
"fastapi>=0.100.0",
|
|
32
|
+
"cryptography>=41.0.0",
|
|
33
|
+
"argon2-cffi>=23.1.0",
|
|
34
|
+
"pydantic>=1.10.0", # FastAPI uses pydantic
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
[project.urls]
|
|
38
|
+
"Homepage" = "https://gitlab.com/vicaniddouglas/py_aide"
|
|
39
|
+
"Bug Tracker" = "https://gitlab.com/vicaniddouglas/py_aide/-/issues"
|
|
40
|
+
|
|
41
|
+
[tool.setuptools.packages.find]
|
|
42
|
+
where = ["src"]
|
|
43
|
+
|
|
44
|
+
[project.optional-dependencies]
|
|
45
|
+
dev = [
|
|
46
|
+
"pytest>=7.0.0",
|
|
47
|
+
"pytest-cov>=4.0.0",
|
|
48
|
+
"black>=23.0.0",
|
|
49
|
+
"isort>=5.12.0",
|
|
50
|
+
"mypy>=1.5.0",
|
|
51
|
+
]
|
py_aide-0.0.3/setup.cfg
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"""
|
|
2
|
+
py_aide - Main package
|
|
3
|
+
This framework uses EVENTLET for WebSocket support.
|
|
4
|
+
Eventlet monkey patching happens IMMEDIATELY when this package is imported.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
# ============================================================================
|
|
8
|
+
# CRITICAL: Eventlet monkey patching - MUST be the first thing that runs
|
|
9
|
+
# ============================================================================
|
|
10
|
+
|
|
11
|
+
import sys
|
|
12
|
+
|
|
13
|
+
try:
|
|
14
|
+
import eventlet
|
|
15
|
+
eventlet.monkey_patch()
|
|
16
|
+
print(f"[py_aide] Eventlet patched successfully (Python {sys.version_info.major}.{sys.version_info.minor})")
|
|
17
|
+
except ImportError:
|
|
18
|
+
print("\n" + "="*70)
|
|
19
|
+
print("CRITICAL: Eventlet not installed")
|
|
20
|
+
print("="*70)
|
|
21
|
+
print("This framework REQUIRES eventlet for WebSocket support.")
|
|
22
|
+
print("Install it with: pip install eventlet")
|
|
23
|
+
print("If you don't need WebSockets, set enable_websocket=False")
|
|
24
|
+
print("="*70 + "\n")
|
|
25
|
+
raise # Stop execution - don't proceed without eventlet
|
|
26
|
+
|
|
27
|
+
from .response import Response, ok, error
|
|
28
|
+
from . import structures
|
|
29
|
+
from . import codes
|
|
30
|
+
from . import database
|
|
31
|
+
from . import enforcer
|
|
32
|
+
from . import encryption
|
|
33
|
+
from . import dates
|
|
34
|
+
from . import servers
|
|
35
|
+
from . import threading
|
|
36
|
+
from . import images
|
|
37
|
+
from . import tokens
|
|
38
|
+
|
|
39
|
+
from .customTypes import Definition, Options
|
|
40
|
+
from . import auth
|
|
41
|
+
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import hashlib
|
|
2
|
+
import secrets
|
|
3
|
+
import base64
|
|
4
|
+
import hmac
|
|
5
|
+
import binascii
|
|
6
|
+
|
|
7
|
+
from ..enforcer import enforce_requirements
|
|
8
|
+
|
|
9
|
+
# --- Configuration Constants ---
|
|
10
|
+
# Using 600,000 iterations (OWASP recommended baseline)
|
|
11
|
+
# Using SHA-256 as the underlying pseudo-random function
|
|
12
|
+
ALGORITHM = 'sha256'
|
|
13
|
+
ITERATIONS = 600_000
|
|
14
|
+
SALT_SIZE = 16 # 128-bit salt
|
|
15
|
+
|
|
16
|
+
@enforce_requirements
|
|
17
|
+
def hash_password(raw_password: str,/) -> str:
|
|
18
|
+
"""
|
|
19
|
+
Transforms a plain-text password into a secure, portable hash string.
|
|
20
|
+
|
|
21
|
+
Process:
|
|
22
|
+
1. Generates a unique 16-byte random salt.
|
|
23
|
+
2. Stretches the password using PBKDF2 with 600,000 rounds.
|
|
24
|
+
3. Encodes binary results to Base64 for safe database storage.
|
|
25
|
+
|
|
26
|
+
Args:
|
|
27
|
+
raw_password: The plain-text string to hash.
|
|
28
|
+
|
|
29
|
+
Returns:
|
|
30
|
+
A formatted string: pbkdf2$sha256$600000$base64_salt$base64_hash
|
|
31
|
+
"""
|
|
32
|
+
# 1. Generate a cryptographically secure random salt
|
|
33
|
+
salt = secrets.token_bytes(SALT_SIZE)
|
|
34
|
+
|
|
35
|
+
# 2. Derive the hash using PBKDF2
|
|
36
|
+
# Password must be encoded to bytes (UTF-8)
|
|
37
|
+
hash_bytes = hashlib.pbkdf2_hmac(
|
|
38
|
+
ALGORITHM,
|
|
39
|
+
raw_password.encode('utf-8'),
|
|
40
|
+
salt,
|
|
41
|
+
ITERATIONS
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
# 3. Convert binary data to Base64 strings for storage
|
|
45
|
+
b64_salt = base64.b64encode(salt).decode('utf-8')
|
|
46
|
+
b64_hash = base64.b64encode(hash_bytes).decode('utf-8')
|
|
47
|
+
|
|
48
|
+
# 4. Return a single "packaged" string
|
|
49
|
+
return f"pbkdf2::{ALGORITHM}::{ITERATIONS}::{b64_salt}::{b64_hash}"
|
|
50
|
+
|
|
51
|
+
@enforce_requirements
|
|
52
|
+
def verify_password(input_password: str, stored_hash: str,/) -> bool:
|
|
53
|
+
"""
|
|
54
|
+
Validates a login attempt against a previously stored hash string.
|
|
55
|
+
|
|
56
|
+
Process:
|
|
57
|
+
1. Parses the stored string to retrieve salt and iteration count.
|
|
58
|
+
2. Re-hashes the input_password using those exact parameters.
|
|
59
|
+
3. Uses a constant-time comparison to prevent timing attacks.
|
|
60
|
+
|
|
61
|
+
Args:
|
|
62
|
+
input_password: The password provided during login.
|
|
63
|
+
stored_hash: The full string generated by hash_password().
|
|
64
|
+
|
|
65
|
+
Returns:
|
|
66
|
+
True if the passwords match, False otherwise.
|
|
67
|
+
"""
|
|
68
|
+
try:
|
|
69
|
+
# 1. Parse the packaged string
|
|
70
|
+
parts = stored_hash.split('::')
|
|
71
|
+
if len(parts) != 5:
|
|
72
|
+
return False
|
|
73
|
+
|
|
74
|
+
_, algo, iterations, b64_salt, b64_hash = parts
|
|
75
|
+
|
|
76
|
+
# 2. Convert metadata and salt back to original types
|
|
77
|
+
iterations = int(iterations)
|
|
78
|
+
salt = base64.b64decode(b64_salt)
|
|
79
|
+
original_hash = base64.b64decode(b64_hash)
|
|
80
|
+
|
|
81
|
+
# 3. Re-hash the new attempt with the OLD salt and OLD iterations
|
|
82
|
+
new_hash = hashlib.pbkdf2_hmac(
|
|
83
|
+
algo,
|
|
84
|
+
input_password.encode('utf-8'),
|
|
85
|
+
salt,
|
|
86
|
+
iterations
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
# 4. Constant-time comparison to protect against timing attacks
|
|
90
|
+
return hmac.compare_digest(new_hash, original_hash)
|
|
91
|
+
|
|
92
|
+
except (ValueError, TypeError, binascii.Error):
|
|
93
|
+
# If the stored hash is corrupted or manipulated, return False
|
|
94
|
+
return False
|
|
95
|
+
|