48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
from datetime import datetime, timedelta, timezone
|
|
from secrets import compare_digest
|
|
|
|
from ..database import db
|
|
from ..database.types import ModelSerializeMixin, UtcDateTime, Serial
|
|
|
|
from flaschengeist import logger
|
|
|
|
|
|
class Session(db.Model, ModelSerializeMixin):
|
|
"""Model for a Session
|
|
|
|
Args:
|
|
expires: Is a Datetime from current Time.
|
|
user: Is an User.
|
|
token: String to verify access later.
|
|
"""
|
|
|
|
__tablename__ = "session"
|
|
expires: datetime = db.Column(UtcDateTime)
|
|
token: str = db.Column(db.String(32), unique=True)
|
|
lifetime: int = db.Column(db.Integer)
|
|
browser: str = db.Column(db.String(127))
|
|
platform: str = db.Column(db.String(64))
|
|
userid: str = ""
|
|
|
|
_id = db.Column("id", Serial, primary_key=True)
|
|
_user_id = db.Column("user_id", Serial, db.ForeignKey("user.id"))
|
|
user_: "User" = db.relationship("User", back_populates="sessions_")
|
|
|
|
@property
|
|
def userid(self):
|
|
return self.user_.userid
|
|
|
|
def refresh(self):
|
|
"""Update the Timestamp
|
|
|
|
Update the Timestamp to the current Time.
|
|
"""
|
|
logger.debug("update timestamp from session with token {{ {} }}".format(self.token))
|
|
self.expires = datetime.now(timezone.utc) + timedelta(seconds=self.lifetime)
|
|
|
|
def __eq__(self, token):
|
|
if isinstance(token, str):
|
|
return compare_digest(self.token, token)
|
|
else:
|
|
return super(Session, self).__eq__(token)
|