Skip to content

Commit 93243eb

Browse files
authored
Merge pull request #22 from python-ellar/pydanticv2_support
feat(Pydantic): Pydantic V2 upgrade
2 parents 48742cf + ee7db0c commit 93243eb

File tree

8 files changed

+40
-39
lines changed

8 files changed

+40
-39
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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ clean: ## Removing cached python compiled files
99
find . -name \*pyo | xargs rm -fv
1010
find . -name \*~ | xargs rm -fv
1111
find . -name __pycache__ | xargs rm -rfv
12+
find . -name .ruff_cache | xargs rm -rfv
1213

1314
install: ## Install dependencies
1415
flit install --deps develop --symlink
@@ -17,13 +18,12 @@ install-full: ## Install dependencies
1718
make install
1819
pre-commit install -f
1920

20-
lint: ## Run code linters
21-
black --check ellar_jwt tests
21+
lint:fmt ## Run code linters
2222
ruff check ellar_jwt tests
2323
mypy ellar_jwt
2424

25-
fmt format: ## Run code formatters
26-
black ellar_jwt tests
25+
fmt format:clean ## Run code formatters
26+
ruff format ellar_jwt tests
2727
ruff check --fix ellar_jwt tests
2828

2929
test: ## Run tests

ellar_jwt/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""JWT Module for Ellar"""
22

3-
__version__ = "0.1.8"
3+
__version__ = "0.2.0"
44
from .module import JWTModule
55
from .schemas import JWTConfiguration
66
from .services import JWTService

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: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import anyio
66
import jwt
7+
from ellar.common import serialize_object
78
from ellar.di import injectable
89
from jwt import InvalidAlgorithmError, InvalidTokenError, PyJWKClient, PyJWKClientError
910

@@ -20,7 +21,9 @@ def __init__(self, jwt_config: JWTConfiguration) -> None:
2021
self.jwt_config = jwt_config
2122

2223
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
24+
jwks_client = (
25+
PyJWKClient(str(jwt_config.jwk_url)) if jwt_config.jwk_url else None
26+
)
2427
return jwks_client
2528

2629
def get_leeway(self, jwt_config: JWTConfiguration) -> timedelta:
@@ -60,7 +63,9 @@ def sign(
6063
Returns an encoded token for the given payload dictionary.
6164
"""
6265
_jwt_config = self._merge_configurations(**jwt_config)
63-
jwt_payload = Token(jwt_config=_jwt_config).build(payload.copy())
66+
jwt_payload = Token(jwt_config=_jwt_config).build(
67+
serialize_object(payload.copy())
68+
)
6469

6570
return jwt.encode(
6671
jwt_payload,
@@ -93,7 +98,7 @@ def decode(
9398
"""
9499
try:
95100
_jwt_config = self._merge_configurations(**jwt_config)
96-
return jwt.decode( # type: ignore[no-any-return]
101+
return jwt.decode( # type:ignore[no-any-return]
97102
token,
98103
self.get_verifying_key(token, _jwt_config),
99104
algorithms=[_jwt_config.algorithm],

mypy.ini

Lines changed: 0 additions & 19 deletions
This file was deleted.

pyproject.toml

Lines changed: 20 additions & 4 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
]
@@ -62,11 +62,8 @@ Homepage = "https://eadwincode.github.io/ellar-jwt/"
6262
test = [
6363
"pytest >= 7.1.3,<8.0.0",
6464
"pytest-cov >= 2.12.0,<5.0.0",
65-
"mypy == 1.5.1",
6665
"ruff ==0.1.7",
6766
"mypy == 1.7.1",
68-
"ruff ==0.1.7",
69-
"black == 23.11.0",
7067
"pytest-asyncio",
7168
"autoflake",
7269
"types-python-dateutil",
@@ -93,3 +90,22 @@ ignore = [
9390

9491
[tool.ruff.isort]
9592
known-third-party = ["ellar"]
93+
94+
[tool.mypy]
95+
96+
show_column_numbers = true
97+
98+
follow_imports = 'normal'
99+
ignore_missing_imports = true
100+
101+
# be strict
102+
disallow_untyped_calls = true
103+
warn_return_any = true
104+
strict_optional = true
105+
warn_no_return = true
106+
warn_redundant_casts = true
107+
warn_unused_ignores = true
108+
109+
disallow_untyped_defs = true
110+
check_untyped_defs = true
111+
implicit_reexport = false

tests/test_jwt_service.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def default(self, obj):
4141

4242
class TestJWTService:
4343
hmac_token_backend = JWTService(
44-
JWTConfiguration(algorithm="HS256", signing_secret_key=SECRET)
44+
JWTConfiguration(algorithm="HS256", signing_secret_key=SECRET),
4545
)
4646
hmac_leeway_token_backend = JWTService(
4747
JWTConfiguration(algorithm="HS256", signing_secret_key=SECRET, leeway=LEEWAY)
@@ -95,7 +95,8 @@ def test_init(self):
9595
# Should reject unknown algorithms
9696
with pytest.raises(ValueError):
9797
JWTConfiguration(
98-
algorithm="oienarst oieanrsto i", signing_secret_key="not_secret"
98+
algorithm="oienarst oieanrsto i",
99+
signing_secret_key="not_secret",
99100
)
100101

101102
JWTConfiguration(algorithm="HS256", signing_secret_key="not_secret")

0 commit comments

Comments
 (0)