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

issue #254 Clear cache when authenticate #691

Closed
Show file tree
Hide file tree
Changes from 1 commit
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
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()
ElienVandermaesenVITO marked this conversation as resolved.
Show resolved Hide resolved
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()
ElienVandermaesenVITO marked this conversation as resolved.
Show resolved Hide resolved
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:
ElienVandermaesenVITO marked this conversation as resolved.
Show resolved Hide resolved
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"})
ElienVandermaesenVITO marked this conversation as resolved.
Show resolved Hide resolved
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 == {}
ElienVandermaesenVITO marked this conversation as resolved.
Show resolved Hide resolved


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