Debian Codegraph @

docs/examples/client_credentials.rst

raw
1 Client Credentials Authentication
2 ---------------------------------
3
4 This is an example of the use of the Globus SDK to carry out an OAuth2
5 Client Credentials Authentication flow.
6
7 The goal here is to have an application authenticate in Globus Auth directly,
8 as itself.
9 Unlike many other OAuth2 flows, the application does not act on behalf of a
10 user, but on its own behalf.
11
12 This flow is suitable for automated cases in which an application, even one as
13 simple as a ``cron`` job, makes use of Globus outside of the context of a
14 specific end-user interaction.
15
16 Get a Client
17 ~~~~~~~~~~~~
18
19 In order to complete an OAuth2 flow to get tokens, you must have a client
20 definition registered with Globus Auth.
21 To do so, follow the relevant documentation for the
22 `Globus Auth Service <https://docs.globus.org/api/auth/>`_ or go directly to
23 `developers.globus.org <https://developers.globus.org/>`_ to do the
24 registration.
25
26 During registration, make sure that the "Native App" checkbox is unchecked.
27
28 Once your client is created, expand it on the Projects page and click "Generate
29 Secret". Save the secret in a secure location accessible from your code.
30
31 Do the Flow
32 ~~~~~~~~~~~
33
34 You should specifically use the :class:`ConfidentialAppAuthClient
35 <globus_sdk.ConfidentialAppAuthClient>` type of ``AuthClient``, as it has been
36 customized to handle this flow.
37
38 The shortest version of the flow looks like this:
39
40 .. code-block:: python
41
42 import globus_sdk
43
44 # you must have a client ID
45 CLIENT_ID = "..."
46 # the secret, loaded from wherever you store it
47 CLIENT_SECRET = "..."
48
49 confidential_client = globus_sdk.ConfidentialAppAuthClient(CLIENT_ID, CLIENT_SECRET)
50 token_response = confidential_client.oauth2_client_credentials_tokens(
51 requested_scopes=globus_sdk.TransferClient.scopes.all
52 )
53
54 # the useful values that you want at the end of this
55 globus_transfer_data = token_response.by_resource_server[
56 globus_sdk.TransferClient.resource_server
57 ]
58 globus_transfer_token = globus_transfer_data["access_token"]
59
60
61 Use the Resulting Tokens
62 ~~~~~~~~~~~~~~~~~~~~~~~~
63
64 The Client Credentials Grant will only produce Access Tokens, not Refresh
65 Tokens, so you should pass its results directly to the :class:`AccessTokenAuthorizer
66 <globus_sdk.AccessTokenAuthorizer>`.
67
68 For example, after running the code above,
69
70 .. code-block:: python
71
72 authorizer = globus_sdk.AccessTokenAuthorizer(globus_transfer_token)
73 tc = globus_sdk.TransferClient(authorizer=authorizer)
74 print(f"Endpoints Belonging to {CLIENT_ID}@clients.auth.globus.org:")
75 for ep in tc.endpoint_search(filter_scope="my-endpoints"):
76 print(f"[{ep['id']}] {ep['display_name']}")
77
78 Note that we're doing a search for "my endpoints", but we refer to the results
79 as belonging to ``<CLIENT_ID>@clients.auth.globus.org``. The "current user" is
80 not any human user, but the client itself.
81
82 Handling Token Expiration
83 ~~~~~~~~~~~~~~~~~~~~~~~~~
84
85 When you get access tokens, you also get their expiration time in seconds.
86 You can inspect the ``globus_transfer_data`` structure in the example to see.
87
88 Tokens should have a long enough lifetime for any short-running operations
89 (less than a day).
90
91 When your tokens are expired, you should just request new ones by making
92 another Client Credentials request.
93 Depending on your needs, you may need to track the expiration times along with
94 your tokens.
95
96 Using ClientCredentialsAuthorizer
97 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
98
99 The SDK also provides a specialized Authorizer which can be used to
100 automatically handle token expiration.
101
102 Use it like so:
103
104 .. code-block:: python
105
106 import globus_sdk
107
108 # you must have a client ID and secret
109 CLIENT_ID = "..."
110 CLIENT_SECRET = "..."
111
112 confidential_client = globus_sdk.ConfidentialAppAuthClient(
113 client_id=CLIENT_ID, client_secret=CLIENT_SECRET
114 )
115 cc_authorizer = globus_sdk.ClientCredentialsAuthorizer(
116 confidential_client, globus_sdk.TransferClient.scopes.all
117 )
118 # create a new client
119 tc = globus_sdk.TransferClient(authorizer=cc_authorizer)
120
121 # usage is still the same
122 print(f"Endpoints Belonging to {CLIENT_ID}@clients.auth.globus.org:")
123 for ep in tc.endpoint_search(filter_scope="my-endpoints"):
124 print(f"[{ep['id']}] {ep['display_name']}")