Skip to content

Instantly share code, notes, and snippets.

@biancarosa
Created September 24, 2016 14:44
Show Gist options
  • Save biancarosa/f9f0f827999322475822890ee2ed6c4e to your computer and use it in GitHub Desktop.
Save biancarosa/f9f0f827999322475822890ee2ed6c4e to your computer and use it in GitHub Desktop.
Falcon example hook
# 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