Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert revisit token revocation #64

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions hq_superset/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import time
from dataclasses import dataclass
from typing import Any

Expand All @@ -6,6 +7,7 @@
OAuth2TokenMixin,
)
from cryptography.fernet import MultiFernet
from sqlalchemy import update
from superset import db

from .const import OAUTH2_DATABASE_NAME
Expand Down Expand Up @@ -76,6 +78,17 @@ def set_client_secret(self, plaintext):
def check_client_secret(self, plaintext):
return self.get_client_secret() == plaintext

def revoke_tokens(self):
revoked_at = int(time.time())
stmt = (
update(OAuth2Token)
.where(OAuth2Token.client_id == self.client_id)
.where(OAuth2Token.access_token_revoked_at == 0)
.values(access_token_revoked_at=revoked_at)
)
db.session.execute(stmt)
db.session.commit()


class OAuth2Token(db.Model, OAuth2TokenMixin):
__bind_key__ = OAUTH2_DATABASE_NAME
Expand Down
1 change: 1 addition & 0 deletions hq_superset/oauth2_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

def save_token(token: dict, request: FlaskOAuth2Request) -> None:
client = request.client
client.revoke_tokens()

token = OAuth2Token(
client_id=client.client_id,
Expand Down
13 changes: 12 additions & 1 deletion hq_superset/tasks.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import os

from superset.extensions import celery_app
from sqlalchemy import delete

from .exceptions import TableMissing
from .models import DataSetChange
from .models import DataSetChange, OAuth2Token, db
from .services import AsyncImportHelper, refresh_hq_datasource


Expand All @@ -26,3 +27,13 @@ def process_dataset_change(request_json):
change.update_dataset()
except TableMissing:
pass


# ToDo: schedule this to run once every week/day
def delete_revoked_tokens():
stmt = (
delete(OAuth2Token)
.where(OAuth2Token.access_token_revoked_at != 0)
)
db.session.execute(stmt)
db.session.commit()