| 1 |
.. _examples-authorization: |
| 2 |
|
| 3 |
API Authorization |
| 4 |
----------------- |
| 5 |
|
| 6 |
Using a ``GlobusAuthorizer`` is hard to grasp without a few examples to reference. |
| 7 |
The basic usage should be to create these at client instantiation time. |
| 8 |
|
| 9 |
Access Token Authorization on AuthClient and TransferClient |
| 10 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 11 |
|
| 12 |
Perhaps you're in a part of your application that only sees Access Tokens. |
| 13 |
Access Tokens are used to directly authenticate calls against Globus APIs, and |
| 14 |
are limited-lifetime credentials. |
| 15 |
You have distinct Access Tokens for each Globus service which you want to |
| 16 |
access. |
| 17 |
|
| 18 |
With the tokens in hand, it's just a simple matter of wrapping the tokens in |
| 19 |
:class:`AccessTokenAuthorizer <globus_sdk.AccessTokenAuthorizer>` objects. |
| 20 |
|
| 21 |
.. code-block:: python |
| 22 |
|
| 23 |
from globus_sdk import AuthClient, TransferClient, AccessTokenAuthorizer |
| 24 |
|
| 25 |
AUTH_ACCESS_TOKEN = "..." |
| 26 |
TRANSFER_ACCESS_TOKEN = "..." |
| 27 |
|
| 28 |
# note that we don't provide the client ID in this case |
| 29 |
# if you're using an Access Token you can't do the OAuth2 flows |
| 30 |
auth_client = AuthClient(authorizer=AccessTokenAuthorizer(AUTH_ACCESS_TOKEN)) |
| 31 |
|
| 32 |
transfer_client = TransferClient( |
| 33 |
authorizer=AccessTokenAuthorizer(TRANSFER_ACCESS_TOKEN) |
| 34 |
) |
| 35 |
|
| 36 |
Refresh Token Authorization on AuthClient and TransferClient |
| 37 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 38 |
|
| 39 |
Refresh Tokens are long-lived credentials used to get new Access Tokens |
| 40 |
whenever they expire. |
| 41 |
However, it would be very awkward to create a new client instance every time |
| 42 |
your credentials expire! |
| 43 |
|
| 44 |
Instead, use a :class:`RefreshTokenAuthorizer |
| 45 |
<globus_sdk.RefreshTokenAuthorizer>` to automatically re-up your credentials |
| 46 |
whenever they near expiration. |
| 47 |
|
| 48 |
Re-upping credentials is an operation that requires having client credentials |
| 49 |
for Globus Auth, so creating the authorizer is more complex this time. |
| 50 |
|
| 51 |
.. code-block:: python |
| 52 |
|
| 53 |
from globus_sdk import ( |
| 54 |
AuthClient, |
| 55 |
TransferClient, |
| 56 |
ConfidentialAppAuthClient, |
| 57 |
RefreshTokenAuthorizer, |
| 58 |
) |
| 59 |
|
| 60 |
# for doing the refresh |
| 61 |
CLIENT_ID = "..." |
| 62 |
CLIENT_SECRET = "..." |
| 63 |
|
| 64 |
# the actual tokens |
| 65 |
AUTH_REFRESH_TOKEN = "..." |
| 66 |
TRANSFER_REFRESH_TOKEN = "..." |
| 67 |
|
| 68 |
# making the authorizer requires that we have an AuthClient which can talk |
| 69 |
# OAuth2 to Globus Auth |
| 70 |
internal_auth_client = ConfidentialAppAuthClient(CLIENT_ID, CLIENT_SECRET) |
| 71 |
|
| 72 |
# now let's bake a couple of authorizers |
| 73 |
auth_authorizer = RefreshTokenAuthorizer(AUTH_REFRESH_TOKEN, internal_auth_client) |
| 74 |
transfer_authorizer = RefreshTokenAuthorizer( |
| 75 |
TRANSFER_REFRESH_TOKEN, internal_auth_client |
| 76 |
) |
| 77 |
|
| 78 |
# auth_client here is totally different from "internal_auth_client" above |
| 79 |
# the former is being used to request new tokens periodically, while this |
| 80 |
# one represents a user authenticated with those tokens |
| 81 |
auth_client = AuthClient(authorizer=auth_authorizer) |
| 82 |
# transfer_client doesn't have to contend with this duality -- it's always |
| 83 |
# representing a user |
| 84 |
transfer_client = TransferClient(authorizer=transfer_authorizer) |
| 85 |
|
| 86 |
Basic Auth on an AuthClient |
| 87 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 88 |
|
| 89 |
If you're using an :class:`AuthClient <globus_sdk.AuthClient>` to do OAuth2 |
| 90 |
flows, you likely want to authenticate it using your client credentials -- the |
| 91 |
client ID and client secret. |
| 92 |
|
| 93 |
The preferred method is to use the ``AuthClient`` subclass which automatically |
| 94 |
specifies its authorizer. |
| 95 |
Internally, this will use a ``BasicAuthorizer`` to do Basic Authentication. |
| 96 |
|
| 97 |
By way of example: |
| 98 |
|
| 99 |
.. code-block:: python |
| 100 |
|
| 101 |
from globus_sdk import ConfidentialAppAuthClient |
| 102 |
|
| 103 |
CLIENT_ID = "..." |
| 104 |
CLIENT_SECRET = "..." |
| 105 |
|
| 106 |
client = ConfidentialAppAuthClient(CLIENT_ID, CLIENT_SECRET) |
| 107 |
|
| 108 |
and you're off to the races! |
| 109 |
|
| 110 |
Under the hood, this is implicitly running |
| 111 |
|
| 112 |
.. code-block:: python |
| 113 |
|
| 114 |
AuthClient(authorizer=BasicAuthorizer(CLIENT_ID, CLIENT_SECRET)) |
| 115 |
|
| 116 |
but don't do this yourself -- ``ConfidentialAppAuthClient`` has different |
| 117 |
methods from the base ``AuthClient``. |