Skip to content

Commit f9d201c

Browse files
committed
Add origin of token to BearerAuth class
1 parent 9053735 commit f9d201c

5 files changed

Lines changed: 12 additions & 11 deletions

File tree

openeo/rest/auth/auth.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ class BearerAuth(OpenEoApiAuthBase):
2929
https://open-eo.github.io/openeo-api/apireference/#section/Authentication/Bearer
3030
"""
3131

32-
def __init__(self, bearer: str):
32+
def __init__(self, bearer: str, origin: str):
3333
self.bearer = bearer
34+
self.origin = origin
3435

3536
def __call__(self, req: Request) -> Request:
3637
# Add bearer authorization header.
@@ -42,12 +43,11 @@ class BasicBearerAuth(BearerAuth):
4243
"""Bearer token for Basic Auth (openEO API 1.0.0 style)"""
4344

4445
def __init__(self, access_token: str):
45-
super().__init__(bearer="basic//{t}".format(t=access_token))
46+
super().__init__(bearer="basic//{t}".format(t=access_token), origin="basic")
4647

4748

4849
class OidcBearerAuth(BearerAuth):
4950
"""Bearer token for OIDC Auth (openEO API 1.0.0 style)"""
5051

5152
def __init__(self, provider_id: str, access_token: str):
52-
super().__init__(bearer="oidc/{p}/{t}".format(p=provider_id, t=access_token))
53-
53+
super().__init__(bearer="oidc/{p}/{t}".format(p=provider_id, t=access_token), origin="oidc")

openeo/rest/auth/testing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def validate_access_token(self, access_token: str):
281281
raise LookupError("Invalid access token")
282282

283283
def invalidate_access_token(self):
284-
self.state["access_token"] = "***invalidated***"
284+
self.state["access_token"] = None
285285

286286
def get_request_history(
287287
self, url: Optional[str] = None, method: Optional[str] = None

openeo/rest/connection.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ def authenticate_basic(self, username: Optional[str] = None, password: Optional[
283283

284284
# Switch to bearer based authentication in further requests.
285285
if jwt_conformance:
286-
self.auth = BearerAuth(bearer=resp["access_token"])
286+
self.auth = BearerAuth(bearer=resp["access_token"], origin="basic")
287287
else:
288288
self.auth = BasicBearerAuth(access_token=resp["access_token"])
289289
return self
@@ -426,7 +426,7 @@ def _authenticate_oidc(
426426
# check for JWT bearer token conformance
427427
jwt_conformance = self.capabilities().has_conformance(CONFORMANCE_JWT_BEARER)
428428
if jwt_conformance:
429-
self.auth = BearerAuth(bearer=token)
429+
self.auth = BearerAuth(bearer=token, origin="oidc")
430430
else:
431431
self.auth = OidcBearerAuth(provider_id=provider_id, access_token=token)
432432
self._oidc_auth_renewer = oidc_auth_renewer
@@ -738,7 +738,7 @@ def authenticate_bearer_token(self, bearer_token: str) -> Connection:
738738
739739
.. versionadded:: 0.38.0
740740
"""
741-
self.auth = BearerAuth(bearer=bearer_token)
741+
self.auth = BearerAuth(bearer=bearer_token, origin="unknown")
742742
self._oidc_auth_renewer = None
743743
return self
744744

@@ -748,7 +748,7 @@ def try_access_token_refresh(self, *, reason: Optional[str] = None) -> bool:
748748
Returns whether a new access token was obtained.
749749
"""
750750
reason = f" Reason: {reason}" if reason else ""
751-
if isinstance(self.auth, OidcBearerAuth) and self._oidc_auth_renewer:
751+
if isinstance(self.auth, BearerAuth) and self.auth.origin == "oidc" and self._oidc_auth_renewer:
752752
try:
753753
self._authenticate_oidc(
754754
authenticator=self._oidc_auth_renewer,

tests/extra/artifacts/_s3sts/test_s3sts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def conn_with_s3sts_capabilities(
9393
requests_mock.get(API_URL, json={"api_version": "1.0.0", **extra_api_capabilities})
9494
requests_mock.get(f"{API_URL}me", json={})
9595
conn = Connection(API_URL)
96-
conn.auth = BearerAuth("oidc/fake/token")
96+
conn.auth = BearerAuth("oidc/fake/token", origin="oidc")
9797
yield conn
9898

9999

tests/rest/test_connection.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ def debug(request: requests.Request, context):
276276

277277
requests_mock.get(url, text=debug)
278278

279-
con = RestApiConnection(api_root, auth=BearerAuth(secret))
279+
con = RestApiConnection(api_root, auth=BearerAuth(secret, origin="unknown"))
280280
res = con.get(url)
281281
assert "hello world" in res.text
282282
assert "User-Agent': 'openeo-python-client/" in res.text
@@ -2379,6 +2379,7 @@ def test_authenticate_eoidc_auto_renew_expired_access_token_initial_refresh_toke
23792379
"_used_oidc_provider": "oi",
23802380
"_used_access_token": access_token2,
23812381
}
2382+
assert False
23822383

23832384

23842385

0 commit comments

Comments
 (0)