Skip to content

Commit

Permalink
Add async/await to extenders
Browse files Browse the repository at this point in the history
  • Loading branch information
ahopkins committed Nov 5, 2017
1 parent 5af0a77 commit 4dd52b8
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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`__
Expand Down
2 changes: 1 addition & 1 deletion example/scopes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
14 changes: 7 additions & 7 deletions sanic_jwt/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions sanic_jwt/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions sanic_jwt/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions sanic_jwt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ 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()
module = importlib.import_module('.'.join(parts))
method = getattr(module, fn)
else:
method = handler
runner = method(*args, **kwargs)
runner = await method(*args, **kwargs)
return runner


Expand Down

0 comments on commit 4dd52b8

Please sign in to comment.