53 lines
2.2 KiB
Python
53 lines
2.2 KiB
Python
import json
|
|
import logging
|
|
import pydapper
|
|
import bcrypt
|
|
|
|
from ..schemas import model
|
|
from .. import local_db
|
|
from ..services import jwt_handler
|
|
|
|
def GetUser(options):
|
|
|
|
try:
|
|
if "password" in options and "username" in options:
|
|
hash = bcrypt.hashpw(options["password"].encode('utf-8'), bcrypt.gensalt( 12 )).decode('utf8')
|
|
pooledConnection = local_db.getPoolConnection()
|
|
commands = pydapper.using(pooledConnection)
|
|
data = commands.query("SELECT id, participant_id, first_name, last_name, user_name, user_email, user_phone, password_hash, api_key FROM user WHERE user_name = ?username? OR user_email = ?username?",
|
|
model=model.User, param={"username" : options["username"]})
|
|
pooledConnection.close()
|
|
# print(data)
|
|
if len(data) == 1:
|
|
if bcrypt.checkpw(options["password"].encode("utf-8"), bytes(data[0].password_hash, "utf-8")):
|
|
result = {
|
|
"id": data[0].id,
|
|
"participant_id": data[0].participant_id,
|
|
"first_name": data[0].first_name,
|
|
"last_name": data[0].last_name,
|
|
"user_name": data[0].user_name,
|
|
"user_phone": data[0].user_phone
|
|
}
|
|
token = jwt_handler.generate_jwt(payload=result, lifetime=60) # generate token valid 60 mins
|
|
result["token"] = token # add token to user data
|
|
return json.dumps(result), 200
|
|
|
|
if len(data) > 1:
|
|
result = {}
|
|
result["message"] = "credential lookup mismatch"
|
|
return json.dumps(result), 500
|
|
|
|
result = {}
|
|
result["message"] = "invalid credentials"
|
|
return json.dumps(result), 403
|
|
|
|
except Exception as ex:
|
|
logging.error(ex)
|
|
print(ex)
|
|
result = {}
|
|
result["message"] = "call failed: " + str(ex)
|
|
return json.dumps(result), 500
|
|
|
|
# $2b$12$uWLE0r32IrtCV30WkMbVwOdltgeibymZyYAf4ZnQb2Bip8hrkGGwG
|
|
# $2b$12$.vEapj9xU8z0RK0IpIGeYuRIl0ktdMt4XdJQBhVn.3K2hmvm7qD3y
|
|
# $2b$12$yL3PiseU70ciwEuMVM4OtuMwR6tNuIT9vvBiBG/uyMrPxa16E2Zqu |