| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import logging |
| 4 |
import typing as t |
| 5 |
import uuid |
| 6 |
|
| 7 |
from globus_sdk._missing import MISSING, MissingType |
| 8 |
from globus_sdk.authorizers import NullAuthorizer |
| 9 |
from globus_sdk.response import GlobusHTTPResponse |
| 10 |
from globus_sdk.scopes import Scope |
| 11 |
from globus_sdk.transport import RequestsTransport, RetryConfig |
| 12 |
|
| 13 |
from ..flow_managers import GlobusNativeAppFlowManager |
| 14 |
from ..response import OAuthRefreshTokenResponse |
| 15 |
from .base_login_client import AuthLoginClient |
| 16 |
|
| 17 |
log = logging.getLogger(__name__) |
| 18 |
|
| 19 |
|
| 20 |
class NativeAppAuthClient(AuthLoginClient): |
| 21 |
""" |
| 22 |
This type of ``AuthLoginClient`` is used to represent a Native App's |
| 23 |
communications with Globus Auth. |
| 24 |
It requires a Client ID, and cannot take an ``authorizer``. |
| 25 |
|
| 26 |
Native Apps are applications, like the Globus CLI, which are run |
| 27 |
client-side and therefore cannot keep secrets. Unable to possess client |
| 28 |
credentials, several Globus Auth interactions have to be specialized to |
| 29 |
accommodate the absence of a secret. |
| 30 |
|
| 31 |
Any keyword arguments given are passed through to the ``AuthClient`` |
| 32 |
constructor. |
| 33 |
|
| 34 |
.. automethodlist:: globus_sdk.NativeAppAuthClient |
| 35 |
""" |
| 36 |
|
| 37 |
def __init__( |
| 38 |
self, |
| 39 |
client_id: uuid.UUID | str, |
| 40 |
environment: str | None = None, |
| 41 |
base_url: str | None = None, |
| 42 |
app_name: str | None = None, |
| 43 |
transport: RequestsTransport | None = None, |
| 44 |
retry_config: RetryConfig | None = None, |
| 45 |
) -> None: |
| 46 |
super().__init__( |
| 47 |
client_id=client_id, |
| 48 |
authorizer=NullAuthorizer(), |
| 49 |
environment=environment, |
| 50 |
base_url=base_url, |
| 51 |
app_name=app_name, |
| 52 |
transport=transport, |
| 53 |
retry_config=retry_config, |
| 54 |
) |
| 55 |
|
| 56 |
def oauth2_start_flow( |
| 57 |
self, |
| 58 |
requested_scopes: str | Scope | t.Iterable[str | Scope], |
| 59 |
*, |
| 60 |
redirect_uri: str | MissingType = MISSING, |
| 61 |
state: str = "_default", |
| 62 |
verifier: str | MissingType = MISSING, |
| 63 |
refresh_tokens: bool = False, |
| 64 |
prefill_named_grant: str | MissingType = MISSING, |
| 65 |
) -> GlobusNativeAppFlowManager: |
| 66 |
""" |
| 67 |
Starts a Native App OAuth2 flow. |
| 68 |
|
| 69 |
This is done internally by instantiating a |
| 70 |
:class:`GlobusNativeAppFlowManager <.GlobusNativeAppFlowManager>` |
| 71 |
|
| 72 |
While the flow is in progress, the ``NativeAppAuthClient`` becomes |
| 73 |
non thread-safe as temporary state is stored during the flow. |
| 74 |
|
| 75 |
:param requested_scopes: The scopes on the token(s) being requested. Defaults to |
| 76 |
``openid profile email urn:globus:auth:scope:transfer.api.globus.org:all`` |
| 77 |
:param redirect_uri: The page that users should be directed to after |
| 78 |
authenticating at the authorize URL. Defaults to |
| 79 |
'https://auth.globus.org/v2/web/auth-code', which displays the resulting |
| 80 |
``auth_code`` for users to copy-paste back into your application (and |
| 81 |
thereby be passed back to the ``GlobusNativeAppFlowManager``) |
| 82 |
:param state: The ``redirect_uri`` page will have this included in a query |
| 83 |
parameter, so you can use it to pass information to that page if you use a |
| 84 |
custom page. It defaults to the string '_default' |
| 85 |
:param verifier: A secret used for the Native App flow. It will by default be a |
| 86 |
freshly generated random string, known only to this |
| 87 |
``GlobusNativeAppFlowManager`` instance |
| 88 |
:param refresh_tokens: When True, request refresh tokens in addition to access |
| 89 |
tokens. [Default: ``False``] |
| 90 |
:param prefill_named_grant: Prefill the named grant label on the consent page |
| 91 |
|
| 92 |
.. tab-set:: |
| 93 |
|
| 94 |
.. tab-item:: Example Usage |
| 95 |
|
| 96 |
You can see an example of this flow :ref:`in the usage examples |
| 97 |
<examples_native_app_login>`. |
| 98 |
|
| 99 |
.. tab-item:: API Info |
| 100 |
|
| 101 |
The Globus Auth specification for Native App grants details |
| 102 |
modifications to the Authorization Code grant flow as |
| 103 |
`The PKCE Security Protocol |
| 104 |
<https://docs.globus.org/api/auth/developer-guide/#pkce>`_. |
| 105 |
""" |
| 106 |
log.debug("Starting Native App Grant Flow") |
| 107 |
self.current_oauth2_flow_manager = GlobusNativeAppFlowManager( |
| 108 |
self, |
| 109 |
requested_scopes=requested_scopes, |
| 110 |
redirect_uri=redirect_uri, |
| 111 |
state=state, |
| 112 |
verifier=verifier, |
| 113 |
refresh_tokens=refresh_tokens, |
| 114 |
prefill_named_grant=prefill_named_grant, |
| 115 |
) |
| 116 |
return self.current_oauth2_flow_manager |
| 117 |
|
| 118 |
def oauth2_refresh_token( |
| 119 |
self, |
| 120 |
refresh_token: str, |
| 121 |
*, |
| 122 |
body_params: dict[str, t.Any] | None = None, |
| 123 |
) -> OAuthRefreshTokenResponse: |
| 124 |
""" |
| 125 |
``NativeAppAuthClient`` specializes the refresh token grant to include |
| 126 |
its client ID as a parameter in the POST body. |
| 127 |
It needs this specialization because it cannot authenticate the refresh |
| 128 |
grant call with client credentials, as is normal. |
| 129 |
|
| 130 |
:param refresh_token: The refresh token to use to get a new access token |
| 131 |
:param body_params: Extra parameters to include in the POST body |
| 132 |
""" |
| 133 |
log.debug("Executing token refresh without client credentials") |
| 134 |
form_data = { |
| 135 |
"refresh_token": refresh_token, |
| 136 |
"grant_type": "refresh_token", |
| 137 |
"client_id": self.client_id, |
| 138 |
} |
| 139 |
return self.oauth2_token( |
| 140 |
form_data, body_params=body_params, response_class=OAuthRefreshTokenResponse |
| 141 |
) |
| 142 |
|
| 143 |
def create_native_app_instance( |
| 144 |
self, |
| 145 |
template_id: uuid.UUID | str, |
| 146 |
name: str, |
| 147 |
) -> GlobusHTTPResponse: |
| 148 |
""" |
| 149 |
Create a new native app instance. The new instance is a confidential client. |
| 150 |
|
| 151 |
:param template_id: The client ID of the calling native app |
| 152 |
:param name: The name given to the new app instance |
| 153 |
|
| 154 |
.. tab-set:: |
| 155 |
|
| 156 |
.. tab-item:: Example Usage |
| 157 |
|
| 158 |
.. code-block:: pycon |
| 159 |
|
| 160 |
>>> ac = globus_sdk.NativeAppAuthClient(...) |
| 161 |
>>> template_id = ... |
| 162 |
>>> r = ac.create_native_app_instance( |
| 163 |
... template_id, |
| 164 |
... "My new native app instance", |
| 165 |
... ) |
| 166 |
>>> client_id = r["client"]["id"] |
| 167 |
|
| 168 |
.. tab-item:: Example Response Data |
| 169 |
|
| 170 |
.. expandtestfixture:: auth.create_native_app_instance |
| 171 |
|
| 172 |
.. tab-item:: API Info |
| 173 |
|
| 174 |
``POST /v2/api/clients`` |
| 175 |
|
| 176 |
.. extdoclink:: Create Client |
| 177 |
:ref: auth/reference/#create_client |
| 178 |
""" |
| 179 |
body: dict[str, t.Any] = { |
| 180 |
"name": name, |
| 181 |
"template_id": template_id, |
| 182 |
} |
| 183 |
return self.post("/v2/api/clients", data={"client": body}) |