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

Add support for API auth with access token #482

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 8 additions & 1 deletion bioblend/galaxy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(
email: Optional[str] = None,
password: Optional[str] = None,
*,
token: Optional[str] = None,
verify: bool = True,
) -> None:
"""
Expand Down Expand Up @@ -79,10 +80,16 @@ def __init__(
:param password: Password of Galaxy account corresponding to the above
e-mail address. Ignored if key is supplied directly.

:type token: str
:param token: An OIDC access token obtained from an OIDC provider
configured in `oidc_backends_config.xml`. Can be used
as a substitue for an API key. You must make sure the access
token has not expired when making an API call.

:param verify: Whether to verify the server's TLS certificate
:type verify: bool
"""
super().__init__(url, key=key, email=email, password=password, verify=verify)
super().__init__(url, key=key, email=email, password=password, token=token, verify=verify)
self.libraries = libraries.LibraryClient(self)
self.histories = histories.HistoryClient(self)
self.workflows = workflows.WorkflowClient(self)
Expand Down
5 changes: 4 additions & 1 deletion bioblend/galaxy/objects/galaxy_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ def __init__(
email: Optional[str] = None,
password: Optional[str] = None,
*,
token: Optional[str] = None,
verify: bool = True,
) -> None:
self.gi = bioblend.galaxy.GalaxyInstance(url, key=api_key, email=email, password=password, verify=verify)
self.gi = bioblend.galaxy.GalaxyInstance(
url, key=api_key, email=email, password=password, token=token, verify=verify
)
self.log = bioblend.log
self.datasets = client.ObjDatasetClient(self)
self.dataset_collections = client.ObjDatasetCollectionClient(self)
Expand Down
8 changes: 7 additions & 1 deletion bioblend/galaxyclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def __init__(
email: Optional[str] = None,
password: Optional[str] = None,
*,
token: Optional[str] = None,
verify: bool = True,
timeout: Optional[float] = None,
) -> None:
Expand Down Expand Up @@ -73,13 +74,18 @@ def __init__(
# password and grab user's key before first request.
if key:
self._key: Optional[str] = key
elif token:
self.token: Optional[str] = token
Comment on lines +77 to +78
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
elif token:
self.token: Optional[str] = token

else:
self._key = None
self.email = email
self.password = password
self.json_headers: dict = {"Content-Type": "application/json"}
# json_headers needs to be set before key can be defined, otherwise authentication with email/password causes an error
self.json_headers["x-api-key"] = self.key
if token:
self.json_headers["Authorization"] = f"Bearer {self.token}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.json_headers["Authorization"] = f"Bearer {self.token}"
self.json_headers["Authorization"] = f"Bearer {token}"

else:
self.json_headers["x-api-key"] = self.key
# Number of attempts before giving up on a GET request.
self._max_get_attempts = 1
# Delay in seconds between subsequent retries.
Expand Down
Loading