flaschengeist/geruecht/controller/accesTokenController.py

126 lines
4.7 KiB
Python
Raw Normal View History

2019-04-11 21:56:55 +00:00
from geruecht.model.accessToken import AccessToken
from geruecht.controller import LOGGER
2019-04-11 21:56:55 +00:00
from datetime import datetime
import time
from threading import Thread
2019-04-11 21:56:55 +00:00
import hashlib
import logging
from logging.handlers import WatchedFileHandler
from geruecht import Singleton
2019-04-11 21:56:55 +00:00
class AccesTokenController(Thread, metaclass=Singleton):
2019-04-17 12:46:46 +00:00
""" Control all createt AccesToken
2019-05-02 16:50:59 +00:00
2019-04-17 12:46:46 +00:00
This Class create, delete, find and manage AccesToken.
Attributes:
tokenList: List of currents AccessToken
lifetime: Variable for the Lifetime of one AccessToken in seconds.
"""
instance = None
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, arg):
2019-04-17 12:46:46 +00:00
""" Initialize AccessTokenController
2019-05-02 16:50:59 +00:00
2019-04-17 12:46:46 +00:00
Initialize Thread and set tokenList empty.
"""
LOGGER.info("Initialize AccessTokenController")
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)
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
2019-05-02 16:50:59 +00:00
2019-04-17 12:46:46 +00:00
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")
2019-04-11 21:56:55 +00:00
for accToken in self.tokenList:
LOGGER.debug("Check if AccessToken {} has Token {}".format(accToken, token))
2019-04-11 21:56:55 +00:00
if accToken == token:
LOGGER.info("Find AccessToken {} with Token {}".format(accToken, token))
2019-04-11 21:56:55 +00:00
return accToken
LOGGER.info("no AccesToken found with Token {}".format(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
"""
LOGGER.info("Create AccessToken")
now = datetime.ctime(datetime.now())
token = hashlib.md5((now + user.dn).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))
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
2019-05-02 16:50:59 +00:00
2019-04-17 12:46:46 +00:00
Verify if the User in the AccesToken has the right group.
Args:
accToken: AccessToken to verify.
group: Group to verify.
2019-05-02 16:50:59 +00:00
2019-04-17 12:46:46 +00:00
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))
2019-12-22 21:27:39 +00:00
return True if group in accToken.user.group else False
def run(self):
2019-04-17 12:46:46 +00:00
""" Starting Controll-Thread
2019-05-02 16:50:59 +00:00
2019-04-17 12:46:46 +00:00
Verify that the AccesToken are not out of date. If one AccessToken out of date it will be deletet from tokenList.
"""
valid_time=7200
LOGGER.info("Start Thread for verification that the AccessToken are not out of date.")
while True:
self.LOGGER.debug("Name: {}".format(self.getName()))
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 > valid_time:
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, valid_time - (datetime.now() - accToken.timestamp).seconds))
self.LOGGER.debug("List of current Tokens: {}".format(self.tokenList))
self.LOGGER.info("Wait 10 Seconds")
time.sleep(10)