Skip to content

Commit

Permalink
issue #254 Clear cache when authenticate
Browse files Browse the repository at this point in the history
  • Loading branch information
ElienVandermaesenVITO committed Dec 18, 2024
1 parent e106045 commit f5d6026
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions openeo/rest/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ def authenticate_basic(self, username: Optional[str] = None, password: Optional[
if username is None:
raise OpenEoClientException("No username/password given or found.")

self._capabilities_cache.clear()
resp = self.get(
'/credentials/basic',
# /credentials/basic is the only endpoint that expects a Basic HTTP auth
Expand Down Expand Up @@ -470,6 +471,7 @@ def _get_oidc_provider(
f"No OIDC provider given. Using first provider {provider_id!r} as advertised by backend."
)

self._capabilities_cache.clear()
provider_info = OidcProviderInfo.from_dict(provider) if parse_info else None

return provider_id, provider_info
Expand Down
3 changes: 3 additions & 0 deletions openeo/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,9 @@ def get(self, key: Union[str, tuple], load: Callable[[], Any]):
self._cache[key] = load()
return self._cache[key]

def clear(self):
self._cache = {}


def str_truncate(text: str, width: int = 64, ellipsis: str = "...") -> str:
"""Shorten a string (with an ellipsis) if it is longer than certain length."""
Expand Down
33 changes: 33 additions & 0 deletions tests/rest/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,39 @@ def test_capabilities_caching(requests_mock):
assert con.capabilities().api_version() == "1.0.0"
assert m.call_count == 1

def test_capabilities_caching_after_authenticate_basic(requests_mock):
user, pwd = "john262", "J0hndo3"
requests_mock.get(API_URL, json={"api_version": "1.0.0", "endpoints": BASIC_ENDPOINTS})
requests_mock.get(API_URL + 'credentials/basic', text=_credentials_basic_handler(user, pwd))

with mock.patch('openeo.rest.connection.AuthConfig') as AuthConfig:
conn = Connection(API_URL)
conn._capabilities_cache._cache={"test":"test1"}
assert conn._capabilities_cache._cache != {}
AuthConfig.return_value.get_basic_auth.return_value = (user, pwd)
conn.authenticate_basic(user, pwd)
assert conn._capabilities_cache._cache == {}


def test_capabilities_caching_after_authenticate_oidc(requests_mock):
requests_mock.get(API_URL, json={"api_version": "1.0.0"})
client_id = "myclient"
requests_mock.get(API_URL + 'credentials/oidc', json={
"providers": [{"id": "fauth", "issuer": "https://fauth.test", "title": "Foo Auth", "scopes": ["openid", "im"]}]
})
oidc_mock = OidcMock(
requests_mock=requests_mock,
expected_grant_type="authorization_code",
expected_client_id=client_id,
expected_fields={"scope": "im openid"},
oidc_issuer="https://fauth.test",
scopes_supported=["openid", "im"],
)
conn = Connection(API_URL)
conn._capabilities_cache._cache = {"test": "test1"}
conn.authenticate_oidc_authorization_code(client_id=client_id, webbrowser_open=oidc_mock.webbrowser_open)
assert conn._capabilities_cache._cache == {}


def test_file_formats(requests_mock):
requests_mock.get("https://oeo.test/", json={"api_version": "1.0.0"})
Expand Down

0 comments on commit f5d6026

Please sign in to comment.