diff --git a/cirro/sdk/portal.py b/cirro/sdk/portal.py index 7f4727c..9f6b2ef 100644 --- a/cirro/sdk/portal.py +++ b/cirro/sdk/portal.py @@ -1,6 +1,8 @@ from cirro_api_client.v1.models import Executor +from cirro.auth.client_creds import ClientCredentialsAuth from cirro.cirro_client import CirroApi +from cirro.config import AppConfig from cirro.sdk.dataset import DataPortalDataset from cirro.sdk.developer import DeveloperHelper from cirro.sdk.exceptions import DataPortalAssetNotFound @@ -40,6 +42,40 @@ def __init__(self, base_url: str = None, client: CirroApi = None): else: self._client = CirroApi(base_url=base_url) + @classmethod + def from_client_credentials(cls, client_id: str, client_secret: str, base_url: str = None) -> 'DataPortal': + """ + Set up the DataPortal object, authenticating using an OAuth client credentials pair + (e.g., a service account or automated integration), rather than an interactive user login. + + Args: + client_id (str): OAuth Client ID + client_secret (str): OAuth Client Secret + base_url (str): Optional base URL of the Cirro instance + (if not provided, it uses the `CIRRO_BASE_URL` environment variable, or the config file) + + Example: + ```python + import os + from cirro import DataPortal + + portal = DataPortal.from_client_credentials( + client_id=os.environ['CIRRO_CLIENT_ID'], + client_secret=os.environ['CIRRO_CLIENT_SECRET'], + base_url="app.cirro.bio" + ) + portal.list_projects() + ``` + """ + app_config = AppConfig(base_url=base_url) + auth_info = ClientCredentialsAuth( + client_id=client_id, + client_secret=client_secret, + auth_endpoint=app_config.auth_endpoint + ) + client = CirroApi(auth_info=auth_info, base_url=base_url) + return cls(client=client) + def list_projects(self) -> DataPortalProjects: """List all the projects available in the Data Portal.""" diff --git a/pyproject.toml b/pyproject.toml index 658a114..845d74d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "cirro" -version = "1.13.0" +version = "1.13.1" description = "CLI tool and SDK for interacting with the Cirro platform" authors = ["Cirro Bio "] license = "MIT" diff --git a/samples/Logging_In.ipynb b/samples/Logging_In.ipynb index e532d23..8216659 100644 --- a/samples/Logging_In.ipynb +++ b/samples/Logging_In.ipynb @@ -54,6 +54,32 @@ "# Wait for the user to complete the login, and then return a DataPortal object\n", "portal = cirro_login.await_completion()" ] + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "### Service Account / Automated Logon\n", + "\n", + "When logging in using a service account it is necessary to use the `from_client_credentials` method to provide the OAuth credentials." + ] + }, + { + "metadata": {}, + "cell_type": "code", + "source": [ + "import os\n", + "from cirro import DataPortal\n", + "\n", + "portal = DataPortal.from_client_credentials(\n", + " client_id=os.environ['CIRRO_CLIENT_ID'],\n", + " client_secret=os.environ['CIRRO_CLIENT_SECRET'],\n", + " base_url=\"app.cirro.bio\"\n", + ")\n", + "portal.list_projects()" + ], + "outputs": [], + "execution_count": null } ], "metadata": {