ki-backend/ki/auth.py

33 lines
766 B
Python
Raw Normal View History

2021-06-12 13:24:26 +02:00
import uuid
import yaml
from app import app, db
from ki.models import User, Token
def auth(username, password):
auth_file_path = app.config["KI_DATA_DIR"] + "/auth.yml"
with open(auth_file_path, "r") as auth_file_stream:
users = yaml.safe_load(auth_file_stream)
if username not in users:
return None
auth_user = users[username]
if auth_user["password"] != password:
return None
user = User.query.filter(User.auth_id.__eq__(username)).first()
if user is None:
2021-06-20 20:13:19 +02:00
user = User(auth_id=username)
2021-06-12 13:24:26 +02:00
db.session.add(user)
2021-06-22 17:35:27 +02:00
2021-06-12 13:24:26 +02:00
token = Token(token=str(uuid.uuid4()), user=user)
db.session.add(token)
db.session.commit()
return token