Debian Codegraph @

src/globus_sdk/login_flows/login_flow_manager.py

raw
1 from __future__ import annotations
2
3 import abc
4
5 import globus_sdk
6 from globus_sdk._missing import none2missing
7 from globus_sdk.gare import GlobusAuthorizationParameters
8
9
10 class LoginFlowManager(metaclass=abc.ABCMeta):
11 """
12 The abstract base class defining the interface for managing login flows.
13
14 Implementing classes must supply a ``run_login_flow`` method.
15 Utility functions starting an authorization-code grant flow and getting an
16 authorization-code URL are provided on the class.
17
18 :ivar AuthLoginClient login_client: A native or confidential login client to be
19 used by the login flow manager.
20 :ivar bool request_refresh_tokens: A signal of whether refresh tokens are expected
21 to be requested, in addition to access tokens.
22 :ivar str native_prefill_named_grant: A string to prefill in a Native App login
23 flow. This value is only to be used if the `login_client` is a native client.
24 """
25
26 def __init__(
27 self,
28 login_client: globus_sdk.AuthLoginClient,
29 *,
30 request_refresh_tokens: bool = False,
31 native_prefill_named_grant: str | None = None,
32 ) -> None:
33 if not isinstance(
34 login_client,
35 (globus_sdk.NativeAppAuthClient, globus_sdk.ConfidentialAppAuthClient),
36 ):
37 raise globus_sdk.GlobusSDKUsageError(
38 f"{type(self).__name__} requires a NativeAppAuthClient or "
39 f"ConfidentialAppAuthClient, but got a {type(login_client).__name__}."
40 )
41
42 self.login_client = login_client
43 self.request_refresh_tokens = request_refresh_tokens
44 self.native_prefill_named_grant = native_prefill_named_grant
45
46 def _get_authorize_url(
47 self, auth_parameters: GlobusAuthorizationParameters, redirect_uri: str
48 ) -> str:
49 """
50 Utility method to provide a simpler interface for subclasses to start an
51 authorization flow and get an authorization URL.
52 """
53 self._oauth2_start_flow(auth_parameters, redirect_uri)
54
55 # prompt is assigned first because its usage is type-ignored below
56 # this makes it clear that the ignore applies to that usage
57 prompt = none2missing(auth_parameters.prompt)
58 return self.login_client.oauth2_get_authorize_url(
59 session_required_identities=none2missing(
60 auth_parameters.session_required_identities
61 ),
62 session_required_single_domain=none2missing(
63 auth_parameters.session_required_single_domain
64 ),
65 session_required_policies=none2missing(
66 auth_parameters.session_required_policies
67 ),
68 session_required_mfa=none2missing(auth_parameters.session_required_mfa),
69 session_message=none2missing(auth_parameters.session_message),
70 prompt=prompt, # type: ignore[arg-type]
71 )
72
73 def _oauth2_start_flow(
74 self, auth_parameters: GlobusAuthorizationParameters, redirect_uri: str
75 ) -> None:
76 """
77 Start an authorization flow with the class's login_client, returning an
78 authorization URL to direct a user to.
79 """
80 login_client = self.login_client
81 requested_scopes = auth_parameters.required_scopes
82 if not requested_scopes:
83 raise globus_sdk.GlobusSDKUsageError(
84 f"{type(self).__name__} cannot start a login flow without scopes "
85 "in the authorization parameters."
86 )
87
88 # Native and Confidential App clients have different signatures for this method,
89 # so they must be type checked & called independently.
90 if isinstance(login_client, globus_sdk.NativeAppAuthClient):
91 login_client.oauth2_start_flow(
92 requested_scopes,
93 redirect_uri=redirect_uri,
94 refresh_tokens=self.request_refresh_tokens,
95 prefill_named_grant=none2missing(self.native_prefill_named_grant),
96 )
97 elif isinstance(login_client, globus_sdk.ConfidentialAppAuthClient):
98 login_client.oauth2_start_flow(
99 redirect_uri,
100 requested_scopes,
101 refresh_tokens=self.request_refresh_tokens,
102 )
103
104 @abc.abstractmethod
105 def run_login_flow(
106 self,
107 auth_parameters: GlobusAuthorizationParameters,
108 ) -> globus_sdk.OAuthTokenResponse:
109 """
110 Run a login flow to get tokens for a user.
111
112 :param auth_parameters: ``GlobusAuthorizationParameters`` passed through
113 to the authentication flow to control how the user will authenticate.
114 """