blob: eb9f96aeb1926efb142cf1c3b74feb3594de2552 [file] [log] [blame]
Fatih Degirmencic2a46012019-09-30 12:07:45 +02001from flask import g
2from flask_httpauth import HTTPBasicAuth, HTTPTokenAuth
3from app.models import User, UserRole
4from app.api.errors import error_response
5
6basic_auth = HTTPBasicAuth()
7token_auth = HTTPTokenAuth()
8
9@basic_auth.verify_password
10def 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
22def 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
28def basic_auth_error():
29 return error_response(401)