maleo-foundation 0.3.72__py3-none-any.whl → 0.3.75__py3-none-any.whl
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.
- maleo_foundation/authentication.py +2 -48
- maleo_foundation/constants.py +2 -0
- maleo_foundation/enums.py +7 -1
- maleo_foundation/managers/middleware.py +8 -8
- maleo_foundation/managers/service.py +8 -4
- maleo_foundation/middlewares/authentication.py +3 -2
- maleo_foundation/middlewares/base.py +312 -197
- maleo_foundation/models/schemas/general.py +1 -127
- maleo_foundation/models/transfers/general/authentication.py +35 -0
- maleo_foundation/{authorization.py → models/transfers/general/authorization.py} +0 -3
- maleo_foundation/models/transfers/general/configurations/__init__.py +2 -0
- maleo_foundation/models/transfers/general/configurations/middleware.py +6 -7
- maleo_foundation/models/transfers/general/operation.py +192 -30
- maleo_foundation/models/transfers/general/request.py +13 -19
- maleo_foundation/models/transfers/general/response.py +14 -0
- maleo_foundation/models/transfers/general/service.py +9 -0
- maleo_foundation/models/transfers/general/user_agent.py +34 -0
- maleo_foundation/utils/exceptions/client.py +22 -23
- maleo_foundation/utils/exceptions/service.py +21 -22
- maleo_foundation/utils/extractor.py +49 -19
- maleo_foundation/utils/logging.py +50 -10
- maleo_foundation/utils/parser.py +7 -0
- {maleo_foundation-0.3.72.dist-info → maleo_foundation-0.3.75.dist-info}/METADATA +3 -1
- {maleo_foundation-0.3.72.dist-info → maleo_foundation-0.3.75.dist-info}/RECORD +26 -24
- maleo_foundation/utils/dependencies/__init__.py +0 -6
- maleo_foundation/utils/dependencies/auth.py +0 -17
- maleo_foundation/utils/dependencies/context.py +0 -8
- {maleo_foundation-0.3.72.dist-info → maleo_foundation-0.3.75.dist-info}/WHEEL +0 -0
- {maleo_foundation-0.3.72.dist-info → maleo_foundation-0.3.75.dist-info}/top_level.txt +0 -0
@@ -1,17 +1,6 @@
|
|
1
|
-
from pydantic import BaseModel, ConfigDict, Field, model_validator
|
2
1
|
from starlette.authentication import AuthCredentials, BaseUser
|
3
|
-
from typing import Optional,
|
4
|
-
from maleo_foundation.
|
5
|
-
from maleo_foundation.models.transfers.general.token import (
|
6
|
-
MaleoFoundationTokenGeneralTransfers,
|
7
|
-
)
|
8
|
-
|
9
|
-
|
10
|
-
class Token(BaseModel):
|
11
|
-
type: BaseEnums.TokenType = Field(..., description="Token's type")
|
12
|
-
payload: MaleoFoundationTokenGeneralTransfers.DecodePayload = Field(
|
13
|
-
..., description="Token's payload"
|
14
|
-
)
|
2
|
+
from typing import Optional, Sequence
|
3
|
+
from maleo_foundation.models.transfers.general.authentication import Token
|
15
4
|
|
16
5
|
|
17
6
|
class Credentials(AuthCredentials):
|
@@ -26,11 +15,6 @@ class Credentials(AuthCredentials):
|
|
26
15
|
return self._token
|
27
16
|
|
28
17
|
|
29
|
-
class CredentialsModel(BaseModel):
|
30
|
-
token: Optional[Token] = Field(None, description="Token")
|
31
|
-
scopes: Optional[Sequence[str]] = Field(None, description="Scopes")
|
32
|
-
|
33
|
-
|
34
18
|
class User(BaseUser):
|
35
19
|
def __init__(
|
36
20
|
self, authenticated: bool = False, username: str = "", email: str = ""
|
@@ -50,33 +34,3 @@ class User(BaseUser):
|
|
50
34
|
@property
|
51
35
|
def identity(self) -> str:
|
52
36
|
return self._email
|
53
|
-
|
54
|
-
|
55
|
-
class UserModel(BaseModel):
|
56
|
-
is_authenticated: bool = Field(False, description="Authenticated")
|
57
|
-
display_name: str = Field("", description="Username")
|
58
|
-
identity: str = Field("", description="Email")
|
59
|
-
|
60
|
-
|
61
|
-
class Authentication(BaseModel):
|
62
|
-
model_config = ConfigDict(arbitrary_types_allowed=True)
|
63
|
-
|
64
|
-
credentials: Credentials = Field(
|
65
|
-
..., description="Credentials's information", exclude=True
|
66
|
-
)
|
67
|
-
credentials_model: CredentialsModel = Field(
|
68
|
-
default_factory=CredentialsModel, # type: ignore
|
69
|
-
description="Credential's model",
|
70
|
-
serialization_alias="credentials",
|
71
|
-
)
|
72
|
-
user: User = Field(..., description="User's information", exclude=True)
|
73
|
-
user_model: UserModel = Field(default_factory=UserModel, description="User's model", serialization_alias="user") # type: ignore
|
74
|
-
|
75
|
-
@model_validator(mode="after")
|
76
|
-
def define_models(self) -> Self:
|
77
|
-
self.credentials_model.token = self.credentials.token
|
78
|
-
self.credentials_model.scopes = self.credentials.scopes
|
79
|
-
self.user_model.is_authenticated = self.user.is_authenticated
|
80
|
-
self.user_model.display_name = self.user.display_name
|
81
|
-
self.user_model.identity = self.user.identity
|
82
|
-
return self
|
maleo_foundation/constants.py
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
import re
|
2
|
+
from fastapi.security import HTTPBearer
|
2
3
|
from typing import List
|
3
4
|
from uuid import UUID
|
4
5
|
from maleo_foundation.enums import BaseEnums
|
5
6
|
from maleo_foundation.types import BaseTypes
|
6
7
|
|
8
|
+
TOKEN_SCHEME = HTTPBearer()
|
7
9
|
EMAIL_REGEX: str = r"^[^\s@]+@[^\s@]+\.[^\s@]+$"
|
8
10
|
TOKEN_COOKIE_KEY_NAME = "token"
|
9
11
|
REFRESH_TOKEN_DURATION_DAYS: int = 7
|
maleo_foundation/enums.py
CHANGED
@@ -69,10 +69,12 @@ class BaseEnums:
|
|
69
69
|
|
70
70
|
class ExceptionType(StrEnum):
|
71
71
|
TIMEOUT = "timeout"
|
72
|
+
BAD = "bad"
|
72
73
|
UNAUTHORIZED = "unauthorized"
|
73
74
|
FORBIDDEN = "forbidden"
|
74
75
|
NOT_FOUND = "not_found"
|
75
76
|
VALIDATION = "validation"
|
77
|
+
RATE_LIMIT = "rate_limit"
|
76
78
|
INTERNAL = "internal"
|
77
79
|
UNAVAILABLE = "unavailable"
|
78
80
|
|
@@ -91,6 +93,7 @@ class BaseEnums:
|
|
91
93
|
SERVICE = "service"
|
92
94
|
|
93
95
|
class OperationLayer(StrEnum):
|
96
|
+
MIDDLEWARE = "middleware"
|
94
97
|
ROUTER = "router"
|
95
98
|
CONTROLLER = "controller"
|
96
99
|
SERVICE = "service"
|
@@ -122,6 +125,7 @@ class BaseEnums:
|
|
122
125
|
READ = "read"
|
123
126
|
UPDATE = "update"
|
124
127
|
DELETE = "delete"
|
128
|
+
OTHER = "other"
|
125
129
|
|
126
130
|
class CreateType(StrEnum):
|
127
131
|
CREATE = "create"
|
@@ -185,12 +189,14 @@ class BaseEnums:
|
|
185
189
|
APPLICATION = "application"
|
186
190
|
CACHE = "cache"
|
187
191
|
CLIENT = "client"
|
192
|
+
CONTROLLER = "controller"
|
188
193
|
DATABASE = "database"
|
189
194
|
MIDDLEWARE = "middleware"
|
190
195
|
REPOSITORY = "repository"
|
196
|
+
ROUTER = "router"
|
191
197
|
SERVICE = "service"
|
192
198
|
|
193
|
-
class
|
199
|
+
class LogLevel(IntEnum):
|
194
200
|
CRITICAL = logging.CRITICAL
|
195
201
|
FATAL = logging.FATAL
|
196
202
|
ERROR = logging.ERROR
|
@@ -4,6 +4,7 @@ from maleo_foundation.models.schemas import BaseGeneralSchemas
|
|
4
4
|
from maleo_foundation.models.transfers.general.configurations.middleware import (
|
5
5
|
MiddlewareConfigurations,
|
6
6
|
)
|
7
|
+
from maleo_foundation.models.transfers.general.settings import Settings
|
7
8
|
from maleo_foundation.middlewares.authentication import add_authentication_middleware
|
8
9
|
from maleo_foundation.middlewares.base import add_base_middleware
|
9
10
|
from maleo_foundation.middlewares.cors import add_cors_middleware
|
@@ -14,12 +15,14 @@ class MiddlewareManager:
|
|
14
15
|
def __init__(
|
15
16
|
self,
|
16
17
|
app: FastAPI,
|
18
|
+
settings: Settings,
|
17
19
|
configurations: MiddlewareConfigurations,
|
18
20
|
keys: BaseGeneralSchemas.RSAKeys,
|
19
21
|
logger: MiddlewareLogger,
|
20
22
|
maleo_foundation: MaleoFoundationClientManager,
|
21
23
|
):
|
22
24
|
self._app = app
|
25
|
+
self._settings = settings
|
23
26
|
self._configurations = configurations
|
24
27
|
self._keys = keys
|
25
28
|
self._logger = logger
|
@@ -33,23 +36,20 @@ class MiddlewareManager:
|
|
33
36
|
def add_cors(self) -> None:
|
34
37
|
add_cors_middleware(
|
35
38
|
app=self._app,
|
36
|
-
allow_origins=self._configurations.
|
37
|
-
allow_methods=self._configurations.
|
38
|
-
allow_headers=self._configurations.
|
39
|
-
allow_credentials=self._configurations.
|
39
|
+
allow_origins=self._configurations.cors.allow_origins,
|
40
|
+
allow_methods=self._configurations.cors.allow_methods,
|
41
|
+
allow_headers=self._configurations.cors.allow_headers,
|
42
|
+
allow_credentials=self._configurations.cors.allow_credentials,
|
40
43
|
expose_headers=self._configurations.cors.expose_headers,
|
41
44
|
)
|
42
45
|
|
43
46
|
def add_base(self):
|
44
47
|
add_base_middleware(
|
45
48
|
app=self._app,
|
49
|
+
settings=self._settings,
|
46
50
|
keys=self._keys,
|
47
51
|
logger=self._logger,
|
48
52
|
maleo_foundation=self._maleo_foundation,
|
49
|
-
allow_origins=self._configurations.general.allow_origins,
|
50
|
-
allow_methods=self._configurations.general.allow_methods,
|
51
|
-
allow_headers=self._configurations.general.allow_headers,
|
52
|
-
allow_credentials=self._configurations.general.allow_credentials,
|
53
53
|
limit=self._configurations.base.limit,
|
54
54
|
window=self._configurations.base.window,
|
55
55
|
cleanup_interval=self._configurations.base.cleanup_interval,
|
@@ -40,6 +40,7 @@ from maleo_foundation.utils.logging import (
|
|
40
40
|
DatabaseLogger,
|
41
41
|
MiddlewareLogger,
|
42
42
|
RepositoryLogger,
|
43
|
+
RouterLogger,
|
43
44
|
ServiceLogger,
|
44
45
|
)
|
45
46
|
from .credential import CredentialManager
|
@@ -60,10 +61,6 @@ class ServiceManager:
|
|
60
61
|
self._log_config = log_config # * Declare log config
|
61
62
|
self._settings = settings # * Initialize settings
|
62
63
|
|
63
|
-
# * Disable google cloud logging if environment is local
|
64
|
-
if self._settings.ENVIRONMENT == "local":
|
65
|
-
self._log_config.google_cloud_logging = None
|
66
|
-
|
67
64
|
# * Initialize Credential Manager
|
68
65
|
self._credential_manager = CredentialManager(
|
69
66
|
settings=self._settings, log_config=self._log_config
|
@@ -156,6 +153,11 @@ class ServiceManager:
|
|
156
153
|
service_key=self.settings.SERVICE_KEY,
|
157
154
|
**self._log_config.model_dump(),
|
158
155
|
)
|
156
|
+
router = RouterLogger(
|
157
|
+
environment=self.settings.ENVIRONMENT,
|
158
|
+
service_key=self.settings.SERVICE_KEY,
|
159
|
+
**self._log_config.model_dump(),
|
160
|
+
)
|
159
161
|
service = ServiceLogger(
|
160
162
|
environment=self.settings.ENVIRONMENT,
|
161
163
|
service_key=self.settings.SERVICE_KEY,
|
@@ -167,6 +169,7 @@ class ServiceManager:
|
|
167
169
|
database=database,
|
168
170
|
middleware=middleware,
|
169
171
|
repository=repository,
|
172
|
+
router=router,
|
170
173
|
service=service,
|
171
174
|
)
|
172
175
|
|
@@ -306,6 +309,7 @@ class ServiceManager:
|
|
306
309
|
self._loggers.application.info("Configuring middlewares")
|
307
310
|
self._middleware = MiddlewareManager(
|
308
311
|
app=self._app,
|
312
|
+
settings=self._settings,
|
309
313
|
configurations=self.configurations.middleware,
|
310
314
|
keys=self._keys,
|
311
315
|
logger=self._loggers.middleware,
|
@@ -3,10 +3,11 @@ from starlette.authentication import AuthenticationBackend, AuthenticationError
|
|
3
3
|
from starlette.middleware.authentication import AuthenticationMiddleware
|
4
4
|
from starlette.requests import HTTPConnection
|
5
5
|
from typing import Tuple
|
6
|
-
from maleo_foundation.authentication import
|
6
|
+
from maleo_foundation.authentication import Credentials, User
|
7
7
|
from maleo_foundation.enums import BaseEnums
|
8
8
|
from maleo_foundation.client.manager import MaleoFoundationClientManager
|
9
9
|
from maleo_foundation.models.schemas import BaseGeneralSchemas
|
10
|
+
from maleo_foundation.models.transfers.general.authentication import Token
|
10
11
|
from maleo_foundation.models.transfers.parameters.token import (
|
11
12
|
MaleoFoundationTokenParametersTransfers,
|
12
13
|
)
|
@@ -67,7 +68,7 @@ class Backend(AuthenticationBackend):
|
|
67
68
|
User(authenticated=True, username=payload.u_u, email=payload.u_e),
|
68
69
|
)
|
69
70
|
|
70
|
-
return Credentials(), User(
|
71
|
+
return Credentials(), User()
|
71
72
|
|
72
73
|
|
73
74
|
def add_authentication_middleware(
|