Debian Codegraph @

docs/experimental/examples/oauth2/globus_app.rst

raw
1
2 .. _using_globus_app:
3
4 .. py:currentmodule:: globus_sdk
5
6 Using a GlobusApp
7 =================
8
9 Programmatic communication with Globus services relies on the authorization of requests.
10 Management and resolution of this authorization can become an arduous task, especially
11 when a script needs to interact with different services each carrying an individual set
12 of complex authentication and authorization requirements. To assist with this task, this
13 library provides a utility construct called a GlobusApp.
14
15 A :py:class:`~GlobusApp` is a distinct object which will manage Globus Auth requirements
16 (i.e., **scopes**) identified by their associated service (i.e., **resource server**).
17 In addition to storing them, a GlobusApp provides a mechanism to resolve unmet
18 requirements through browser- and API-based authorization flows, supplying the resulting
19 tokens to bound clients as requested.
20
21 There are two flavors of GlobusApp:
22
23 * :py:class:`~UserApp`, a GlobusApp for interactions between an end user and Globus
24 services. Operations are performed as a *user identity*.
25
26 * :py:class:`~ClientApp`, a GlobusApp for interactions between a client
27 (i.e. service account) and Globus services. Operations are performed as a
28 *client identity*.
29
30 Setup
31 -----
32
33 A GlobusApp is a heavily configurable object. For common scripting usage however,
34 instantiation only requires two parameters:
35
36 #. **App Name** - A human readable name to identify your app in HTTP requests and token
37 caching (e.g., "My Cool Weathervane").
38
39 #. **Client Info** - either a *Native Client's* ID or a *Confidential Client's* ID and
40 secret pair.
41
42 * There are important distinctions to consider when choosing your client type; see
43 `Developing an Application Using Globus Auth <https://docs.globus.org/api/auth/developer-guide/#developing-apps>`_.
44
45 A simplified heuristic to help choose the client type however is:
46
47 * Use a *Confidential Client* when your client needs to own cloud resources
48 itself and will be used in a trusted environment where you can securely
49 hold a secret.
50
51 * Use a *Native Client* when your client will be facilitating interactions
52 between a user and a service, particularly if it is bundled within a
53 script or cli tool to be distributed to end-users' machines.
54
55
56 .. Note::
57
58 Both UserApps and ClientApps require a client.
59
60 In a UserApp, the client sends requests representing a user identity; whereas in
61 a ClientApp, the requests represent the client identity itself.
62
63
64 Once instantiated, a GlobusApp can be passed to any service client using the init
65 ``app`` keyword argument (e.g. ``TransferClient(app=my_app)``). Doing this will bind the
66 app to the client, registering a default set of scopes requirements for the service
67 client's resource server and configuring the app as the service client's auth provider.
68
69
70 .. tab-set::
71
72 .. tab-item:: UserApp
73
74 Construct a UserApp then bind it to a Transfer client and a Flows client.
75
76 .. Note::
77
78 ``UserApp.__init__(...)`` currently only supports Native clients.
79 Confidential client support is forthcoming.
80
81 .. code-block:: python
82
83 import globus_sdk
84
85 CLIENT_ID = "..."
86 my_app = globus_sdk.UserApp("my-user-app", client_id=CLIENT_ID)
87
88 transfer_client = globus_sdk.TransferClient(app=my_app)
89 flows_client = globus_sdk.FlowsClient(app=my_app)
90
91
92 .. tab-item:: ClientApp
93
94 Construct a ClientApp, then bind it to a Transfer client and a Flows client.
95
96 .. Note::
97
98 ``ClientApp.__init__(...)`` requires the ``client_secret`` keyword argument.
99 Native clients, which lack secrets, are not allowed.
100
101 .. code-block:: python
102
103 import globus_sdk
104
105 CLIENT_ID = "..."
106 CLIENT_SECRET = "..."
107 my_app = globus_sdk.ClientApp(
108 "my-client-app", client_id=CLIENT_ID, client_secret=CLIENT_SECRET
109 )
110
111 transfer_client = globus_sdk.TransferClient(app=my_app)
112 flows_client = globus_sdk.FlowsClient(app=my_app)
113
114
115 Usage
116 -----
117
118 From this point, the app manages scope validation, token caching and routing for any
119 bound clients.
120
121 In the above example, listing a client's or user's flows becomes as simple as:
122
123 .. code-block:: python
124
125 flows = flows_client.list_flows()["flows"]
126
127 If cached tokens are missing, expired, or otherwise insufficient (e.g., the first time
128 you run the script), the app will automatically initiate an auth flow to acquire new
129 tokens. With a UserApp, the app will print a URL to the terminal with a prompt
130 instructing a the user to follow the link and enter the code they're given back into the
131 terminal. With a ClientApp, the app will retrieve tokens programmatically through a
132 Globus Auth API.
133
134 Once this auth flow has finished, the app will cache tokens for future use and
135 invocation of your requested method will proceed as expected.
136
137
138 Manually Running Login Flows
139 ----------------------------
140
141 While your app will automatically initiate and oversee login flows when needed,
142 sometimes an author may want to explicitly control when an authorization occurs. To
143 manually trigger a login flow, call ``GlobusApp.login(...)``. The app will evaluate the
144 current scope requirements against available tokens, initiating a login flow if it
145 determines that any requirements across any resource servers are unmet. Resulting tokens
146 will be cached for future use.
147
148 This method accepts two optional keyword args:
149
150 - ``auth_params``, a collection of additional auth parameters to customize the login.
151 This allows for specifications such as requiring that a user be logged in with an
152 MFA token or rendering the authorization webpage with a specific message.
153 - ``force``, a boolean flag instructing the app to perform a login flow regardless of
154 whether it is required.
155
156
157 .. code-block:: python
158
159 from globus_sdk.gare import GlobusAuthorizationParameters
160
161 ...
162
163 my_app.login(
164 auth_params=GlobusAuthorizationParameters(
165 session_message="Please authenticate with MFA",
166 session_required_mfa=True,
167 )
168 )
169
170
171 Manually Defining Scope Requirements
172 ------------------------------------
173
174 Globus service client classes all maintain an internal list of default scope
175 requirements to be attached to any bound app. These scopes represent an approximation of
176 a "standard set" for each service. This list however is not sufficient for all use
177 cases.
178
179 For example, the FlowsClient defines its default scopes as ``flows:view_flows`` and
180 ``flows:run_status`` (read-only access). These scopes will not be sufficient for a
181 script which needs to create new flows or modify existing ones. For that script, the
182 author must manually attach the ``flows:manage_flows`` scope to the app.
183
184 This can be done in one of two ways:
185
186 #. Through a service client initialization, using the ``app_scopes`` kwarg.
187
188 .. code-block:: python
189
190 from globus_sdk import Scope, FlowsClient
191
192 FlowsClient(app=my_app, app_scopes=[Scope(FlowsClient.scopes.manage_flows)])
193
194 This approach results in an app which only requires the ``flows:manage_flows``
195 scope. The default scopes (``flows:view_flows`` and ``flows:run_status``) are not
196 registered.
197
198 #. Through a service client's ``add_app_scope`` method.
199
200 .. code-block:: python
201
202 from globus_sdk import FlowsClient
203
204 flows_client = FlowsClient(app=my_app)
205 flows_client.add_app_scope(FlowsClient.scopes.manage_flows)
206
207 This approach will add the ``flows:manage_flows`` scope to the app's existing set of
208 scopes. Since ``app_scopes`` was omitted in the client initialization, the default
209 scopes are registered as well.