|
| 1 | +.. This file is automatically generated. Do not edit this file directly. |
| 2 | +
|
| 3 | +Google Cloud IoT Core Device Federated Authentication Python Samples |
| 4 | +=============================================================================== |
| 5 | + |
| 6 | +.. image:: https://gstatic.com/cloudssh/images/open-btn.png |
| 7 | + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/python-iot&page=editor&open_in_editor=iot/api-client/accesstoken_example/README.rst |
| 8 | + |
| 9 | + |
| 10 | +This directory contains samples for Google Cloud IoT Core Device Federated Authentication. Devices authenticated to Cloud IoT Core can use the `Token Service <https://cloud.google.com/iot/alpha/docs/reference/cloudiottoken/rest>`_ federated authentication to request `OAuth 2.0 access tokens <https://developers.google.com/identity/protocols/oauth2>`_ in exchange for their `Cloud IoT Core JWTs <https://cloud.google.com/iot/docs/how-tos/credentials/jwts>`_. The OAuth 2.0 credentials can be used to call different `Google Cloud APIs <https://developers.google.com/identity/protocols/oauth2/scopes>`_ with fine-grained permissions and access control using `Workload Identity Federation <https://cloud.google.com/iam/docs/workload-identity-federation>`_. For more information, see `documentation <https://cloud.google.com/iot/alpha/docs/how-tos/federated_auth>`_. |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +.. _Google Cloud IoT Core Device Federated Authentication: https://cloud.google.com/iot/alpha/docs/how-tos/federated_auth |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +Setup |
| 22 | +------------------------------------------------------------------------------- |
| 23 | + |
| 24 | + |
| 25 | +Authentication |
| 26 | +++++++++++++++ |
| 27 | + |
| 28 | +This sample requires you to have authentication setup. Refer to the |
| 29 | +`Authentication Getting Started Guide`_ for instructions on setting up |
| 30 | +credentials for applications. |
| 31 | + |
| 32 | +.. _Authentication Getting Started Guide: |
| 33 | + https://cloud.google.com/docs/authentication/getting-started |
| 34 | + |
| 35 | +Install Dependencies |
| 36 | +++++++++++++++++++++ |
| 37 | + |
| 38 | +#. Clone python-iot and change directory to the sample directory you want to use. |
| 39 | + |
| 40 | + .. code-block:: bash |
| 41 | +
|
| 42 | + $ git clone https://github.com/googleapis/python-iot.git |
| 43 | +
|
| 44 | +#. Install `pip`_ and `virtualenv`_ if you do not already have them. You may want to refer to the `Python Development Environment Setup Guide`_ for Google Cloud Platform for instructions. |
| 45 | + |
| 46 | + .. _Python Development Environment Setup Guide: |
| 47 | + https://cloud.google.com/python/setup |
| 48 | + |
| 49 | +#. Create a virtualenv. Samples are compatible with Python 3.6+. |
| 50 | + |
| 51 | + .. code-block:: bash |
| 52 | +
|
| 53 | + $ virtualenv env |
| 54 | + $ source env/bin/activate |
| 55 | +
|
| 56 | +#. Install the dependencies needed to run the samples. |
| 57 | + |
| 58 | + .. code-block:: bash |
| 59 | +
|
| 60 | + $ pip install -r requirements.txt |
| 61 | +
|
| 62 | +.. _pip: https://pip.pypa.io/ |
| 63 | +.. _virtualenv: https://virtualenv.pypa.io/ |
| 64 | + |
| 65 | +Samples |
| 66 | +------------------------------------------------------------------------------- |
| 67 | + |
| 68 | +accesstoken |
| 69 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 70 | + |
| 71 | +.. image:: https://gstatic.com/cloudssh/images/open-btn.png |
| 72 | + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/python-iot&page=editor&open_in_editor=iot/api-client/accesstoken_example/accesstoken.py,iot/api-client/accesstoken_example/README.rst |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +To run this sample: |
| 78 | + |
| 79 | +.. code-block:: bash |
| 80 | +
|
| 81 | + $ python accesstoken.py |
| 82 | +
|
| 83 | + usage: accesstoken.py [-h] [--algorithm {RS256,ES256}] |
| 84 | + [--private_key_file PRIVATE_KEY_FILE] |
| 85 | + [--cloud_region CLOUD_REGION] [--device_id DEVICE_ID] |
| 86 | + [--scope SCOPE] [--project_id PROJECT_ID] |
| 87 | + [--registry_id REGISTRY_ID] [--topic_id TOPIC_ID] |
| 88 | + [--bucket_name BUCKET_NAME] [--data_path DATA_PATH] |
| 89 | + [--service_account_email SERVICE_ACCOUNT_EMAIL] |
| 90 | + [--device_access_token DEVICE_ACCESS_TOKEN] |
| 91 | + [--command_to_be_sent_to_device COMMAND_TO_BE_SENT_TO_DEVICE] |
| 92 | + {generate-access-token,publish-pubsub-message,send-command-to-iot-device,download-cloud-storage-file,exchange-device-token-for-service-account-token} |
| 93 | + ... |
| 94 | +
|
| 95 | + This sample app demonstrates the capabilites of Google Cloud IoT Core device federated authentication feature. |
| 96 | + Devices authenticated to Cloud IoT Core can use the [Token Service](https://cloud.google.com/iot/alpha/docs/reference/cloudiottoken/rest) federated authentication to request [OAuth 2.0 access tokens](https://developers.google.com/identity/protocols/oauth2) in exchange for their [Cloud IoT Core JWTs](https://cloud.google.com/iot/docs/how-tos/credentials/jwts). |
| 97 | + The OAuth 2.0 credentials can be used to call different [Google Cloud APIs](https://developers.google.com/identity/protocols/oauth2/scopes) with fine-grained permissions and access control using [Workload Identity Federation](https://cloud.google.com/iam/docs/workload-identity-federation). |
| 98 | + For more information, see https://cloud.google.com/iot/alpha/docs/how-tos/federated_auth |
| 99 | +
|
| 100 | + Usage example: |
| 101 | +
|
| 102 | + python accesstoken.py \ |
| 103 | + --project_id=my-project-id \ |
| 104 | + --cloud_region=us-central1 \ |
| 105 | + --registry_id=my-registry-id \ |
| 106 | + --device_id=my-device-id \ |
| 107 | + --private_key_file=./resources/rsa_private.pem \ |
| 108 | + --scope=https://www.googleapis.com/auth/cloud-platform \ |
| 109 | + --algorithm=RS256 \ |
| 110 | + generate-access-token |
| 111 | +
|
| 112 | + positional arguments: |
| 113 | + {generate-access-token,publish-pubsub-message,send-command-to-iot-device,download-cloud-storage-file,exchange-device-token-for-service-account-token} |
| 114 | + generate-access-token |
| 115 | + Generates OAuth 2.0 Google Access Token. |
| 116 | + publish-pubsub-message |
| 117 | + Publishes a message to Cloud Pub/Sub topic. |
| 118 | + send-command-to-iot-device |
| 119 | + Sends a command to an IoT device. |
| 120 | + download-cloud-storage-file |
| 121 | + Downloads a file from Cloud Storage bucket. |
| 122 | + exchange-device-token-for-service-account-token |
| 123 | + Exchanges device access token to service account |
| 124 | + access token. |
| 125 | +
|
| 126 | + optional arguments: |
| 127 | + -h, --help show this help message and exit |
| 128 | + --algorithm {RS256,ES256} |
| 129 | + Encryption algorithm used to generate the device JWT. |
| 130 | + --private_key_file PRIVATE_KEY_FILE |
| 131 | + Path to private key file. |
| 132 | + --cloud_region CLOUD_REGION |
| 133 | + GCP cloud region. |
| 134 | + --device_id DEVICE_ID |
| 135 | + Device ID. |
| 136 | + --scope SCOPE Scope for OAuth 2.0 access token. Space delimited |
| 137 | + strings. See the full list of scopes at: https://devel |
| 138 | + opers.google.com/identity/protocols/oauth2/scopes |
| 139 | + --project_id PROJECT_ID |
| 140 | + GCP cloud project name. |
| 141 | + --registry_id REGISTRY_ID |
| 142 | + Registry ID. |
| 143 | + --topic_id TOPIC_ID Cloud Pub/Sub topic ID. |
| 144 | + --bucket_name BUCKET_NAME |
| 145 | + Cloud Storage bucket name. |
| 146 | + --data_path DATA_PATH |
| 147 | + Path to file to be uploaded. |
| 148 | + --service_account_email SERVICE_ACCOUNT_EMAIL |
| 149 | + Service account email to exchange device access token |
| 150 | + to service account token. |
| 151 | + --device_access_token DEVICE_ACCESS_TOKEN |
| 152 | + Device access token to exchange for service account |
| 153 | + access token. |
| 154 | + --command_to_be_sent_to_device COMMAND_TO_BE_SENT_TO_DEVICE |
| 155 | + Command to be sent to the IoT device. |
| 156 | +
|
| 157 | +
|
| 158 | +
|
| 159 | +
|
| 160 | +
|
| 161 | +.. _Google Cloud SDK: https://cloud.google.com/sdk/ |
0 commit comments