Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make PyJWT an optional dependency #3518

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion redis/auth/token.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from abc import ABC, abstractmethod
from datetime import datetime, timezone

import jwt
from redis.auth.err import InvalidTokenSchemaErr


Expand Down Expand Up @@ -81,6 +80,12 @@ class JWToken(TokenInterface):
REQUIRED_FIELDS = {"exp"}

def __init__(self, token: str):
try:
import jwt
except ImportError as ie:
raise ImportError(
f"The PyJWT library is required for {self.__class__.__name__}.",
) from ie
self._value = token
self._decoded = jwt.decode(
self._value,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
python_requires=">=3.8",
install_requires=[
'async-timeout>=4.0.3; python_full_version<"3.11.3"',
"PyJWT~=2.9.0",
],
classifiers=[
"Development Status :: 5 - Production/Stable",
Expand All @@ -61,5 +60,6 @@
extras_require={
"hiredis": ["hiredis>=3.0.0"],
"ocsp": ["cryptography>=36.0.1", "pyopenssl==23.2.1", "requests>=2.31.0"],
"jwt": ["PyJWT~=2.9.0"],
},
)
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from unittest.mock import Mock
from urllib.parse import urlparse

import jwt
import pytest
import redis
from packaging.version import Version
Expand Down Expand Up @@ -615,6 +614,7 @@ def cache_key(request) -> CacheKey:


def mock_identity_provider() -> IdentityProviderInterface:
jwt = pytest.importorskip("jwt")
mock_provider = Mock(spec=IdentityProviderInterface)
token = {"exp": datetime.now(timezone.utc).timestamp() + 3600, "oid": "username"}
encoded = jwt.encode(token, "secret", algorithm="HS256")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_asyncio/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from enum import Enum
from typing import Union

import jwt
import pytest
import pytest_asyncio
import redis.asyncio as redis
Expand Down Expand Up @@ -247,6 +246,7 @@ async def mock_cluster_resp_slaves(create_redis, **kwargs):


def mock_identity_provider() -> IdentityProviderInterface:
jwt = pytest.importorskip("jwt")
mock_provider = Mock(spec=IdentityProviderInterface)
token = {"exp": datetime.now(timezone.utc).timestamp() + 3600, "oid": "username"}
encoded = jwt.encode(token, "secret", algorithm="HS256")
Expand Down
3 changes: 2 additions & 1 deletion tests/test_auth/test_token.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from datetime import datetime, timezone

import jwt
import pytest
from redis.auth.err import InvalidTokenSchemaErr
from redis.auth.token import JWToken, SimpleToken
Expand Down Expand Up @@ -39,6 +38,8 @@ def test_simple_token(self):
assert token.get_expires_at_ms() == -1

def test_jwt_token(self):
jwt = pytest.importorskip("jwt")

token = {
"exp": datetime.now(timezone.utc).timestamp() + 100,
"iat": datetime.now(timezone.utc).timestamp(),
Expand Down
Loading