from geruecht.model.accessToken import AccessToken from geruecht.controller import LOGGER from datetime import datetime import time from threading import Thread import hashlib import logging from logging.handlers import WatchedFileHandler 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. """ class __OnlyOne: def __init__(self, arg): self.val = arg def __str__(self): return repr(self) + self.val instance = None tokenList = None lifetime = 60 def __init__(self, arg): """ Initialize AccessTokenController Initialize Thread and set tokenList empty. """ LOGGER.info("Initialize AccessTokenController") if not AccesTokenController.instance: AccesTokenController.instance = AccesTokenController.__OnlyOne(arg) else: AccesTokenController.instance.val = arg LOGGER.debug("Build Logger for VerificationThread") FORMATTER = logging.Formatter("%(asctime)s — %(name)s — %(levelname)s — %(message)s") logFileHandler = WatchedFileHandler("Verification.log") logFileHandler.setFormatter(FORMATTER) self.LOGGER = logging.getLogger("VerificationThread") self.LOGGER.setLevel(logging.DEBUG) self.LOGGER.addHandler(logFileHandler) self.LOGGER.propagate = False LOGGER.debug("Initialize 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. """ LOGGER.info("Search for Token: {}".format(token)) LOGGER.debug("Iterate through List of current Tokens") for accToken in self.tokenList: LOGGER.debug("Check if AccessToken {} has Token {}".format(accToken, token)) if accToken == token: LOGGER.info("Find AccessToken {} with Token {}".format(accToken, token)) return accToken LOGGER.info("no AccesToken found with Token {}".format(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 """ LOGGER.info("Create AccessToken") now = datetime.ctime(datetime.now()) token = hashlib.md5((now + user.password).encode('utf-8')).hexdigest() accToken = AccessToken(user, token) LOGGER.debug("Add AccessToken {} to current Tokens".format(accToken)) self.tokenList.append(accToken) LOGGER.info("Finished create AccessToken {} with Token {}".format(accToken, 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) LOGGER.debug("Check if AccessToken {} has group {}".format(accToken, 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. """ LOGGER.info("Start Thread for verification that the AccessToken are not out of date.") while True: self.LOGGER.debug("Start to iterate through List of current Tokens") for accToken in self.tokenList: self.LOGGER.debug("Check if AccessToken {} is out of date".format(accToken)) if (datetime.now() - accToken.timestamp).seconds > 7200: print("delete", accToken) self.LOGGER.info("Delete AccessToken {} from List of current Tokens".format(accToken)) self.tokenList.remove(accToken) else: self.LOGGER.debug("AccessToken {} is up to date. {} seconds left".format(accToken, 7200 - (datetime.now() - accToken.timestamp).seconds)) self.LOGGER.debug("List of current Tokens: {}".format(self.tokenList)) self.LOGGER.info("Wait 10 Seconds") time.sleep(10)