from werkzeug.security import safe_str_cmp
# safe comparing string in different encoding system
#from resources.user import User
from models.user import UserModel

# wow just learn many thing :)
# set comprehension
# list comprehension :)

#username_mapping = { u.username : u for u in users }
#userid_mapping = { u.id : u for u in users }

def authenticate(username,password):
    #user = username_mapping.get(username, None) # my new knowledge
    user = UserModel.find_by_username(username)
    if user and safe_str_cmp(user.password,password):
        return user

def identity(payload):
    user_id = payload['identity']
    #return userid_mapping.get(user_id, None)
    return UserModel.find_by_id(user_id)
