API - FlaskSQLAlchemy update rollback
This commit is contained in:
@ -4,6 +4,6 @@ from fittrackee.utils import clean
|
||||
def clean_tokens(days: int) -> int:
|
||||
sql = """
|
||||
DELETE FROM oauth2_token
|
||||
WHERE oauth2_token.issued_at + oauth2_token.expires_in < :limit;
|
||||
WHERE oauth2_token.issued_at + oauth2_token.expires_in < %(limit)s;
|
||||
"""
|
||||
return clean(sql, days)
|
||||
|
@ -8,22 +8,23 @@ from authlib.integrations.sqla_oauth2 import (
|
||||
)
|
||||
from sqlalchemy.engine.base import Connection
|
||||
from sqlalchemy.event import listens_for
|
||||
from sqlalchemy.orm import mapped_column, relationship
|
||||
from sqlalchemy.ext.declarative import DeclarativeMeta
|
||||
from sqlalchemy.orm.mapper import Mapper
|
||||
from sqlalchemy.orm.session import Session
|
||||
from sqlalchemy.sql import text
|
||||
|
||||
from fittrackee import db
|
||||
|
||||
BaseModel: DeclarativeMeta = db.Model
|
||||
|
||||
class OAuth2Client(OAuth2ClientMixin, db.Model): # type: ignore
|
||||
|
||||
class OAuth2Client(BaseModel, OAuth2ClientMixin):
|
||||
__tablename__ = 'oauth2_client'
|
||||
|
||||
id = mapped_column(db.Integer, primary_key=True)
|
||||
user_id = mapped_column(
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
user_id = db.Column(
|
||||
db.Integer, db.ForeignKey('users.id', ondelete='CASCADE'), index=True
|
||||
)
|
||||
user = relationship('User')
|
||||
user = db.relationship('User')
|
||||
|
||||
def serialize(self, with_secret: bool = False) -> Dict:
|
||||
client = {
|
||||
@ -62,9 +63,7 @@ def on_old_oauth2_delete(
|
||||
).delete(synchronize_session=False)
|
||||
|
||||
|
||||
class OAuth2AuthorizationCode(
|
||||
OAuth2AuthorizationCodeMixin, db.Model # type: ignore
|
||||
):
|
||||
class OAuth2AuthorizationCode(BaseModel, OAuth2AuthorizationCodeMixin):
|
||||
__tablename__ = 'oauth2_code'
|
||||
__table_args__ = (
|
||||
db.Index(
|
||||
@ -73,21 +72,21 @@ class OAuth2AuthorizationCode(
|
||||
),
|
||||
)
|
||||
|
||||
id = mapped_column(db.Integer, primary_key=True)
|
||||
user_id = mapped_column(
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
user_id = db.Column(
|
||||
db.Integer, db.ForeignKey('users.id', ondelete='CASCADE'), index=True
|
||||
)
|
||||
user = relationship('User')
|
||||
user = db.relationship('User')
|
||||
|
||||
|
||||
class OAuth2Token(OAuth2TokenMixin, db.Model): # type: ignore
|
||||
class OAuth2Token(BaseModel, OAuth2TokenMixin):
|
||||
__tablename__ = 'oauth2_token'
|
||||
|
||||
id = mapped_column(db.Integer, primary_key=True)
|
||||
user_id = mapped_column(
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
user_id = db.Column(
|
||||
db.Integer, db.ForeignKey('users.id', ondelete='CASCADE'), index=True
|
||||
)
|
||||
user = relationship('User')
|
||||
user = db.relationship('User')
|
||||
|
||||
def is_refresh_token_active(self) -> bool:
|
||||
if self.is_revoked():
|
||||
@ -99,10 +98,10 @@ class OAuth2Token(OAuth2TokenMixin, db.Model): # type: ignore
|
||||
def revoke_client_tokens(cls, client_id: str) -> None:
|
||||
sql = """
|
||||
UPDATE oauth2_token
|
||||
SET access_token_revoked_at = :revoked_at
|
||||
WHERE client_id = :client_id;
|
||||
SET access_token_revoked_at = %(revoked_at)s
|
||||
WHERE client_id = %(client_id)s;
|
||||
"""
|
||||
db.session.execute(
|
||||
text(sql), {'client_id': client_id, 'revoked_at': int(time.time())}
|
||||
db.engine.execute(
|
||||
sql, {'client_id': client_id, 'revoked_at': int(time.time())}
|
||||
)
|
||||
db.session.commit()
|
||||
|
Reference in New Issue
Block a user