-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecurity.py
25 lines (21 loc) · 884 Bytes
/
security.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
from werkzeug.security import safe_str_cmp
from models.user import UserModel
def authenticate(username, password):
"""
Function that gets called when a user calls the /auth endpoint
with their username and password.
:param username: User's username (string)
:param password: User's un-encrypted password (string)
:return: UserModel object if authentication was successful, else None
"""
user = UserModel.find_by_username(username)
if user and safe_str_cmp(user.password, password):
return user
def identity(payload):
"""
Function that gets called when user has already authenticated, and Flask-JWT verified their authorization header is correct.
:param payload: User id in a dictionary with 'identity' as key
:return: A UserModel object
"""
user_id = payload['identity']
return UserModel.find_by_id(user_id)