spakky-fastapi 0.8.0__py3-none-any.whl → 0.12.0__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.
File without changes
@@ -1,20 +1,21 @@
1
1
  from typing import Any, TypeVar, Callable, Annotated, Awaitable, TypeAlias, Concatenate
2
+ from inspect import iscoroutinefunction
2
3
  from logging import Logger
3
4
  from dataclasses import InitVar, field, dataclass
4
5
 
5
6
  from fastapi import Depends
6
7
  from fastapi.security import OAuth2PasswordBearer
7
8
  from spakky.aop.advice import Around
8
- from spakky.aop.advisor import IAsyncAdvisor
9
- from spakky.aop.aspect import AsyncAspect
9
+ from spakky.aop.advisor import IAdvisor, IAsyncAdvisor
10
+ from spakky.aop.aspect import Aspect, AsyncAspect
10
11
  from spakky.aop.error import SpakkyAOPError
11
12
  from spakky.aop.order import Order
12
- from spakky.bean.autowired import autowired
13
13
  from spakky.core.annotation import FunctionAnnotation
14
- from spakky.core.types import AsyncFunc, P
14
+ from spakky.core.types import AsyncFunc, Func, P
15
15
  from spakky.cryptography.error import InvalidJWTFormatError, JWTDecodingError
16
16
  from spakky.cryptography.jwt import JWT
17
17
  from spakky.cryptography.key import Key
18
+
18
19
  from spakky_fastapi.error import Unauthorized
19
20
 
20
21
  R_co = TypeVar("R_co", covariant=True)
@@ -24,7 +25,10 @@ class AuthenticationFailedError(SpakkyAOPError):
24
25
  message = "사용자 인증에 실패했습니다."
25
26
 
26
27
 
27
- IAuthenticatedFunction: TypeAlias = Callable[Concatenate[Any, JWT, P], Awaitable[R_co]]
28
+ IAuthenticatedFunction: TypeAlias = (
29
+ Callable[Concatenate[Any, JWT, P], R_co]
30
+ | Callable[Concatenate[Any, JWT, P], Awaitable[R_co]]
31
+ )
28
32
 
29
33
 
30
34
  @dataclass
@@ -46,19 +50,47 @@ class JWTAuth(FunctionAnnotation):
46
50
  return super().__call__(obj)
47
51
 
48
52
 
53
+ @Order(1)
54
+ @Aspect()
55
+ class JWTAuthAdvisor(IAdvisor):
56
+ __logger: Logger
57
+ __key: Key
58
+
59
+ def __init__(self, logger: Logger, key: Key) -> None:
60
+ super().__init__()
61
+ self.__logger = logger
62
+ self.__key = key
63
+
64
+ @Around(lambda x: JWTAuth.contains(x) and not iscoroutinefunction(x))
65
+ def around(self, joinpoint: Func, *args: Any, **kwargs: Any) -> Any:
66
+ annotation: JWTAuth = JWTAuth.single(joinpoint)
67
+ for keyword in annotation.token_keywords:
68
+ token: str = kwargs[keyword]
69
+ try:
70
+ jwt: JWT = JWT(token=token)
71
+ except (InvalidJWTFormatError, JWTDecodingError) as e:
72
+ raise Unauthorized(AuthenticationFailedError()) from e
73
+ if jwt.is_expired:
74
+ raise Unauthorized(AuthenticationFailedError())
75
+ if jwt.verify(self.__key) is False:
76
+ raise Unauthorized(AuthenticationFailedError())
77
+ self.__logger.info(f"[{type(self).__name__}] {jwt.payload!r}")
78
+ kwargs[keyword] = jwt
79
+ return joinpoint(*args, **kwargs)
80
+
81
+
49
82
  @Order(1)
50
83
  @AsyncAspect()
51
84
  class AsyncJWTAuthAdvisor(IAsyncAdvisor):
52
85
  __logger: Logger
53
86
  __key: Key
54
87
 
55
- @autowired
56
88
  def __init__(self, logger: Logger, key: Key) -> None:
57
89
  super().__init__()
58
90
  self.__logger = logger
59
91
  self.__key = key
60
92
 
61
- @Around(JWTAuth.contains)
93
+ @Around(lambda x: JWTAuth.contains(x) and iscoroutinefunction(x))
62
94
  async def around_async(self, joinpoint: AsyncFunc, *args: Any, **kwargs: Any) -> Any:
63
95
  annotation: JWTAuth = JWTAuth.single(joinpoint)
64
96
  for keyword in annotation.token_keywords:
@@ -6,6 +6,7 @@ from pydantic import BaseModel
6
6
  from starlette.middleware.base import BaseHTTPMiddleware, DispatchFunction
7
7
  from starlette.responses import Response
8
8
  from starlette.types import ASGIApp
9
+
9
10
  from spakky_fastapi.error import InternalServerError, SpakkyFastAPIError
10
11
 
11
12
  Next: TypeAlias = Callable[[Request], Awaitable[Response]]
File without changes
@@ -0,0 +1,24 @@
1
+ from logging import Logger
2
+
3
+ from fastapi import FastAPI
4
+ from spakky.application.interfaces.pluggable import IPluggable
5
+ from spakky.application.interfaces.registry import IRegistry
6
+
7
+ from spakky_fastapi.post_processor import FastAPIBeanPostProcessor
8
+
9
+
10
+ class FastAPIPlugin(IPluggable):
11
+ app: FastAPI
12
+ logger: Logger
13
+
14
+ def __init__(self, app: FastAPI, logger: Logger) -> None:
15
+ self.app = app
16
+ self.logger = logger
17
+
18
+ def register(self, registry: IRegistry) -> None:
19
+ registry.register_bean_post_processor(
20
+ FastAPIBeanPostProcessor(
21
+ self.app,
22
+ self.logger,
23
+ )
24
+ )
@@ -0,0 +1,10 @@
1
+ from spakky.application.interfaces.pluggable import IPluggable
2
+ from spakky.application.interfaces.registry import IRegistry
3
+
4
+ from spakky_fastapi.aspects.jwt_auth import AsyncJWTAuthAdvisor, JWTAuthAdvisor
5
+
6
+
7
+ class JWTAuthPlugin(IPluggable):
8
+ def register(self, registry: IRegistry) -> None:
9
+ registry.register_bean(JWTAuthAdvisor)
10
+ registry.register_bean(AsyncJWTAuthAdvisor)
@@ -6,8 +6,9 @@ from dataclasses import asdict
6
6
  from fastapi import APIRouter, FastAPI
7
7
  from fastapi.exceptions import FastAPIError
8
8
  from fastapi.utils import create_response_field # type: ignore
9
- from spakky.bean.interfaces.bean_container import IBeanContainer
10
- from spakky.bean.interfaces.bean_processor import IBeanPostProcessor
9
+ from spakky.application.interfaces.bean_container import IBeanContainer
10
+ from spakky.application.interfaces.bean_processor import IBeanPostProcessor
11
+
11
12
  from spakky_fastapi.stereotypes.api_controller import (
12
13
  ApiController,
13
14
  Route,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: spakky-fastapi
3
- Version: 0.8.0
3
+ Version: 0.12.0
4
4
  Summary: Highly abstracted Framework core to use DDD & DI/IoC & AOP & Etc...
5
5
  Author: Spakky
6
6
  Author-email: sejong418@icloud.com
@@ -11,7 +11,7 @@ Classifier: Programming Language :: Python :: 3.11
11
11
  Classifier: Programming Language :: Python :: 3.12
12
12
  Requires-Dist: fastapi (>=0.109.2,<0.110.0)
13
13
  Requires-Dist: orjson (>=3.9.15,<4.0.0)
14
- Requires-Dist: spakky-core (>=0.10.1)
14
+ Requires-Dist: spakky-core (>=0.13,<0.14)
15
15
  Requires-Dist: websockets (>=12.0,<13.0)
16
16
  Description-Content-Type: text/markdown
17
17
 
@@ -0,0 +1,16 @@
1
+ spakky_fastapi/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ spakky_fastapi/aspects/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
+ spakky_fastapi/aspects/jwt_auth.py,sha256=1OSpCQDWiBzseH5IPfgutjQCNKabnNFMCRNQRBoENE8,4007
4
+ spakky_fastapi/error.py,sha256=SWyNZk1kxbTCiy-tLxB8saLpX5rBxwvy7q7JRCtNQV0,1124
5
+ spakky_fastapi/middlewares/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
+ spakky_fastapi/middlewares/error_handling.py,sha256=SeqEZeGYdRvQTAcbVlcP6bGHXGi5RL9S6ZnsG6PDgTE,1712
7
+ spakky_fastapi/plugins/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
+ spakky_fastapi/plugins/fast_api.py,sha256=lSXABplfyl3jASaT3NV4Z1hC-EtHqilXqyN98Y756v4,652
9
+ spakky_fastapi/plugins/jwt_auth.py,sha256=UN05gI9cYHrQERIYYigLqsFMxOdgvceusg6AZO1KN_Y,392
10
+ spakky_fastapi/post_processor.py,sha256=gTUubo-vOy_uB6rytT8vuNuRgQV2fOdsDqPx93cf8oI,2983
11
+ spakky_fastapi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
12
+ spakky_fastapi/stereotypes/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
13
+ spakky_fastapi/stereotypes/api_controller.py,sha256=NM0gyTQfuHnENtvyfm5T9Y427gQdxdUcVYDzAXryF2M,19320
14
+ spakky_fastapi-0.12.0.dist-info/METADATA,sha256=cV48md3pyfDYnbg5xNWyFaDuWYkj4qSKqObkaYAVEm8,1706
15
+ spakky_fastapi-0.12.0.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
16
+ spakky_fastapi-0.12.0.dist-info/RECORD,,
@@ -1,12 +0,0 @@
1
- spakky_fastapi/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- spakky_fastapi/error.py,sha256=SWyNZk1kxbTCiy-tLxB8saLpX5rBxwvy7q7JRCtNQV0,1124
3
- spakky_fastapi/jwt_auth.py,sha256=Ug3DPEHQQn14gFb-j_o_NW0SUF-4NyfR67FLngjPCCQ,2822
4
- spakky_fastapi/middlewares/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
- spakky_fastapi/middlewares/error_handling.py,sha256=7tslQBKdh3y7oMNTyDbT4fp2WyR0Tt3SDy-VgHnVU40,1711
6
- spakky_fastapi/post_processor.py,sha256=M2fGXhAFvCy1wHKnGxymfPiR7kuy5Iw85AhKsGUpD3o,2968
7
- spakky_fastapi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
- spakky_fastapi/stereotypes/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
- spakky_fastapi/stereotypes/api_controller.py,sha256=NM0gyTQfuHnENtvyfm5T9Y427gQdxdUcVYDzAXryF2M,19320
10
- spakky_fastapi-0.8.0.dist-info/METADATA,sha256=pdrDLGNdyOWhi_cJNWs5el4gD9NUCH_X7RMrAt1re8A,1701
11
- spakky_fastapi-0.8.0.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
12
- spakky_fastapi-0.8.0.dist-info/RECORD,,