Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@bigoper
Last active June 22, 2018 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bigoper/5315789e418ed81c3b3310ef42e34e10 to your computer and use it in GitHub Desktop.
Save bigoper/5315789e418ed81c3b3310ef42e34e10 to your computer and use it in GitHub Desktop.
auth.py
from eve.auth import TokenAuth
from flask import Response, abort, request, current_app
class MyTokenAuth(TokenAuth):
def check_auth(self, token, allowed_roles, resource, method):
"""For the purpose of this example the implementation is as simple as
possible. A 'real' token should probably contain a hash of the
username/password combo, which should then validated against the account
data stored on the DB.
"""
print('TOKEN: {}'.format(token))
accounts = current_app.data.driver.db['accounts']
account = accounts.find_one({'token': token})
print('-- ACCOUNT: {}'.format(account))
if account:
active = account["active"]
if active:
return True
return False
def authenticate(self):
""" Returns a standard a 401. Override if you want to change the
response.
"""
resp = Response(None, 401, {'WWW-Authenticate': 'Basic realm="%s"' %
__package__})
abort(401, description='Please provide proper credentials :)',
response=resp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment