diff --git a/README.md b/README.md index 0ccdd13..c25e19a 100644 --- a/README.md +++ b/README.md @@ -593,7 +593,7 @@ Example: from sanic_jwt.handlers import extend_payload - def my_foo_bar_payload_extender(authenticator, payload, *args, **kwargs): + async def my_foo_bar_payload_extender(authenticator, payload, *args, **kwargs): payload = extend_payload(authenticator, payload, *args, **kwargs) payload.update({ @@ -610,7 +610,7 @@ Purpose: A handler method used to add scopes into a payload. It is a convenience Example: - def my_scope_extender(user, *args, **kwargs): + async def my_scope_extender(user, *args, **kwargs): return user.scopes __`SANIC_JWT_LEEWAY`__ diff --git a/example/scopes.py b/example/scopes.py index 73245f6..863f7bb 100644 --- a/example/scopes.py +++ b/example/scopes.py @@ -44,7 +44,7 @@ async def authenticate(request, *args, **kwargs): return user -def my_scope_extender(user, *args, **kwargs): +async def my_scope_extender(user, *args, **kwargs): return user.scopes diff --git a/sanic_jwt/authentication.py b/sanic_jwt/authentication.py index 7c06077..7a1f313 100644 --- a/sanic_jwt/authentication.py +++ b/sanic_jwt/authentication.py @@ -53,14 +53,14 @@ def _decode(self, token, verify=True): def _get_algorithm(self): return self.app.config.SANIC_JWT_ALGORITHM - def _get_payload(self, user): - payload = utils.execute_handler(self.app.config.SANIC_JWT_HANDLER_PAYLOAD, self, user) + async def _get_payload(self, user): + payload = await utils.execute_handler(self.app.config.SANIC_JWT_HANDLER_PAYLOAD, self, user) # TODO: # - Add verification check to make sure payload is a dict with a `user_id` key - payload = utils.execute_handler(self.app.config.SANIC_JWT_HANDLER_PAYLOAD_EXTEND, self, payload) + payload = await utils.execute_handler(self.app.config.SANIC_JWT_HANDLER_PAYLOAD_EXTEND, self, payload) if self.app.config.SANIC_JWT_HANDLER_PAYLOAD_SCOPES is not None: - scopes = utils.execute_handler(self.app.config.SANIC_JWT_HANDLER_PAYLOAD_SCOPES, user) + scopes = await utils.execute_handler(self.app.config.SANIC_JWT_HANDLER_PAYLOAD_SCOPES, user) if not isinstance(scopes, (tuple, list)): scopes = [scopes] payload[self.app.config.SANIC_JWT_SCOPES_NAME] = scopes @@ -116,14 +116,14 @@ def _get_user_id(self, user): user_id = getattr(user, self.app.config.SANIC_JWT_USER_ID) return user_id - def get_access_token(self, user): - payload = self._get_payload(user) + async def get_access_token(self, user): + payload = await self._get_payload(user) secret = self._get_secret() algorithm = self._get_algorithm() return jwt.encode(payload, secret, algorithm=algorithm) - def get_refresh_token(self, user): + async def get_refresh_token(self, user): refresh_token = utils.generate_token() user_id = self._get_user_id(user) self.store_refresh_token(user_id=user_id, refresh_token=refresh_token) diff --git a/sanic_jwt/blueprint.py b/sanic_jwt/blueprint.py index b1132f0..063c8ba 100644 --- a/sanic_jwt/blueprint.py +++ b/sanic_jwt/blueprint.py @@ -6,8 +6,8 @@ bp = Blueprint('auth_bp') -def get_access_token_output(request, user): - access_token = request.app.auth.get_access_token(user) +async def get_access_token_output(request, user): + access_token = await request.app.auth.get_access_token(user) output = { request.app.config.SANIC_JWT_ACCESS_TOKEN_NAME: access_token @@ -48,7 +48,7 @@ async def authenticate(request, *args, **kwargs): except Exception as e: raise e - access_token, output = get_access_token_output(request, user) + access_token, output = await get_access_token_output(request, user) if request.app.config.SANIC_JWT_REFRESH_TOKEN_ENABLED: refresh_token = request.app.auth.get_refresh_token(user) diff --git a/sanic_jwt/handlers.py b/sanic_jwt/handlers.py index 0ecf0f7..96e60a4 100644 --- a/sanic_jwt/handlers.py +++ b/sanic_jwt/handlers.py @@ -3,7 +3,7 @@ from sanic_jwt import utils -def build_payload(authenticator, user, *args, **kwargs): +async def build_payload(authenticator, user, *args, **kwargs): if isinstance(user, dict): user_id = user.get(authenticator.app.config.SANIC_JWT_USER_ID) else: @@ -14,7 +14,7 @@ def build_payload(authenticator, user, *args, **kwargs): } -def extend_payload(authenticator, payload, *args, **kwargs): +async def extend_payload(authenticator, payload, *args, **kwargs): delta = timedelta(seconds=authenticator.app.config.SANIC_JWT_EXPIRATION_DELTA) exp = datetime.utcnow() + delta additional = { diff --git a/sanic_jwt/utils.py b/sanic_jwt/utils.py index 8b7f047..1f944f6 100644 --- a/sanic_jwt/utils.py +++ b/sanic_jwt/utils.py @@ -8,7 +8,7 @@ def generate_token(n=24): return str(binascii.hexlify(os.urandom(n)), 'utf-8') -def execute_handler(handler, *args, **kwargs): +async def execute_handler(handler, *args, **kwargs): if isinstance(handler, str): parts = handler.split('.') fn = parts.pop() @@ -16,7 +16,7 @@ def execute_handler(handler, *args, **kwargs): method = getattr(module, fn) else: method = handler - runner = method(*args, **kwargs) + runner = await method(*args, **kwargs) return runner