Skip to content

Commit

Permalink
Add autoclean of refresh token expired
Browse files Browse the repository at this point in the history
  • Loading branch information
SeaweedbrainCY committed Nov 1, 2024
1 parent 8fd7e87 commit 1af2dd1
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from flask_apscheduler import APScheduler
from monitoring.sentry import sentry_configuration
from flask_migrate import Migrate
from datetime import datetime
import datetime as dt
from flask import request, redirect, make_response


Expand Down Expand Up @@ -48,24 +48,41 @@ def create_app():
@scheduler.task('interval', id='clean_email_verification_token_from_db', hours=12, misfire_grace_time=900)
def clean_email_verification_token_from_db():
with flask.app_context():
logging.info("🧹 Cleaning email verification tokens from database")
logging.info("Cleaning email verification tokens from database ...")
from zero_totp_db_model.model import EmailVerificationToken

tokens = db.session.query(EmailVerificationToken).all()
count = 0
for token in tokens:
if float(token.expiration) < datetime.now().timestamp():
if float(token.expiration) < dt.datetime.now().timestamp():
db.session.delete(token)
db.session.commit()
logging.info(f"❌ Deleted token for user {token.user_id} at {datetime.now()}")
count += 1

logging.info(f"Deleted {count} email verification tokens at {dt.datetime.now(dt.UTC).isoformat()}")

@scheduler.task('interval', id='clean_rate_limiting_from_db', hours=2, misfire_grace_time=900)
def clean_rate_limiting_from_db():
with flask.app_context():
logging.info("🧹 Cleaning rate limits from database")
logging.info("Cleaning rate limits from database ...")
from database.rate_limiting_repo import RateLimitingRepo
RateLimitingRepo().flush_outdated_limit()
logging.info(f"✅ Rate limits cleaned at {datetime.utcnow()}")
logging.info(f"Rate limits cleaned at {dt.datetime.now(dt.UTC).isoformat()}")


@scheduler.task('interval', id='clean_rate_limiting_from_db', hours=24, misfire_grace_time=900)
def clean_expired_refresh_token():
with flask.app_context():
logging.info("Cleaning expired refresh tokens from database ...")
from zero_totp_db_model.model import RefreshToken
tokens = db.session.query(RefreshToken).all()
count=0
for token in tokens:
if float(token.expiration) < dt.datetime.now(dt.UTC).timestamp():
db.session.delete(token)
db.session.commit()
count += 1
logging.info(f"Deleted {count} expired refresh tokens at {dt.datetime.now(dt.UTC).isoformat()}")

# DEPRECATED
#@flask.before_request
Expand Down

0 comments on commit 1af2dd1

Please sign in to comment.