Fatih Degirmenci | c2a4601 | 2019-09-30 12:07:45 +0200 | [diff] [blame^] | 1 | from flask import g |
| 2 | from flask_httpauth import HTTPBasicAuth, HTTPTokenAuth |
| 3 | from app.models import User, UserRole |
| 4 | from app.api.errors import error_response |
| 5 | |
| 6 | basic_auth = HTTPBasicAuth() |
| 7 | token_auth = HTTPTokenAuth() |
| 8 | |
| 9 | @basic_auth.verify_password |
| 10 | def verify_password(username, password): |
| 11 | user = User.query.filter_by(username=username).first() |
| 12 | current_user_role_id = user.role_id |
| 13 | regular_user_role_id = UserRole.query.filter_by(role_name='regular').first().id |
| 14 | if current_user_role_id == regular_user_role_id: |
| 15 | return False |
| 16 | if user is None: |
| 17 | return False |
| 18 | g.current_user = user |
| 19 | return user.check_password(password) |
| 20 | |
| 21 | @token_auth.verify_token |
| 22 | def verify_token(token): |
| 23 | g.current_user = User.check_token(token) if token else None |
| 24 | g.current_user = User.check_none_regular_user(token) if token else None |
| 25 | return g.current_user is not None |
| 26 | |
| 27 | @basic_auth.error_handler |
| 28 | def basic_auth_error(): |
| 29 | return error_response(401) |