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

Account for shared usage between TS and galaxy apps #16746

Merged
merged 1 commit into from
Sep 26, 2023
Merged
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
16 changes: 6 additions & 10 deletions lib/galaxy/managers/api_keys.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
from typing import Optional

from sqlalchemy import (
false,
select,
update,
)

from galaxy.model import (
APIKeys,
User,
)
from galaxy.model.base import transaction
from galaxy.structured_app import BasicSharedApp

Expand All @@ -19,11 +13,12 @@ def __init__(self, app: BasicSharedApp):
self.app = app
self.session = self.app.model.context

def get_api_key(self, user: User) -> Optional["APIKeys"]:
def get_api_key(self, user):
APIKeys = self.app.model.APIKeys
stmt = select(APIKeys).filter_by(user_id=user.id, deleted=False).order_by(APIKeys.create_time.desc()).limit(1)
return self.session.scalars(stmt).first()

def create_api_key(self, user: User) -> "APIKeys":
def create_api_key(self, user):
guid = self.app.security.get_new_guid()
new_key = self.app.model.APIKeys()
new_key.user_id = user.id
Expand All @@ -33,15 +28,15 @@ def create_api_key(self, user: User) -> "APIKeys":
self.session.commit()
return new_key

def get_or_create_api_key(self, user: User) -> str:
def get_or_create_api_key(self, user) -> str:
# Logic Galaxy has always used - but it would appear to have a race
# condition. Worth fixing? Would kind of need a message queue to fix
# in multiple process mode.
api_key = self.get_api_key(user)
key = api_key.key if api_key else self.create_api_key(user).key
return key

def delete_api_key(self, user: User) -> None:
def delete_api_key(self, user) -> None:
"""Marks the current user API key as deleted."""
# Before it was possible to create multiple API keys for the same user although they were not considered valid
# So all non-deleted keys are marked as deleted for backward compatibility
Expand All @@ -50,6 +45,7 @@ def delete_api_key(self, user: User) -> None:
self.session.commit()

def _mark_all_api_keys_as_deleted(self, user_id: int):
APIKeys = self.app.model.APIKeys
stmt = (
update(APIKeys)
.where(APIKeys.user_id == user_id)
Expand Down