Skip to content

Commit 499b300

Browse files
committed
Implement list_auth_providers
1 parent 29c44fc commit 499b300

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

openeo/rest/connection.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os
1010
import shlex
1111
import urllib.parse
12+
import uuid
1213
import warnings
1314
from collections import OrderedDict
1415
from pathlib import Path, PurePosixPath
@@ -211,6 +212,37 @@ def _get_refresh_token_store(self) -> RefreshTokenStore:
211212
self._refresh_token_store = RefreshTokenStore()
212213
return self._refresh_token_store
213214

215+
def list_auth_providers(self) -> list[dict]:
216+
providers = []
217+
cap = self.capabilities()
218+
219+
# Add OIDC providers
220+
oidc_path = "/credentials/oidc"
221+
if cap.supports_endpoint(oidc_path, method="GET"):
222+
try:
223+
data = self.get(oidc_path, expected_status=200).json()
224+
if isinstance(data, dict):
225+
for provider in data.get("providers", []):
226+
provider["type"] = "oidc"
227+
providers.append(provider)
228+
except OpenEoApiError:
229+
pass
230+
231+
# Add Basic provider
232+
basic_path = "/credentials/basic"
233+
if cap.supports_endpoint(basic_path, method="GET"):
234+
providers.append(
235+
{
236+
"id": uuid.uuid4().hex,
237+
"issuer": self.build_url(basic_path),
238+
"type": "basic",
239+
"title": "HTTP Basic",
240+
"description": "HTTP Basic authentication using username and password",
241+
}
242+
)
243+
244+
return providers
245+
214246
def authenticate_basic(self, username: Optional[str] = None, password: Optional[str] = None) -> Connection:
215247
"""
216248
Authenticate a user to the backend using basic username and password.

tests/rest/test_connection.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,59 @@ def test_create_connection_lazy_refresh_token_store(requests_mock):
767767
)
768768

769769

770+
def test_list_auth_providers(requests_mock, api_version):
771+
requests_mock.get(
772+
API_URL,
773+
json={
774+
"api_version": api_version,
775+
"endpoints": [
776+
{"methods": ["GET"], "path": "/credentials/basic"},
777+
{"methods": ["GET"], "path": "/credentials/oidc"},
778+
],
779+
},
780+
)
781+
requests_mock.get(
782+
API_URL + "credentials/oidc",
783+
json={
784+
"providers": [
785+
{"id": "p1", "issuer": "https://openeo.example", "title": "openEO", "scopes": ["openid"]},
786+
{"id": "p2", "issuer": "https://other.example", "title": "Other", "scopes": ["openid"]},
787+
]
788+
},
789+
)
790+
791+
conn = Connection(API_URL)
792+
providers = conn.list_auth_providers()
793+
assert len(providers) == 3
794+
795+
p1 = next(filter(lambda x: x["id"] == "p1", providers), None)
796+
assert isinstance(p1, dict)
797+
assert p1["type"] == "oidc"
798+
assert p1["issuer"] == "https://openeo.example"
799+
assert p1["title"] == "openEO"
800+
801+
p2 = next(filter(lambda x: x["id"] == "p2", providers), None)
802+
assert isinstance(p2, dict)
803+
assert p2["type"] == "oidc"
804+
assert p2["issuer"] == "https://other.example"
805+
assert p2["title"] == "Other"
806+
807+
basic = next(filter(lambda x: x["type"] == "basic", providers), None)
808+
assert isinstance(basic, dict)
809+
assert isinstance(basic["id"], str)
810+
assert len(basic["id"]) > 0
811+
assert basic["issuer"] == API_URL + "credentials/basic"
812+
assert basic["title"] == "Basic"
813+
814+
815+
def test_list_auth_providers_empty(requests_mock, api_version):
816+
requests_mock.get(API_URL, json={"api_version": api_version, "endpoints": []})
817+
818+
conn = Connection(API_URL)
819+
providers = conn.list_auth_providers()
820+
assert len(providers) == 0
821+
822+
770823
def test_authenticate_basic_no_support(requests_mock, api_version):
771824
requests_mock.get(API_URL, json={"api_version": api_version, "endpoints": []})
772825

0 commit comments

Comments
 (0)