CLI - add command to clean expired oauth2 tokens
This commit is contained in:
13
fittrackee/oauth2/clean.py
Normal file
13
fittrackee/oauth2/clean.py
Normal file
@ -0,0 +1,13 @@
|
||||
import time
|
||||
|
||||
from fittrackee import db
|
||||
|
||||
|
||||
def clean_tokens(days: int) -> int:
|
||||
limit = int(time.time()) - (days * 86400)
|
||||
sql = """
|
||||
DELETE FROM oauth2_token
|
||||
WHERE oauth2_token.issued_at + oauth2_token.expires_in < %(limit)s;
|
||||
"""
|
||||
result = db.engine.execute(sql, {'limit': limit})
|
||||
return result.rowcount
|
29
fittrackee/oauth2/commands.py
Normal file
29
fittrackee/oauth2/commands.py
Normal file
@ -0,0 +1,29 @@
|
||||
import logging
|
||||
|
||||
import click
|
||||
|
||||
from fittrackee.cli.app import app
|
||||
|
||||
from .clean import clean_tokens
|
||||
|
||||
handler = logging.StreamHandler()
|
||||
logger = logging.getLogger('fittrackee_clean_tokens')
|
||||
logger.setLevel(logging.INFO)
|
||||
logger.addHandler(handler)
|
||||
|
||||
|
||||
@click.group(name='oauth2')
|
||||
def oauth2_cli() -> None:
|
||||
"""Manage OAuth2 tokens."""
|
||||
pass
|
||||
|
||||
|
||||
@oauth2_cli.command('clean')
|
||||
@click.option('--days', type=int)
|
||||
def clean(
|
||||
days: int,
|
||||
) -> None:
|
||||
"""Clean tokens expired for more than provided number of days"""
|
||||
with app.app_context():
|
||||
deleted_rows = clean_tokens(days)
|
||||
logger.info(f'Expired deleted tokens: {deleted_rows}.')
|
Reference in New Issue
Block a user