Last active
June 22, 2018 19:02
-
-
Save bigoper/5315789e418ed81c3b3310ef42e34e10 to your computer and use it in GitHub Desktop.
auth.py
This file contains hidden or 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
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