2019-04-11 21:56:55 +00:00
|
|
|
from geruecht.model.accessToken import AccessToken
|
2020-01-18 23:37:40 +00:00
|
|
|
import geruecht.controller as gc
|
|
|
|
import geruecht.controller.userController as uc
|
2020-01-18 22:31:49 +00:00
|
|
|
from geruecht.model import BAR
|
2019-05-02 23:40:13 +00:00
|
|
|
from geruecht.controller import LOGGER
|
2019-12-28 10:31:45 +00:00
|
|
|
from datetime import datetime, timedelta
|
2019-04-11 21:56:55 +00:00
|
|
|
import hashlib
|
2019-12-28 20:52:49 +00:00
|
|
|
from . import Singleton
|
2019-04-11 21:56:55 +00:00
|
|
|
|
2020-01-18 23:37:40 +00:00
|
|
|
userController = uc.UserController()
|
2020-01-18 22:31:49 +00:00
|
|
|
|
2019-12-28 10:31:45 +00:00
|
|
|
class AccesTokenController(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.
|
|
|
|
"""
|
2019-05-02 23:40:13 +00:00
|
|
|
instance = None
|
2019-04-11 21:56:55 +00:00
|
|
|
tokenList = None
|
|
|
|
|
2019-12-30 08:22:43 +00:00
|
|
|
def __init__(self, lifetime=1800):
|
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.
|
|
|
|
"""
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.info("Initialize AccessTokenController")
|
2020-01-18 23:37:40 +00:00
|
|
|
self.lifetime = gc.accConfig
|
2019-05-02 23:40:13 +00:00
|
|
|
|
2019-04-11 21:56:55 +00:00
|
|
|
self.tokenList = []
|
|
|
|
|
2020-01-18 23:37:40 +00:00
|
|
|
def checkBar(self, user):
|
|
|
|
if (userController.checkBarUser(user)):
|
2020-03-04 20:38:21 +00:00
|
|
|
if BAR not in user.group:
|
|
|
|
user.group.append(BAR)
|
|
|
|
else:
|
|
|
|
while BAR in user.group:
|
|
|
|
user.group.remove(BAR)
|
2020-01-18 22:31:49 +00:00
|
|
|
|
2019-12-28 10:31:45 +00:00
|
|
|
def validateAccessToken(self, token, group):
|
|
|
|
""" Verify Accestoken
|
2019-05-02 16:50:59 +00:00
|
|
|
|
2019-12-28 10:31:45 +00:00
|
|
|
Verify an Accestoken and Group so if the User has permission or not.
|
|
|
|
Retrieves the accestoken if valid else retrieves False
|
2019-04-17 12:46:46 +00:00
|
|
|
|
|
|
|
Args:
|
2019-12-28 10:31:45 +00:00
|
|
|
token: Token to verify.
|
|
|
|
group: Group like 'moneymaster', 'gastro', 'user' or 'bar'
|
2019-04-17 12:46:46 +00:00
|
|
|
Returns:
|
2019-12-28 10:31:45 +00:00
|
|
|
An the AccesToken for this given Token or False.
|
2019-04-17 12:46:46 +00:00
|
|
|
"""
|
2019-12-28 10:31:45 +00:00
|
|
|
LOGGER.info("Verify AccessToken with token: {} and group: {}".format(token, group))
|
2019-04-11 21:56:55 +00:00
|
|
|
for accToken in self.tokenList:
|
2019-12-28 10:31:45 +00:00
|
|
|
LOGGER.debug("Check is token {} same as in AccessToken {}".format(token, accToken))
|
2019-04-11 21:56:55 +00:00
|
|
|
if accToken == token:
|
2019-12-28 10:31:45 +00:00
|
|
|
LOGGER.debug("AccessToken is {}".format(accToken))
|
|
|
|
endTime = accToken.timestamp + timedelta(seconds=self.lifetime)
|
|
|
|
now = datetime.now()
|
|
|
|
LOGGER.debug("Check if AccessToken's Endtime {} is bigger then now {}".format(endTime, now))
|
|
|
|
if now <= endTime:
|
2020-01-18 22:31:49 +00:00
|
|
|
self.checkBar(accToken.user)
|
2019-12-28 10:31:45 +00:00
|
|
|
LOGGER.debug("Check if AccesToken {} has same group {}".format(accToken, group))
|
|
|
|
if self.isSameGroup(accToken, group):
|
|
|
|
accToken.updateTimestamp()
|
|
|
|
LOGGER.info("Found AccessToken {} with token: {} and group: {}".format(accToken, token, group))
|
|
|
|
return accToken
|
|
|
|
else:
|
|
|
|
LOGGER.debug("AccessToken {} is no longer valid and will removed".format(accToken))
|
|
|
|
self.tokenList.remove(accToken)
|
|
|
|
LOGGER.info("Found no valid AccessToken with token: {} and group: {}".format(token, group))
|
|
|
|
return False
|
2019-04-11 21:56:55 +00:00
|
|
|
|
2020-01-26 22:31:22 +00:00
|
|
|
def createAccesToken(self, user, ldap_conn):
|
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
|
|
|
|
"""
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.info("Create AccessToken")
|
2019-04-12 12:51:37 +00:00
|
|
|
now = datetime.ctime(datetime.now())
|
2019-12-19 07:12:29 +00:00
|
|
|
token = hashlib.md5((now + user.dn).encode('utf-8')).hexdigest()
|
2020-01-18 22:31:49 +00:00
|
|
|
self.checkBar(user)
|
2020-01-26 22:31:22 +00:00
|
|
|
accToken = AccessToken(user, token, ldap_conn, datetime.now())
|
2019-05-02 23:40:13 +00:00
|
|
|
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
|
|
|
|
|
2020-01-18 22:31:49 +00:00
|
|
|
def isSameGroup(self, accToken, groups):
|
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.
|
2020-01-18 22:31:49 +00:00
|
|
|
groups: 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
|
|
|
|
"""
|
2020-01-18 22:31:49 +00:00
|
|
|
print("controll if", accToken, "hase groups", groups)
|
|
|
|
LOGGER.debug("Check if AccessToken {} has group {}".format(accToken, groups))
|
|
|
|
for group in groups:
|
|
|
|
if group in accToken.user.group: return True
|
|
|
|
return False
|