Skip to content

Commit 1fde89c

Browse files
committed
refactor: moved config of auth method to backend config
1 parent 1dc599c commit 1fde89c

File tree

7 files changed

+38
-26
lines changed

7 files changed

+38
-26
lines changed

app/config/openeo/settings.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
from pydantic import BaseModel
44

55

6+
class OpenEOAuthMethod(str, Enum):
7+
CLIENT_CREDENTIALS = "CLIENT_CREDENTIALS"
8+
USER_CREDENTIALS = "USER_CREDENTIALS"
9+
10+
611
class OpenEOBackendConfig(BaseModel):
12+
auth_method: OpenEOAuthMethod = OpenEOAuthMethod.USER_CREDENTIALS
713
client_credentials: Optional[str] = None
814
token_provider: Optional[str] = None
915
token_prefix: Optional[str] = None
10-
11-
12-
class OpenEOAuthMethod(str, Enum):
13-
CLIENT_CREDENTIALS = "CLIENT_CREDENTIALS"
14-
USER_CREDENTIALS = "USER_CREDENTIALS"

app/config/settings.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@ class Settings(BaseSettings):
4141
)
4242

4343
# openEO Settings
44-
openeo_auth_method: OpenEOAuthMethod = Field(
45-
default=OpenEOAuthMethod.USER_CREDENTIALS,
46-
json_schema_extra={"env": "OPENEO_AUTH_METHOD"},
47-
)
48-
4944
openeo_backends: str | None = Field(
5045
default="", json_schema_extra={"env": "OPENEO_BACKENDS"}
5146
)
@@ -60,21 +55,21 @@ def load_openeo_backends_from_env(self):
6055
required_fields = []
6156
if self.openeo_backends:
6257

63-
if self.openeo_auth_method == OpenEOAuthMethod.CLIENT_CREDENTIALS:
64-
required_fields = ["client_credentials"]
65-
elif self.openeo_auth_method == OpenEOAuthMethod.USER_CREDENTIALS:
66-
required_fields = ["token_provider"]
67-
6858
try:
6959
raw = json.loads(self.openeo_backends)
7060
for host, cfg in raw.items():
7161
backend = OpenEOBackendConfig(**cfg)
7262

63+
if backend.auth_method == OpenEOAuthMethod.CLIENT_CREDENTIALS:
64+
required_fields = ["client_credentials"]
65+
elif backend.auth_method == OpenEOAuthMethod.USER_CREDENTIALS:
66+
required_fields = ["token_provider"]
67+
7368
for field in required_fields:
7469
if not getattr(backend, field, None):
7570
raise ValueError(
7671
f"Backend '{host}' must define '{field}' when "
77-
f"OPENEO_AUTH_METHOD={self.openeo_auth_method}"
72+
f"OPENEO_AUTH_METHOD={backend.auth_method}"
7873
)
7974
self.openeo_backend_config[host] = OpenEOBackendConfig(**cfg)
8075
except Exception:

app/platforms/implementations/openeo.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,17 @@ async def _authenticate_user(
8888
if url not in settings.openeo_backend_config:
8989
raise ValueError(f"No OpenEO backend configuration found for URL: {url}")
9090

91-
if settings.openeo_auth_method == OpenEOAuthMethod.USER_CREDENTIALS:
91+
if (
92+
settings.openeo_backend_config[url].auth_method
93+
== OpenEOAuthMethod.USER_CREDENTIALS
94+
):
9295
logger.debug("Using user credentials for OpenEO connection authentication")
9396
bearer_token = await self._get_bearer_token(user_token, url)
9497
connection.authenticate_bearer_token(bearer_token=bearer_token)
95-
elif settings.openeo_auth_method == OpenEOAuthMethod.CLIENT_CREDENTIALS:
98+
elif (
99+
settings.openeo_backend_config[url].auth_method
100+
== OpenEOAuthMethod.CLIENT_CREDENTIALS
101+
):
96102
logger.debug(
97103
"Using client credentials for OpenEO connection authentication"
98104
)
@@ -105,7 +111,8 @@ async def _authenticate_user(
105111
)
106112
else:
107113
raise ValueError(
108-
f"Unsupported OpenEO authentication method: {settings.openeo_auth_method}"
114+
"Unsupported OpenEO authentication method: "
115+
f"{settings.openeo_backend_config[url].auth_method}"
109116
)
110117

111118
return connection
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,20 @@ Here is an example of how to structure this configuration:
2828
```json
2929
{
3030
"https://openeo.backend1.com": {
31+
"auth_method": "CLIENT_CREDENTIALS",
3132
"client_credentials": "oidc_provider/client_id/secret_secret",
33+
},
34+
"https://openeo.backend2.com": {
35+
"auth_method": "USER_CREDENTIALS",
3236
"token_provider": "backend",
3337
"token_prefix": "oidc/backend"
34-
},
38+
},
3539
...
3640
}
3741
```
3842
Each backend configuration can include the following fields:
3943

44+
- `auth_method`: The authentication method to use for the openEO backend. This value can either be `USER_CREDENTIALS` or `CLIENT_CREDENTIALS`. The default value is set to `USER_CREDENTIALS`.
4045
- `client_credentials`: The client credentials for authenticating with the openEO backend. This is required if the `OPENEO_AUTH_METHOD` is set to `CLIENT_CREDENTIALS`. It is a single string in the format `oidc_provider/client_id/client_secret` that should be split into its components when used.
4146
- `token_provider`: The provider refers to the OIDC IDP alias that needs to be used to exchange the incoming token to an external token. This is required if the `OPENEO_AUTH_METHOD` is set to `USER_CREDENTIALS`. For example, if you have a Keycloak setup with an IDP alias `openeo-idp`, you would set this field to `openeo-idp`. This means that when a user authenticates with their token, the Dispatcher will use the `openeo-idp` to exchange the user's token for a token that is valid for the openEO backend.
4247
- `token_prefix`: An optional prefix to be added to the token when authenticating (e.g., "CDSE"). The prefix is required by some backends to identify the token type. This will be prepended to the exchanged token when authenticating with the openEO backend.
@@ -64,5 +69,5 @@ KEYCLOAK_CLIENT_SECRET=apex-client-secret
6469
6570
# openEO Settings
6671
OPENEO_AUTH_METHOD=USER_CREDENTIALS
67-
OPENEO_BACKENDS='{"https://openeo.backend1.com" {"client_credentials": "oidc_provider/client_id/secret_secret", "token_provider": "backend", "token_prefix": "oidc/backend"}}'
72+
OPENEO_BACKENDS='{"https://openeo.backend1.com" {"auth_method": "CLIENT_CREDENTIALS", "client_credentials": "oidc_provider/client_id/secret_secret"}, "https://openeo.backend2.com" {"auth_method": "USER_CREDENTIALS", "token_provider": "backend", "token_prefix": "oidc/backend"}}'
6873
```

docs/getting_started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ docker run -d --name postgres -p 5432:5432 \
4949

5050
### Configure the environment
5151

52-
Create a `.env` file in the root directory of the project and set the necessary environment variables as described in the [Environment Configuration](environment.md) documentation.
52+
Create a `.env` file in the root directory of the project and set the necessary environment variables as described in the [Environment Configuration](configuration.md) documentation.
5353

5454
### Apply Database Migrations
5555
Ensure your database schema is up-to-date by running:

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ theme:
99
nav:
1010
- Home: index.md
1111
- Getting Started: getting_started.md
12+
- Configuration: configuration.md
1213
- Contributing: contributing.md
1314
- Architecture: architecture.md
1415

tests/platforms/test_openeo_platform.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,10 @@ def test_connection_expired_no_bearer(platform):
240240
new_callable=AsyncMock,
241241
)
242242
async def test_authenticate_user_with_user_credentials(mock_exchange, platform):
243+
url = "https://openeo.vito.be"
244+
243245
# enable user credentials path
244-
settings.openeo_auth_method = OpenEOAuthMethod.USER_CREDENTIALS
246+
settings.openeo_backend_config[url].auth_method = OpenEOAuthMethod.USER_CREDENTIALS
245247

246248
# set up a fake connection with the expected method
247249
conn = MagicMock()
@@ -251,7 +253,6 @@ async def test_authenticate_user_with_user_credentials(mock_exchange, platform):
251253
mock_exchange.return_value = {"access_token": "exchanged-token"}
252254

253255
# choose a url that maps via BACKEND_PROVIDER_ID_MAP (hostname only)
254-
url = "https://openeo.vito.be"
255256
returned = await platform._authenticate_user("user-token", url, conn)
256257

257258
# assertions
@@ -272,15 +273,17 @@ async def test_authenticate_user_with_user_credentials(mock_exchange, platform):
272273
async def test_authenticate_user_with_client_credentials(
273274
mock_exchange, monkeypatch, platform
274275
):
276+
url = "https://openeo.vito.be"
275277
# disable user credentials path -> use client credentials
276-
settings.openeo_auth_method = OpenEOAuthMethod.CLIENT_CREDENTIALS
278+
settings.openeo_backend_config[url].auth_method = (
279+
OpenEOAuthMethod.CLIENT_CREDENTIALS
280+
)
277281

278282
# prepare fake connection and spy method
279283
conn = MagicMock()
280284
conn.authenticate_oidc_client_credentials = MagicMock()
281285

282286
# ensure the exchange mock exists but is not awaited
283-
url = "https://openeo.vito.be"
284287
returned = await platform._authenticate_user("user-token", url, conn)
285288

286289
# client creds path should be used

0 commit comments

Comments
 (0)