| from flask import g |
| from flask_httpauth import HTTPBasicAuth, HTTPTokenAuth |
| from app.models import User, UserRole |
| from app.api.errors import error_response |
| |
| basic_auth = HTTPBasicAuth() |
| token_auth = HTTPTokenAuth() |
| |
| @basic_auth.verify_password |
| def verify_password(username, password): |
| user = User.query.filter_by(username=username).first() |
| current_user_role_id = user.role_id |
| regular_user_role_id = UserRole.query.filter_by(role_name='regular').first().id |
| if current_user_role_id == regular_user_role_id: |
| return False |
| if user is None: |
| return False |
| g.current_user = user |
| return user.check_password(password) |
| |
| @token_auth.verify_token |
| def verify_token(token): |
| g.current_user = User.check_token(token) if token else None |
| g.current_user = User.check_none_regular_user(token) if token else None |
| return g.current_user is not None |
| |
| @basic_auth.error_handler |
| def basic_auth_error(): |
| return error_response(401) |