hns-login/website/models.py
Nathan Woodburn 1414be5fc8
All checks were successful
Build Docker / Build Docker (push) Successful in 28s
feat: Add plain auth method
2024-06-19 14:07:05 +10:00

74 lines
2.0 KiB
Python

import time
from flask_sqlalchemy import SQLAlchemy
from authlib.integrations.sqla_oauth2 import (
OAuth2ClientMixin,
OAuth2AuthorizationCodeMixin,
OAuth2TokenMixin,
)
db = SQLAlchemy()
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(40), unique=True)
profile_picture = db.Column(db.String(255), nullable=True)
def __str__(self):
return self.username
def get_user_id(self):
return self.id
def get_user_profile_picture(self):
return self.profile_picture
def check_password(self, password):
return password == 'valid'
class OAuth2Client(db.Model, OAuth2ClientMixin):
__tablename__ = 'oauth2_client'
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(
db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'))
user = db.relationship('User')
class OAuth2AuthorizationCode(db.Model, OAuth2AuthorizationCodeMixin):
__tablename__ = 'oauth2_code'
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(
db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'))
user = db.relationship('User')
class OAuth2Token(db.Model, OAuth2TokenMixin):
__tablename__ = 'oauth2_token'
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(
db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'))
user = db.relationship('User')
def is_refresh_token_active(self):
if self.revoked:
return False
expires_at = self.issued_at + self.expires_in * 2
return expires_at >= time.time()
class AuthTokens(db.Model):
id = db.Column(db.Integer, primary_key=True)
service = db.Column(db.String(255))
user_name = db.Column(db.String(255))
access_token = db.Column(db.String(255))
refresh_token = db.Column(db.String(255))
expires_at = db.Column(db.Integer)
def is_refresh_token_active(self):
if self.revoked:
return False
return self.expires_at >= time.time()