class AccessToken(db.Model): access_token_id = db.Column(db.Integer, primary_key=True) identity_token_id = db.Column(db.Integer, db.ForeignKey(IdentityToken.identity_token_id), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey(User.user_id), nullable=False, index=True) token = db.Column(db.String(256), nullable=False, unique=True) enabled = db.Column(db.Boolean, nullable=False) client_addr = db.Column(db.String(256), nullable=False) client_timestamp = db.Column(db.DateTime, nullable=False) server_addr = db.Column(db.String(256), nullable=True) server_timestamp = db.Column(db.DateTime, nullable=True) identity_token = db.relationship(IdentityToken, backref=db.backref('access_tokens', lazy='dynamic')) user = db.relationship(User, backref=db.backref('access_tokens', lazy='dynamic')) def __init__(self, identity_token, user, token, enabled, client_addr, client_timestamp, server_addr, server_timestamp): self.identity_token = identity_token self.user = user self.token = token self.enabled = enabled self.client_addr = client_addr self.client_timestamp = client_timestamp self.server_addr = server_addr self.server_timestamp = server_timestamp @property def server_addr_str(self): known_servers = { '162.243.195.82': 'michaelfogleman.com', } return known_servers.get(self.server_addr, self.server_addr) @property def age(self): return datetime.datetime.utcnow() - self.client_timestamp def check_token(self, token, max_age): print("checking token:", token) if self.age > max_age: return False return check_password_hash(self.token, token)
class IdentityToken(db.Model): identity_token_id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey(User.user_id), nullable=False, index=True) name = db.Column(db.String(256), nullable=False) token = db.Column(db.String(256), nullable=False) enabled = db.Column(db.Boolean, nullable=False) created = db.Column(db.DateTime, nullable=False) touched = db.Column(db.DateTime, nullable=False) user = db.relationship(User, backref=db.backref('identity_tokens', lazy='dynamic')) def __init__(self, user, name, token, enabled): self.user = user self.name = name self.token = token self.enabled = enabled self.created = datetime.datetime.utcnow() self.touched = self.created def check_token(self, token): return check_password_hash(self.token, token) def touch(self): self.touched = datetime.datetime.utcnow() db.session.commit()
class Stream(db.Model): """ Model for storing Stream related details. """ __tablename__ = "streams" stream_id = db.Column(db.Integer, primary_key=True, autoincrement=True) stream_name = db.Column(db.VARCHAR(60), unique=True, nullable=False) stream_desc = db.Column(db.String(255), nullable=True) date_added = db.Column(db.DateTime, nullable=False) created_by = db.Column(db.Integer, db.ForeignKey('users.id')) questions = db.relationship('Question', backref='stream', lazy='dynamic') def __init__(self, stream_name, created_by, stream_desc=""): self.stream_name = stream_name self.stream_desc = stream_desc self.created_by = created_by self.date_added = datetime.datetime.now()
class Question(db.Model): """ Model for storing Question related details. """ __tablename__ = "questions" question_id = db.Column(db.Integer, primary_key=True, autoincrement=True) question_name = db.Column(db.VARCHAR(12), unique=True, nullable=False) question_data = db.Column(db.JSON, nullable=True) date_added = db.Column(db.DateTime, nullable=False) stream_id = db.Column(db.Integer, db.ForeignKey('streams.stream_id')) created_by = db.Column(db.Integer, unique=False, nullable=False) def __init__(self, stream_name, stream_desc, stream_id, created_by): self.question_name = stream_name self.question_desc = stream_desc self.created_by = created_by self.stream_id = stream_id self.date_added = datetime.datetime.now()
class Question(db.Model): """ Model for storing Question related details. """ __tablename__ = "questions" question_id = db.Column(db.Integer, primary_key=True, autoincrement=True) question_name = db.Column(db.VARCHAR(64), unique=False, nullable=False) question_data = db.Column(db.JSON, nullable=True) date_added = db.Column(db.DateTime, nullable=False) stream_id = db.Column(db.Integer, db.ForeignKey('streams.stream_id')) created_by = db.Column(db.Integer, unique=False, nullable=False) def __init__(self, question_name, question_data, stream_id, created_by): self.question_name = question_name self.question_data = question_data self.created_by = created_by self.stream_id = stream_id self.date_added = datetime.datetime.now() def as_dict(self): return {c.name: getattr(self, c.name) for c in self.__table__.columns}
class Token(db.Model): id = db.Column(UUID(as_uuid=True), primary_key=True, index=True) sub = db.Column(UUID(as_uuid=True), db.ForeignKey('user.id', ondelete='CASCADE'), nullable=False) exp = db.Column(db.DateTime(), nullable=False) status = db.Column(db.Enum(TokenEnum), nullable=False) def set_inactive(self): self.status = TokenEnum.INACTIVE def __init__(self, token=None, *args, **kwargs): if token is not None: kwargs['id'] = hash_token_to_uuid(token) payload = jwt.decode(token, verify=False) kwargs['sub'] = uuid.UUID(int=int(payload['sub'])) kwargs['exp'] = datetime.utcfromtimestamp(payload['exp']) kwargs['status'] = TokenEnum.ACTIVE super(Token, self).__init__(**kwargs) def __repr__(self): return '<Token id={}, sub={}>'.format(self.id, self.sub)