variamos-security 0.0.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.
- variamos_security-0.0.1/PKG-INFO +17 -0
- variamos_security-0.0.1/README.md +128 -0
- variamos_security-0.0.1/setup.cfg +4 -0
- variamos_security-0.0.1/setup.py +22 -0
- variamos_security-0.0.1/variamos_security/__init__.py +10 -0
- variamos_security-0.0.1/variamos_security.egg-info/PKG-INFO +17 -0
- variamos_security-0.0.1/variamos_security.egg-info/SOURCES.txt +8 -0
- variamos_security-0.0.1/variamos_security.egg-info/dependency_links.txt +1 -0
- variamos_security-0.0.1/variamos_security.egg-info/requires.txt +4 -0
- variamos_security-0.0.1/variamos_security.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: variamos_security
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: VariaMos security commons for Python
|
|
5
|
+
Home-page: UNKNOWN
|
|
6
|
+
Author: VariaMos
|
|
7
|
+
Author-email: variamosple@gmail.com
|
|
8
|
+
License: UNKNOWN
|
|
9
|
+
Platform: UNKNOWN
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Requires-Python: >=3.9
|
|
14
|
+
Provides-Extra: fastapi
|
|
15
|
+
|
|
16
|
+
UNKNOWN
|
|
17
|
+
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# VariaMos Security Library
|
|
2
|
+
|
|
3
|
+
This library provides security functionalities for the VariaMos platform, including JWT creation, session validation, role and permission checks, and exception handling.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
To install the dependencies, run:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install -r requirements.txt
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Configuration
|
|
14
|
+
|
|
15
|
+
1. **Create Public and Private Keys for Session Management:**
|
|
16
|
+
|
|
17
|
+
- Generate a private key:
|
|
18
|
+
```bash
|
|
19
|
+
openssl genpkey -algorithm RSA -out jwtRS256.key -pkeyopt rsa_keygen_bits:4096
|
|
20
|
+
```
|
|
21
|
+
- Generate a public key:
|
|
22
|
+
```bash
|
|
23
|
+
openssl rsa -in jwtRS256.key -pubout -out jwtRS256.key.pub
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
2. **Set Environment Variables:**
|
|
27
|
+
|
|
28
|
+
- Set the `VARIAMOS_PRIVATE_KEY_PATH` in your environment:
|
|
29
|
+
```env
|
|
30
|
+
VARIAMOS_PRIVATE_KEY_PATH=./jwtRS256.key
|
|
31
|
+
```
|
|
32
|
+
- Set the `VARIAMOS_PUBLIC_KEY_PATH` in your environment:
|
|
33
|
+
```env
|
|
34
|
+
VARIAMOS_PUBLIC_KEY_PATH=./jwtRS256.key.pub
|
|
35
|
+
```
|
|
36
|
+
- Set the `VARIAMOS_JWT_EXP_IN_SECONDS` in your environment (optional, default is 900 seconds):
|
|
37
|
+
```env
|
|
38
|
+
VARIAMOS_JWT_EXP_IN_SECONDS=900
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
|
|
43
|
+
### Functions
|
|
44
|
+
|
|
45
|
+
#### `create_jwt`
|
|
46
|
+
|
|
47
|
+
- **Function:** `create_jwt(user: SessionUser, aud: str = None) -> str`
|
|
48
|
+
- **Description:** Creates a JWT for the given user.
|
|
49
|
+
- **Parameters:**
|
|
50
|
+
- `user` (SessionUser): The user for whom the JWT is created.
|
|
51
|
+
- `aud` (str, optional): The audience for the JWT.
|
|
52
|
+
- **Returns:** The generated JWT as a string.
|
|
53
|
+
|
|
54
|
+
#### `is_authenticated`
|
|
55
|
+
|
|
56
|
+
- **Function:** `is_authenticated(request: Request) -> SessionUser`
|
|
57
|
+
- **Description:** Validates the session and returns the authenticated user.
|
|
58
|
+
- **Parameters:**
|
|
59
|
+
- `request` (Request): The incoming request.
|
|
60
|
+
- **Returns:** The authenticated user as a `SessionUser`.
|
|
61
|
+
|
|
62
|
+
#### `has_roles`
|
|
63
|
+
|
|
64
|
+
- **Function:** `has_roles(roles: List[str] = [])`
|
|
65
|
+
- **Description:** Middleware to check if the user has the required roles.
|
|
66
|
+
- **Parameters:**
|
|
67
|
+
- `roles` (List[str]): The list of roles to check.
|
|
68
|
+
- **Returns:** The authenticated user as a `SessionUser`.
|
|
69
|
+
|
|
70
|
+
#### `has_permissions`
|
|
71
|
+
|
|
72
|
+
- **Function:** `has_permissions(permissions: List[str] = [], roles: List[str] = [])`
|
|
73
|
+
- **Description:** Middleware to check if the user has the required permissions and roles.
|
|
74
|
+
- **Parameters:**
|
|
75
|
+
- `permissions` (List[str]): The list of permissions to check.
|
|
76
|
+
- `roles` (List[str]): The list of roles to check.
|
|
77
|
+
- **Returns:** The authenticated user as a `SessionUser`.
|
|
78
|
+
|
|
79
|
+
### Exception Handling
|
|
80
|
+
|
|
81
|
+
#### `variamos_security_exception`
|
|
82
|
+
|
|
83
|
+
- **Class:** `VariamosSecurityException`
|
|
84
|
+
- **Description:** Custom exception class for security-related errors.
|
|
85
|
+
- **Parameters:**
|
|
86
|
+
|
|
87
|
+
- `status_code` (int): The HTTP status code.
|
|
88
|
+
- `detail` (ResponseModel): The response model containing error details.
|
|
89
|
+
- `headers` (dict, optional): Additional headers.
|
|
90
|
+
|
|
91
|
+
- **Function:** `variamos_security_exception_handler(request: Request, exc: VariamosSecurityException)`
|
|
92
|
+
- **Description:** Exception handler for `VariamosSecurityException`.
|
|
93
|
+
- **Parameters:**
|
|
94
|
+
- `request` (Request): The incoming request.
|
|
95
|
+
- `exc` (VariamosSecurityException): The raised exception.
|
|
96
|
+
- **Returns:** A JSON response with the error details.
|
|
97
|
+
|
|
98
|
+
### Configuration
|
|
99
|
+
|
|
100
|
+
#### `security_config`
|
|
101
|
+
|
|
102
|
+
- **Function:** `load_keys()`
|
|
103
|
+
- **Description:** Loads the private and public keys from the environment.
|
|
104
|
+
- **Function:** `get_private_key()`
|
|
105
|
+
- **Description:** Returns the private key.
|
|
106
|
+
- **Function:** `get_public_key()`
|
|
107
|
+
- **Description:** Returns the public key.
|
|
108
|
+
|
|
109
|
+
## Models
|
|
110
|
+
|
|
111
|
+
### `session_info`
|
|
112
|
+
|
|
113
|
+
- **Class:** `SessionInfo`
|
|
114
|
+
- **Description:** Model representing session information.
|
|
115
|
+
|
|
116
|
+
### `session_user`
|
|
117
|
+
|
|
118
|
+
- **Class:** `SessionUser`
|
|
119
|
+
- **Description:** Model representing a session user.
|
|
120
|
+
|
|
121
|
+
### `response_model`
|
|
122
|
+
|
|
123
|
+
- **Class:** `ResponseModel`
|
|
124
|
+
- **Description:** Model representing a response with data or error information.
|
|
125
|
+
|
|
126
|
+
## License
|
|
127
|
+
|
|
128
|
+
This project is licensed under the MIT License.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
from setuptools import setup, find_packages
|
|
2
|
+
|
|
3
|
+
setup(
|
|
4
|
+
name='variamos_security',
|
|
5
|
+
version='0.0.1',
|
|
6
|
+
packages=find_packages(),
|
|
7
|
+
install_requires=[
|
|
8
|
+
'python-jose'
|
|
9
|
+
],
|
|
10
|
+
extras_require={
|
|
11
|
+
"fastapi": ["fastapi>=0.115.0"],
|
|
12
|
+
},
|
|
13
|
+
author='VariaMos',
|
|
14
|
+
author_email='variamosple@gmail.com',
|
|
15
|
+
description='VariaMos security commons for Python',
|
|
16
|
+
classifiers=[
|
|
17
|
+
'Programming Language :: Python :: 3',
|
|
18
|
+
'License :: OSI Approved :: MIT License',
|
|
19
|
+
'Operating System :: OS Independent',
|
|
20
|
+
],
|
|
21
|
+
python_requires='>=3.9',
|
|
22
|
+
)
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
from .config.security_config import load_keys, get_private_key, get_public_key
|
|
2
|
+
from .functions.create_jwt import create_jwt
|
|
3
|
+
from .functions.is_authenticated import get_token, is_session_expired, validate_token, validate_session, is_authenticated
|
|
4
|
+
from .functions.has_roles import validate_roles, has_roles
|
|
5
|
+
from .functions.has_permissions import validate_roles_and_permissions, has_permissions
|
|
6
|
+
from .functions.mappers import session_info_to_session_user
|
|
7
|
+
from .model.response_model import ResponseModel
|
|
8
|
+
from .model.session_info import SessionInfo
|
|
9
|
+
from .model.session_user import SessionUser
|
|
10
|
+
from .exceptions.variamos_security_exception import VariamosSecurityException, variamos_security_exception_handler
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: variamos-security
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: VariaMos security commons for Python
|
|
5
|
+
Home-page: UNKNOWN
|
|
6
|
+
Author: VariaMos
|
|
7
|
+
Author-email: variamosple@gmail.com
|
|
8
|
+
License: UNKNOWN
|
|
9
|
+
Platform: UNKNOWN
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Requires-Python: >=3.9
|
|
14
|
+
Provides-Extra: fastapi
|
|
15
|
+
|
|
16
|
+
UNKNOWN
|
|
17
|
+
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
setup.py
|
|
3
|
+
variamos_security/__init__.py
|
|
4
|
+
variamos_security.egg-info/PKG-INFO
|
|
5
|
+
variamos_security.egg-info/SOURCES.txt
|
|
6
|
+
variamos_security.egg-info/dependency_links.txt
|
|
7
|
+
variamos_security.egg-info/requires.txt
|
|
8
|
+
variamos_security.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
variamos_security
|