|
| 1 | +# Client Examples |
| 2 | +Client examples cover scenarios in which a program wishes to use |
| 3 | +the planet auth utilities as a client, obtaining credentials |
| 4 | +from authentication services so that they may be used to |
| 5 | +make authenticated requests to other network services. |
| 6 | + |
| 7 | +## OAuth Client Authentication |
| 8 | +A custom auth client profile may be used to configure the AuthClient |
| 9 | +to use an arbitrary OAuth2 auth service to obtain access tokens for |
| 10 | +use with application APIs. |
| 11 | +See [Configuration and Profiles](./configuration.md) for more information |
| 12 | +on client types and profiles. |
| 13 | + |
| 14 | +1. Create a `~/.planet/<profile_name>/auth_client.json` or `~/.planet/<profile_name>/auth_client.sops.json` file. |
| 15 | +For example, to create a custom profile named "my-custom-profile", create the following: |
| 16 | +```json linenums="1" title="~/.planet/my-custom-profile/auth_client.json" |
| 17 | +{% include 'auth-client-config/oauth-auth-code-grant-public-client.json' %} |
| 18 | +``` |
| 19 | +2. Initialize the client library with the specified profile. Note: if the environment variable |
| 20 | +`PL_AUTH_PROFILE` is set, it will be detected automatically by [planet_auth_utils.PlanetAuthFactory][], |
| 21 | +and it will not be necessary to explicitly pass in the profile name: |
| 22 | +```python linenums="1" |
| 23 | +{% include 'auth-client/oauth/initialize-client-lib-on-disk-profile.py' %} |
| 24 | +``` |
| 25 | +An alternative to creating a file on disk is to initialize_from_profile a client |
| 26 | +purely in memory. For some runtime environments where local storage may |
| 27 | +not be available or trusted, this may be more appropriate: |
| 28 | +```python linenums="1" |
| 29 | +{% include 'auth-client/oauth/initialize-client-lib-in-memory.py' %} |
| 30 | +``` |
| 31 | +3. Perform initial login to obtain and save long term credentials, if required |
| 32 | +by the configured profile. An initial login is usually required for auth |
| 33 | +clients that act on behalf of a user and need to perform an interactive login. |
| 34 | +Clients configured for service operation may frequently skip this step: |
| 35 | +```python linenums="1" |
| 36 | +{% include 'auth-client/oauth/perform-oauth-initial-login.py' %} |
| 37 | +``` |
| 38 | +4. Make authenticated requests: |
| 39 | + * Option 1 - Using `requests`: |
| 40 | +```python linenums="1" |
| 41 | +{% include 'auth-client/oauth/make-oauth-authenticated-requests-request.py' %} |
| 42 | +``` |
| 43 | + * Option 2 - Using `httpx`: |
| 44 | +```python linenums="1" |
| 45 | +{% include 'auth-client/oauth/make-oauth-authenticated-httpx-request.py' %} |
| 46 | +``` |
| 47 | + |
| 48 | +## Performing a Device Login |
| 49 | +The procedure for performing initial user login on a UI limited device |
| 50 | +is slightly different. Rather than simply calling `login()`, it is necessary |
| 51 | +to initiate the process with a call to `device_login_initiate()`, display the |
| 52 | +returned information to the user so that the user may authorize the client |
| 53 | +asynchronously, and complete the process by calling `device_login_complete()`. |
| 54 | +This procedure only applies to clients that are permitted to use the |
| 55 | +Device Authorization OAuth flow. |
| 56 | + |
| 57 | +```python linenums="1" |
| 58 | +{% include 'auth-client/oauth/perform-oauth-device-login.py' %} |
| 59 | +``` |
| 60 | + |
| 61 | +## Planet Legacy Authentication |
| 62 | +The proprietary Planet legacy authentication protocol is targeted for future |
| 63 | +deprecation. It should not be used for any new development. |
| 64 | + |
| 65 | +### Initial Login |
| 66 | +```python linenums="1" |
| 67 | +{% include 'auth-client/planet-legacy/perform-legacy-initial-login.py' %} |
| 68 | +``` |
| 69 | + |
| 70 | +### Authenticated `requests` Call |
| 71 | +```python linenums="1" |
| 72 | +{% include 'auth-client/planet-legacy/make-legacy-authenticated-requests-request.py' %} |
| 73 | +``` |
0 commit comments