from datetime import datetime, timedelta, timezone from . import ModelSerializeMixin, UtcDateTime from .user import User from flaschengeist.database import db from secrets import compare_digest 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(30)) platform: str = db.Column(db.String(30)) _id = db.Column("id", db.Integer, primary_key=True) _user: User = db.relationship("User", back_populates="_sessions") _user_id = db.Column("user_id", db.Integer, db.ForeignKey("user.id")) def refresh(self): """Update the Timestamp Update the Timestamp to the current Time. """ logger.debug("update timestamp from session with token {{ {} }}".format(self)) self.expires = datetime.now(timezone.utc) + timedelta(seconds=self.lifetime) def __eq__(self, token): return compare_digest(self.token, token)