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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions openeo/rest/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ def authenticate_basic(self, username: Optional[str] = None, password: Optional[
).json()
# Switch to bearer based authentication in further requests.
self.auth = BasicBearerAuth(access_token=resp["access_token"])
self._capabilities_cache.clear()
return self

def _get_oidc_provider(
Expand Down Expand Up @@ -543,6 +544,7 @@ def _authenticate_oidc(
_log.warning("No OIDC refresh token to store.")
token = tokens.access_token
self.auth = OidcBearerAuth(provider_id=provider_id, access_token=token)
self._capabilities_cache.clear()
self._oidc_auth_renewer = oidc_auth_renewer
return self

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
80 changes: 80 additions & 0 deletions tests/rest/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,86 @@ 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"

def get_capabilities(request, context):
endpoints = BASIC_ENDPOINTS
ElienVandermaesenVITO marked this conversation as resolved.
Show resolved Hide resolved
if "Authorization" in request.headers:
endpoints.append({"path": "/account/status", "methods": ["GET"]})
return {"api_version": "1.0.0", "endpoints": endpoints}

get_capabilities_mock = requests_mock.get(API_URL, json=get_capabilities)
requests_mock.get(API_URL + 'credentials/basic', text=_credentials_basic_handler(user, pwd))

con = Connection(API_URL)
assert con.capabilities().capabilities == {
"api_version": "1.0.0",
"endpoints": [
{"methods": ["GET"], "path": "/credentials/basic"},
],
}
assert get_capabilities_mock.call_count == 1
con.capabilities()
assert get_capabilities_mock.call_count == 1

con.authenticate_basic(user, pwd)
assert get_capabilities_mock.call_count == 1
assert con.capabilities().capabilities == {
"api_version": "1.0.0",
"endpoints": [
{"methods": ["GET"], "path": "/credentials/basic"},
{"methods": ["GET"], "path": "/account/status"},
],
}
assert get_capabilities_mock.call_count == 2



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"

def get_capabilities(request, context):
endpoints = BASIC_ENDPOINTS
if "Authorization" in request.headers:
endpoints.append({"path": "/account/status", "methods": ["GET"]})
return {"api_version": "1.0.0", "endpoints": endpoints}

get_capabilities_mock = requests_mock.get(API_URL, json=get_capabilities)
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)
assert conn.capabilities().capabilities == {
"api_version": "1.0.0",
"endpoints": [
{"methods": ["GET"], "path": "/credentials/basic"},
],
}
assert get_capabilities_mock.call_count == 1
conn.capabilities()
assert get_capabilities_mock.call_count == 1

conn.authenticate_oidc_authorization_code(client_id=client_id, webbrowser_open=oidc_mock.webbrowser_open)
assert get_capabilities_mock.call_count == 1
assert conn.capabilities().capabilities == {
"api_version": "1.0.0",
"endpoints": [
{"methods": ["GET"], "path": "/credentials/basic"},
{"methods": ["GET"], "path": "/account/status"},
],
}
assert get_capabilities_mock.call_count == 2


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