flaschengeist/geruecht/controller/accesTokenController.py

99 lines
3.1 KiB
Python
Raw Normal View History

2019-04-11 21:56:55 +00:00
from geruecht.model.accessToken import AccessToken
from datetime import datetime
import time
from threading import Thread
2019-04-11 21:56:55 +00:00
import hashlib
class AccesTokenController(Thread):
2019-04-17 12:46:46 +00:00
""" Control all createt AccesToken
This Class create, delete, find and manage AccesToken.
Attributes:
tokenList: List of currents AccessToken
lifetime: Variable for the Lifetime of one AccessToken in seconds.
"""
2019-04-11 21:56:55 +00:00
tokenList = None
2019-04-17 12:46:46 +00:00
lifetime = 60
2019-04-11 21:56:55 +00:00
def __init__(self):
2019-04-17 12:46:46 +00:00
""" Initialize AccessTokenController
Initialize Thread and set tokenList empty.
"""
print("init AccesTokenControlle")
print("init threading")
Thread.__init__(self)
2019-04-11 21:56:55 +00:00
self.tokenList = []
def findAccesToken(self, token):
2019-04-17 12:46:46 +00:00
""" Find a Token in current AccessTokens
Iterate throw all availables AccesTokens and retrieve one, if they are the same.
Args:
token: Token to find
Returns:
An AccessToken if found or None if not found.
"""
print("search for AccesToken", token)
2019-04-11 21:56:55 +00:00
for accToken in self.tokenList:
if accToken == token:
print("find AccesToken", accToken, "with token", token)
2019-04-11 21:56:55 +00:00
return accToken
print("no AccesToken with", token)
2019-04-11 21:56:55 +00:00
return None
def createAccesToken(self, user):
2019-04-17 12:46:46 +00:00
""" Create an AccessToken
Create an AccessToken for an User and add it to the tokenList.
Args:
user: For wich User is to create an AccessToken
Returns:
A created Token for User
"""
print("create AccesToken")
now = datetime.ctime(datetime.now())
token = hashlib.md5((now + user.password).encode('utf-8')).hexdigest()
2019-04-11 21:56:55 +00:00
self.tokenList.append(AccessToken(user, token))
print(self.tokenList)
print("finished create AccesToken", token)
2019-04-11 21:56:55 +00:00
return token
def isSameGroup(self, accToken, group):
2019-04-17 12:46:46 +00:00
""" Verify group in AccessToken
Verify if the User in the AccesToken has the right group.
Args:
accToken: AccessToken to verify.
group: Group to verify.
Returns:
A Bool. If the same then True else False
"""
print("controll if", accToken, "hase group", group)
2019-04-11 21:56:55 +00:00
return True if accToken.user.group == group else False
def run(self):
2019-04-17 12:46:46 +00:00
""" Starting Controll-Thread
Verify that the AccesToken are not out of date. If one AccessToken out of date it will be deletet from tokenList.
"""
while True:
print("start allocate")
for accToken in self.tokenList:
print("controle", accToken)
2019-04-23 22:08:25 +00:00
if (datetime.now() - accToken.timestamp).seconds > 7200:
print("delete", accToken)
self.tokenList.remove(accToken)
else:
print("time is only", (datetime.now() - accToken.timestamp).seconds)
print(self.tokenList)
print("wait")
time.sleep(10)