|
| 1 | +# Application Managed Sessions - OAuth2 |
| 2 | + |
| 3 | +If an application cannot or should not use a login session initiated by the |
| 4 | +[`planet auth`](../../cli/cli-reference/#auth) CLI command, the application will be |
| 5 | +responsible for managing the process on its own, persisting session state as |
| 6 | +needed. |
| 7 | + |
| 8 | +Application managed sessions may be used with all authentication protocols. |
| 9 | +Application developers may control whether sessions are visible to the CLI. |
| 10 | +This is managed with the `save_state_to_storage` parameter on the `planet.Auth` |
| 11 | +constructor methods illustrated below. |
| 12 | + |
| 13 | +The process varies depending on the authentication protocol used. |
| 14 | +Depending on the use case, applications may need to support multiple authentication |
| 15 | +methods, just as the [`planet`](../../cli/cli-reference) CLI command supports interacting with Planet APIs |
| 16 | +using either a user or a service user account. |
| 17 | + |
| 18 | +## OAuth2 Session for Users |
| 19 | +User session initialization inherently involves using a web browser to |
| 20 | +complete user authentication. This architecture allows for greater security |
| 21 | +by keeping the user's password from being directly exposed to the application |
| 22 | +code. This also allows for flexibility in user federation and multifactor |
| 23 | +authentication procedures without the complexity of these needing to |
| 24 | +be exposed to the application developer who is focused on geospatial |
| 25 | +operations using the Planet platform, and not the nuances of user |
| 26 | +authentication and authorization. |
| 27 | + |
| 28 | +### OAuth2 User Client Registration |
| 29 | +Developers of applications must register client applications with Planet, and |
| 30 | +will be issued a Client ID as part of that process. Developers should register |
| 31 | +a client for each distinct application so that end-users may discretely manage |
| 32 | +applications permitted to access Planet APIs on their behalf. |
| 33 | + |
| 34 | +See [OAuth2 Client Registration](http://docs.planet.com/develop/authentication/#interactive-client-registration) |
| 35 | +for more information. |
| 36 | + |
| 37 | +### With a Local Web Browser |
| 38 | +In environments where a local browser is available, the Planet SDK library can manage |
| 39 | +the process of launching the browser locally, transferring control to the Planet |
| 40 | +authorization services for session initialization, and accepting a network |
| 41 | +callback from the local browser to regain control once the authorization |
| 42 | +process is complete. At a network protocol level, this establishes the user |
| 43 | +login session using the OAuth2 authorization code flow. |
| 44 | + |
| 45 | +To use this method using the SDK, the following requirements must be met: |
| 46 | + |
| 47 | +* The application must be able to launch a local web browser. |
| 48 | +* The web browser must be able to connect to Planet services. |
| 49 | +* The application must be able to listen on a network port that is accessible |
| 50 | + to the browser. |
| 51 | + |
| 52 | +#### Examples - OAuth2 Authorization Code Flow |
| 53 | + |
| 54 | +##### In Memory Session State |
| 55 | +When an application cannot safely store user session state, it may operate purely in memory. When this |
| 56 | +method is used, the user will be prompted to complete the login process each time the application is run. |
| 57 | + |
| 58 | +```python linenums="1" title="Login as a user using a local browser with in memory only state persistance" |
| 59 | +{% include 'auth-session-management/app_managed_auth_state__in_memory__oauth_user_authcode__with_browser.py' %} |
| 60 | +``` |
| 61 | + |
| 62 | +##### Session State Shared with CLI |
| 63 | +Applications may save their session state in a way that is shared with the CLI. With saved state, |
| 64 | +the user will only be prompted to complete the login process once. |
| 65 | +```python linenums="1" title="Login as a user using a local browser with sessions persisted on disk and shared with the CLI" |
| 66 | +{% include 'auth-session-management/app_managed_auth_state__on_disk_cli_shared__oauth_user_authcode__with_browser.py' %} |
| 67 | +``` |
| 68 | + |
| 69 | +##### Session State Saved to Application Storage |
| 70 | +Applications may save their session state to application provided storage. With saved state, |
| 71 | +the user should only be prompted to complete the login process once. Using application provided storage |
| 72 | +will result in the session state not being shared with the CLI. |
| 73 | + |
| 74 | +Applications needing to use their own storage will do so by providing |
| 75 | +the `Auth` layer in the SDK with a custom implementation of the |
| 76 | +[`planet_auth.ObjectStorageProvider`](https://planet-auth.readthedocs.io/en/latest/api-planet-auth/#planet_auth.ObjectStorageProvider) |
| 77 | +abstract base class. See examples below for more details. |
| 78 | + |
| 79 | +```python linenums="1" title="Login as a user using a local browser with sessions persisted to application provided storage" |
| 80 | +{% include 'auth-session-management/app_managed_auth_state__app_custom_storage__oauth_user_authcode__with_browser.py' %} |
| 81 | +``` |
| 82 | + |
| 83 | +### Without a Local Web Browser |
| 84 | +In environments where a local web browser is not available, additional steps must |
| 85 | +be taken by the application author to initialize the user session. |
| 86 | +For example, a remote shell to a cloud environment is not likely |
| 87 | +to be able to open a browser on the user's desktop or receive network callbacks |
| 88 | +from the user's desktop browser. In these cases, a browser is |
| 89 | +still required. To complete login in such a case, the SDK will generate a URL and a |
| 90 | +verification code that must be presented to the user. The user must visit the |
| 91 | +URL out of band to complete the login process while the application polls for |
| 92 | +the completion of the login process using the SDK. At a network protocol |
| 93 | +level, this establishes the user login session using the OAuth2 device |
| 94 | +code flow. |
| 95 | + |
| 96 | +To use this method using the SDK, the following requirements must be met: |
| 97 | + |
| 98 | +* The application must be able to connect to Planet services. |
| 99 | +* The application must be able to display instructions to the user, directing |
| 100 | + them to a web location to complete login. |
| 101 | + |
| 102 | +As above, this may be done with state only persisted in memory, with state |
| 103 | +shared with the CLI, or with state saved to application provided storage. |
| 104 | + |
| 105 | +#### Examples - OAuth2 Device Code Flow |
| 106 | + |
| 107 | +##### In Memory Session State |
| 108 | +```python linenums="1" title="Login as a user using an external browser with in memory only state persistance" |
| 109 | +{% include 'auth-session-management/app_managed_auth_state__in_memory__oauth_user_devicecode__external_browser.py' %} |
| 110 | +``` |
| 111 | + |
| 112 | +##### Session State Shared with CLI |
| 113 | +```python linenums="1" title="Login as a user using an external browser with sessions persisted on disk and shared with the CLI" |
| 114 | +{% include 'auth-session-management/app_managed_auth_state__on_disk_cli_shared__oauth_user_devicecode__external_browser.py' %} |
| 115 | +``` |
| 116 | + |
| 117 | +##### Session State Saved to Application Storage |
| 118 | +```python linenums="1" title="Login as a user using an external browser with sessions persisted to application provided storage" |
| 119 | +{% include 'auth-session-management/app_managed_auth_state__app_custom_storage__oauth_user_devicecode__external_browser.py' %} |
| 120 | +``` |
| 121 | + |
| 122 | +## OAuth2 Session for Service Accounts |
| 123 | +Service account session initialization is simpler than user session |
| 124 | +initialization, and does not require a web browser. |
| 125 | + |
| 126 | +While preserving session state for user sessions was a concern driven |
| 127 | +in part by a concern for the user experience of using a web browser for |
| 128 | +initialization, for service accounts it remains a concern to avoid |
| 129 | +throttling by the authorization service. |
| 130 | + |
| 131 | +If applications are expected to run longer than the life of an access token |
| 132 | +(a few hours), then in memory operations are acceptable (for example: a long-running |
| 133 | +data processing job). If application lifespan is short and frequent, |
| 134 | +then the application should take steps to persist the session state (for |
| 135 | +example: a command line utility run repeatedly from a shell with a short lifespan). |
| 136 | + |
| 137 | +Like the session state itself, service account initialization parameters are |
| 138 | +sensitive, and it is the responsibility of the application to store them |
| 139 | +securely. |
| 140 | + |
| 141 | +At a network protocol level, OAuth2 service account sessions are implemented |
| 142 | +using the OAuth2 authorization code flow. This carries with it some additional |
| 143 | +security concerns, discussed in |
| 144 | +[RFC 6819 §4.4.4](https://datatracker.ietf.org/doc/html/rfc6819#section-4.4.4). |
| 145 | +Because of these considerations, service accounts should only be used for |
| 146 | +workflows that are independent of a controlling user. |
| 147 | + |
| 148 | +As above, this may be done with state only persisted in memory, with state |
| 149 | +shared with the CLI, or with state saved to application provided storage. |
| 150 | + |
| 151 | +### OAuth2 M2M Client Registration |
| 152 | +Service accounts are managed under the |
| 153 | +**OAuth Clients** panel on the [Planet Insights Account](https://insights.planet.com/account/#/) page. |
| 154 | + |
| 155 | +See [Sentinel Hub Authentication](https://docs.sentinel-hub.com/api/latest/api/overview/authentication/) for further information. |
| 156 | + |
| 157 | +### Examples - OAuth2 Client Credentials Flow |
| 158 | + |
| 159 | +#### In Memory Session State |
| 160 | +```python linenums="1" title="Access APIs using a service account with in memory only state persistance" |
| 161 | +{% include 'auth-session-management/app_managed_auth_state__in_memory__oauth_m2m.py' %} |
| 162 | +``` |
| 163 | + |
| 164 | +#### Session State Shared with CLI |
| 165 | +```python linenums="1" title="Access APIs using a service account with sessions persisted on disk and shared with the CLI" |
| 166 | +{% include 'auth-session-management/app_managed_auth_state__on_disk_cli_shared__oauth_m2m.py' %} |
| 167 | +``` |
| 168 | + |
| 169 | +#### Session State Saved to Application Storage |
| 170 | +```python linenums="1" title="Access APIs using a service account with sessions persisted to application provided storage" |
| 171 | +{% include 'auth-session-management/app_managed_auth_state__app_custom_storage__oauth_m2m.py' %} |
| 172 | +``` |
| 173 | + |
| 174 | +---- |
0 commit comments