Skip to content

Instantly share code, notes, and snippets.

@mikeywaites
Last active December 3, 2017 16:49
Show Gist options
  • Save mikeywaites/7ca1bf8a2865b49f36caaa0ea1a8c202 to your computer and use it in GitHub Desktop.
Save mikeywaites/7ca1bf8a2865b49f36caaa0ea1a8c202 to your computer and use it in GitHub Desktop.
api middleware get_client_token
from .utils import decode_client_token, get_token_from_request
def get_client_token(endpoint):
"""Validate the request has a valid JWT token. If the provided api_key and token
a valid the JWT will be decoded and stored on g.token for use later on.
"""
token = get_token_from_request()
if not token:
payload = {'message': 'Request does not contain token'}
endpoint.return_error(
401,
payload=payload
)
payload = decode_client_token(g.api_client, token)
if not payload:
payload = {'message': 'Invalid token'}
endpoint.return_error(
401,
payload=payload,
)
g.token = payload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment