Skip to content

Commit

Permalink
Fix #664 Django example installation store improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch committed Jun 2, 2022
1 parent 844b38d commit e4ac644
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
34 changes: 22 additions & 12 deletions examples/django/oauth_app/slack_datastores.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,9 @@ def save(self, installation: Installation):
i = installation.to_dict()
if is_naive(i["installed_at"]):
i["installed_at"] = make_aware(i["installed_at"])
if i.get("bot_token_expires_at") is not None and is_naive(
i["bot_token_expires_at"]
):
if i.get("bot_token_expires_at") is not None and is_naive(i["bot_token_expires_at"]):
i["bot_token_expires_at"] = make_aware(i["bot_token_expires_at"])
if i.get("user_token_expires_at") is not None and is_naive(
i["user_token_expires_at"]
):
if i.get("user_token_expires_at") is not None and is_naive(i["user_token_expires_at"]):
i["user_token_expires_at"] = make_aware(i["user_token_expires_at"])
i["client_id"] = self.client_id
row_to_update = (
Expand All @@ -62,9 +58,7 @@ def save_bot(self, bot: Bot):
b = bot.to_dict()
if is_naive(b["installed_at"]):
b["installed_at"] = make_aware(b["installed_at"])
if b.get("bot_token_expires_at") is not None and is_naive(
b["bot_token_expires_at"]
):
if b.get("bot_token_expires_at") is not None and is_naive(b["bot_token_expires_at"]):
b["bot_token_expires_at"] = make_aware(b["bot_token_expires_at"])
b["client_id"] = self.client_id

Expand Down Expand Up @@ -145,6 +139,24 @@ def find_installation(

if len(rows) > 0:
i = rows[0]
if user_id is not None:
# Fetch the latest bot token
latest_bot_rows = (
SlackInstallation.objects.filter(client_id=self.client_id)
.exclude(bot_token__isnull=True)
.filter(enterprise_id=e_id)
.filter(team_id=t_id)
.order_by(F("installed_at").desc())[:1]
)
if len(latest_bot_rows) > 0:
b = latest_bot_rows[0]
i.bot_id = b.bot_id
i.bot_user_id = b.bot_user_id
i.bot_scopes = b.bot_scopes
i.bot_token = b.bot_token
i.bot_refresh_token = b.bot_refresh_token
i.bot_token_expires_at = b.bot_token_expires_at

return Installation(
app_id=i.app_id,
enterprise_id=i.enterprise_id,
Expand Down Expand Up @@ -191,9 +203,7 @@ def issue(self) -> str:
return state

def consume(self, state: str) -> bool:
rows = SlackOAuthState.objects.filter(state=state).filter(
expire_at__gte=timezone.now()
)
rows = SlackOAuthState.objects.filter(state=state).filter(expire_at__gte=timezone.now())
if len(rows) > 0:
for row in rows:
row.delete()
Expand Down
4 changes: 3 additions & 1 deletion examples/django/oauth_app/slack_listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
from .slack_datastores import DjangoInstallationStore, DjangoOAuthStateStore

logger = logging.getLogger(__name__)
client_id, client_secret, signing_secret, scopes = (
client_id, client_secret, signing_secret, scopes, user_scopes = (
os.environ["SLACK_CLIENT_ID"],
os.environ["SLACK_CLIENT_SECRET"],
os.environ["SLACK_SIGNING_SECRET"],
os.environ.get("SLACK_SCOPES", "commands").split(","),
os.environ.get("SLACK_USER_SCOPES", "search:read").split(","),
)

app = App(
Expand All @@ -26,6 +27,7 @@
client_id=client_id,
client_secret=client_secret,
scopes=scopes,
user_scopes=user_scopes,
# If you want to test token rotation, enabling the following line will make it easy
# token_rotation_expiration_minutes=1000000,
installation_store=DjangoInstallationStore(
Expand Down

0 comments on commit e4ac644

Please sign in to comment.