| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import typing as t |
| 4 |
import uuid |
| 5 |
|
| 6 |
from globus_sdk import AuthLoginClient, ConfidentialAppAuthClient, GlobusSDKUsageError |
| 7 |
from globus_sdk.gare import GlobusAuthorizationParameters |
| 8 |
from globus_sdk.scopes import Scope |
| 9 |
|
| 10 |
from .app import GlobusApp |
| 11 |
from .authorizer_factory import ClientCredentialsAuthorizerFactory |
| 12 |
from .config import DEFAULT_CONFIG, GlobusAppConfig |
| 13 |
|
| 14 |
|
| 15 |
class ClientApp(GlobusApp): |
| 16 |
""" |
| 17 |
A ``GlobusApp`` for managing authentication state of a service account for use |
| 18 |
in service clients. |
| 19 |
|
| 20 |
A ``ClientApp`` requires the use of a confidential client created in a |
| 21 |
`Globus Project <https://app.globus.org/settings/developers>`. Client info may |
| 22 |
be passed either with the **client_id** and **client_secret** parameters or as |
| 23 |
a full **login_client**. |
| 24 |
|
| 25 |
``ClientApps`` are configured by supplying a :class:`GlobusAppConfig` object to the |
| 26 |
**config** parameter. Of note however, **login_flow_manager** must not be set; a |
| 27 |
``ClientApp`` does not use a login flow manager. |
| 28 |
|
| 29 |
See :class:`GlobusApp` for method signatures. |
| 30 |
|
| 31 |
.. rubric:: Example Usage: |
| 32 |
|
| 33 |
.. code-block:: python |
| 34 |
|
| 35 |
app = ClientApp("myapp", client_id=CLIENT_ID, client_secret=CLIENT_SECRET) |
| 36 |
transfer_client = TransferClient(app=app) |
| 37 |
res = transfer_client.endpoint_search("Tutorial Collection") |
| 38 |
|
| 39 |
|
| 40 |
:param app_name: A human-readable string to identify this app. |
| 41 |
:param login_client: A login client bound to a specific native client id or |
| 42 |
confidential client id/secret. Mutually exclusive with **client_id** and |
| 43 |
**client_secret**. |
| 44 |
:param client_id: A confidential client ID. Mutually exclusive with |
| 45 |
**login_client**. |
| 46 |
:param client_secret: A confidential client secret. Mutually exclusive with |
| 47 |
**login_client**. |
| 48 |
:param scope_requirements: A mapping of resource server to initial scope |
| 49 |
requirements. |
| 50 |
:param config: A data class containing configuration parameters for the app. |
| 51 |
""" |
| 52 |
|
| 53 |
_login_client: ConfidentialAppAuthClient |
| 54 |
_authorizer_factory: ClientCredentialsAuthorizerFactory # type: ignore |
| 55 |
|
| 56 |
def __init__( |
| 57 |
self, |
| 58 |
app_name: str = "Unnamed Globus App", |
| 59 |
*, |
| 60 |
login_client: ConfidentialAppAuthClient | None = None, |
| 61 |
client_id: uuid.UUID | str | None = None, |
| 62 |
client_secret: str | None = None, |
| 63 |
scope_requirements: ( |
| 64 |
t.Mapping[str, str | Scope | t.Iterable[str | Scope]] | None |
| 65 |
) = None, |
| 66 |
config: GlobusAppConfig = DEFAULT_CONFIG, |
| 67 |
) -> None: |
| 68 |
if config.login_flow_manager is not None: |
| 69 |
raise GlobusSDKUsageError("A ClientApp cannot use a login_flow_manager") |
| 70 |
|
| 71 |
if login_client and not isinstance(login_client, ConfidentialAppAuthClient): |
| 72 |
raise GlobusSDKUsageError( |
| 73 |
"A ClientApp must use a ConfidentialAppAuthClient for its login_client" |
| 74 |
) |
| 75 |
|
| 76 |
super().__init__( |
| 77 |
app_name, |
| 78 |
login_client=login_client, |
| 79 |
client_id=client_id, |
| 80 |
client_secret=client_secret, |
| 81 |
scope_requirements=scope_requirements, |
| 82 |
config=config, |
| 83 |
) |
| 84 |
|
| 85 |
def _initialize_login_client( |
| 86 |
self, |
| 87 |
app_name: str, |
| 88 |
config: GlobusAppConfig, |
| 89 |
client_id: uuid.UUID | str, |
| 90 |
client_secret: str | None, |
| 91 |
) -> AuthLoginClient: |
| 92 |
if not client_secret: |
| 93 |
raise GlobusSDKUsageError( |
| 94 |
"A ClientApp requires a client_secret to initialize its own login " |
| 95 |
"client." |
| 96 |
) |
| 97 |
|
| 98 |
return ConfidentialAppAuthClient( |
| 99 |
client_id=client_id, |
| 100 |
client_secret=client_secret, |
| 101 |
app_name=app_name, |
| 102 |
environment=config.environment, |
| 103 |
) |
| 104 |
|
| 105 |
def _initialize_authorizer_factory(self) -> None: |
| 106 |
self._authorizer_factory = ClientCredentialsAuthorizerFactory( |
| 107 |
token_storage=self.token_storage, |
| 108 |
confidential_client=self._login_client, |
| 109 |
scope_requirements=self._scope_requirements, |
| 110 |
) |
| 111 |
|
| 112 |
def _run_login_flow( |
| 113 |
self, auth_params: GlobusAuthorizationParameters | None = None |
| 114 |
) -> None: |
| 115 |
""" |
| 116 |
Run an authorization flow to get new tokens which are stored and available |
| 117 |
for the next authorizer gotten by get_authorizer. |
| 118 |
|
| 119 |
As a ClientApp this is just a client credentials call. |
| 120 |
|
| 121 |
:param auth_params: A GlobusAuthorizationParameters to control authentication |
| 122 |
only the required_scopes parameter is used. |
| 123 |
""" |
| 124 |
auth_params = self._auth_params_with_required_scopes(auth_params) |
| 125 |
if not auth_params.required_scopes: |
| 126 |
raise GlobusSDKUsageError( |
| 127 |
"A ClientApp cannot get tokens without configured required scopes." |
| 128 |
) |
| 129 |
token_response = self._login_client.oauth2_client_credentials_tokens( |
| 130 |
requested_scopes=auth_params.required_scopes |
| 131 |
) |
| 132 |
self._authorizer_factory.store_token_response_and_clear_cache(token_response) |