| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import textwrap |
| 4 |
import typing as t |
| 5 |
from contextlib import contextmanager |
| 6 |
|
| 7 |
import globus_sdk |
| 8 |
from globus_sdk._internal.utils import get_nice_hostname |
| 9 |
from globus_sdk.exc.base import GlobusError |
| 10 |
from globus_sdk.gare import GlobusAuthorizationParameters |
| 11 |
|
| 12 |
from .login_flow_manager import LoginFlowManager |
| 13 |
|
| 14 |
if t.TYPE_CHECKING: |
| 15 |
from globus_sdk.globus_app import GlobusAppConfig |
| 16 |
|
| 17 |
|
| 18 |
class CommandLineLoginFlowEOFError(GlobusError, EOFError): |
| 19 |
""" |
| 20 |
An error raised when a CommandLineLoginFlowManager reads an EOF when prompting for |
| 21 |
a code. |
| 22 |
""" |
| 23 |
|
| 24 |
|
| 25 |
class CommandLineLoginFlowManager(LoginFlowManager): |
| 26 |
""" |
| 27 |
A login flow manager which drives authorization-code token grants through the |
| 28 |
command line. |
| 29 |
|
| 30 |
:param AuthLoginClient login_client: The client that will be making Globus |
| 31 |
Auth API calls required for a login flow. |
| 32 |
|
| 33 |
.. note:: |
| 34 |
If this client is a :class:`globus_sdk.ConfidentialAppAuthClient`, an |
| 35 |
explicit `redirect_uri` param is required. |
| 36 |
|
| 37 |
:param str redirect_uri: The redirect URI to use for the login flow. When the |
| 38 |
`login_client` is a native client, this defaults to a Globus-hosted URL. |
| 39 |
:param bool request_refresh_tokens: A signal of whether refresh tokens are expected |
| 40 |
to be requested, in addition to access tokens. |
| 41 |
:param str native_prefill_named_grant: A string to prefill in a Native App login |
| 42 |
flow. This value is only used if the `login_client` is a native client. |
| 43 |
""" |
| 44 |
|
| 45 |
def __init__( |
| 46 |
self, |
| 47 |
login_client: globus_sdk.AuthLoginClient, |
| 48 |
*, |
| 49 |
redirect_uri: str | None = None, |
| 50 |
request_refresh_tokens: bool = False, |
| 51 |
native_prefill_named_grant: str | None = None, |
| 52 |
) -> None: |
| 53 |
super().__init__( |
| 54 |
login_client, |
| 55 |
request_refresh_tokens=request_refresh_tokens, |
| 56 |
native_prefill_named_grant=native_prefill_named_grant, |
| 57 |
) |
| 58 |
|
| 59 |
if redirect_uri is None: |
| 60 |
# Confidential clients must always define their own custom redirect URI. |
| 61 |
if isinstance(login_client, globus_sdk.ConfidentialAppAuthClient): |
| 62 |
msg = "Use of a Confidential client requires an explicit redirect_uri." |
| 63 |
raise globus_sdk.GlobusSDKUsageError(msg) |
| 64 |
|
| 65 |
# Native clients may infer the globus-provided helper page if omitted. |
| 66 |
redirect_uri = login_client.base_url + "v2/web/auth-code" |
| 67 |
self.redirect_uri = redirect_uri |
| 68 |
|
| 69 |
@classmethod |
| 70 |
def for_globus_app( |
| 71 |
cls, |
| 72 |
app_name: str, |
| 73 |
login_client: globus_sdk.AuthLoginClient, |
| 74 |
config: GlobusAppConfig, |
| 75 |
) -> CommandLineLoginFlowManager: |
| 76 |
""" |
| 77 |
Create a ``CommandLineLoginFlowManager`` for use in a GlobusApp. |
| 78 |
|
| 79 |
:param app_name: The name of the app. Will be prefilled in native auth flows. |
| 80 |
:param login_client: A client used to make Globus Auth API calls. |
| 81 |
:param config: A GlobusApp-bounded object used to configure login flow manager. |
| 82 |
:raises GlobusSDKUsageError: if login_redirect_uri is not set on the config |
| 83 |
but a ConfidentialAppAuthClient is supplied. |
| 84 |
""" |
| 85 |
hostname = get_nice_hostname() |
| 86 |
if hostname: |
| 87 |
prefill = f"{app_name} on {hostname}" |
| 88 |
else: |
| 89 |
prefill = app_name |
| 90 |
|
| 91 |
return cls( |
| 92 |
login_client, |
| 93 |
redirect_uri=config.login_redirect_uri, |
| 94 |
request_refresh_tokens=config.request_refresh_tokens, |
| 95 |
native_prefill_named_grant=prefill, |
| 96 |
) |
| 97 |
|
| 98 |
def run_login_flow( |
| 99 |
self, |
| 100 |
auth_parameters: GlobusAuthorizationParameters, |
| 101 |
) -> globus_sdk.OAuthTokenResponse: |
| 102 |
""" |
| 103 |
Run an interactive login flow on the command line to get tokens for the user. |
| 104 |
|
| 105 |
:param auth_parameters: ``GlobusAuthorizationParameters`` passed through |
| 106 |
to the authentication flow to control how the user will authenticate. |
| 107 |
""" |
| 108 |
authorize_url = self._get_authorize_url(auth_parameters, self.redirect_uri) |
| 109 |
self.print_authorize_url(authorize_url) |
| 110 |
auth_code = self.prompt_for_code() |
| 111 |
|
| 112 |
return self.login_client.oauth2_exchange_code_for_tokens(auth_code) |
| 113 |
|
| 114 |
def print_authorize_url(self, authorize_url: str) -> None: |
| 115 |
""" |
| 116 |
Prompt the user to authenticate using the provided ``authorize_url``. |
| 117 |
|
| 118 |
:param authorize_url: The URL at which the user will login and consent to |
| 119 |
application accesses. |
| 120 |
""" |
| 121 |
login_prompt = "Please authenticate with Globus here:" |
| 122 |
print(textwrap.dedent(f""" |
| 123 |
{login_prompt} |
| 124 |
{"-" * len(login_prompt)} |
| 125 |
{authorize_url} |
| 126 |
{"-" * len(login_prompt)} |
| 127 |
""")) |
| 128 |
|
| 129 |
def prompt_for_code(self) -> str: |
| 130 |
""" |
| 131 |
Prompt the user to enter an authorization code. |
| 132 |
|
| 133 |
:returns: The authorization code entered by the user. |
| 134 |
""" |
| 135 |
code_prompt = "Enter the resulting Authorization Code here: " |
| 136 |
with self._handle_input_errors(): |
| 137 |
return input(code_prompt).strip() |
| 138 |
|
| 139 |
@contextmanager |
| 140 |
def _handle_input_errors(self) -> t.Iterator[None]: |
| 141 |
try: |
| 142 |
yield |
| 143 |
except EOFError as e: |
| 144 |
msg = ( |
| 145 |
"An EOF was read when an authorization code was expected." |
| 146 |
" (Are you running this in an interactive terminal?)" |
| 147 |
) |
| 148 |
raise CommandLineLoginFlowEOFError(msg) from e |