Skip to content

Commit

Permalink
Add Connection.authenticate_bearer_token
Browse files Browse the repository at this point in the history
related to #719/#723
  • Loading branch information
soxofaan committed Feb 7, 2025
1 parent 29a3276 commit 9a7f727
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
23 changes: 19 additions & 4 deletions openeo/rest/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,8 @@ def authenticate_oidc_authorization_code(
.. deprecated:: 0.19.0
Usage of the Authorization Code flow is deprecated (because of its complexity) and will be removed.
It is recommended to use the Device Code flow with :py:meth:`authenticate_oidc_device`
or Client Credentials flow with :py:meth:`authenticate_oidc_client_credentials`.
It is recommended to use the Device Code flow with :py:meth:`Connection.authenticate_oidc_device`
or Client Credentials flow with :py:meth:`Connection.authenticate_oidc_client_credentials`.
"""
provider_id, client_info = self._get_oidc_provider_and_client_info(
provider_id=provider_id, client_id=client_id, client_secret=client_secret,
Expand Down Expand Up @@ -612,8 +612,6 @@ def authenticate_oidc_access_token(self, access_token: str, provider_id: Optiona
:param access_token: OIDC access token
:param provider_id: id of the OIDC provider as listed by the openEO backend (``/credentials/oidc``).
If not specified, the first (default) OIDC provider will be used.
:param skip_verification: Skip clients-side verification of the provider_id
against the backend's list of providers to avoid and related OIDC configuration
.. versionadded:: 0.31.0
Expand All @@ -625,6 +623,23 @@ def authenticate_oidc_access_token(self, access_token: str, provider_id: Optiona
self._oidc_auth_renewer = None
return self

def authenticate_bearer_token(self, bearer_token: str) -> Connection:
"""
Set up authorization headers directly with a bearer token.
.. warning::
This helper is for advanced usage only.
In general, it is recommended to use the more standard OIDC authentication methods
like :py:meth:`Connection.authenticate_oidc`.
:param bearer_token: openEO-style bearer token.
.. versionadded:: 0.38.0
"""
self.auth = BearerAuth(bearer=bearer_token)
self._oidc_auth_renewer = None
return self

def request(
self,
method: str,
Expand Down
6 changes: 6 additions & 0 deletions tests/rest/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2512,6 +2512,12 @@ def test_authenticate_oidc_access_token_wrong_provider(self):
):
connection.authenticate_oidc_access_token(access_token="Th3Tok3n!@#", provider_id="nope")

def test_authenticate_bearer_token(self):
connection = Connection(API_URL)
connection.authenticate_bearer_token("custom/foo/b666r")
assert isinstance(connection.auth, BearerAuth)
assert connection.auth.bearer == "custom/foo/b666r"


class TestLoadCollection:
def test_load_collection_arguments_basic(self, dummy_backend):
Expand Down

0 comments on commit 9a7f727

Please sign in to comment.