| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import typing as t |
| 4 |
import uuid |
| 5 |
|
| 6 |
from globus_sdk import ( |
| 7 |
AuthClient, |
| 8 |
AuthLoginClient, |
| 9 |
ConfidentialAppAuthClient, |
| 10 |
GlobusSDKUsageError, |
| 11 |
NativeAppAuthClient, |
| 12 |
Scope, |
| 13 |
) |
| 14 |
from globus_sdk.gare import GlobusAuthorizationParameters |
| 15 |
from globus_sdk.login_flows import CommandLineLoginFlowManager, LoginFlowManager |
| 16 |
from globus_sdk.token_storage import ( |
| 17 |
HasRefreshTokensValidator, |
| 18 |
NotExpiredValidator, |
| 19 |
TokenStorage, |
| 20 |
UnchangingIdentityIDValidator, |
| 21 |
ValidatingTokenStorage, |
| 22 |
) |
| 23 |
|
| 24 |
from .app import GlobusApp |
| 25 |
from .authorizer_factory import ( |
| 26 |
AccessTokenAuthorizerFactory, |
| 27 |
RefreshTokenAuthorizerFactory, |
| 28 |
) |
| 29 |
from .config import DEFAULT_CONFIG, KNOWN_LOGIN_FLOW_MANAGERS, GlobusAppConfig |
| 30 |
from .protocols import LoginFlowManagerProvider |
| 31 |
|
| 32 |
|
| 33 |
class UserApp(GlobusApp): |
| 34 |
""" |
| 35 |
A ``GlobusApp`` for managing authentication state of a user for use in service |
| 36 |
clients. |
| 37 |
|
| 38 |
Typically, a ``UserApp`` will use a native client, requiring a **client_id** |
| 39 |
created in a `Globus Project <https://app.globus.org/settings/developers>`_. |
| 40 |
More advanced use cases however, may additionally supply a **client_secret** or |
| 41 |
full **login_client** with confidential client credentials. |
| 42 |
|
| 43 |
``UserApps`` are configured by supplying a :class:`GlobusAppConfig` object to the |
| 44 |
**config** parameter. Of note, login flow behavior involves printing and prompting |
| 45 |
the user for input using std::in and std::out. This behavior can be customized with |
| 46 |
the **login_flow_manager** config attribute. |
| 47 |
|
| 48 |
See :class:`GlobusApp` for method signatures. |
| 49 |
|
| 50 |
.. rubric:: Example Usage: |
| 51 |
|
| 52 |
.. code-block:: python |
| 53 |
|
| 54 |
app = UserApp("myapp", client_id=NATIVE_CLIENT_ID) |
| 55 |
transfer_client = TransferClient(app=app) |
| 56 |
res = transfer_client.endpoint_search("Tutorial Collection") |
| 57 |
|
| 58 |
:param app_name: A human-readable string to identify this app. |
| 59 |
:param login_client: A login client bound to a specific native client id or |
| 60 |
confidential client id/secret. Mutually exclusive with **client_id** and |
| 61 |
**client_secret**. |
| 62 |
:param client_id: A native or confidential client ID. Mutually exclusive with |
| 63 |
**login_client**. |
| 64 |
:param client_secret: A confidential client secret. Mutually exclusive with |
| 65 |
**login_client**. |
| 66 |
:param scope_requirements: A mapping of resource server to initial scope |
| 67 |
requirements. |
| 68 |
:param config: A data class containing configuration parameters for the app. |
| 69 |
""" |
| 70 |
|
| 71 |
_login_client: NativeAppAuthClient | ConfidentialAppAuthClient |
| 72 |
_authorizer_factory: ( # type: ignore |
| 73 |
AccessTokenAuthorizerFactory | RefreshTokenAuthorizerFactory |
| 74 |
) |
| 75 |
|
| 76 |
def __init__( |
| 77 |
self, |
| 78 |
app_name: str = "Unnamed Globus App", |
| 79 |
*, |
| 80 |
login_client: AuthLoginClient | None = None, |
| 81 |
client_id: uuid.UUID | str | None = None, |
| 82 |
client_secret: str | None = None, |
| 83 |
scope_requirements: ( |
| 84 |
t.Mapping[str, str | Scope | t.Iterable[str | Scope]] | None |
| 85 |
) = None, |
| 86 |
config: GlobusAppConfig = DEFAULT_CONFIG, |
| 87 |
) -> None: |
| 88 |
super().__init__( |
| 89 |
app_name, |
| 90 |
login_client=login_client, |
| 91 |
client_id=client_id, |
| 92 |
client_secret=client_secret, |
| 93 |
scope_requirements=scope_requirements, |
| 94 |
config=config, |
| 95 |
) |
| 96 |
|
| 97 |
self._login_flow_manager = self._resolve_login_flow_manager( |
| 98 |
app_name=self.app_name, |
| 99 |
login_client=self._login_client, |
| 100 |
config=config, |
| 101 |
) |
| 102 |
|
| 103 |
def _resolve_login_flow_manager( |
| 104 |
self, app_name: str, login_client: AuthLoginClient, config: GlobusAppConfig |
| 105 |
) -> LoginFlowManager: |
| 106 |
login_flow_manager = config.login_flow_manager |
| 107 |
if isinstance(login_flow_manager, LoginFlowManager): |
| 108 |
return login_flow_manager |
| 109 |
|
| 110 |
elif isinstance(login_flow_manager, LoginFlowManagerProvider): |
| 111 |
provider = login_flow_manager |
| 112 |
elif login_flow_manager is None: |
| 113 |
provider = CommandLineLoginFlowManager |
| 114 |
elif login_flow_manager in KNOWN_LOGIN_FLOW_MANAGERS: |
| 115 |
provider = KNOWN_LOGIN_FLOW_MANAGERS[login_flow_manager] |
| 116 |
else: |
| 117 |
allowed_keys = ", ".join(repr(k) for k in KNOWN_LOGIN_FLOW_MANAGERS.keys()) |
| 118 |
raise GlobusSDKUsageError( |
| 119 |
f"Unsupported login_flow_manager value: {login_flow_manager!r}. " |
| 120 |
f"Expected {allowed_keys}, a <LoginFlowManagerProvider>, or a " |
| 121 |
f"<LoginFlowManager>." |
| 122 |
) |
| 123 |
|
| 124 |
return provider.for_globus_app( |
| 125 |
app_name=app_name, config=config, login_client=login_client |
| 126 |
) |
| 127 |
|
| 128 |
def _initialize_login_client( |
| 129 |
self, |
| 130 |
app_name: str, |
| 131 |
config: GlobusAppConfig, |
| 132 |
client_id: uuid.UUID | str, |
| 133 |
client_secret: str | None, |
| 134 |
) -> AuthLoginClient: |
| 135 |
if client_secret: |
| 136 |
return ConfidentialAppAuthClient( |
| 137 |
app_name=app_name, |
| 138 |
client_id=client_id, |
| 139 |
client_secret=client_secret, |
| 140 |
environment=config.environment, |
| 141 |
) |
| 142 |
else: |
| 143 |
return NativeAppAuthClient( |
| 144 |
app_name=app_name, |
| 145 |
client_id=client_id, |
| 146 |
environment=config.environment, |
| 147 |
) |
| 148 |
|
| 149 |
def _initialize_validating_token_storage( |
| 150 |
self, |
| 151 |
token_storage: TokenStorage, |
| 152 |
consent_client: AuthClient, |
| 153 |
scope_requirements: t.Mapping[str, t.Sequence[Scope]], |
| 154 |
) -> ValidatingTokenStorage: |
| 155 |
validating_token_storage = super()._initialize_validating_token_storage( |
| 156 |
token_storage, consent_client, scope_requirements |
| 157 |
) |
| 158 |
validating_token_storage.validators.append(UnchangingIdentityIDValidator()) |
| 159 |
return validating_token_storage |
| 160 |
|
| 161 |
def _initialize_authorizer_factory(self) -> None: |
| 162 |
if self.config.request_refresh_tokens: |
| 163 |
self._authorizer_factory = RefreshTokenAuthorizerFactory( |
| 164 |
token_storage=self.token_storage, auth_login_client=self._login_client |
| 165 |
) |
| 166 |
self.token_storage.validators.insert(0, HasRefreshTokensValidator()) |
| 167 |
else: |
| 168 |
self._authorizer_factory = AccessTokenAuthorizerFactory( |
| 169 |
token_storage=self.token_storage |
| 170 |
) |
| 171 |
self.token_storage.validators.insert(0, NotExpiredValidator()) |
| 172 |
|
| 173 |
def _run_login_flow( |
| 174 |
self, auth_params: GlobusAuthorizationParameters | None = None |
| 175 |
) -> None: |
| 176 |
""" |
| 177 |
Run an authorization flow to get new tokens which are stored and available |
| 178 |
for the next authorizer gotten by get_authorizer. |
| 179 |
|
| 180 |
As a UserApp this always involves an interactive login flow with the user |
| 181 |
driven by the app's LoginFlowManager. |
| 182 |
|
| 183 |
:param auth_params: A GlobusAuthorizationParameters to control how the user |
| 184 |
will authenticate. If not passed |
| 185 |
""" |
| 186 |
auth_params = self._auth_params_with_required_scopes(auth_params) |
| 187 |
token_response = self._login_flow_manager.run_login_flow(auth_params) |
| 188 |
self._authorizer_factory.store_token_response_and_clear_cache(token_response) |