Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
jdries committed Nov 17, 2020
2 parents bfa5661 + ed4d67d commit 642de15
Show file tree
Hide file tree
Showing 5 changed files with 680 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
- Add `DataCube.aggregate_spatial()`

### Changed
- Get/create default `RefreshTokenStore` lazily in `Connection`

### Removed

Expand Down
638 changes: 638 additions & 0 deletions examples/notebooks/ProcessAsAService.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions examples/notebooks/my_ndvi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"process_graph": {"loadcollection1": {"process_id": "load_collection", "arguments": {"bands": ["TOC-B04_10M", "TOC-B08_10M"], "id": "TERRASCOPE_S2_TOC_V2", "spatial_extent": null, "temporal_extent": null}}, "filtertemporal1": {"process_id": "filter_temporal", "arguments": {"data": {"from_node": "loadcollection1"}, "extent": [{"from_parameter": "date"}, {"from_parameter": "date"}]}}, "filterbbox1": {"process_id": "filter_bbox", "arguments": {"data": {"from_node": "filtertemporal1"}, "extent": {"west": 6.8371137, "east": 6.8566699, "north": 50.5647147, "south": 50.560007, "crs": "EPSG:4326"}}}, "reducedimension1": {"process_id": "reduce_dimension", "arguments": {"data": {"from_node": "filterbbox1"}, "dimension": "bands", "reducer": {"process_graph": {"arrayelement1": {"process_id": "array_element", "arguments": {"data": {"from_parameter": "data"}, "index": 1}}, "arrayelement2": {"process_id": "array_element", "arguments": {"data": {"from_parameter": "data"}, "index": 0}}, "subtract1": {"process_id": "subtract", "arguments": {"x": {"from_node": "arrayelement1"}, "y": {"from_node": "arrayelement2"}}}, "add1": {"process_id": "add", "arguments": {"x": {"from_node": "arrayelement2"}, "y": {"from_node": "arrayelement1"}}}, "divide1": {"process_id": "divide", "arguments": {"x": {"from_node": "subtract1"}, "y": {"from_node": "add1"}}, "result": true}}}}, "result": true}}, "summary": "My NDVI process"}
11 changes: 8 additions & 3 deletions openeo/rest/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def __init__(
)

self._auth_config = auth_config
self._refresh_token_store = refresh_token_store or RefreshTokenStore()
self._refresh_token_store = refresh_token_store

@classmethod
def version_discovery(cls, url: str, session: requests.Session = None) -> str:
Expand Down Expand Up @@ -258,6 +258,11 @@ def _get_auth_config(self) -> AuthConfig:
self._auth_config = AuthConfig()
return self._auth_config

def _get_refresh_token_store(self) -> RefreshTokenStore:
if self._refresh_token_store is None:
self._refresh_token_store = RefreshTokenStore()
return self._refresh_token_store

def authenticate_basic(self, username: str = None, password: str = None) -> 'Connection':
"""
Authenticate a user to the backend using basic username and password.
Expand Down Expand Up @@ -381,7 +386,7 @@ def _authenticate_oidc(
tokens = authenticator.get_tokens()
_log.info("Obtained tokens: {t}".format(t=[k for k, v in tokens._asdict().items() if v]))
if tokens.refresh_token and store_refresh_token:
self._refresh_token_store.set_refresh_token(
self._get_refresh_token_store().set_refresh_token(
issuer=authenticator.provider_info.issuer,
client_id=authenticator.client_id,
refresh_token=tokens.refresh_token
Expand Down Expand Up @@ -470,7 +475,7 @@ def authenticate_oidc_refresh_token(
)

if refresh_token is None:
refresh_token = self._refresh_token_store.get_refresh_token(
refresh_token = self._get_refresh_token_store().get_refresh_token(
issuer=client_info.provider.issuer,
client_id=client_info.client_id
)
Expand Down
31 changes: 31 additions & 0 deletions tests/rest/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,37 @@ def text_callback(request, context):
assert AuthConfig.call_count == 1


def test_create_connection_lazy_refresh_token_store(requests_mock):
user, pwd = "john262", "J0hndo3"
client_id = "myclient"
client_secret = "$3cr3t"
requests_mock.get(API_URL, json={"api_version": "1.0.0"})
issuer = "https://oidc.test"
oidc_discovery_url = "https://oidc.test/.well-known/openid-configuration"
requests_mock.get(API_URL + 'credentials/oidc', json={
"providers": [{"id": "oi", "issuer": issuer, "title": "example", "scopes": ["openid"]}]
})
oidc_mock = OidcMock(
requests_mock=requests_mock,
expected_grant_type="client_credentials",
expected_client_id=client_id,
expected_fields={"client_secret": client_secret},
oidc_discovery_url=oidc_discovery_url,
)

with mock.patch('openeo.rest.connection.RefreshTokenStore') as RefreshTokenStore:
conn = Connection(API_URL)
assert RefreshTokenStore.call_count == 0
# Create RefreshTokenStore lazily when necessary
conn.authenticate_oidc_client_credentials(
client_id=client_id, client_secret=client_secret, store_refresh_token=True
)
assert RefreshTokenStore.call_count == 1
RefreshTokenStore.return_value.set_refresh_token.assert_called_with(
issuer=issuer, client_id=client_id, refresh_token=oidc_mock.state["refresh_token"]
)


def test_authenticate_basic(requests_mock, api_version):
user, pwd = "john262", "J0hndo3"
requests_mock.get(API_URL, json={"api_version": api_version})
Expand Down

0 comments on commit 642de15

Please sign in to comment.