-
Notifications
You must be signed in to change notification settings - Fork 47
Implement list_auth_providers #826
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |||||||||
| import os | ||||||||||
| import shlex | ||||||||||
| import urllib.parse | ||||||||||
| import uuid | ||||||||||
| import warnings | ||||||||||
| from collections import OrderedDict | ||||||||||
| from pathlib import Path, PurePosixPath | ||||||||||
|
|
@@ -211,6 +212,37 @@ def _get_refresh_token_store(self) -> RefreshTokenStore: | |||||||||
| self._refresh_token_store = RefreshTokenStore() | ||||||||||
| return self._refresh_token_store | ||||||||||
|
|
||||||||||
| def list_auth_providers(self) -> list[dict]: | ||||||||||
| providers = [] | ||||||||||
| cap = self.capabilities() | ||||||||||
|
|
||||||||||
| # Add OIDC providers | ||||||||||
| oidc_path = "/credentials/oidc" | ||||||||||
| if cap.supports_endpoint(oidc_path, method="GET"): | ||||||||||
| try: | ||||||||||
m-mohr marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
| data = self.get(oidc_path, expected_status=200).json() | ||||||||||
| if isinstance(data, dict): | ||||||||||
| for provider in data.get("providers", []): | ||||||||||
| provider["type"] = "oidc" | ||||||||||
| providers.append(provider) | ||||||||||
| except OpenEoApiError: | ||||||||||
| pass | ||||||||||
|
||||||||||
| except OpenEoApiError: | |
| pass | |
| except OpenEoApiError as e: | |
| warnings.warn(f"Unable to load the OpenID Connect provider list: {e.message}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we try to avoid usage warnings (I see we still have some cases of that in connection.py), as it generally composes not that good with other tooling. Instead use _log.warning().
Also when including the exception in the warning message, we typically just do {e!r} so that more useful info is included (error code, http code, error message, correlation id )
so:
_log.warning(f"Unable to load the OpenID Connect provider list: {e!r}")
(!r is a shortcut for generic repr()-style rendering of the exception)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a warning for now.
Must say I don't like the {e!r} though.
Showing in a GUI (like QGIS, not Python coding)
Unable to load the OpenID Connect provider list: OpenEoApiError('[500] Internal: Maintanence ongoing')
feels like bad UX compared to:
Unable to load the OpenID Connect provider list: Maintanence ongoing')
But if the former is consistently done everywhere, I guess that's how it shall be for now...
m-mohr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
m-mohr marked this conversation as resolved.
Show resolved
Hide resolved
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -767,6 +767,59 @@ def test_create_connection_lazy_refresh_token_store(requests_mock): | |
| ) | ||
|
|
||
|
|
||
| def test_list_auth_providers(requests_mock, api_version): | ||
| requests_mock.get( | ||
| API_URL, | ||
| json={ | ||
| "api_version": api_version, | ||
| "endpoints": [ | ||
| {"methods": ["GET"], "path": "/credentials/basic"}, | ||
| {"methods": ["GET"], "path": "/credentials/oidc"}, | ||
| ], | ||
| }, | ||
m-mohr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
| requests_mock.get( | ||
| API_URL + "credentials/oidc", | ||
| json={ | ||
| "providers": [ | ||
| {"id": "p1", "issuer": "https://openeo.example", "title": "openEO", "scopes": ["openid"]}, | ||
| {"id": "p2", "issuer": "https://other.example", "title": "Other", "scopes": ["openid"]}, | ||
| ] | ||
| }, | ||
| ) | ||
|
|
||
| conn = Connection(API_URL) | ||
| providers = conn.list_auth_providers() | ||
| assert len(providers) == 3 | ||
|
|
||
| p1 = next(filter(lambda x: x["id"] == "p1", providers), None) | ||
| assert isinstance(p1, dict) | ||
| assert p1["type"] == "oidc" | ||
| assert p1["issuer"] == "https://openeo.example" | ||
| assert p1["title"] == "openEO" | ||
|
|
||
| p2 = next(filter(lambda x: x["id"] == "p2", providers), None) | ||
| assert isinstance(p2, dict) | ||
| assert p2["type"] == "oidc" | ||
| assert p2["issuer"] == "https://other.example" | ||
| assert p2["title"] == "Other" | ||
|
|
||
| basic = next(filter(lambda x: x["type"] == "basic", providers), None) | ||
| assert isinstance(basic, dict) | ||
| assert isinstance(basic["id"], str) | ||
| assert len(basic["id"]) > 0 | ||
| assert basic["issuer"] == API_URL + "credentials/basic" | ||
| assert basic["title"] == "Internal" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of using separate asserts, one for each fields, I prefer a single assert that checks the response as a whole, which is perfectly doable here, e.g. assert providers == [
{
"type": "oidc",
"issuer": ...
"title": ...
},
{
...
]The advantage of this is that you can easily see and understand the expected output.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't sure whether this might lead to problems when the (stub) API (for whatever reason) would change the order of the array?! |
||
|
|
||
|
|
||
| def test_list_auth_providers_empty(requests_mock, api_version): | ||
| requests_mock.get(API_URL, json={"api_version": api_version, "endpoints": []}) | ||
|
|
||
| conn = Connection(API_URL) | ||
| providers = conn.list_auth_providers() | ||
| assert len(providers) == 0 | ||
|
|
||
|
|
||
| def test_authenticate_basic_no_support(requests_mock, api_version): | ||
| requests_mock.get(API_URL, json={"api_version": api_version, "endpoints": []}) | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.