-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.py
34 lines (28 loc) · 906 Bytes
/
auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from flask import g, jsonify
from flask_httpauth import HTTPBasicAuth, HTTPTokenAuth, MultiAuth
from argon2.exceptions import VerifyMismatchError
import models
basic_auth = HTTPBasicAuth()
token_auth = HTTPTokenAuth(scheme='Token')
auth = MultiAuth(token_auth, basic_auth)
@basic_auth.verify_password
def verify_password(email_or_username, password):
try:
user = models.User.get(
(models.User.username == email_or_username) |
(models.User.email == email_or_username)
)
if not user.verify_password(password):
return False
except (models.User.DoesNotExist, VerifyMismatchError):
return False
else:
g.user = user
return True
@token_auth.verify_token
def verify_token(token):
user = models.User.verify_auth_token(token)
if user is not None:
g.user = user
return True
return False