Created
September 24, 2016 14:44
-
-
Save biancarosa/f9f0f827999322475822890ee2ed6c4e to your computer and use it in GitHub Desktop.
Falcon example hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# hooks/auth.py | |
import falcon | |
from api.models.user_token import UserToken | |
def authorize(): | |
def hook(req, resp, resource, params): | |
token = req.context.get('token') | |
if token is None: | |
description = ('Please provide an auth token as part of the request.') | |
raise falcon.HTTPUnauthorized('Auth token required', 'Auth token required', description, href='http://docs.example.com/auth') | |
user_token = UserToken.get_by_token(token) | |
if not user_token or not user_token.is_valid(): | |
description = ('The provided auth token is not valid. Please request a new token and try again.') | |
raise falcon.HTTPUnauthorized('Authentication required', 'Authentication required', description, href='http://docs.example.com/auth', scheme='Token; UUID') | |
else: | |
req.context['current_user'] = user_token.user | |
return hook |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment