Skip to content

Commit 6899eeb

Browse files
committed
pydantic v2 upgrade: Added support for pydantic v2 and dropped black linting
1 parent 48742cf commit 6899eeb

File tree

5 files changed

+18
-15
lines changed

5 files changed

+18
-15
lines changed

.github/workflows/test_full.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ jobs:
3737
run: pip install flit
3838
- name: Install Dependencies
3939
run: flit install --symlink
40-
- name: Black
41-
run: black --check ellar_jwt tests
4240
- name: Linting check
43-
run: ruff check ellar_jwt tests & ruff check tests
41+
run: ruff check ellar_jwt tests
4442
- name: mypy
4543
run: mypy ellar_jwt

Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ install-full: ## Install dependencies
1818
pre-commit install -f
1919

2020
lint: ## Run code linters
21-
black --check ellar_jwt tests
2221
ruff check ellar_jwt tests
2322
mypy ellar_jwt
2423

2524
fmt format: ## Run code formatters
26-
black ellar_jwt tests
25+
ruff format ellar_jwt tests
2726
ruff check --fix ellar_jwt tests
2827

2928
test: ## Run tests

ellar_jwt/schemas.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from datetime import timedelta
44

55
from ellar.common import Serializer
6+
from ellar.pydantic import AnyUrl, Field, field_validator
67
from jwt import algorithms
7-
from pydantic import AnyHttpUrl, Field, validator
88

99

1010
class JWTConfiguration(Serializer):
@@ -26,14 +26,14 @@ class JWTConfiguration(Serializer):
2626
audience: t.Optional[str] = Field(None)
2727

2828
issuer: t.Optional[str] = Field(None)
29-
jwk_url: t.Optional[AnyHttpUrl] = Field(None)
29+
jwk_url: t.Optional[AnyUrl] = Field(None)
3030

3131
jti: t.Optional[str] = Field("jti")
3232
lifetime: timedelta = Field(timedelta(minutes=5))
3333

3434
json_encoder: t.Any = Field(default=json.JSONEncoder)
3535

36-
@validator("algorithm")
36+
@field_validator("algorithm", mode="before")
3737
def _validate_algorithm(cls, value: str) -> str:
3838
"""
3939
Ensure that the nominated algorithm is recognized, and that cryptography is installed for those

ellar_jwt/services.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import anyio
66
import jwt
7+
from ellar.common import serialize_object
8+
from ellar.core import Config
79
from ellar.di import injectable
810
from jwt import InvalidAlgorithmError, InvalidTokenError, PyJWKClient, PyJWKClientError
911

@@ -16,11 +18,14 @@
1618

1719
@injectable
1820
class JWTService:
19-
def __init__(self, jwt_config: JWTConfiguration) -> None:
21+
def __init__(self, jwt_config: JWTConfiguration, config: Config) -> None:
2022
self.jwt_config = jwt_config
23+
self._encoders = config.SERIALIZER_CUSTOM_ENCODER
2124

2225
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+
)
2429
return jwks_client
2530

2631
def get_leeway(self, jwt_config: JWTConfiguration) -> timedelta:
@@ -60,7 +65,9 @@ def sign(
6065
Returns an encoded token for the given payload dictionary.
6166
"""
6267
_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+
)
6471

6572
return jwt.encode(
6673
jwt_payload,
@@ -93,9 +100,9 @@ def decode(
93100
"""
94101
try:
95102
_jwt_config = self._merge_configurations(**jwt_config)
96-
return jwt.decode( # type: ignore[no-any-return]
103+
return jwt.decode(
97104
token,
98-
self.get_verifying_key(token, _jwt_config),
105+
self.get_verifying_key(token, _jwt_config), # type:ignore[arg-type]
99106
algorithms=[_jwt_config.algorithm],
100107
audience=_jwt_config.audience,
101108
issuer=_jwt_config.issuer,

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ classifiers = [
4040
]
4141

4242
dependencies = [
43-
"ellar >= 0.4.4",
43+
"ellar >= 0.5.8",
4444
"pyjwt>=1.7.1,<3",
4545
"pyjwt[crypto]"
4646
]
@@ -66,7 +66,6 @@ test = [
6666
"ruff ==0.1.7",
6767
"mypy == 1.7.1",
6868
"ruff ==0.1.7",
69-
"black == 23.11.0",
7069
"pytest-asyncio",
7170
"autoflake",
7271
"types-python-dateutil",

0 commit comments

Comments
 (0)