from geruecht.model.accessToken import AccessToken from datetime import datetime import time from threading import Thread import hashlib class AccesTokenController(Thread): """ 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. """ tokenList = None lifetime = 60 def __init__(self): """ Initialize AccessTokenController Initialize Thread and set tokenList empty. """ print("init AccesTokenControlle") print("init threading") Thread.__init__(self) self.tokenList = [] def findAccesToken(self, token): """ 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) for accToken in self.tokenList: if accToken == token: print("find AccesToken", accToken, "with token", token) return accToken print("no AccesToken with", token) return None def createAccesToken(self, user): """ 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() self.tokenList.append(AccessToken(user, token)) print(self.tokenList) print("finished create AccesToken", token) return token def isSameGroup(self, accToken, group): """ 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) return True if accToken.user.group == group else False def run(self): """ 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) 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)