diff --git a/AuthX/core/jwt.py b/AuthX/core/jwt.py index e49ec479..572529e7 100644 --- a/AuthX/core/jwt.py +++ b/AuthX/core/jwt.py @@ -5,17 +5,19 @@ import jwt from AuthX.core.config import JWT_ALGORITHM -from AuthX.database.redis import RedisBackend class JWTBackend: def __init__( self, - cache_backend: RedisBackend, + private_key: Optional[bytes], + public_key: bytes, access_expiration: int, refresh_expiration: int, ) -> None: - self._cache = cache_backend + + self._private_key = private_key + self._public_key = public_key self._access_expiration = access_expiration self._refresh_expiration = refresh_expiration @@ -42,7 +44,9 @@ async def _user_in_logout(self, id: int, iat: datetime) -> bool: async def decode_token(self, token: str, leeway: int = 0) -> Optional[dict]: if token: try: - payload = jwt.decode(token, leeway=leeway, algorithms=JWT_ALGORITHM,) + payload = jwt.decode( + token, self._public_key, leeway=leeway, algorithms=JWT_ALGORITHM, + ) id = payload.get("id") iat = datetime.utcfromtimestamp(int(payload.get("iat"))) checks = await asyncio.gather( @@ -71,10 +75,10 @@ def _create_token( payload.update({"iat": iat, "exp": exp, "type": token_type}) - return jwt.encode(payload, algorithm=JWT_ALGORITHM, key=None).decode() + return jwt.encode(payload, self._private_key, algorithm=JWT_ALGORITHM).decode() def create_access_token(self, payload: dict) -> str: - return self._create_token(payload, self._access_expiration, "access") + return self._create_token(payload, "access", self._access_expiration) def create_refresh_token(self, payload: dict) -> str: return self._create_token(payload, "refresh", self._refresh_expiration) diff --git a/requirements.txt b/requirements.txt index 23736751..ca99e0c9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,9 +6,7 @@ astroid async-timeout atomicwrites attrs -autoflake bcrypt -black certifi cffi charset-normalizer @@ -19,7 +17,6 @@ cryptography dnspython email-validator fastapi -flake8 h11 httpcore httpx diff --git a/tests/core/test_core_email.py b/tests/core/test_core_email.py new file mode 100644 index 00000000..ba59c2ba --- /dev/null +++ b/tests/core/test_core_email.py @@ -0,0 +1,14 @@ +from unittest import mock + +import pytest + +from AuthX.core.email import EmailClient + + +@pytest.mark.asyncio +@pytest.mark.parametrize("language", ["en"]) +@mock.patch("aiosmtplib.send", mock.AsyncMock(return_value=None)) +async def test_email_client(language: str): + email_client = EmailClient(None, None, None, None, language, "", "", "") + await email_client.send_confirmation_email("", "") + await email_client.send_forgot_password_email("", "")