spakky-fastapi 0.7.16__py3-none-any.whl → 0.7.19__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.
@@ -1,5 +1,4 @@
1
- from typing import Any, TypeVar, Protocol, Awaitable, runtime_checkable
2
- from inspect import signature
1
+ from typing import Any, TypeVar, Callable, Annotated, Awaitable, TypeAlias, Concatenate
3
2
  from logging import Logger
4
3
  from dataclasses import InitVar, field, dataclass
5
4
 
@@ -25,26 +24,14 @@ class AuthenticationFailedError(SpakkyAOPError):
25
24
  message = "사용자 인증에 실패했습니다."
26
25
 
27
26
 
28
- @runtime_checkable
29
- class IAuthenticatedFunction(Protocol[P, R_co]):
30
- __defaults__: tuple[Any, ...] | None
31
- __kwdefaults__: dict[str, Any] | None
32
-
33
- # pylint: disable=no-self-argument
34
- def __call__(
35
- self_, # type: ignore
36
- self: Any,
37
- token: JWT,
38
- *args: P.args,
39
- **kwargs: P.kwargs,
40
- ) -> Awaitable[R_co]:
41
- raise NotImplementedError
27
+ IAuthenticatedFunction: TypeAlias = Callable[Concatenate[Any, JWT, P], Awaitable[R_co]]
42
28
 
43
29
 
44
30
  @dataclass
45
31
  class JWTAuth(FunctionAnnotation):
46
32
  token_url: InitVar[str]
47
33
  authenticator: OAuth2PasswordBearer = field(init=False)
34
+ token_keywords: list[str] = field(init=False, default_factory=list)
48
35
 
49
36
  def __post_init__(self, token_url: str) -> None:
50
37
  self.authenticator = OAuth2PasswordBearer(tokenUrl=token_url)
@@ -52,12 +39,10 @@ class JWTAuth(FunctionAnnotation):
52
39
  def __call__(
53
40
  self, obj: IAuthenticatedFunction[P, R_co]
54
41
  ) -> IAuthenticatedFunction[P, R_co]:
55
- parameters = list(signature(obj).parameters.values())
56
- if obj.__defaults__ is not None:
57
- extra = obj.__defaults__
58
- else:
59
- extra = tuple(Depends() for x in parameters[1:] if x.name != "token")
60
- obj.__defaults__ = (Depends(self.authenticator),) + extra
42
+ for key, value in obj.__annotations__.items():
43
+ if value == JWT:
44
+ obj.__annotations__[key] = Annotated[JWT, Depends(self.authenticator)]
45
+ self.token_keywords.append(key)
61
46
  return super().__call__(obj)
62
47
 
63
48
 
@@ -75,15 +60,17 @@ class AsyncJWTAuthAdvisor(IAsyncAdvisor):
75
60
 
76
61
  @Around(JWTAuth.contains)
77
62
  async def around_async(self, joinpoint: AsyncFunc, *args: Any, **kwargs: Any) -> Any:
78
- token: str = kwargs["token"]
79
- try:
80
- jwt: JWT = JWT(token=token)
81
- except (InvalidJWTFormatError, JWTDecodingError) as e:
82
- raise Unauthorized(AuthenticationFailedError()) from e
83
- if jwt.is_expired:
84
- raise Unauthorized(AuthenticationFailedError())
85
- if jwt.verify(self.__key) is False:
86
- raise Unauthorized(AuthenticationFailedError())
87
- self.__logger.info(f"[{type(self).__name__}] {jwt.payload!r}")
88
- kwargs["token"] = jwt
63
+ annotation: JWTAuth = JWTAuth.single(joinpoint)
64
+ for keyword in annotation.token_keywords:
65
+ token: str = kwargs[keyword]
66
+ try:
67
+ jwt: JWT = JWT(token=token)
68
+ except (InvalidJWTFormatError, JWTDecodingError) as e:
69
+ raise Unauthorized(AuthenticationFailedError()) from e
70
+ if jwt.is_expired:
71
+ raise Unauthorized(AuthenticationFailedError())
72
+ if jwt.verify(self.__key) is False:
73
+ raise Unauthorized(AuthenticationFailedError())
74
+ self.__logger.info(f"[{type(self).__name__}] {jwt.payload!r}")
75
+ kwargs[keyword] = jwt
89
76
  return await joinpoint(*args, **kwargs)
@@ -26,7 +26,7 @@ class FastAPIBeanPostProcessor(IBeanPostProcessor):
26
26
  return bean
27
27
  controller = ApiController.single(bean)
28
28
  router: APIRouter = APIRouter(prefix=controller.prefix, tags=controller.tags)
29
- for name, method in getmembers(bean):
29
+ for name, method in getmembers(bean, callable):
30
30
  route: Route | None = Route.single_or_none(method)
31
31
  websocket_route: WebSocketRoute | None = WebSocketRoute.single_or_none(method)
32
32
  if route is None and websocket_route is None:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: spakky-fastapi
3
- Version: 0.7.16
3
+ Version: 0.7.19
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.7.13)
14
+ Requires-Dist: spakky-core (>=0.7.14)
15
15
  Requires-Dist: websockets (>=12.0,<13.0)
16
16
  Description-Content-Type: text/markdown
17
17
 
@@ -1,13 +1,13 @@
1
1
  spakky_fastapi/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
2
  spakky_fastapi/error.py,sha256=SWyNZk1kxbTCiy-tLxB8saLpX5rBxwvy7q7JRCtNQV0,1124
3
- spakky_fastapi/jwt_auth.py,sha256=ymi3VaZEk8CjvABpoxgXIeLEs6PiGu7daC4AF0bis-M,2999
3
+ spakky_fastapi/jwt_auth.py,sha256=Ug3DPEHQQn14gFb-j_o_NW0SUF-4NyfR67FLngjPCCQ,2822
4
4
  spakky_fastapi/middlewares/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
5
  spakky_fastapi/middlewares/error_handling.py,sha256=w_zy4PtEdGkFNN79AHh7TfwWUGSQ1JFNvPo3Dy8ojDM,1325
6
- spakky_fastapi/post_processor.py,sha256=LPmaL3bhZeFSQHO3_A8CnxiBj05HmNKl0VLcG7MGzVE,2879
6
+ spakky_fastapi/post_processor.py,sha256=33czA6PLYzNxnwQ5dKz0fXjpM_7kyMf9kaY88b_GoCI,2889
7
7
  spakky_fastapi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
8
  spakky_fastapi/routing.py,sha256=3OhnnDIzaDeNhuIMWpPG17--98ti6LsgdfENsWr332g,19181
9
9
  spakky_fastapi/stereotypes/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
10
  spakky_fastapi/stereotypes/api_controller.py,sha256=hLeIYM-olZHwvFZkd1joQ-5-OQAiZXznFu4CEhrcskk,196
11
- spakky_fastapi-0.7.16.dist-info/METADATA,sha256=6DQ255miSFzNddrpuULtGIr3yorlw24DlvA7rL7dBng,1702
12
- spakky_fastapi-0.7.16.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
13
- spakky_fastapi-0.7.16.dist-info/RECORD,,
11
+ spakky_fastapi-0.7.19.dist-info/METADATA,sha256=Epf3M5LlqNrHnnwrv0UDDnIuSKW4wRqo11C91P2BS38,1702
12
+ spakky_fastapi-0.7.19.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
13
+ spakky_fastapi-0.7.19.dist-info/RECORD,,