finished ##218

This commit is contained in:
Tim Gröger 2020-03-07 14:56:44 +01:00
parent 0d1c116da7
commit 39095af891
3 changed files with 53 additions and 12 deletions

View File

@ -53,13 +53,13 @@ class AccesTokenController(metaclass=Singleton):
""" """
LOGGER.info("Verify AccessToken with token: {} and group: {}".format(token, group)) LOGGER.info("Verify AccessToken with token: {} and group: {}".format(token, group))
for accToken in self.tokenList: for accToken in self.tokenList:
LOGGER.debug("Check is token {} same as in AccessToken {}".format(token, accToken))
if accToken == token:
LOGGER.debug("AccessToken is {}".format(accToken)) LOGGER.debug("AccessToken is {}".format(accToken))
endTime = accToken.timestamp + timedelta(seconds=self.lifetime) endTime = accToken.timestamp + timedelta(seconds=accToken.lifetime)
now = datetime.now() now = datetime.now()
LOGGER.debug("Check if AccessToken's Endtime {} is bigger then now {}".format(endTime, now)) LOGGER.debug("Check if AccessToken's Endtime {} is bigger then now {}".format(endTime, now))
if now <= endTime: if now <= endTime:
LOGGER.debug("Check is token {} same as in AccessToken {}".format(token, accToken))
if accToken == token:
self.checkBar(accToken.user) self.checkBar(accToken.user)
LOGGER.debug("Check if AccesToken {} has same group {}".format(accToken, group)) LOGGER.debug("Check if AccesToken {} has same group {}".format(accToken, group))
if self.isSameGroup(accToken, group): if self.isSameGroup(accToken, group):
@ -67,11 +67,14 @@ class AccesTokenController(metaclass=Singleton):
LOGGER.info("Found AccessToken {} with token: {} and group: {}".format(accToken, token, group)) LOGGER.info("Found AccessToken {} with token: {} and group: {}".format(accToken, token, group))
return accToken return accToken
else: else:
LOGGER.debug("AccessToken {} is no longer valid and will removed".format(accToken)) self.deleteAccessToken(accToken)
self.tokenList.remove(accToken)
LOGGER.info("Found no valid AccessToken with token: {} and group: {}".format(token, group)) LOGGER.info("Found no valid AccessToken with token: {} and group: {}".format(token, group))
return False return False
def deleteAccessToken(self, accToken):
LOGGER.debug("AccessToken {} is no longer valid and will removed".format(accToken))
self.tokenList.remove(accToken)
def createAccesToken(self, user, ldap_conn): def createAccesToken(self, user, ldap_conn):
""" Create an AccessToken """ Create an AccessToken
@ -87,7 +90,7 @@ class AccesTokenController(metaclass=Singleton):
now = datetime.ctime(datetime.now()) now = datetime.ctime(datetime.now())
token = hashlib.md5((now + user.dn).encode('utf-8')).hexdigest() token = hashlib.md5((now + user.dn).encode('utf-8')).hexdigest()
self.checkBar(user) self.checkBar(user)
accToken = AccessToken(user, token, ldap_conn, datetime.now()) accToken = AccessToken(user, token, ldap_conn, self.lifetime, datetime.now())
LOGGER.debug("Add AccessToken {} to current Tokens".format(accToken)) LOGGER.debug("Add AccessToken {} to current Tokens".format(accToken))
self.tokenList.append(accToken) self.tokenList.append(accToken)
LOGGER.info("Finished create AccessToken {} with Token {}".format(accToken, token)) LOGGER.info("Finished create AccessToken {} with Token {}".format(accToken, token))

View File

@ -17,7 +17,7 @@ class AccessToken():
token = None token = None
ldap_conn = None ldap_conn = None
def __init__(self, user, token, ldap_conn, timestamp=datetime.now()): def __init__(self, user, token, ldap_conn, lifetime, timestamp=datetime.now()):
""" Initialize Class AccessToken """ Initialize Class AccessToken
No more to say. No more to say.
@ -30,6 +30,7 @@ class AccessToken():
LOGGER.debug("Initialize AccessToken") LOGGER.debug("Initialize AccessToken")
self.user = user self.user = user
self.timestamp = timestamp self.timestamp = timestamp
self.lifetime = lifetime
self.token = token self.token = token
self.ldap_conn = ldap_conn self.ldap_conn = ldap_conn

View File

@ -77,6 +77,43 @@ def _getUsers(**kwargs):
except Exception as err: except Exception as err:
return jsonify({"error": str(err)}), 500 return jsonify({"error": str(err)}), 500
@app.route("/getLifeTime", methods=['GET'])
@login_required(groups=[MONEY, GASTRO, VORSTAND, EXTERN, USER])
def _getLifeTime(**kwargs):
try:
if 'accToken' in kwargs:
accToken = kwargs['accToken']
return jsonify({"value": accToken.lifetime})
except Exception as err:
return jsonify({"error": str(err)}), 500
@app.route("/saveLifeTime", methods=['POST'])
@login_required(groups=[MONEY, GASTRO, VORSTAND, EXTERN, USER])
def _saveLifeTime(**kwargs):
try:
if 'accToken' in kwargs:
accToken = kwargs['accToken']
data = request.get_json()
lifetime = data['value']
accToken.lifetime = lifetime
accToken.updateTimestamp()
return jsonify({"value": accToken.lifetime})
except Exception as err:
return jsonify({"error": str(err)}), 500
@app.route("/logout", methods=['GET'])
@login_required(groups=[MONEY, GASTRO, VORSTAND, EXTERN, USER])
def _logout(**kwargs):
try:
if 'accToken' in kwargs:
accToken = kwargs['accToken']
accesTokenController.deleteAccessToken(accToken)
return jsonify({"ok": "ok"})
except Exception as err:
return jsonify({"error": str(err)}), 500
@app.route("/login", methods=['POST']) @app.route("/login", methods=['POST'])
def _login(): def _login():
""" Login User """ Login User