2024-02-09 23:04:20 +11:00
|
|
|
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)
|
2024-06-14 16:10:18 +10:00
|
|
|
profile_picture = db.Column(db.String(255), nullable=True)
|
2024-02-09 23:04:20 +11:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.username
|
|
|
|
|
|
|
|
def get_user_id(self):
|
|
|
|
return self.id
|
2024-06-14 16:10:18 +10:00
|
|
|
|
|
|
|
def get_user_profile_picture(self):
|
|
|
|
return self.profile_picture
|
|
|
|
|
2024-02-09 23:04:20 +11:00
|
|
|
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
|
2024-06-19 14:07:05 +10:00
|
|
|
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()
|