2019-04-11 21:56:55 +00:00
|
|
|
from datetime import datetime
|
2020-03-10 08:19:11 +00:00
|
|
|
from geruecht.logger import getDebugLogger
|
2019-05-02 23:40:13 +00:00
|
|
|
|
2020-03-10 18:23:52 +00:00
|
|
|
debug = getDebugLogger()
|
2019-04-11 21:56:55 +00:00
|
|
|
|
|
|
|
class AccessToken():
|
2019-04-17 12:46:46 +00:00
|
|
|
""" Model for an AccessToken
|
2019-05-02 16:50:59 +00:00
|
|
|
|
2019-04-17 12:46:46 +00:00
|
|
|
Attributes:
|
|
|
|
timestamp: Is a Datetime from current Time.
|
|
|
|
user: Is an User.
|
|
|
|
token: String to verify access later.
|
|
|
|
"""
|
2019-04-11 21:56:55 +00:00
|
|
|
|
|
|
|
timestamp = None
|
|
|
|
user = None
|
|
|
|
token = None
|
|
|
|
|
2020-06-04 22:34:32 +00:00
|
|
|
def __init__(self, id, user, token, lifetime, timestamp=datetime.now(), browser=None, platform=None):
|
2019-04-17 12:46:46 +00:00
|
|
|
""" Initialize Class AccessToken
|
2019-05-02 16:50:59 +00:00
|
|
|
|
2019-04-17 12:46:46 +00:00
|
|
|
No more to say.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
User: Is an User to set.
|
|
|
|
token: Is a String to verify later
|
|
|
|
timestamp: Default current time, but can set to an other datetime-Object.
|
|
|
|
"""
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.debug("init accesstoken")
|
2020-06-04 21:03:39 +00:00
|
|
|
self.id = id
|
2019-04-11 21:56:55 +00:00
|
|
|
self.user = user
|
|
|
|
self.timestamp = timestamp
|
2020-03-07 13:56:44 +00:00
|
|
|
self.lifetime = lifetime
|
2019-04-11 21:56:55 +00:00
|
|
|
self.token = token
|
2020-03-17 19:37:01 +00:00
|
|
|
self.lock_bar = False
|
2020-06-04 22:34:32 +00:00
|
|
|
self.browser = browser
|
|
|
|
self.platform = platform
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.debug("accesstoken is {{ {} }}".format(self))
|
2019-04-11 21:56:55 +00:00
|
|
|
|
|
|
|
def updateTimestamp(self):
|
2019-04-17 12:46:46 +00:00
|
|
|
""" Update the Timestamp
|
|
|
|
|
|
|
|
Update the Timestamp to the current Time.
|
|
|
|
"""
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.debug("update timestamp from accesstoken {{ {} }}".format(self))
|
2019-04-11 21:56:55 +00:00
|
|
|
self.timestamp = datetime.now()
|
|
|
|
|
2020-06-04 22:34:32 +00:00
|
|
|
def toJSON(self):
|
|
|
|
""" Create Dic to dump in JSON
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A Dic with static Attributes.
|
|
|
|
"""
|
|
|
|
dic = {
|
|
|
|
"id": self.id,
|
|
|
|
"timestamp": {'year': self.timestamp.year,
|
|
|
|
'month': self.timestamp.month,
|
|
|
|
'day': self.timestamp.day,
|
|
|
|
'hour': self.timestamp.hour,
|
|
|
|
'minute': self.timestamp.minute,
|
|
|
|
'second': self.timestamp.second
|
|
|
|
},
|
|
|
|
"lifetime": self.lifetime,
|
|
|
|
"browser": self.browser,
|
|
|
|
"platform": self.platform
|
|
|
|
}
|
|
|
|
return dic
|
|
|
|
|
2019-04-11 21:56:55 +00:00
|
|
|
def __eq__(self, token):
|
|
|
|
return True if self.token == token else False
|
|
|
|
|
|
|
|
def __sub__(self, other):
|
|
|
|
return other - self.timestamp
|
|
|
|
|
|
|
|
def __str__(self):
|
2020-03-09 18:54:51 +00:00
|
|
|
return "AccessToken(user={}, token={}, timestamp={}, lifetime={}".format(self.user, self.token, self.timestamp, self.lifetime)
|
2019-04-11 21:56:55 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
2020-03-09 18:54:51 +00:00
|
|
|
return "AccessToken(user={}, token={}, timestamp={}, lifetime={}".format(self.user, self.token, self.timestamp, self.lifetime)
|