Skip to content

Commit

Permalink
Merge pull request #206 from rahulraina7/master
Browse files Browse the repository at this point in the history
Add support for PyJwt_2_0_0
  • Loading branch information
ahopkins authored Jan 5, 2021
2 parents 86ad444 + eb31e97 commit 8f79086
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 58 deletions.
6 changes: 3 additions & 3 deletions sanic_jwt/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,14 @@ async def _get_secret(self, token=None, payload=None, encode=False):
if not payload:
algorithm = self._get_algorithm()
payload = jwt.decode(
token, verify=False, algorithms=[algorithm]
token, options={"verify_signature": False}, algorithms=[algorithm]
)
user_id = payload.get("user_id")
return await utils.call(
self.retrieve_user_secret,
user_id=user_id,
encode=self._is_asymmetric and encode,
)

if self._is_asymmetric and encode:
return self.config.private_key()

Expand Down Expand Up @@ -495,7 +494,8 @@ async def generate_access_token(
extend_payload, payload=payload, user=user
)

return jwt.encode(payload, secret, algorithm=algorithm).decode("utf-8")
access_token = jwt.encode(payload, secret, algorithm=algorithm)
return access_token

async def generate_refresh_token(self, request, user):
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def open_local(paths, mode="r", encoding="utf8"):
for reqs in extras_require.values():
extras_require["all"].extend(reqs)

install_requires = ["pyjwt"]
install_requires = ["pyjwt==2.0.0",]

setup(
name="sanic-jwt",
Expand Down
43 changes: 22 additions & 21 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from sanic_jwt import Claim, exceptions, Initialize
from sanic_jwt.decorators import protected

Sanic.test_mode = True

class User:
def __init__(self, id, username, password):
Expand All @@ -17,22 +18,22 @@ def to_dict(self):
return {prop: getattr(self, prop, None) for prop in properties}


@pytest.yield_fixture
@pytest.fixture
def users():
yield [User(1, "user1", "abcxyz"), User(2, "user2", "abcxyz")]


@pytest.yield_fixture
@pytest.fixture
def username_table(users):
yield {u.username: u for u in users}


@pytest.yield_fixture
@pytest.fixture
def userid_table(users):
yield {u.user_id: u for u in users}


@pytest.yield_fixture
@pytest.fixture
def authenticate(username_table):
async def authenticate(request, *args, **kwargs):
username = request.json.get("username", None)
Expand All @@ -55,7 +56,7 @@ async def authenticate(request, *args, **kwargs):
yield authenticate


@pytest.yield_fixture
@pytest.fixture
def retrieve_user(userid_table):
async def retrieve_user(request, payload, *args, **kwargs):
if payload:
Expand All @@ -69,15 +70,15 @@ async def retrieve_user(request, payload, *args, **kwargs):
yield retrieve_user


@pytest.yield_fixture
@pytest.fixture
def retrieve_user_secret():
async def retrieve_user_secret(user_id, **kwargs):
return f"foobar<{user_id}>"

yield retrieve_user_secret


@pytest.yield_fixture
@pytest.fixture
def app(username_table, authenticate):

sanic_app = Sanic("sanic-jwt-test")
Expand Down Expand Up @@ -109,7 +110,7 @@ def protected_regression_verify(request, verify):
yield (sanic_app, sanic_jwt)


@pytest.yield_fixture
@pytest.fixture
def app_with_refresh_token(username_table, authenticate):

sanic_app = Sanic("sanic-jwt-test")
Expand All @@ -124,7 +125,7 @@ def app_with_refresh_token(username_table, authenticate):
yield (sanic_app, sanic_jwt)


@pytest.yield_fixture
@pytest.fixture
def app_with_user_secrets(username_table, authenticate, retrieve_user_secret):

sanic_app = Sanic("sanic-jwt-test")
Expand All @@ -143,7 +144,7 @@ async def protected_request(request):
yield (sanic_app, sanic_jwt)


@pytest.yield_fixture
@pytest.fixture
def app_with_url_prefix(username_table, authenticate):

sanic_app = Sanic("sanic-jwt-test")
Expand All @@ -163,7 +164,7 @@ async def protected_request(request):
yield (sanic_app, sanic_jwt)


@pytest.yield_fixture
@pytest.fixture
def app_with_bp_setup_without_init(username_table, authenticate):
sanic_app = Sanic("sanic-jwt-test")

Expand All @@ -190,7 +191,7 @@ async def bp_protected_request(request):
yield (sanic_app, sanic_bp)


@pytest.yield_fixture
@pytest.fixture
def app_with_bp(app_with_bp_setup_without_init):
sanic_app, sanic_bp = app_with_bp_setup_without_init
sanic_jwt_init = Initialize(sanic_app, authenticate=authenticate)
Expand All @@ -202,7 +203,7 @@ def app_with_bp(app_with_bp_setup_without_init):
yield (sanic_app, sanic_jwt_init, sanic_bp, sanic_jwt_init_bp)


@pytest.yield_fixture
@pytest.fixture
def app_with_extended_exp(username_table, authenticate):

sanic_app = Sanic("sanic-jwt-test")
Expand All @@ -222,7 +223,7 @@ async def protected_request(request):
yield (sanic_app, sanic_jwt)


@pytest.yield_fixture
@pytest.fixture
def app_with_leeway(username_table, authenticate):

sanic_app = Sanic("sanic-jwt-test")
Expand All @@ -242,7 +243,7 @@ async def protected_request(request):
yield (sanic_app, sanic_jwt)


@pytest.yield_fixture
@pytest.fixture
def app_with_nbf(username_table, authenticate):

sanic_app = Sanic("sanic-jwt-test")
Expand All @@ -265,7 +266,7 @@ async def protected_request(request):
yield (sanic_app, sanic_jwt)


@pytest.yield_fixture
@pytest.fixture
def app_with_iat(username_table, authenticate):

sanic_app = Sanic("sanic-jwt-test")
Expand All @@ -285,7 +286,7 @@ async def protected_request(request):
yield (sanic_app, sanic_jwt)


@pytest.yield_fixture
@pytest.fixture
def app_with_iss(username_table, authenticate):

sanic_app = Sanic("sanic-jwt-test")
Expand All @@ -305,7 +306,7 @@ async def protected_request(request):
yield (sanic_app, sanic_jwt)


@pytest.yield_fixture
@pytest.fixture
def app_with_aud(username_table, authenticate):

sanic_app = Sanic("sanic-jwt-test")
Expand All @@ -325,7 +326,7 @@ async def protected_request(request):
yield (sanic_app, sanic_jwt)


@pytest.yield_fixture
@pytest.fixture
def app_with_retrieve_user(retrieve_user, authenticate):

sanic_app = Sanic("sanic-jwt-test")
Expand All @@ -345,7 +346,7 @@ async def protected_request(request):
yield (sanic_app, sanic_jwt)


@pytest.yield_fixture
@pytest.fixture
def app_with_extra_verification(authenticate):
def user2(payload):
return payload.get("user_id") == 2
Expand All @@ -367,7 +368,7 @@ async def protected_request(request):
yield (sanic_app, sanic_jwt)


@pytest.yield_fixture
@pytest.fixture
def app_with_custom_claims(authenticate):
class User2Claim(Claim):
key = "username"
Expand Down
4 changes: 2 additions & 2 deletions tests/test_authentication_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from sanic_jwt import Authentication, Initialize


@pytest.yield_fixture
@pytest.fixture
def app1():
class MyAuthentication(Authentication):
async def store_refresh_token(self, *args, **kwargs):
Expand All @@ -30,7 +30,7 @@ def extract_payload(self, request, verify=True, *args, **kwargs):
yield app


@pytest.yield_fixture
@pytest.fixture
def app2():
class MyAuthentication(Authentication):
async def store_refresh_token(self, *args, **kwargs):
Expand Down
2 changes: 2 additions & 0 deletions tests/test_claims.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def test_nbf(app_with_nbf):
sanic_jwt.config.secret(),
algorithms=sanic_jwt.config.algorithm(),
verify=False,
leeway=60*9
)
exp = payload.get("exp", None)
exp = datetime.utcfromtimestamp(exp)
Expand Down Expand Up @@ -265,6 +266,7 @@ def test_aud(app_with_aud):
sanic_jwt.config.secret(),
algorithms=sanic_jwt.config.algorithm(),
verify=False,
audience=sanic_jwt.config.claim_aud()
)

assert "aud" in payload
Expand Down
10 changes: 5 additions & 5 deletions tests/test_complete_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
from sanic_jwt import Authentication, exceptions, Initialize, protected


@pytest.yield_fixture
@pytest.fixture
def cache():
yield {}


@pytest.yield_fixture
@pytest.fixture
def my_authentication_class(users, cache):
class MyAuthentication(Authentication):
async def authenticate(self, request, *args, **kwargs):
Expand Down Expand Up @@ -70,7 +70,7 @@ async def extend_payload(self, payload, user=None, *args, **kwargs):
yield MyAuthentication


@pytest.yield_fixture
@pytest.fixture
def sanic_app(users, my_authentication_class, cache):
sanic_app = Sanic("sanic-jwt-test")

Expand All @@ -86,7 +86,7 @@ async def protected_request(request):
yield sanic_app


@pytest.yield_fixture
@pytest.fixture
def app_full_auth_cls(sanic_app, my_authentication_class):

sanicjwt = Initialize(
Expand All @@ -98,7 +98,7 @@ def app_full_auth_cls(sanic_app, my_authentication_class):
yield (sanic_app, sanicjwt)


@pytest.yield_fixture
@pytest.fixture
def app_full_bytes_refresh_token(
users, sanic_app, my_authentication_class, cache
):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_endpoints_async_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def user_id(self):
users = [User(1, "user1", "abcxyz")]


@pytest.yield_fixture
@pytest.fixture
def app_with_async_methods():

cache = {}
Expand Down Expand Up @@ -106,7 +106,7 @@ async def protected_request(request):


class TestEndpointsAsync(object):
@pytest.yield_fixture
@pytest.fixture
def authenticated_response(self, app_with_async_methods):
app, sanicjwt = app_with_async_methods
_, response = app.test_client.post(
Expand Down
4 changes: 2 additions & 2 deletions tests/test_endpoints_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from sanic_jwt import Initialize, protected


@pytest.yield_fixture
@pytest.fixture
def app_with_refresh_token_and_cookie(users, authenticate):

cache = {}
Expand Down Expand Up @@ -66,7 +66,7 @@ async def protected_request(request):


class TestEndpointsCookies(object):
@pytest.yield_fixture
@pytest.fixture
def authenticated_response(self, app_with_refresh_token_and_cookie):
sanic_app, sanicjwt = app_with_refresh_token_and_cookie
_, response = sanic_app.test_client.post(
Expand Down
4 changes: 2 additions & 2 deletions tests/test_endpoints_dict_first.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ async def to_dict(self):
raise Exception("i am not supposed to be called")


@pytest.yield_fixture
@pytest.fixture
def app_with_dict_test():

the_user = MyCustomDict(user_id=1)
Expand Down Expand Up @@ -39,7 +39,7 @@ async def authenticate(request, *args, **kwargs):


class TestEndpointsAsync(object):
@pytest.yield_fixture
@pytest.fixture
def authenticated_response(self, app_with_dict_test):
app, sanicjwt = app_with_dict_test
_, response = app.test_client.post(
Expand Down
8 changes: 4 additions & 4 deletions tests/test_endpoints_jwt_cryptography.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@
from sanic_jwt.decorators import protected


@pytest.yield_fixture
@pytest.fixture
def public_rsa_key():
yield Path(__file__).parent / "resources" / "rsa-test-public.pem"


@pytest.yield_fixture
@pytest.fixture
def private_rsa_key():
yield Path(__file__).parent / "resources" / "rsa-test-key.pem"


@pytest.yield_fixture
@pytest.fixture
def public_ec_key():
yield Path(__file__).parent / "resources" / "ec-test-public.pem"


@pytest.yield_fixture
@pytest.fixture
def private_ec_key():
yield Path(__file__).parent / "resources" / "ec-test-key.pem"

Expand Down
4 changes: 2 additions & 2 deletions tests/test_endpoints_query_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from sanic_jwt import Initialize, protected


@pytest.yield_fixture
@pytest.fixture
def app_with_refresh_token(users, authenticate):

cache = {}
Expand Down Expand Up @@ -65,7 +65,7 @@ async def protected_request(request):


class TestEndpointsQueryString(object):
@pytest.yield_fixture
@pytest.fixture
def authenticated_response(self, app_with_refresh_token):
sanic_app, sanicjwt = app_with_refresh_token
_, response = sanic_app.test_client.post(
Expand Down
Loading

0 comments on commit 8f79086

Please sign in to comment.