| 1 |
.. _globus_apps: |
| 2 |
|
| 3 |
.. currentmodule:: globus_sdk |
| 4 |
|
| 5 |
GlobusApps |
| 6 |
========== |
| 7 |
|
| 8 |
|
| 9 |
.. note:: |
| 10 |
|
| 11 |
Currently ``GlobusApp`` can only be used in scripts (e.g., notebooks or automation) |
| 12 |
and applications directly responsible for a user's login flow. |
| 13 |
|
| 14 |
Web services and other hosted applications operating as an OAuth2 resource server |
| 15 |
should see :ref:`globus_authorizers` instead. |
| 16 |
|
| 17 |
|
| 18 |
:class:`GlobusApp` is a high level construct designed to simplify authentication for |
| 19 |
interactions with :ref:`globus-sdk services <services>`. |
| 20 |
|
| 21 |
A ``GlobusApp`` uses an OAuth2 client to obtain and manage OAuth2 tokens required for |
| 22 |
API interactions. OAuth2 clients must be created external to the SDK by registering an |
| 23 |
application at the |
| 24 |
`Globus Developer's Console <https://app.globus.org/settings/developers>`_. |
| 25 |
|
| 26 |
The following section provides a comparison of the specific types of ``GlobusApps`` |
| 27 |
to aid in selecting the proper one for your use case. |
| 28 |
|
| 29 |
Types of GlobusApps |
| 30 |
------------------- |
| 31 |
|
| 32 |
There are two flavors of ``GlobusApp`` available in the SDK: |
| 33 |
|
| 34 |
* :class:`UserApp`, for interactions in which an end user communicates with Globus |
| 35 |
services and |
| 36 |
* :class:`ClientApp`, for interactions in which an OAuth2 client, operating as a |
| 37 |
"service account", communicates with Globus services. |
| 38 |
|
| 39 |
The following table provides a comparison of these two options: |
| 40 |
|
| 41 |
|
| 42 |
.. list-table:: |
| 43 |
:widths: 50 50 |
| 44 |
:header-rows: 1 |
| 45 |
|
| 46 |
* - **UserApp** |
| 47 |
- **ClientApp** |
| 48 |
|
| 49 |
* - Appropriate for performing actions as a specific end user (e.g., the |
| 50 |
`Globus CLI <https://docs.globus.org/cli/>`_) |
| 51 |
- Appropriate for automating actions as a service account |
| 52 |
|
| 53 |
* - Created resources (e.g., collections or flows) by default are owned by an end |
| 54 |
user |
| 55 |
- Created resources (e.g., collections or flows) by default are owned by the |
| 56 |
OAuth2 client |
| 57 |
|
| 58 |
* - Existing resource access is evaluated based on an end user's permissions |
| 59 |
- Existing resource access is evaluated based on the OAuth2 client's permissions |
| 60 |
|
| 61 |
* - OAuth2 tokens are obtained by putting an end user through a login flow |
| 62 |
(this occurs in a web browser) |
| 63 |
- OAuth2 tokens are obtained by programmatically exchanging an OAuth2 client's |
| 64 |
secret |
| 65 |
|
| 66 |
* - Should typically use a "native" OAuth2 client (`Register a thick client |
| 67 |
<https://app.globus.org/settings/developers/registration/public_installed_client>`_) |
| 68 |
|
| 69 |
May use a "confidential" OAuth2 client (`Register a portal or science gateway |
| 70 |
<https://app.globus.org/settings/developers/registration/confidential_client>`_) |
| 71 |
- Must use a "confidential" OAuth2 client |
| 72 |
|
| 73 |
(`Register a service account |
| 74 |
<https://app.globus.org/settings/developers/registration/client_identity>`_) |
| 75 |
|
| 76 |
|
| 77 |
.. note:: |
| 78 |
|
| 79 |
Not all Globus operations support both app types. |
| 80 |
|
| 81 |
Particularly when dealing with sensitive data, services may enforce that a |
| 82 |
a user be the primary data access actor. In these cases, a ``ClientApp`` |
| 83 |
will be rejected and a ``UserApp`` must be used instead. |
| 84 |
|
| 85 |
|
| 86 |
Closing Resources via GlobusApps |
| 87 |
-------------------------------- |
| 88 |
|
| 89 |
When used as context managers, ``GlobusApp``\s automatically call their |
| 90 |
``close()`` method on exit. |
| 91 |
|
| 92 |
Closing an app closes the token storage attached to it, unless it was created |
| 93 |
explicitly. |
| 94 |
This covers any token storage created by the app on init, but not those which |
| 95 |
are created and passed in via the config. |
| 96 |
|
| 97 |
For most cases, users are recommended to use the context manager form, and to |
| 98 |
allow ``GlobusApp`` to both create and close the token storage. |
| 99 |
For example, |
| 100 |
|
| 101 |
.. code-block:: python |
| 102 |
|
| 103 |
from globus_sdk import GlobusAppConfig, UserApp |
| 104 |
|
| 105 |
# create an app configured to create its own sqlite storage |
| 106 |
config = GlobusAppConfig(token_storage="sqlite") |
| 107 |
with UserApp("sample-app", client_id="FILL_IN_HERE", config=config) as app: |
| 108 |
... # any clients, usage, etc. |
| 109 |
|
| 110 |
# after the context manager, any storage is implicitly closed |
| 111 |
|
| 112 |
|
| 113 |
However, when token storage instances are created by the user, they are not automatically |
| 114 |
closed, and the user is responsible for closing them. For example, |
| 115 |
in the following usage, the user must close the token storage: |
| 116 |
|
| 117 |
.. code-block:: python |
| 118 |
|
| 119 |
from globus_sdk import GlobusAppConfig, UserApp |
| 120 |
from globus_sdk.token_storage import SQLiteTokenStorage |
| 121 |
|
| 122 |
# this token storage is created by the user and will not be closed automatically |
| 123 |
sql_storage = SQLiteTokenStorage("tokens.sqlite") |
| 124 |
|
| 125 |
# create an app configured to use this storage |
| 126 |
config = GlobusAppConfig(token_storage=sql_storage) |
| 127 |
with UserApp("sample-app", client_id="FILL_IN_HERE", config=config) as app: |
| 128 |
do_stuff(app) |
| 129 |
|
| 130 |
# a second app uses the same storage, and shares the resource |
| 131 |
with UserApp("a-different-app", client_id="OTHER_ID", config=config) as app: |
| 132 |
do_stuff(app) |
| 133 |
|
| 134 |
# At this stage, the storage will still be open. |
| 135 |
# It should be explicitly closed: |
| 136 |
sql_storage.close() |
| 137 |
|
| 138 |
|
| 139 |
Reference |
| 140 |
--------- |
| 141 |
|
| 142 |
The interfaces of these classes, defined below, intentionally include many |
| 143 |
"sane defaults" (i.e., storing oauth2 access tokens in a json file). These defaults |
| 144 |
may be overridden to customize the app's behavior. For more information on what |
| 145 |
you can customize and how, see :ref:`globus_app_config`. |
| 146 |
|
| 147 |
.. autoclass:: GlobusApp() |
| 148 |
:members: |
| 149 |
:exclude-members: scope_requirements |
| 150 |
:member-order: bysource |
| 151 |
|
| 152 |
.. |
| 153 |
In the above class, "scope_requirements" is excluded because it's a ``@property``. |
| 154 |
Sphinx wants to document it as a method but that's not how it's invoked. Instead |
| 155 |
documentation is included in the class docstring as an ``ivar``. |
| 156 |
|
| 157 |
Implementations |
| 158 |
^^^^^^^^^^^^^^^ |
| 159 |
|
| 160 |
.. autoclass:: UserApp |
| 161 |
:members: |
| 162 |
|
| 163 |
.. autoclass:: ClientApp |
| 164 |
:members: |