From 1d9d068d96db51836827fc03c42871d163f3bbb6 Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Wed, 24 Apr 2019 23:56:37 +0300 Subject: [PATCH 1/2] Add twine to Makefile --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index d2dabb6..db54b91 100644 --- a/Makefile +++ b/Makefile @@ -22,5 +22,5 @@ black: black ./ -l 79 --safe release: clean - python setup.py sdist upload - python setup.py bdist_wheel upload + python setup.py sdist bdist_wheel + twine upload dist/* \ No newline at end of file From 1a40cb92de78dc9fa770fa367cb37c74516d6b99 Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Thu, 25 Apr 2019 11:46:38 +0300 Subject: [PATCH 2/2] Formatting, add falsy scopes --- Makefile | 1 + docs/source/conf.py | 2 +- docs/source/pages/changelog.rst | 12 +++ docs/source/pages/scoped.rst | 4 + sanic_jwt/__init__.py | 9 +- sanic_jwt/authentication.py | 20 ++-- sanic_jwt/configuration.py | 8 +- sanic_jwt/decorators.py | 99 +++++++++++-------- sanic_jwt/endpoints.py | 6 +- sanic_jwt/initialization.py | 11 +-- sanic_jwt/utils.py | 1 - setup.py | 2 +- tests/conftest.py | 13 +-- tests/test_async_options.py | 4 +- tests/test_authentication.py | 2 +- tests/test_authentication_custom.py | 2 +- tests/test_cache.py | 2 +- tests/test_claims.py | 1 + tests/test_complete_authentication.py | 8 +- tests/test_configuration.py | 2 +- tests/test_custom_claims.py | 7 +- tests/test_decorators.py | 8 +- tests/test_decorators_override_config.py | 2 +- tests/test_endpoints_async_methods.py | 3 +- tests/test_endpoints_auth.py | 1 + tests/test_endpoints_blueprint.py | 1 + tests/test_endpoints_cbv.py | 7 +- tests/test_endpoints_cookies.py | 5 +- tests/test_endpoints_custom.py | 2 +- tests/test_endpoints_dict_first.py | 2 +- tests/test_endpoints_extra.py | 6 +- tests/test_endpoints_init_on_bp_and_app.py | 1 + tests/test_endpoints_init_on_bp_multiple.py | 1 + ...test_endpoints_init_on_bp_retrieve_user.py | 2 +- tests/test_endpoints_init_on_bp_single.py | 4 +- tests/test_endpoints_jwt_cryptography.py | 4 +- tests/test_endpoints_query_string.py | 5 +- tests/test_endpoints_scoped.py | 75 +++++++++++++- tests/test_endpoints_scoped_simple.py | 3 +- tests/test_endpoints_sync_methods.py | 7 +- tests/test_exceptions.py | 1 + tests/test_extend_payload.py | 7 +- tests/test_extra_verifications.py | 4 +- tests/test_initialize.py | 8 +- tests/test_me_invalid.py | 5 +- tests/test_utils.py | 8 +- 46 files changed, 233 insertions(+), 155 deletions(-) diff --git a/Makefile b/Makefile index db54b91..d2f9740 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,7 @@ test: black: black ./ -l 79 --safe + isort -rc sanic_jwt tests release: clean python setup.py sdist bdist_wheel diff --git a/docs/source/conf.py b/docs/source/conf.py index def5ce7..eada4a2 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -56,7 +56,7 @@ # The short X.Y version. version = u"1.3" # The full version, including alpha/beta/rc tags. -release = u"1.3.0" +release = u"1.3.1" # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/docs/source/pages/changelog.rst b/docs/source/pages/changelog.rst index ce0f15c..95eccbf 100644 --- a/docs/source/pages/changelog.rst +++ b/docs/source/pages/changelog.rst @@ -4,6 +4,18 @@ Changelog The format is based on `Keep a Changelog `_ and this project adheres to `Semantic Versioning `_. +++++++++++++++++++++++++++ +Version 1.3.1 - 2019-04-25 +++++++++++++++++++++++++++ + +| **Added** +| - Support for ``False`` and ``None`` scopes. + +| **Changed** +| - Use ``request.args`` instead of ``request.query_args`` to resolve `Depracation Warning `_. +| + + ++++++++++++++++++++++++++ Version 1.3.0 - 2019-04-24 ++++++++++++++++++++++++++ diff --git a/docs/source/pages/scoped.rst b/docs/source/pages/scoped.rst index 52d9796..3cb3d9f 100644 --- a/docs/source/pages/scoped.rst +++ b/docs/source/pages/scoped.rst @@ -187,6 +187,10 @@ If you have initialized Sanic JWT on a ``Blueprint``, then you will need to pass async def users(request, id): ... +.. note:: + + If you provide a ``False`` or ``None`` value to the ``@scoped`` decorator, it will effectively remove **all** protection. This means all requests, whether authenticated or not, will be accepted. + Parameters ~~~~~~~~~~ diff --git a/sanic_jwt/__init__.py b/sanic_jwt/__init__.py index a1e8dd6..639f281 100644 --- a/sanic_jwt/__init__.py +++ b/sanic_jwt/__init__.py @@ -1,4 +1,4 @@ -__version__ = "1.3.0" +__version__ = "1.3.1" __author__ = "Adam Hopkins" __credits__ = "Richard Kuesters" @@ -7,12 +7,9 @@ from .authentication import Authentication from .claim import Claim from .configuration import Configuration -from .decorators import inject_user -from .decorators import protected -from .decorators import scoped +from .decorators import inject_user, protected, scoped from .endpoints import BaseEndpoint -from .initialization import Initialize -from .initialization import initialize +from .initialization import Initialize, initialize from .responses import Responses logging.getLogger(__name__).addHandler(logging.NullHandler()) diff --git a/sanic_jwt/authentication.py b/sanic_jwt/authentication.py index 568a4bb..d1a3d18 100644 --- a/sanic_jwt/authentication.py +++ b/sanic_jwt/authentication.py @@ -1,17 +1,17 @@ import inspect -import jwt import logging import warnings +from datetime import datetime, timedelta -from .exceptions import InvalidCustomClaimError -from .exceptions import InvalidVerification -from .exceptions import InvalidVerificationError -from .exceptions import SanicJWTException -from datetime import datetime -from datetime import timedelta +import jwt -from . import exceptions -from . import utils +from . import exceptions, utils +from .exceptions import ( + InvalidCustomClaimError, + InvalidVerification, + InvalidVerificationError, + SanicJWTException +) logger = logging.getLogger(__name__) claim_label = {"iss": "issuer", "iat": "iat", "nbf": "nbf", "aud": "audience"} @@ -279,7 +279,7 @@ def _get_token_from_query_string(self, request, refresh_token): query_string_token_name = getattr( self.config, query_string_token_name_key ) - return request.raw_args.get(query_string_token_name(), None) + return request.args.get(query_string_token_name(), None) def _get_token(self, request, refresh_token=False): """ diff --git a/sanic_jwt/configuration.py b/sanic_jwt/configuration.py index d492bf9..c7bcc9a 100644 --- a/sanic_jwt/configuration.py +++ b/sanic_jwt/configuration.py @@ -2,12 +2,8 @@ import copy import logging -from . import exceptions -from . import utils -from .cache import get_cached -from .cache import is_cached -from .cache import to_cache - +from . import exceptions, utils +from .cache import get_cached, is_cached, to_cache defaults = { "access_token_name": "access_token", diff --git a/sanic_jwt/decorators.py b/sanic_jwt/decorators.py index 4da1f6f..2b0f360 100644 --- a/sanic_jwt/decorators.py +++ b/sanic_jwt/decorators.py @@ -1,17 +1,15 @@ import logging - from contextlib import contextmanager from copy import deepcopy from functools import wraps from inspect import isawaitable + from sanic import Blueprint -from sanic.views import HTTPMethodView from sanic.response import redirect +from sanic.views import HTTPMethodView -from . import exceptions -from . import utils -from .cache import clear_cache -from .cache import to_cache +from . import exceptions, utils +from .cache import clear_cache, to_cache from .validators import validate_scopes logger = logging.getLogger(__name__) @@ -139,49 +137,61 @@ def scoped( def decorator(f): @wraps(f) async def decorated_function(request, *args, **kwargs): - protect_kwargs = deepcopy(kwargs) - protect_kwargs.update( - { - "initialized_on": initialized_on, - "kw": kw, - "request": request, - "f": f, - "return_response": False, - } - ) - _, instance = await _do_protection(*args, **protect_kwargs) - - if request.method == "OPTIONS": - return instance - - user_scopes = instance.auth.extract_scopes(request) - override = instance.auth.override_scope_validator - destructure = instance.auth.destructure_scopes - if user_scopes is None: - # If there are no defined scopes in the payload, - # deny access - is_authorized = False - status = 403 - reasons = "Invalid scope." - raise exceptions.Unauthorized(reasons, status_code=status) + if issubclass(request.__class__, HTTPMethodView): + request = args[0] - else: - is_authorized = await validate_scopes( - request, - scopes, - user_scopes, - require_all=require_all, - require_all_actions=require_all_actions, - override=override, - destructure=destructure, - request_args=args, - request_kwargs=kwargs, + if scopes is not None and scopes is not False: + protect_kwargs = deepcopy(kwargs) + protect_kwargs.update( + { + "initialized_on": initialized_on, + "kw": kw, + "request": request, + "f": f, + "return_response": False, + } ) - if not is_authorized: + _, instance = await _do_protection(*args, **protect_kwargs) + + if request.method == "OPTIONS": + return instance + + user_scopes = instance.auth.extract_scopes(request) + override = instance.auth.override_scope_validator + destructure = instance.auth.destructure_scopes + if user_scopes is None: + # If there are no defined scopes in the payload, + # deny access + is_authorized = False status = 403 reasons = "Invalid scope." + + # TODO: + # - add login_redirect_url raise exceptions.Unauthorized(reasons, status_code=status) + else: + is_authorized = await validate_scopes( + request, + scopes, + user_scopes, + require_all=require_all, + require_all_actions=require_all_actions, + override=override, + destructure=destructure, + request_args=args, + request_kwargs=kwargs, + ) + if not is_authorized: + status = 403 + reasons = "Invalid scope." + + # TODO: + # - add login_redirect_url + raise exceptions.Unauthorized( + reasons, status_code=status + ) + # the user is authorized. # run the handler method and return the response # NOTE: it's possible to use return await.utils(f, ...) in @@ -201,6 +211,9 @@ def inject_user(initialized_on=None, **kw): def decorator(f): @wraps(f) async def decorated_function(request, *args, **kwargs): + if issubclass(request.__class__, HTTPMethodView): + request = args[0] + if initialized_on and isinstance( initialized_on, Blueprint ): # noqa diff --git a/sanic_jwt/endpoints.py b/sanic_jwt/endpoints.py index c21956f..689ac2c 100644 --- a/sanic_jwt/endpoints.py +++ b/sanic_jwt/endpoints.py @@ -1,9 +1,7 @@ -from sanic.response import json -from sanic.response import text +from sanic.response import json, text from sanic.views import HTTPMethodView -from . import exceptions -from . import utils +from . import exceptions, utils from .base import BaseDerivative from .decorators import protected diff --git a/sanic_jwt/initialization.py b/sanic_jwt/initialization.py index ec224af..024520c 100644 --- a/sanic_jwt/initialization.py +++ b/sanic_jwt/initialization.py @@ -1,17 +1,12 @@ import inspect - from collections import namedtuple -from sanic import Blueprint -from sanic import Sanic +from sanic import Blueprint, Sanic -from sanic_jwt import endpoints -from sanic_jwt import exceptions +from sanic_jwt import endpoints, exceptions from sanic_jwt.authentication import Authentication from sanic_jwt.configuration import Configuration -from sanic_jwt.decorators import inject_user -from sanic_jwt.decorators import protected -from sanic_jwt.decorators import scoped +from sanic_jwt.decorators import inject_user, protected, scoped from sanic_jwt.responses import Responses _Handler = namedtuple( diff --git a/sanic_jwt/utils.py b/sanic_jwt/utils.py index ec8b21f..83c2813 100644 --- a/sanic_jwt/utils.py +++ b/sanic_jwt/utils.py @@ -3,7 +3,6 @@ import inspect import logging import os - from pathlib import Path from . import exceptions diff --git a/setup.py b/setup.py index ed836bf..a735d07 100644 --- a/setup.py +++ b/setup.py @@ -33,7 +33,7 @@ def open_local(paths, mode="r", encoding="utf8"): setup( name="sanic-jwt", - version="1.3.0", + version=version, description="JWT oauth flow for Sanic", long_description=long_description, long_description_content_type="text/markdown", diff --git a/tests/conftest.py b/tests/conftest.py index 86d9d97..b5ba10d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,13 +1,8 @@ -import pytest - -from sanic import Blueprint -from sanic import Sanic -from sanic.response import json -from sanic.response import text +from sanic import Blueprint, Sanic +from sanic.response import json, text -from sanic_jwt import Initialize -from sanic_jwt import Claim -from sanic_jwt import exceptions +import pytest +from sanic_jwt import Claim, exceptions, Initialize from sanic_jwt.decorators import protected diff --git a/tests/test_async_options.py b/tests/test_async_options.py index 694651e..bf25956 100644 --- a/tests/test_async_options.py +++ b/tests/test_async_options.py @@ -4,12 +4,12 @@ """ -import pytest +import jwt from sanic import Blueprint, Sanic from sanic.response import text from sanic.views import HTTPMethodView -import jwt +import pytest from sanic_jwt import Authentication, initialize, protected ALL_METHODS = ["GET", "OPTIONS"] diff --git a/tests/test_authentication.py b/tests/test_authentication.py index 6dc8398..e3026bd 100644 --- a/tests/test_authentication.py +++ b/tests/test_authentication.py @@ -1,7 +1,7 @@ -import pytest from sanic import Sanic from sanic.response import json +import pytest from sanic_jwt import Authentication, exceptions, Initialize diff --git a/tests/test_authentication_custom.py b/tests/test_authentication_custom.py index 919ded1..dfb7e97 100644 --- a/tests/test_authentication_custom.py +++ b/tests/test_authentication_custom.py @@ -1,6 +1,6 @@ -import pytest from sanic import Sanic +import pytest from sanic_jwt import Authentication, Initialize diff --git a/tests/test_cache.py b/tests/test_cache.py index d682a9a..cc53385 100644 --- a/tests/test_cache.py +++ b/tests/test_cache.py @@ -1,6 +1,6 @@ import pytest -from sanic_jwt.cache import is_cached from sanic_jwt import exceptions +from sanic_jwt.cache import is_cached def test_cache_is_not_running(): diff --git a/tests/test_claims.py b/tests/test_claims.py index 0d6b287..6d814e5 100644 --- a/tests/test_claims.py +++ b/tests/test_claims.py @@ -1,6 +1,7 @@ from datetime import datetime, timedelta import jwt + from freezegun import freeze_time diff --git a/tests/test_complete_authentication.py b/tests/test_complete_authentication.py index 5643490..cd9a73c 100644 --- a/tests/test_complete_authentication.py +++ b/tests/test_complete_authentication.py @@ -1,10 +1,12 @@ +from datetime import datetime, timedelta + import jwt -import pytest from sanic import Sanic from sanic.response import json -from datetime import datetime, timedelta + +import pytest from freezegun import freeze_time -from sanic_jwt import Authentication, Initialize, exceptions, protected +from sanic_jwt import Authentication, exceptions, Initialize, protected @pytest.yield_fixture diff --git a/tests/test_configuration.py b/tests/test_configuration.py index 259ae87..e174fe4 100644 --- a/tests/test_configuration.py +++ b/tests/test_configuration.py @@ -1,7 +1,7 @@ -import pytest from sanic import Sanic from sanic.response import json +import pytest from sanic_jwt import Configuration, exceptions, initialize, Initialize from sanic_jwt.configuration import ConfigItem diff --git a/tests/test_custom_claims.py b/tests/test_custom_claims.py index 01f2ecf..c0e14ad 100644 --- a/tests/test_custom_claims.py +++ b/tests/test_custom_claims.py @@ -1,9 +1,8 @@ import jwt -import pytest from sanic import Sanic -from sanic_jwt import Initialize -from sanic_jwt import Claim -from sanic_jwt import exceptions + +import pytest +from sanic_jwt import Claim, exceptions, Initialize def test_claim_initialized_properly(app_with_custom_claims): diff --git a/tests/test_decorators.py b/tests/test_decorators.py index e161eec..76d308d 100644 --- a/tests/test_decorators.py +++ b/tests/test_decorators.py @@ -1,11 +1,9 @@ from sanic import Sanic from sanic.blueprints import Blueprint -from sanic.response import json -from sanic.response import text +from sanic.response import json, text + from sanic_jwt import Initialize -from sanic_jwt.decorators import protected -from sanic_jwt.decorators import scoped -from sanic_jwt.decorators import inject_user +from sanic_jwt.decorators import inject_user, protected, scoped def test_forgotten_initialized_on_protected(): diff --git a/tests/test_decorators_override_config.py b/tests/test_decorators_override_config.py index 04f2c48..c53f0d6 100644 --- a/tests/test_decorators_override_config.py +++ b/tests/test_decorators_override_config.py @@ -1,11 +1,11 @@ from datetime import datetime, timedelta import jwt -from freezegun import freeze_time from sanic import Sanic from sanic.blueprints import Blueprint from sanic.response import json +from freezegun import freeze_time from sanic_jwt import Initialize from sanic_jwt.decorators import protected diff --git a/tests/test_endpoints_async_methods.py b/tests/test_endpoints_async_methods.py index 7ba4377..81e6567 100644 --- a/tests/test_endpoints_async_methods.py +++ b/tests/test_endpoints_async_methods.py @@ -5,8 +5,7 @@ from sanic.response import json import pytest -from sanic_jwt import Initialize -from sanic_jwt import exceptions +from sanic_jwt import exceptions, Initialize from sanic_jwt.decorators import protected diff --git a/tests/test_endpoints_auth.py b/tests/test_endpoints_auth.py index a8b25fb..db60f3b 100644 --- a/tests/test_endpoints_auth.py +++ b/tests/test_endpoints_auth.py @@ -1,4 +1,5 @@ import jwt + import pytest diff --git a/tests/test_endpoints_blueprint.py b/tests/test_endpoints_blueprint.py index b8300e3..32dd5fa 100644 --- a/tests/test_endpoints_blueprint.py +++ b/tests/test_endpoints_blueprint.py @@ -1,6 +1,7 @@ from sanic import Sanic from sanic.blueprints import Blueprint from sanic.response import json + from sanic_jwt import Initialize from sanic_jwt.decorators import protected diff --git a/tests/test_endpoints_cbv.py b/tests/test_endpoints_cbv.py index 3b9e12a..7a75594 100644 --- a/tests/test_endpoints_cbv.py +++ b/tests/test_endpoints_cbv.py @@ -1,12 +1,11 @@ import jwt - from sanic import Sanic from sanic.response import json -from sanic_jwt import exceptions -from sanic_jwt import Initialize -from sanic_jwt.decorators import protected from sanic.views import HTTPMethodView +from sanic_jwt import exceptions, Initialize +from sanic_jwt.decorators import protected + class User(object): def __init__(self, id, username, password): diff --git a/tests/test_endpoints_cookies.py b/tests/test_endpoints_cookies.py index 66a309f..5d4fc9f 100644 --- a/tests/test_endpoints_cookies.py +++ b/tests/test_endpoints_cookies.py @@ -1,13 +1,12 @@ import binascii import os +import jwt from sanic import Sanic from sanic.response import json -import jwt import pytest -from sanic_jwt import Initialize -from sanic_jwt import protected +from sanic_jwt import Initialize, protected @pytest.yield_fixture diff --git a/tests/test_endpoints_custom.py b/tests/test_endpoints_custom.py index afe2547..af9f68e 100644 --- a/tests/test_endpoints_custom.py +++ b/tests/test_endpoints_custom.py @@ -1,7 +1,7 @@ from sanic import Sanic from sanic.response import json -from sanic_jwt import Initialize, Authentication, BaseEndpoint +from sanic_jwt import Authentication, BaseEndpoint, Initialize msg = "custom {} endpoint" diff --git a/tests/test_endpoints_dict_first.py b/tests/test_endpoints_dict_first.py index f4a1f76..9eea1f3 100644 --- a/tests/test_endpoints_dict_first.py +++ b/tests/test_endpoints_dict_first.py @@ -1,6 +1,6 @@ -import pytest from sanic import Sanic +import pytest from sanic_jwt import exceptions, Initialize diff --git a/tests/test_endpoints_extra.py b/tests/test_endpoints_extra.py index 0fded77..195985f 100644 --- a/tests/test_endpoints_extra.py +++ b/tests/test_endpoints_extra.py @@ -1,7 +1,7 @@ -from sanic import Sanic, response +from sanic import response, Sanic from sanic.response import json -from sanic_jwt import initialize -from sanic_jwt import BaseEndpoint + +from sanic_jwt import BaseEndpoint, initialize class MagicLoginHandler(BaseEndpoint): diff --git a/tests/test_endpoints_init_on_bp_and_app.py b/tests/test_endpoints_init_on_bp_and_app.py index 48ffe3d..e6b7653 100644 --- a/tests/test_endpoints_init_on_bp_and_app.py +++ b/tests/test_endpoints_init_on_bp_and_app.py @@ -1,6 +1,7 @@ from sanic import Sanic from sanic.blueprints import Blueprint from sanic.response import json + from sanic_jwt import Initialize from sanic_jwt.decorators import protected diff --git a/tests/test_endpoints_init_on_bp_multiple.py b/tests/test_endpoints_init_on_bp_multiple.py index 4149fe1..91f2c85 100644 --- a/tests/test_endpoints_init_on_bp_multiple.py +++ b/tests/test_endpoints_init_on_bp_multiple.py @@ -1,6 +1,7 @@ from sanic import Sanic from sanic.blueprints import Blueprint from sanic.response import json + from sanic_jwt import Initialize from sanic_jwt.decorators import protected diff --git a/tests/test_endpoints_init_on_bp_retrieve_user.py b/tests/test_endpoints_init_on_bp_retrieve_user.py index 3481500..cb84ff3 100644 --- a/tests/test_endpoints_init_on_bp_retrieve_user.py +++ b/tests/test_endpoints_init_on_bp_retrieve_user.py @@ -1,5 +1,5 @@ -from sanic_jwt import Initialize import pytest +from sanic_jwt import Initialize @pytest.fixture diff --git a/tests/test_endpoints_init_on_bp_single.py b/tests/test_endpoints_init_on_bp_single.py index d1b58ba..2d02fd7 100644 --- a/tests/test_endpoints_init_on_bp_single.py +++ b/tests/test_endpoints_init_on_bp_single.py @@ -1,9 +1,9 @@ from sanic import Sanic from sanic.blueprints import Blueprint from sanic.response import json + from sanic_jwt import Initialize -from sanic_jwt.decorators import protected -from sanic_jwt.decorators import scoped +from sanic_jwt.decorators import protected, scoped blueprint = Blueprint("Test") diff --git a/tests/test_endpoints_jwt_cryptography.py b/tests/test_endpoints_jwt_cryptography.py index e9b4440..e66e05d 100644 --- a/tests/test_endpoints_jwt_cryptography.py +++ b/tests/test_endpoints_jwt_cryptography.py @@ -2,11 +2,11 @@ import os from pathlib import Path -import pytest from sanic import Sanic from sanic.response import json -from sanic_jwt import Configuration, Initialize, exceptions +import pytest +from sanic_jwt import Configuration, exceptions, Initialize from sanic_jwt.decorators import protected diff --git a/tests/test_endpoints_query_string.py b/tests/test_endpoints_query_string.py index 591f789..5cc5297 100644 --- a/tests/test_endpoints_query_string.py +++ b/tests/test_endpoints_query_string.py @@ -1,13 +1,12 @@ import binascii import os +import jwt from sanic import Sanic from sanic.response import json -import jwt import pytest -from sanic_jwt import Initialize -from sanic_jwt import protected +from sanic_jwt import Initialize, protected @pytest.yield_fixture diff --git a/tests/test_endpoints_scoped.py b/tests/test_endpoints_scoped.py index c644f1b..9c6eeb4 100644 --- a/tests/test_endpoints_scoped.py +++ b/tests/test_endpoints_scoped.py @@ -1,7 +1,7 @@ -import pytest from sanic import Sanic from sanic.response import json, text +import pytest from sanic_jwt import exceptions, Initialize from sanic_jwt.decorators import protected, scoped @@ -148,6 +148,11 @@ async def client_id_async_scope(request, *args, **kwargs): async def protected_route9(request, id): return json({"protected": True, "scoped": True, "id": id}) + @sanic_app.route("/protected/scoped/10") + @scoped(None) + async def protected_route10(request): + return json({"protected": False, "scoped": False}) + yield sanic_app @@ -345,6 +350,11 @@ def test_scopes_anonymous_user(self, app_with_scopes): "reasons" ) + _, response = sanic_app.test_client.get("/protected/scoped/10") + assert response.status == 200 + assert response.json.get("protected") is False + assert response.json.get("scoped") is False + def test_scopes_user1(self, app_with_scopes, user1): sanic_app, sanicjwt = app_with_scopes access_token = user1.json.get( @@ -466,6 +476,15 @@ def test_scopes_user1(self, app_with_scopes, user1): assert response.json.get("exception") == "Unauthorized" assert "Invalid scope." in response.json.get("reasons") + _, response = sanic_app.test_client.get( + "/protected/scoped/10", + headers={"Authorization": "Bearer {}".format(access_token)}, + ) + + assert response.status == 200 + assert response.json.get("protected") is False + assert response.json.get("scoped") is False + def test_scopes_user2(self, app_with_scopes, user2): sanic_app, sanicjwt = app_with_scopes access_token = user2.json.get( @@ -585,6 +604,15 @@ def test_scopes_user2(self, app_with_scopes, user2): assert response.status == 403 assert "Invalid scope." in response.json.get("reasons") + _, response = sanic_app.test_client.get( + "/protected/scoped/10", + headers={"Authorization": "Bearer {}".format(access_token)}, + ) + + assert response.status == 200 + assert response.json.get("protected") is False + assert response.json.get("scoped") is False + def test_scopes_user3(self, app_with_scopes, user3): sanic_app, sanicjwt = app_with_scopes access_token = user3.json.get( @@ -703,6 +731,15 @@ def test_scopes_user3(self, app_with_scopes, user3): assert response.json.get("exception") == "Unauthorized" assert "Invalid scope." in response.json.get("reasons") + _, response = sanic_app.test_client.get( + "/protected/scoped/10", + headers={"Authorization": "Bearer {}".format(access_token)}, + ) + + assert response.status == 200 + assert response.json.get("protected") is False + assert response.json.get("scoped") is False + def test_scopes_user4(self, app_with_scopes, user4): sanic_app, sanicjwt = app_with_scopes access_token = user4.json.get( @@ -842,6 +879,15 @@ def test_scopes_user4(self, app_with_scopes, user4): assert response.json.get("exception") == "Unauthorized" assert "Invalid scope." in response.json.get("reasons") + _, response = sanic_app.test_client.get( + "/protected/scoped/10", + headers={"Authorization": "Bearer {}".format(access_token)}, + ) + + assert response.status == 200 + assert response.json.get("protected") is False + assert response.json.get("scoped") is False + def test_scopes_user5(self, app_with_scopes, user5): sanic_app, sanicjwt = app_with_scopes access_token = user5.json.get( @@ -961,6 +1007,15 @@ def test_scopes_user5(self, app_with_scopes, user5): assert response.json.get("exception") == "Unauthorized" assert "Invalid scope." in response.json.get("reasons") + _, response = sanic_app.test_client.get( + "/protected/scoped/10", + headers={"Authorization": "Bearer {}".format(access_token)}, + ) + + assert response.status == 200 + assert response.json.get("protected") is False + assert response.json.get("scoped") is False + def test_scopes_user6(self, app_with_scopes, user6): sanic_app, sanicjwt = app_with_scopes access_token = user6.json.get( @@ -1080,6 +1135,15 @@ def test_scopes_user6(self, app_with_scopes, user6): assert response.json.get("exception") == "Unauthorized" assert "Invalid scope." in response.json.get("reasons") + _, response = sanic_app.test_client.get( + "/protected/scoped/10", + headers={"Authorization": "Bearer {}".format(access_token)}, + ) + + assert response.status == 200 + assert response.json.get("protected") is False + assert response.json.get("scoped") is False + def test_no_user_scopes(app_with_scopes): sanic_app, sanicjwt = app_with_scopes @@ -1104,6 +1168,15 @@ def test_no_user_scopes(app_with_scopes): assert response.json.get("exception") == "Unauthorized" assert "Invalid scope." in response.json.get("reasons") + _, response = sanic_app.test_client.get( + "/protected/scoped/10", + headers={"Authorization": "Bearer {}".format(access_token)}, + ) + + assert response.status == 200 + assert response.json.get("protected") is False + assert response.json.get("scoped") is False + def test_scoped_option(app_with_scopes): sanic_app, sanicjwt = app_with_scopes diff --git a/tests/test_endpoints_scoped_simple.py b/tests/test_endpoints_scoped_simple.py index 1785e19..f6c6a83 100644 --- a/tests/test_endpoints_scoped_simple.py +++ b/tests/test_endpoints_scoped_simple.py @@ -1,10 +1,9 @@ from sanic import Sanic from sanic.response import json -from sanic_jwt import initialize +from sanic_jwt import initialize from sanic_jwt.decorators import scoped - app = Sanic() initialize(app, authenticate=lambda: True) diff --git a/tests/test_endpoints_sync_methods.py b/tests/test_endpoints_sync_methods.py index 508af15..3a6c647 100644 --- a/tests/test_endpoints_sync_methods.py +++ b/tests/test_endpoints_sync_methods.py @@ -1,15 +1,14 @@ import binascii import os import uuid - from datetime import datetime, timedelta -from freezegun import freeze_time + from sanic import Sanic from sanic.response import json import pytest -from sanic_jwt import Initialize -from sanic_jwt import exceptions +from freezegun import freeze_time +from sanic_jwt import exceptions, Initialize from sanic_jwt.decorators import protected diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index 73b700e..922cd71 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -1,4 +1,5 @@ from sanic.exceptions import abort + from sanic_jwt.decorators import protected diff --git a/tests/test_extend_payload.py b/tests/test_extend_payload.py index 6266ee1..483067f 100644 --- a/tests/test_extend_payload.py +++ b/tests/test_extend_payload.py @@ -1,8 +1,7 @@ -from sanic import Sanic -from sanic_jwt import Initialize -from sanic_jwt import Authentication -from sanic_jwt import exceptions import jwt +from sanic import Sanic + +from sanic_jwt import Authentication, exceptions, Initialize # import pytest diff --git a/tests/test_extra_verifications.py b/tests/test_extra_verifications.py index 34517da..b0f3ec4 100644 --- a/tests/test_extra_verifications.py +++ b/tests/test_extra_verifications.py @@ -1,7 +1,7 @@ from sanic import Sanic from sanic.response import json -from sanic_jwt import Initialize -from sanic_jwt import protected + +from sanic_jwt import Initialize, protected def test_extra_verification_passing(app_with_extra_verification): diff --git a/tests/test_initialize.py b/tests/test_initialize.py index 02d4465..78032d0 100644 --- a/tests/test_initialize.py +++ b/tests/test_initialize.py @@ -1,13 +1,9 @@ -from sanic import Blueprint -from sanic import Sanic +from sanic import Blueprint, Sanic from sanic.response import text from sanic.views import HTTPMethodView import pytest - -from sanic_jwt import Initialize -from sanic_jwt import exceptions -from sanic_jwt import initialize +from sanic_jwt import exceptions, Initialize, initialize def test_store_refresh_token_and_retrieve_refresh_token_ommitted(): diff --git a/tests/test_me_invalid.py b/tests/test_me_invalid.py index cc8aefe..6b40441 100644 --- a/tests/test_me_invalid.py +++ b/tests/test_me_invalid.py @@ -1,9 +1,10 @@ -import pytest from datetime import datetime, timedelta import jwt -from freezegun import freeze_time from sanic.response import json + +import pytest +from freezegun import freeze_time from sanic_jwt.decorators import protected diff --git a/tests/test_utils.py b/tests/test_utils.py index 615d9bd..efe2099 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,8 +1,8 @@ -import pytest -from sanic_jwt import utils -from sanic_jwt import exceptions -from pathlib import Path from os import path +from pathlib import Path + +import pytest +from sanic_jwt import exceptions, utils @pytest.fixture