|
4 | 4 |
|
5 | 5 | import anyio |
6 | 6 | import jwt |
| 7 | +from ellar.common import serialize_object |
| 8 | +from ellar.core import Config |
7 | 9 | from ellar.di import injectable |
8 | 10 | from jwt import InvalidAlgorithmError, InvalidTokenError, PyJWKClient, PyJWKClientError |
9 | 11 |
|
|
16 | 18 |
|
17 | 19 | @injectable |
18 | 20 | class JWTService: |
19 | | - def __init__(self, jwt_config: JWTConfiguration) -> None: |
| 21 | + def __init__(self, jwt_config: JWTConfiguration, config: Config) -> None: |
20 | 22 | self.jwt_config = jwt_config |
| 23 | + self._encoders = config.SERIALIZER_CUSTOM_ENCODER |
21 | 24 |
|
22 | 25 | def get_jwks_client(self, jwt_config: JWTConfiguration) -> t.Optional[PyJWKClient]: |
23 | | - jwks_client = PyJWKClient(jwt_config.jwk_url) if jwt_config.jwk_url else None |
| 26 | + jwks_client = ( |
| 27 | + PyJWKClient(str(jwt_config.jwk_url)) if jwt_config.jwk_url else None |
| 28 | + ) |
24 | 29 | return jwks_client |
25 | 30 |
|
26 | 31 | def get_leeway(self, jwt_config: JWTConfiguration) -> timedelta: |
@@ -60,7 +65,9 @@ def sign( |
60 | 65 | Returns an encoded token for the given payload dictionary. |
61 | 66 | """ |
62 | 67 | _jwt_config = self._merge_configurations(**jwt_config) |
63 | | - jwt_payload = Token(jwt_config=_jwt_config).build(payload.copy()) |
| 68 | + jwt_payload = Token(jwt_config=_jwt_config).build( |
| 69 | + serialize_object(payload.copy(), encoders=self._encoders) |
| 70 | + ) |
64 | 71 |
|
65 | 72 | return jwt.encode( |
66 | 73 | jwt_payload, |
@@ -93,9 +100,9 @@ def decode( |
93 | 100 | """ |
94 | 101 | try: |
95 | 102 | _jwt_config = self._merge_configurations(**jwt_config) |
96 | | - return jwt.decode( # type: ignore[no-any-return] |
| 103 | + return jwt.decode( |
97 | 104 | token, |
98 | | - self.get_verifying_key(token, _jwt_config), |
| 105 | + self.get_verifying_key(token, _jwt_config), # type:ignore[arg-type] |
99 | 106 | algorithms=[_jwt_config.algorithm], |
100 | 107 | audience=_jwt_config.audience, |
101 | 108 | issuer=_jwt_config.issuer, |
|
0 commit comments