maleo-foundation 0.1.67__tar.gz → 0.1.68__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 (86) hide show
  1. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/PKG-INFO +1 -1
  2. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/authentication.py +14 -2
  3. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/managers/middleware.py +2 -2
  4. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/middlewares/authentication.py +9 -5
  5. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/middlewares/base.py +15 -3
  6. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/utils/exceptions.py +6 -2
  7. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation.egg-info/PKG-INFO +1 -1
  8. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation.egg-info/SOURCES.txt +0 -1
  9. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/pyproject.toml +1 -1
  10. maleo_foundation-0.1.67/maleo_foundation/middlewares/__init__.py +0 -98
  11. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/README.md +0 -0
  12. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/__init__.py +0 -0
  13. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/client/__init__.py +0 -0
  14. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/client/manager.py +0 -0
  15. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/client/services/__init__.py +0 -0
  16. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/client/services/signature.py +0 -0
  17. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/client/services/token.py +0 -0
  18. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/constants.py +0 -0
  19. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/enums.py +0 -0
  20. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/expanded_types/__init__.py +0 -0
  21. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/expanded_types/client.py +0 -0
  22. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/expanded_types/general.py +0 -0
  23. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/expanded_types/query.py +0 -0
  24. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/expanded_types/service.py +0 -0
  25. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/expanded_types/signature.py +0 -0
  26. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/expanded_types/token.py +0 -0
  27. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/extended_types.py +0 -0
  28. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/managers/__init__.py +0 -0
  29. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/managers/client/__init__.py +0 -0
  30. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/managers/client/base.py +0 -0
  31. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/managers/client/google/__init__.py +0 -0
  32. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/managers/client/google/base.py +0 -0
  33. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/managers/client/google/secret.py +0 -0
  34. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/managers/client/google/storage.py +0 -0
  35. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/managers/client/maleo.py +0 -0
  36. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/managers/db.py +0 -0
  37. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/managers/service.py +0 -0
  38. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/middlewares/cors.py +0 -0
  39. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/__init__.py +0 -0
  40. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/responses.py +0 -0
  41. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/schemas/__init__.py +0 -0
  42. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/schemas/general.py +0 -0
  43. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/schemas/parameter.py +0 -0
  44. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/schemas/result.py +0 -0
  45. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/schemas/signature.py +0 -0
  46. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/schemas/token.py +0 -0
  47. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/table.py +0 -0
  48. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/transfers/__init__.py +0 -0
  49. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/transfers/general/__init__.py +0 -0
  50. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/transfers/general/signature.py +0 -0
  51. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/transfers/general/token.py +0 -0
  52. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/transfers/parameters/__init__.py +0 -0
  53. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/transfers/parameters/client.py +0 -0
  54. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/transfers/parameters/general.py +0 -0
  55. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/transfers/parameters/service.py +0 -0
  56. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/transfers/parameters/signature.py +0 -0
  57. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/transfers/parameters/token.py +0 -0
  58. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/transfers/results/__init__.py +0 -0
  59. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/transfers/results/client/__init__.py +0 -0
  60. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/transfers/results/client/controllers/__init__.py +0 -0
  61. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/transfers/results/client/controllers/http.py +0 -0
  62. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/transfers/results/client/service.py +0 -0
  63. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/transfers/results/service/__init__.py +0 -0
  64. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/transfers/results/service/controllers/__init__.py +0 -0
  65. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/transfers/results/service/controllers/rest.py +0 -0
  66. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/transfers/results/service/general.py +0 -0
  67. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/transfers/results/service/query.py +0 -0
  68. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/transfers/results/signature.py +0 -0
  69. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/models/transfers/results/token.py +0 -0
  70. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/types.py +0 -0
  71. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/utils/__init__.py +0 -0
  72. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/utils/controller.py +0 -0
  73. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/utils/extractor.py +0 -0
  74. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/utils/formatter/__init__.py +0 -0
  75. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/utils/formatter/case.py +0 -0
  76. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/utils/loaders/__init__.py +0 -0
  77. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/utils/loaders/json.py +0 -0
  78. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/utils/loaders/key.py +0 -0
  79. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/utils/loaders/yaml.py +0 -0
  80. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/utils/logging.py +0 -0
  81. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/utils/mergers.py +0 -0
  82. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation/utils/query.py +0 -0
  83. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation.egg-info/dependency_links.txt +0 -0
  84. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation.egg-info/requires.txt +0 -0
  85. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/maleo_foundation.egg-info/top_level.txt +0 -0
  86. {maleo_foundation-0.1.67 → maleo_foundation-0.1.68}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: maleo_foundation
3
- Version: 0.1.67
3
+ Version: 0.1.68
4
4
  Summary: Foundation package for Maleo
5
5
  Author-email: Agra Bima Yuda <agra@nexmedis.com>
6
6
  License: MIT
@@ -3,16 +3,28 @@ from starlette.authentication import (
3
3
  BaseUser
4
4
  )
5
5
  from typing import Optional, Sequence
6
+ from maleo_foundation.models.transfers.general.token import BaseTokenGeneralTransfers
7
+ from maleo_foundation.types import BaseTypes
6
8
 
7
9
  class Credentials(AuthCredentials):
8
10
  def __init__(
9
11
  self,
10
- token:Optional[str] = None,
12
+ token:BaseTypes.OptionalString = None,
13
+ payload:Optional[BaseTokenGeneralTransfers.DecodePayload] = None,
11
14
  scopes:Optional[Sequence[str]] = None
12
15
  ) -> None:
13
- self.token = token
16
+ self._token = token
17
+ self._payload = payload
14
18
  super().__init__(scopes)
15
19
 
20
+ @property
21
+ def token(self) -> BaseTypes.OptionalString:
22
+ return self._token
23
+
24
+ @property
25
+ def payload(self) -> Optional[BaseTokenGeneralTransfers.DecodePayload]:
26
+ return self._payload
27
+
16
28
  class User(BaseUser):
17
29
  def __init__(
18
30
  self,
@@ -1,10 +1,10 @@
1
1
  from fastapi import FastAPI
2
2
  from pydantic import BaseModel, Field
3
- from typing import List, Optional
3
+ from typing import List
4
4
  from maleo_foundation.client.manager import MaleoFoundationClientManager
5
5
  from maleo_foundation.models.schemas import BaseGeneralSchemas
6
6
  from maleo_foundation.middlewares.authentication import add_authentication_middleware
7
- from maleo_foundation.middlewares.base import add_base_middleware, RequestProcessor
7
+ from maleo_foundation.middlewares.base import add_base_middleware
8
8
  from maleo_foundation.middlewares.cors import add_cors_middleware
9
9
  from maleo_foundation.utils.logging import MiddlewareLogger
10
10
 
@@ -1,5 +1,6 @@
1
+ from datetime import datetime, timezone
1
2
  from fastapi import FastAPI
2
- from starlette.authentication import AuthenticationBackend
3
+ from starlette.authentication import AuthenticationBackend, AuthenticationError
3
4
  from starlette.middleware.authentication import AuthenticationMiddleware
4
5
  from starlette.requests import HTTPConnection
5
6
  from typing import Tuple
@@ -7,6 +8,7 @@ from maleo_foundation.authentication import Credentials, User
7
8
  from maleo_foundation.client.manager import MaleoFoundationClientManager
8
9
  from maleo_foundation.models.schemas import BaseGeneralSchemas
9
10
  from maleo_foundation.models.transfers.parameters.token import BaseTokenParametersTransfers
11
+ from maleo_foundation.utils.exceptions import BaseExceptions
10
12
  from maleo_foundation.utils.extractor import BaseExtractors
11
13
  from maleo_foundation.utils.logging import MiddlewareLogger
12
14
 
@@ -27,16 +29,18 @@ class Backend(AuthenticationBackend):
27
29
  scheme, token = auth.split()
28
30
  if scheme != 'Bearer':
29
31
  self._logger.info(f"Request | IP: {client_ip} | URL: {conn.url.path} - Result | General: Authorization scheme is not Bearer")
30
- return Credentials(), User(authenticated=False)
32
+ raise AuthenticationError("Authorization scheme must be Bearer token")
31
33
 
32
34
  decode_token_parameters = BaseTokenParametersTransfers.Decode(key=self._keys.public, token=token)
33
35
  decode_token_result = self._maleo_foundation.services.token.decode(parameters=decode_token_parameters)
34
36
  if not decode_token_result.success:
35
37
  self._logger.error(f"Request | IP: {client_ip} | URL: {conn.url.path} - Result | General: Failed decoding authorization token")
36
- return Credentials(token=token), User(authenticated=False)
38
+ raise AuthenticationError("Invalid Bearer token, unable to decode token")
39
+ if decode_token_result.data.exp_dt <= datetime.now(tz=timezone.utc):
40
+ raise AuthenticationError("Expired Bearer token, request new or refresh token")
37
41
 
38
42
  self._logger.info(f"Request | IP: {client_ip} | URL: {conn.url.path} - Result | Username: {decode_token_result.data.u_u} | Email: {decode_token_result.data.u_e}")
39
- return Credentials(token=token), User(authenticated=True, username=decode_token_result.data.u_u, email=decode_token_result.data.u_e)
43
+ return Credentials(token=token, payload=decode_token_result.data, scopes=["authenticated"]), User(authenticated=True, username=decode_token_result.data.u_u, email=decode_token_result.data.u_e)
40
44
 
41
45
  def add_authentication_middleware(app:FastAPI, keys:BaseGeneralSchemas.RSAKeys, logger:MiddlewareLogger, maleo_foundation:MaleoFoundationClientManager) -> None:
42
46
  """
@@ -64,4 +68,4 @@ def add_authentication_middleware(app:FastAPI, keys:BaseGeneralSchemas.RSAKeys,
64
68
  add_authentication_middleware(app=app, limit=10, window=1, cleanup_interval=60, ip_timeout=300)
65
69
  ```
66
70
  """
67
- app.add_middleware(AuthenticationMiddleware, backend=Backend(keys, logger, maleo_foundation))
71
+ app.add_middleware(AuthenticationMiddleware, backend=Backend(keys, logger, maleo_foundation), on_error=BaseExceptions.authentication_error_handler)
@@ -8,6 +8,7 @@ from fastapi import FastAPI, Request, Response, status
8
8
  from fastapi.responses import JSONResponse
9
9
  from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
10
10
  from typing import Awaitable, Callable, Optional, Sequence
11
+ from maleo_foundation.authentication import Credentials, User
11
12
  from maleo_foundation.client.manager import MaleoFoundationClientManager
12
13
  from maleo_foundation.models.schemas import BaseGeneralSchemas
13
14
  from maleo_foundation.models.responses import BaseResponses
@@ -136,6 +137,7 @@ class BaseMiddleware(BaseHTTPMiddleware):
136
137
  def _build_response(
137
138
  self,
138
139
  request:Request,
140
+ authentication_info:str,
139
141
  response:Response,
140
142
  request_timestamp:datetime,
141
143
  response_timestamp:datetime,
@@ -146,7 +148,7 @@ class BaseMiddleware(BaseHTTPMiddleware):
146
148
  response = self._add_response_headers(request, response, request_timestamp, response_timestamp, process_time)
147
149
  log_func = getattr(self._logger, log_level)
148
150
  log_func(
149
- f"Request | IP: {client_ip} | Method: {request.method} | URL: {request.url.path} | "
151
+ f"Request {authentication_info} | IP: {client_ip} | Method: {request.method} | URL: {request.url.path} | "
150
152
  f"Headers: {dict(request.headers)} - Response | Status: {response.status_code} | "
151
153
  )
152
154
  return response
@@ -154,11 +156,12 @@ class BaseMiddleware(BaseHTTPMiddleware):
154
156
  def _handle_exception(
155
157
  self,
156
158
  request:Request,
159
+ authentication_info:str,
157
160
  error,
158
161
  request_timestamp:datetime,
159
162
  response_timestamp:datetime,
160
163
  process_time:int,
161
- client_ip
164
+ client_ip:str = "unknown"
162
165
  ):
163
166
  traceback_str = traceback.format_exc().split("\n")
164
167
  error_details = {
@@ -176,7 +179,7 @@ class BaseMiddleware(BaseHTTPMiddleware):
176
179
  )
177
180
 
178
181
  self._logger.error(
179
- f"Request | IP: {client_ip} | Method: {request.method} | URL: {request.url.path} | "
182
+ f"Request {authentication_info} | IP: {client_ip} | Method: {request.method} | URL: {request.url.path} | "
180
183
  f"Headers: {dict(request.headers)} - Response | Status: 500 | Exception:\n{json.dumps(error_details, indent=4)}"
181
184
  )
182
185
 
@@ -190,12 +193,18 @@ class BaseMiddleware(BaseHTTPMiddleware):
190
193
  request_timestamp = datetime.now(tz=timezone.utc) #* Record the request timestamp
191
194
  start_time = time.perf_counter() #* Record the start time
192
195
  client_ip = BaseExtractors.extract_client_ip(request) #* Get request IP with improved extraction
196
+ user:User = request.user
197
+ if not user.is_authenticated:
198
+ authentication_info = "| Unauthenticated"
199
+ else:
200
+ authentication_info = f"| Username: {user.display_name} | Email:{user.identity}"
193
201
 
194
202
  try:
195
203
  #* 1. Rate limit check
196
204
  if self._check_rate_limit(client_ip):
197
205
  return self._build_response(
198
206
  request=request,
207
+ authentication_info=authentication_info,
199
208
  response=JSONResponse(
200
209
  content=BaseResponses.RateLimitExceeded().model_dump(),
201
210
  status_code=status.HTTP_429_TOO_MANY_REQUESTS,
@@ -212,6 +221,7 @@ class BaseMiddleware(BaseHTTPMiddleware):
212
221
  if pre_response is not None:
213
222
  return self._build_response(
214
223
  request=request,
224
+ authentication_info=authentication_info,
215
225
  response=pre_response,
216
226
  request_timestamp=request_timestamp,
217
227
  response_timestamp=datetime.now(tz=timezone.utc),
@@ -224,6 +234,7 @@ class BaseMiddleware(BaseHTTPMiddleware):
224
234
  response = await call_next(request)
225
235
  response = self._build_response(
226
236
  request=request,
237
+ authentication_info=authentication_info,
227
238
  response=response,
228
239
  request_timestamp=request_timestamp,
229
240
  response_timestamp=datetime.now(tz=timezone.utc),
@@ -237,6 +248,7 @@ class BaseMiddleware(BaseHTTPMiddleware):
237
248
  except Exception as e:
238
249
  return self._handle_exception(
239
250
  request=request,
251
+ authentication_info=authentication_info,
240
252
  error=e,
241
253
  request_timestamp=request_timestamp,
242
254
  response_timestamp=datetime.now(tz=timezone.utc),
@@ -11,14 +11,18 @@ from maleo_foundation.models.transfers.results.service.query import BaseServiceQ
11
11
  from maleo_foundation.utils.logging import BaseLogger
12
12
 
13
13
  class BaseExceptions:
14
+ @staticmethod
15
+ def authentication_error_handler(request:Request, exc:Exception):
16
+ return JSONResponse(content=BaseResponses.Unauthorized(other=str(exc)).model_dump(), status_code=status.HTTP_401_UNAUTHORIZED)
17
+
14
18
  @staticmethod
15
19
  async def validation_exception_handler(request:Request, exc:RequestValidationError):
16
- return JSONResponse(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, content=BaseResponses.ValidationError(other=exc.errors()).model_dump())
20
+ return JSONResponse(content=BaseResponses.ValidationError(other=exc.errors()).model_dump(), status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)
17
21
 
18
22
  @staticmethod
19
23
  async def http_exception_handler(request:Request, exc:StarletteHTTPException):
20
24
  if exc.status_code == 404:
21
- return JSONResponse(status_code=status.HTTP_404_NOT_FOUND, content=BaseResponses.NotFound().model_dump())
25
+ return JSONResponse(content=BaseResponses.NotFound().model_dump(), status_code=status.HTTP_404_NOT_FOUND)
22
26
 
23
27
  #* Handle other HTTP exceptions normally
24
28
  return None
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: maleo_foundation
3
- Version: 0.1.67
3
+ Version: 0.1.68
4
4
  Summary: Foundation package for Maleo
5
5
  Author-email: Agra Bima Yuda <agra@nexmedis.com>
6
6
  License: MIT
@@ -34,7 +34,6 @@ maleo_foundation/managers/client/google/__init__.py
34
34
  maleo_foundation/managers/client/google/base.py
35
35
  maleo_foundation/managers/client/google/secret.py
36
36
  maleo_foundation/managers/client/google/storage.py
37
- maleo_foundation/middlewares/__init__.py
38
37
  maleo_foundation/middlewares/authentication.py
39
38
  maleo_foundation/middlewares/base.py
40
39
  maleo_foundation/middlewares/cors.py
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "maleo_foundation"
7
- version = "0.1.67"
7
+ version = "0.1.68"
8
8
  description = "Foundation package for Maleo"
9
9
  authors = [
10
10
  { name = "Agra Bima Yuda", email = "agra@nexmedis.com" }
@@ -1,98 +0,0 @@
1
- from fastapi import FastAPI
2
- from logging import Logger
3
- from typing import Optional, Sequence
4
- from .base import add_base_middleware, RequestProcessor
5
- from .cors import add_cors_middleware
6
-
7
- class MiddlewareManager:
8
- _default_limit:int = 10
9
- _default_window:int = 1
10
- _default_cleanup_interval:int = 60
11
- _default_ip_timeout:int = 300
12
- _default_allow_origins:Sequence[str] = ()
13
- _default_allow_methods:Sequence[str] = ("GET",)
14
- _default_allow_headers:Sequence[str] = ()
15
- _default_allow_credentials:bool = False
16
- _default_expose_headers:Sequence[str] = ()
17
- _default_request_processor:Optional[RequestProcessor] = None
18
-
19
- def __init__(self, app:FastAPI):
20
- self.app = app
21
-
22
- def add_all_middlewares(
23
- self,
24
- logger:Logger,
25
- limit:int = _default_limit,
26
- window:int = _default_window,
27
- cleanup_interval:int = _default_cleanup_interval,
28
- ip_timeout:int = _default_ip_timeout,
29
- allow_origins:Sequence[str] = _default_allow_origins,
30
- allow_methods:Sequence[str] = _default_allow_methods,
31
- allow_headers:Sequence[str] = _default_allow_headers,
32
- allow_credentials:bool = _default_allow_credentials,
33
- expose_headers:Sequence[str] = _default_expose_headers,
34
- request_processor:Optional[RequestProcessor] = _default_request_processor
35
- ):
36
- self.add_cors_middleware(
37
- allow_origins=allow_origins,
38
- allow_methods=allow_methods,
39
- allow_headers=allow_headers,
40
- allow_credentials=allow_credentials,
41
- expose_headers=expose_headers
42
- )
43
- self.add_base_middleware(
44
- logger=logger,
45
- allow_origins=allow_origins,
46
- allow_methods=allow_methods,
47
- allow_headers=allow_headers,
48
- allow_credentials=allow_credentials,
49
- limit=limit,
50
- window=window,
51
- cleanup_interval=cleanup_interval,
52
- ip_timeout=ip_timeout,
53
- request_processor=request_processor
54
- )
55
-
56
- def add_cors_middleware(
57
- self,
58
- allow_origins:Sequence[str] = _default_allow_origins,
59
- allow_methods:Sequence[str] = _default_allow_methods,
60
- allow_headers:Sequence[str] = _default_allow_headers,
61
- allow_credentials:bool = _default_allow_credentials,
62
- expose_headers:Sequence[str] = _default_expose_headers
63
- ):
64
- add_cors_middleware(
65
- app=self.app,
66
- allow_origins=allow_origins,
67
- allow_methods=allow_methods,
68
- allow_headers=allow_headers,
69
- allow_credentials=allow_credentials,
70
- expose_headers=expose_headers
71
- )
72
-
73
- def add_base_middleware(
74
- self,
75
- logger:Logger,
76
- allow_origins:Sequence[str] = _default_allow_origins,
77
- allow_methods:Sequence[str] = _default_allow_methods,
78
- allow_headers:Sequence[str] = _default_allow_headers,
79
- allow_credentials:bool = _default_allow_credentials,
80
- limit:int = _default_limit,
81
- window:int = _default_window,
82
- cleanup_interval:int = _default_cleanup_interval,
83
- ip_timeout:int = _default_ip_timeout,
84
- request_processor:Optional[RequestProcessor] = _default_request_processor
85
- ):
86
- add_base_middleware(
87
- app=self.app,
88
- logger=logger,
89
- allow_origins=allow_origins,
90
- allow_methods=allow_methods,
91
- allow_headers=allow_headers,
92
- allow_credentials=allow_credentials,
93
- limit=limit,
94
- window=window,
95
- cleanup_interval=cleanup_interval,
96
- ip_timeout=ip_timeout,
97
- request_processor=request_processor
98
- )