From c148db18cac11604506f30ea88def9c5492ee50f Mon Sep 17 00:00:00 2001 From: Yasser Tahiri Date: Sat, 18 Sep 2021 15:07:07 +0100 Subject: [PATCH 1/3] Fix JWT File --- AuthX/core/jwt.py | 19 ++++++++++++------- requirements.txt | 3 --- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/AuthX/core/jwt.py b/AuthX/core/jwt.py index e49ec479..f743483b 100644 --- a/AuthX/core/jwt.py +++ b/AuthX/core/jwt.py @@ -3,19 +3,22 @@ from typing import Optional 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 +45,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 +76,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 From 42bd7fcb39b654d865250679d11ce5c4639aecd1 Mon Sep 17 00:00:00 2001 From: Yasser Tahiri Date: Sat, 18 Sep 2021 15:07:23 +0100 Subject: [PATCH 2/3] Test Email --- tests/core/test_core_email.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 tests/core/test_core_email.py 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("", "") From 7f214bdc9a5a79e5c84a03c0c7ca2315a368547d Mon Sep 17 00:00:00 2001 From: Yasser Tahiri Date: Sat, 18 Sep 2021 15:07:53 +0100 Subject: [PATCH 3/3] Lint files --- AuthX/core/jwt.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/AuthX/core/jwt.py b/AuthX/core/jwt.py index f743483b..572529e7 100644 --- a/AuthX/core/jwt.py +++ b/AuthX/core/jwt.py @@ -3,14 +3,13 @@ from typing import Optional import jwt -from AuthX.core.config import JWT_ALGORITHM +from AuthX.core.config import JWT_ALGORITHM class JWTBackend: def __init__( self, - private_key: Optional[bytes], public_key: bytes, access_expiration: int,