Currently I'm using two types of authentication in my API:
- Local user + Local password
- DB user + DB password
For this I want to define a decorator for each type. Example:
@authenticator.local_authentication
or
@authenticator.db_authentication
Currently I have a working version of the local authentication decorator.
I want to create one for @authenticator.db_authentication
. I'm using Miguel's post as reference to add db authentication support.
The example currently works with HTTPBasicAuth
.
Seems to be I need to overwrite auth.login_required
and auth.verify_password
which need to be defined when using: @auth.login_required
to handle authentication.
Ideally I want to define just something like this in my API methods:
@authenticator.db_authentication
def get(self):
...
This is the working code that requires modification:
from flask_httpauth import HTTPBasicAuth
auth = HTTPBasicAuth()
@auth.verify_password
def verify_password(username_or_token, password):
"""Validates username or password in database.
:param username_or_token:
:param password:
:return: user
"""
return authenticator.db_authentication(username_or_token, password)
class Status(Resource):
"""Used for verifying API status"""
@auth.login_required
def get(self):
"""
:return:
"""
log.info(request.remote_addr + ' ' + request.__repr__())
log.info('api() | GET | Received request for Status')
response = json.dumps('Status: Hello %s!' % g.user.username)
return Response(response, status=200, mimetype=settings.api_mime_type)
@authenticator.local_authentication
def post(self):
log.info(request.remote_addr + ' ' + request.__repr__())
log.info('api() | POST | Received request for Status')
response = json.dumps('Status: POST. %s' % settings.api_ok)
return Response(response, status=202, mimetype=settings.api_mime_type)
I want to change @auth.login_required
to @authenticator.db_authentication
Example of @authenticator.local_authentication
in a different file below:
def check_auth(username, password):
""" Basic authentication: local username and password.
:param username:
:param password:
:return:
"""
return username == settings.api_account and password == settings.api_password
def authentication_error():
"""
Authentication error.
:return:
"""
response = jsonify({'message': "Authenticate."})
response.headers['WWW-Authenticate'] = settings.api_realm
response.status_code = 401
return response
def local_authentication(f):
"""Decorator to check local authentication.
:param f: A function
:return: itself: Decorator check_credentials
"""
@wraps(f)
def check_credentials(*args, **kwargs):
auth = request.authorization
if not auth:
return authentication_error()
elif not check_auth(auth.username, auth.password):
return authentication_error()
return f(*args, **kwargs)
return check_credentials
def db_authentication(username_or_token, password):
"""First try to authenticate by token.
:param username_or_token:
:param password:
:return: boolean
"""
user = Model.ApiUsers.verify_auth_token(username_or_token)
if not user:
# Try to authenticate with database password.
user = Model.ApiUsers.query.filter_by(username=username_or_token).first()
if not user or not user.verify_password(password):
return False
g.user = user
return True