Fixed typos and timezone

This commit is contained in:
Ferdinand Thiessen 2020-10-15 02:19:51 +02:00
parent 790e65791d
commit 3f9fdc773c
4 changed files with 14 additions and 11 deletions

View File

@ -36,26 +36,27 @@ def _create_token():
""" Login User """ Login User
Login in User and create an AccessToken for the User. Login in User and create an AccessToken for the User.
Requires POST data {'userid': string, 'password': string}
Returns: Returns:
A JSON-File with user information and created token or errors A JSON-File with user information and created token or errors
""" """
logger.debug("Start log in.") logger.debug("Start log in.")
data = request.get_json() data = request.get_json()
username = data['username'] userid = data['userid']
password = data['password'] password = data['password']
logger.debug("search user {{ {} }} in database".format(username)) logger.debug("search user {{ {} }} in database".format(userid))
user = userController.login_user(username, password) user = userController.login_user(userid, password)
if not user: if not user:
raise Unauthorized raise Unauthorized
logger.debug("user is {{ {} }}".format(user)) logger.debug("user is {{ {} }}".format(user))
token = access_controller.create(user, user_agent=request.user_agent) token = access_controller.create(user, user_agent=request.user_agent)
logger.debug("access token is {{ {} }}".format(token)) logger.debug("access token is {{ {} }}".format(token))
logger.info("User {{ {} }} success login.".format(username)) logger.info("User {{ {} }} success login.".format(userid))
# Lets cleanup the DB # Lets cleanup the DB
access_controller.clear_expired() access_controller.clear_expired()
return jsonify({"user": user, "token": token.token}) return jsonify({"user": user, "token": token, "permissions": user.get_permissions()})
@auth_bp.route("/auth", methods=['GET']) @auth_bp.route("/auth", methods=['GET'])

View File

@ -3,7 +3,7 @@ from ..models.accessToken import AccessToken
from flaschengeist.system.database import db from flaschengeist.system.database import db
from flaschengeist import logger from flaschengeist import logger
from werkzeug.exceptions import Forbidden from werkzeug.exceptions import Forbidden
from datetime import datetime, timedelta from datetime import datetime, timezone
from . import Singleton from . import Singleton

View File

@ -1,4 +1,4 @@
from datetime import datetime, timedelta from datetime import datetime, timedelta, timezone
from ..database import db from ..database import db
from secrets import compare_digest from secrets import compare_digest
from flaschengeist import logger from flaschengeist import logger
@ -39,7 +39,7 @@ class AccessToken(db.Model):
""" """
return { return {
"token": self.token, "token": self.token,
"expires": self.expires, "expires": self.expires.replace(tzinfo=timezone.utc),
"lifetime": self.lifetime, "lifetime": self.lifetime,
"browser": self.browser, "browser": self.browser,
"platform": self.platform "platform": self.platform

View File

@ -61,6 +61,9 @@ class User(db.Model):
if 'display_name' in data: if 'display_name' in data:
self.display_name = data['display_name'] self.display_name = data['display_name']
def get_permissions(self):
return [permission.name for role in self.roles for permission in role.permissions]
def has_permissions(self, permissions): def has_permissions(self, permissions):
for role in self.roles: for role in self.roles:
for permission in role.permissions: for permission in role.permissions:
@ -70,13 +73,12 @@ class User(db.Model):
def serialize(self): def serialize(self):
return { return {
# TODO: username should be UID? "userid": self.uid,
"username": self.uid,
"display_name": self.display_name, "display_name": self.display_name,
"firstname": self.firstname, "firstname": self.firstname,
"lastname": self.lastname, "lastname": self.lastname,
"mail": self.mail, "mail": self.mail,
"roles": ["user"] + [r.name for r in self.roles] "roles": [r.name for r in self.roles]
} }