forked from kompetenzinventar/ki-backend
implement login
This commit is contained in:
32
ki/auth.py
Normal file
32
ki/auth.py
Normal file
@ -0,0 +1,32 @@
|
||||
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:
|
||||
user = User(nickname=username, auth_id=username)
|
||||
db.session.add(user)
|
||||
|
||||
token = Token(token=str(uuid.uuid4()), user=user)
|
||||
db.session.add(token)
|
||||
db.session.commit()
|
||||
|
||||
return token
|
Reference in New Issue
Block a user