Skip to content

Commit

Permalink
Merge pull request #22 from python-ellar/pydanticv2_support
Browse files Browse the repository at this point in the history
feat(Pydantic): Pydantic V2 upgrade
  • Loading branch information
eadwinCode authored Dec 7, 2023
2 parents 48742cf + ee7db0c commit 93243eb
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 39 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/test_full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ jobs:
run: pip install flit
- name: Install Dependencies
run: flit install --symlink
- name: Black
run: black --check ellar_jwt tests
- name: Linting check
run: ruff check ellar_jwt tests & ruff check tests
run: ruff check ellar_jwt tests
- name: mypy
run: mypy ellar_jwt
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ clean: ## Removing cached python compiled files
find . -name \*pyo | xargs rm -fv
find . -name \*~ | xargs rm -fv
find . -name __pycache__ | xargs rm -rfv
find . -name .ruff_cache | xargs rm -rfv

install: ## Install dependencies
flit install --deps develop --symlink
Expand All @@ -17,13 +18,12 @@ install-full: ## Install dependencies
make install
pre-commit install -f

lint: ## Run code linters
black --check ellar_jwt tests
lint:fmt ## Run code linters
ruff check ellar_jwt tests
mypy ellar_jwt

fmt format: ## Run code formatters
black ellar_jwt tests
fmt format:clean ## Run code formatters
ruff format ellar_jwt tests
ruff check --fix ellar_jwt tests

test: ## Run tests
Expand Down
2 changes: 1 addition & 1 deletion ellar_jwt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""JWT Module for Ellar"""

__version__ = "0.1.8"
__version__ = "0.2.0"
from .module import JWTModule
from .schemas import JWTConfiguration
from .services import JWTService
Expand Down
6 changes: 3 additions & 3 deletions ellar_jwt/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from datetime import timedelta

from ellar.common import Serializer
from ellar.pydantic import AnyUrl, Field, field_validator
from jwt import algorithms
from pydantic import AnyHttpUrl, Field, validator


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

issuer: t.Optional[str] = Field(None)
jwk_url: t.Optional[AnyHttpUrl] = Field(None)
jwk_url: t.Optional[AnyUrl] = Field(None)

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

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

@validator("algorithm")
@field_validator("algorithm", mode="before")
def _validate_algorithm(cls, value: str) -> str:
"""
Ensure that the nominated algorithm is recognized, and that cryptography is installed for those
Expand Down
11 changes: 8 additions & 3 deletions ellar_jwt/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import anyio
import jwt
from ellar.common import serialize_object
from ellar.di import injectable
from jwt import InvalidAlgorithmError, InvalidTokenError, PyJWKClient, PyJWKClientError

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

def get_jwks_client(self, jwt_config: JWTConfiguration) -> t.Optional[PyJWKClient]:
jwks_client = PyJWKClient(jwt_config.jwk_url) if jwt_config.jwk_url else None
jwks_client = (
PyJWKClient(str(jwt_config.jwk_url)) if jwt_config.jwk_url else None
)
return jwks_client

def get_leeway(self, jwt_config: JWTConfiguration) -> timedelta:
Expand Down Expand Up @@ -60,7 +63,9 @@ def sign(
Returns an encoded token for the given payload dictionary.
"""
_jwt_config = self._merge_configurations(**jwt_config)
jwt_payload = Token(jwt_config=_jwt_config).build(payload.copy())
jwt_payload = Token(jwt_config=_jwt_config).build(
serialize_object(payload.copy())
)

return jwt.encode(
jwt_payload,
Expand Down Expand Up @@ -93,7 +98,7 @@ def decode(
"""
try:
_jwt_config = self._merge_configurations(**jwt_config)
return jwt.decode( # type: ignore[no-any-return]
return jwt.decode( # type:ignore[no-any-return]
token,
self.get_verifying_key(token, _jwt_config),
algorithms=[_jwt_config.algorithm],
Expand Down
19 changes: 0 additions & 19 deletions mypy.ini

This file was deleted.

24 changes: 20 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ classifiers = [
]

dependencies = [
"ellar >= 0.4.4",
"ellar >= 0.5.8",
"pyjwt>=1.7.1,<3",
"pyjwt[crypto]"
]
Expand All @@ -62,11 +62,8 @@ Homepage = "https://eadwincode.github.io/ellar-jwt/"
test = [
"pytest >= 7.1.3,<8.0.0",
"pytest-cov >= 2.12.0,<5.0.0",
"mypy == 1.5.1",
"ruff ==0.1.7",
"mypy == 1.7.1",
"ruff ==0.1.7",
"black == 23.11.0",
"pytest-asyncio",
"autoflake",
"types-python-dateutil",
Expand All @@ -93,3 +90,22 @@ ignore = [

[tool.ruff.isort]
known-third-party = ["ellar"]

[tool.mypy]

show_column_numbers = true

follow_imports = 'normal'
ignore_missing_imports = true

# be strict
disallow_untyped_calls = true
warn_return_any = true
strict_optional = true
warn_no_return = true
warn_redundant_casts = true
warn_unused_ignores = true

disallow_untyped_defs = true
check_untyped_defs = true
implicit_reexport = false
5 changes: 3 additions & 2 deletions tests/test_jwt_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def default(self, obj):

class TestJWTService:
hmac_token_backend = JWTService(
JWTConfiguration(algorithm="HS256", signing_secret_key=SECRET)
JWTConfiguration(algorithm="HS256", signing_secret_key=SECRET),
)
hmac_leeway_token_backend = JWTService(
JWTConfiguration(algorithm="HS256", signing_secret_key=SECRET, leeway=LEEWAY)
Expand Down Expand Up @@ -95,7 +95,8 @@ def test_init(self):
# Should reject unknown algorithms
with pytest.raises(ValueError):
JWTConfiguration(
algorithm="oienarst oieanrsto i", signing_secret_key="not_secret"
algorithm="oienarst oieanrsto i",
signing_secret_key="not_secret",
)

JWTConfiguration(algorithm="HS256", signing_secret_key="not_secret")
Expand Down

0 comments on commit 93243eb

Please sign in to comment.