A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cloud.google.com/appengine/migration-center/standard/python/migrate-app-identity below:

Migrating App Identity to OIDC ID Tokens | App Engine migration center

Skip to main content Migrating App Identity to OIDC ID Tokens

Stay organized with collections Save and categorize content based on your preferences.

When an app that is running in the Python 2 runtime sends a request to another App Engine app, it can use the App Engine App Identity API to assert its identity. The app that receives the request can use this identity to determine if it should process the request.

If your Python 3 apps need to assert their identity when sending requests to other App Engine apps, you can use OpenID Connect (OIDC) ID tokens that are issued and decoded by Google's OAuth 2.0 APIs.

Here's an overview of using OIDC ID tokens to assert and verify identity:

  1. An App Engine app named "App A" retrieves an ID token from the Google Cloud runtime environment.
  2. App A adds this token to a request header just before sending the request to App B, which is another App Engine app.
  3. App B uses Google's OAuth 2.0 APIs to verify the token payload. The decoded payload contains the verified identity of App A in the form of the email address of App A's default service account.
  4. App B compares the identity in the payload to a list of identities that it is allowed to respond to. If the request came from an allowed app, App B processes the request and responds.

This guide describes how to update your App Engine apps to use OpenID Connect (OIDC) ID tokens to assert identity, and update your other App Engine apps to use ID tokens to verify identity before processing a request.

Key differences between the App Identity and OIDC APIs Overview of the migration process

To migrate your Python apps to use OIDC APIs to assert and verify identity:

  1. In apps that need to assert identity when sending requests to other App Engine apps:

    1. Wait until your app is running in a Python 3 environment to migrate to ID tokens.

      While it is possible to use ID tokens in the Python 2 runtime, the steps in Python 2 are complex and are needed only temporarily until you update your app to run in the Python 3 runtime.

    2. Once your app is running in Python 3, update the app to request an ID token and add the token to a request header.

  2. In apps that need to verify identity before processing a request:

    1. Start by upgrading your Python 2 apps to support both ID tokens and the App Identity API identities. This will enable your apps to verify and process requests from either Python 2 apps that use the App Identity API or Python 3 apps that use ID tokens.

    2. Once your upgraded Python 2 apps are stable, migrate them to the Python 3 runtime. Keep supporting both ID tokens and the App Identity API identities until you are certain that the apps no longer need to support requests from legacy apps.

    3. When you no longer need to process requests from legacy App Engine apps, remove the code that verifies App Identity API identities.

  3. After testing your apps, deploy the app that processes requests first. Then deploy your updated Python 3 app that uses ID tokens to assert identity.

Asserting identity

Wait until your app is running in a Python 3 environment, then follow these steps to upgrade the app to assert identity with ID tokens:

  1. Install the google-auth client library.

  2. Add code to request an ID token from Google's OAuth 2.0 APIs and add the token to a request header before sending a request.

  3. Test your updates.

Installing the google-auth client library for Python 3 apps

To make the google-auth client library available to your Python3 app, create a requirements.txt file in the same folder as your app.yaml file and add the following line:

     google-auth

When you deploy your app, App Engine will download all of the dependencies that are defined in the requirements.txt file.

For local development, we recommend that you install dependencies in a virtual environment such as venv.

Adding code to assert identity

Search through your code and find all instances of sending requests to other App Engine apps. Update those instances to do the following before sending the request:

  1. Add the following imports:

    from google.auth.transport import requests as reqs
    from google.oauth2 import id_token
    
  2. Use google.oauth2.id_token.fetch_id_token(request, audience) to retrieve an ID token. Include the following parameters in the method call:

  3. In your request object, set the following header:

    'Authorization': 'ID {}'.format(token)
    

For example:

Testing updates for asserting identity

To run your app locally and test if the app can successfully send ID tokens:

  1. Follow these steps to make the credentials of the default App Engine service account available in your local environment (the Google OAuth APIs require these credentials to generate an ID token):

    1. Enter the following gcloud command to retrieve the service account key for your project's default App Engine account:

      gcloud iam service-accounts keys create ~/key.json --iam-account project-ID@appspot.gserviceaccount.com

      Replace project-ID with the ID of your Google Cloud project.

      The service account key file is now downloaded to your machine. You can move and rename this file however you would like. Make sure you store this file securely, because it can be used to authenticate as your service account. If you lose the file, or if the file is exposed to unauthorized users, delete the service account key and create a new one.

    2. Enter the following command:

      <code>export GOOGLE_APPLICATION_CREDENTIALS=<var>service-account-key</var></code>
      

    Replace service-account-key with the absolute pathname of the file that contains the service account key you downloaded.

  2. In the same shell in which you exported the GOOGLE_APPLICATION_CREDENTIALS environment variable, start your Python app.

  3. Send a request from the app and confirm that it succeeds. If you don't already have an app that can receive requests and use ID tokens to verify identities:

    1. Download the sample "incoming" app.
    2. In the sample's main.py file, add the ID of your Google Cloud project to the allowed_app_ids. For example:

       allowed_app_ids = [
          '<APP_ID_1>',
          '<APP_ID_2>',
          'my-project-id'
        ]
      
    3. Run the updated sample in the Python 2 local development server.

Verifying and processing requests

To upgrade your Python 2 apps to use either ID tokens or App Identity API identities before processing requests:

  1. Install the google-auth client library.

  2. Update your code to do the following:

    1. If the request contains the X-Appengine-Inbound-Appid header, use that header to verify identity. Apps running in a legacy runtime such as Python 2 will contain this header.

    2. If the request does not contain the X-Appengine-Inbound-Appid header, check for an OIDC ID token. If the token exists, verify the token payload and check the identity of the sender.

  3. Test your updates.

Installing the google-auth client library for Python 2 apps

To make the google-auth client library available to your Python 2 app:

  1. Create a requirements.txt file in the same folder as your app.yaml file and add the following line:

     google-auth==1.19.2
    

    We recommend you use the 1.19.2 version of the Cloud Logging client library since it supports Python 2.7 apps.

  2. In your app's app.yaml file, specify the SSL library in the libraries section if it isn't already specified:

    libraries:
    - name: ssl
      version: latest
    
  3. Create a directory to store your third-party libraries, such as lib/. Then use pip install to install the libraries into the directory. For example:

    pip install -t lib -r requirements.txt
  4. Create an appengine_config.py file in the same folder as your app.yaml file. Add the following to your appengine_config.py file:

    # appengine_config.py
    import pkg_resources
    from google.appengine.ext import vendor
    
    # Set path to your libraries folder.
    path = 'lib'
    # Add libraries installed in the path folder.
    vendor.add(path)
    # Add libraries to pkg_resources working set to find the distribution.
    pkg_resources.working_set.add_entry(path)

    The appengine_config.py file in the preceding example assumes that the the lib folder is located in the current working directory. If you can't guarantee that lib will always be in the current working directory, specify the full path to the lib folder. For example:

    import os
    path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'lib')

For local development, we recommend that you install dependencies in a virtual environment such as virtualenv for Python 2.

Updating code for verifying requests

Search through your code and find all instances of getting the value of the X-Appengine-Inbound-Appid header. Update those instances to do the following:

  1. Add the following imports:

    from google.auth.transport import requests as reqs
    from google.oauth2 import id_token
    
  2. If the incoming request doesn't contain the X-Appengine-Inbound-Appid header, look for the Authorization header and retrieve its value.

    The header value is formatted as "ID: token".

  3. Use google.oauth2.id_token.verify_oauth2_token(token, request, audience) to verify and retrieve the decoded token payload. Include the following parameters in the method call:

  4. The verify_oauth2_token method returns the decoded token payload, which contains several name/value pairs, including the email address of the default service account for the app that generated the token.

  5. Extract the username from the email address in the token payload.

    The username is the same as the project ID of that app that sent the request. This is the same value that was previously returned in the X-Appengine-Inbound-Appid header.

  6. If the username/project ID is in the list of allowed project IDs, process the request.

For example:

Testing updates for verifying identity

To test that your app can use either an ID token or the X-Appengine-Inbound-Appid header to verify requests, run the app in the Python 2 local development server and send requests from Python 2 apps (which will use the App Identity API) and from Python 3 apps that send ID tokens.

If you haven't updated your apps to send ID tokens:

  1. Download the sample "requesting" app.

  2. Add service account credentials to your local environment as described in Testing updates for asserting apps.

  3. Use standard Python 3 commands to start the Python 3 sample app.

  4. Send a request from the sample app and confirm that it succeeds.

Deploying your apps

Once you are ready to deploy your apps, you should:

  1. Test the apps on App Engine.

  2. If the apps run without errors, use traffic splitting to slowly ramp up traffic for your updated apps. Monitor the apps closely for any issues before routing more traffic to the updated apps.

Using a different service account for asserting identity

When you request an ID token, the request uses the identity of the App Engine default service account by default. When you verify the token, the token payload contains the email address of the default service account, which maps to the project ID of your app.

The App Engine default service account has a very high level of permission by default. It can view and edit your entire Google Cloud project, so in most cases this account is not appropriate to use when your app needs to authenticate with Cloud services.

However, the default service account is safe to use when asserting app identity because you are only using the ID token to verify the identity of the app that sent a request. The actual permissions that have been granted to the service account are not considered or needed during this process.

If you still prefer to use a different service account for your ID token requests, do the following:

  1. Set an environment variable named GOOGLE_APPLICATION_CREDENTIALS to the path of a JSON file that contains the credentials of the service account. See our recommendations for safely storing these credentials.

  2. Use google.oauth2.id_token.fetch_id_token(request, audience) to retrieve an ID token.

  3. When you verify this token, the token payload will contain the email address of the new service account.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-08-07 UTC.

[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[[["Python 3 App Engine apps must use OpenID Connect (OIDC) ID tokens to assert their identity when sending requests to other App Engine apps, unlike Python 2 apps which automatically have their identity asserted."],["The process involves the requesting app retrieving an OIDC ID token, adding it to the request header, and the receiving app verifying the token payload using Google's OAuth 2.0 APIs, which contains the email address of the requesting app's default service account."],["Apps receiving requests should be updated to handle both the `X-Appengine-Inbound-Appid` header from Python 2 apps and OIDC ID tokens from Python 3 apps, verifying identity through either the header's project ID or the token's service account email."],["When migrating, update apps that process requests first to support both methods, then update the Python 3 apps that send requests, and finally, after migration is complete, remove code that verifies the `X-Appengine-Inbound-Appid` header when only Python 3 apps are in use."],["The default App Engine service account can be safely used for asserting app identity, as the OIDC ID token verifies the identity of the requesting app, not its permissions, and a different service account can be used by setting the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to the JSON file that contains the service account's credentials."]]],[]]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4