Skip to content

Commit 346ad82

Browse files
committed
Make PyJWT an optional dependency
1 parent 8427c7b commit 346ad82

File tree

5 files changed

+9
-5
lines changed

5 files changed

+9
-5
lines changed

redis/auth/token.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from abc import ABC, abstractmethod
22
from datetime import datetime, timezone
33

4-
import jwt
54
from redis.auth.err import InvalidTokenSchemaErr
65

76

@@ -81,6 +80,10 @@ class JWToken(TokenInterface):
8180
REQUIRED_FIELDS = {"exp"}
8281

8382
def __init__(self, token: str):
83+
try:
84+
import jwt
85+
except ImportError as ie:
86+
raise ImportError(f"The PyJWT library is required for {self.__class__.__name__}.") from ie
8487
self._value = token
8588
self._decoded = jwt.decode(
8689
self._value,

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
python_requires=">=3.8",
3939
install_requires=[
4040
'async-timeout>=4.0.3; python_full_version<"3.11.3"',
41-
"PyJWT~=2.9.0",
4241
],
4342
classifiers=[
4443
"Development Status :: 5 - Production/Stable",
@@ -61,5 +60,6 @@
6160
extras_require={
6261
"hiredis": ["hiredis>=3.0.0"],
6362
"ocsp": ["cryptography>=36.0.1", "pyopenssl==23.2.1", "requests>=2.31.0"],
63+
"jwt": ["PyJWT~=2.9.0"],
6464
},
6565
)

tests/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from unittest.mock import Mock
1111
from urllib.parse import urlparse
1212

13-
import jwt
1413
import pytest
1514
import redis
1615
from packaging.version import Version
@@ -615,6 +614,7 @@ def cache_key(request) -> CacheKey:
615614

616615

617616
def mock_identity_provider() -> IdentityProviderInterface:
617+
jwt = pytest.importorskip("jwt")
618618
mock_provider = Mock(spec=IdentityProviderInterface)
619619
token = {"exp": datetime.now(timezone.utc).timestamp() + 3600, "oid": "username"}
620620
encoded = jwt.encode(token, "secret", algorithm="HS256")

tests/test_asyncio/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from enum import Enum
66
from typing import Union
77

8-
import jwt
98
import pytest
109
import pytest_asyncio
1110
import redis.asyncio as redis
@@ -247,6 +246,7 @@ async def mock_cluster_resp_slaves(create_redis, **kwargs):
247246

248247

249248
def mock_identity_provider() -> IdentityProviderInterface:
249+
jwt = pytest.importorskip("jwt")
250250
mock_provider = Mock(spec=IdentityProviderInterface)
251251
token = {"exp": datetime.now(timezone.utc).timestamp() + 3600, "oid": "username"}
252252
encoded = jwt.encode(token, "secret", algorithm="HS256")

tests/test_auth/test_token.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from datetime import datetime, timezone
22

3-
import jwt
43
import pytest
54
from redis.auth.err import InvalidTokenSchemaErr
65
from redis.auth.token import JWToken, SimpleToken
@@ -39,6 +38,8 @@ def test_simple_token(self):
3938
assert token.get_expires_at_ms() == -1
4039

4140
def test_jwt_token(self):
41+
jwt = pytest.importorskip("jwt")
42+
4243
token = {
4344
"exp": datetime.now(timezone.utc).timestamp() + 100,
4445
"iat": datetime.now(timezone.utc).timestamp(),

0 commit comments

Comments
 (0)