2022-02-08 11:26:20 +00:00
|
|
|
from app.models import User
|
2021-09-14 10:52:23 +00:00
|
|
|
from flask_httpauth import HTTPBasicAuth, HTTPTokenAuth
|
|
|
|
from sqlalchemy import or_
|
|
|
|
from werkzeug.http import HTTP_STATUS_CODES
|
|
|
|
|
|
|
|
basic_auth = HTTPBasicAuth()
|
|
|
|
token_auth = HTTPTokenAuth()
|
|
|
|
|
|
|
|
|
|
|
|
@basic_auth.verify_password
|
|
|
|
def verify_password(email_or_username, password):
|
2022-02-03 11:39:16 +00:00
|
|
|
user = User.query.filter(
|
|
|
|
or_(
|
|
|
|
User.username == email_or_username,
|
|
|
|
User.email == email_or_username.lower()
|
|
|
|
)
|
|
|
|
).first()
|
2021-09-14 10:52:23 +00:00
|
|
|
if user and user.verify_password(password):
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
|
|
|
@basic_auth.error_handler
|
|
|
|
def basic_auth_error(status):
|
|
|
|
return {'error': HTTP_STATUS_CODES.get(status, 'Unknown error')}, status
|
|
|
|
|
|
|
|
|
|
|
|
@token_auth.verify_token
|
|
|
|
def verify_token(token):
|
|
|
|
return User.check_token(token) if token else None
|
|
|
|
|
|
|
|
|
|
|
|
@token_auth.error_handler
|
|
|
|
def token_auth_error(status):
|
|
|
|
return {'error': HTTP_STATUS_CODES.get(status, 'Unknown error')}, status
|