An end-user makes the first request to the Cloud Run server.
The client loads in the browser.
The user provides login credentials through the Google sign-in dialog from Identity Platform. An alert welcomes the signed-in user.
Control is redirected back to the server. The end-user votes using the client, which fetches an ID token from Identity Platform and adds it to the vote request header.
When the server receives the request, it verifies the Identity Platform ID token, confirming that the end-user is appropriately authenticated. Then server sends the vote to Cloud SQL, using its own credentials.
The sample is implemented as client and server, as described next.
This sample uses Firebase SDKs to integrate with Identity Platform in order to sign-in and manage users. To connect to Identity Platform, the client-side JavaScript holds the reference to the project's credentials as a config object and imports the necessary Firebase JavaScript SDKs:
The Firebase JavaScript SDK handles the sign-in flow by prompting the end-user to sign-in to their Google Account via a popup window. It then redirects them back to the service.
When a user successfully signs in, the client uses Firebase methods to mint an ID token. The client adds the ID token to the Authorization
header of its request to the server.
The server uses the Firebase Admin SDK to verify the user ID token sent from the client. If the provided ID token has the correct format, is not expired, and is properly signed, the method returns the decoded ID token. The server extracts the Identity Platform uid
for that user.
The server connects to the Cloud SQL instance Unix domain socket using the format: /cloudsql/CLOUD_SQL_CONNECTION_NAME
.
DataSource
bean, which, coupled with Spring JDBC, provides a JdbcTemplate
object bean that allows for operations such as querying and modifying a database.Secret Manager provides centralized and secure storage of sensitive data such as Cloud SQL configuration. The server injects the Cloud SQL credentials from Secret Manager at runtime via an environment variable. Learn more about Using secrets with Cloud Run.
Identity Platform requires manual setup in the Google Cloud console.
In the Google Cloud console, enable the Identity Platform API:
Configure your project:
In a new window, go to the Google Auth Platform > Overview page.
Click Get Started and follow the project configuration setup.
In the App information dialog:
In the Audience dialog, select External.
In the Contact information dialog, enter a contact email.
Agree to the user data policy, then click Create.
Create and obtain your OAuth Client ID and Secret:
In the Google Cloud console, go to the APIs & Services > Credentials page.
At the top of the page, click Create Credentials and select OAuth client ID
.
From Application Type, select Web Application and supply the name.
Click Create
The client_id
and client_secret
values will be used in the next step.
Configure Google as a provider:
In the Google Cloud console, go to the Identity Providers page.
Click Add A Provider.
Select Google from the list.
In the Web SDK Configuration settings, enter the client_id
and client_secret
values from the previous step.
Under Configure your application, click Setup Details.
Copy the configuration to your application:
apiKey
and authDomain
values into the sample's static/config.js
to initialize the Identity Platform Client SDK.Follow the steps to complete infrastructure provisioning and deployment:
Create a Cloud SQL instance with postgreSQL database using the console or CLI:
gcloudsqlinstancescreateCLOUD_SQL_INSTANCE_NAME\--database-version=POSTGRES_16\--region=CLOUD_SQL_REGION\--cpu=2\--memory=7680MB\--root-password=DB_PASSWORD
Add your Cloud SQL credential values to postgres-secrets.json
:
Create a versioned secret using the console or CLI:
gcloudsecretscreateidp-sql-secrets\--replication-policy="automatic"\--data-file=postgres-secrets.json
Create a service account for the server using the console or CLI:
gcloudiamservice-accountscreateidp-sql-identity
Grant roles for Secret Manager and Cloud SQL access using the console or CLI:
Allow the service account associated with the server to access the created secret:
gcloudsecretsadd-iam-policy-bindingidp-sql-secrets\--memberserviceAccount:idp-sql-identity@PROJECT_ID.iam.gserviceaccount.com\--roleroles/secretmanager.secretAccessor
Allow the service account associated with the server to access Cloud SQL:
gcloudprojectsadd-iam-policy-bindingPROJECT_ID\--memberserviceAccount:idp-sql-identity@PROJECT_ID.iam.gserviceaccount.com\--roleroles/cloudsql.client
Create an Artifact Registry:
gcloudartifactsrepositoriescreateREPOSITORY\--repository-formatdocker\--locationREGION
REPOSITORY
is the name of the repository. For each repository location in a project, repository names must be unique.Build the container image using Cloud Build:
gcloudbuildssubmit--tagREGION-docker.pkg.dev/PROJECT_ID/REPOSITORY/idp-sql
gcloudbuildssubmit--tagREGION-docker.pkg.dev/PROJECT_ID/REPOSITORY/idp-sql
This sample uses Jib to build Docker images using common Java tools. Jib optimizes container builds without the need for a Dockerfile or having Docker installed. Learn more about building Java containers with Jib.
Use the gcloud credential helper to authorize Docker to push to your Artifact Registry.
gcloudauthconfigure-docker
Use the Jib Maven Plugin to build and push the container to Artifact Registry.
mvncompilejib:build-Dimage=REGION-docker.pkg.dev/PROJECT_ID/REPOSITORY/idp-sql
Deploy the container image to Cloud Run using the console or CLI. Note that the server is deployed to allow unauthenticated access. This is so that the user can load the client and begin the process. The server verifies the ID token added to the vote request manually, authenticating the end-user.
gcloudrundeployidp-sql\--imageREGION-docker.pkg.dev/PROJECT_ID/REPOSITORY/idp-sql\--allow-unauthenticated\--service-accountidp-sql-identity@PROJECT_ID.iam.gserviceaccount.com\--add-cloudsql-instancesPROJECT_ID:REGION:CLOUD_SQL_INSTANCE_NAME\--update-secretsCLOUD_SQL_CREDENTIALS_SECRET=idp-sql-secrets:latest
Also note the flags, --service-account
, --add-cloudsql-instances
, and --update-secrets
, which specify the service identity, the Cloud SQL instance connection, and the secret name with version as an environment variable, respectively.
Identity Platform requires that you authorize the Cloud Run service URL as an allowed redirect after it has signed in the user:
Edit the Google provider by clicking the pen icon in the Identity Providers page.
Click Add Domain under Authorized Domains on the right panel, and enter the Cloud Run service URL.
You can locate the service URL in the logs after the build or deployment or you can find it anytime using:
gcloudrunservicesdescribeidp-sql--format'value(status.url)'
Go to the APIs & Services > Credentials page
Click the pencil icon beside your OAuth Client ID to edit it and under the Authorized redirect URIs click the
Add URI button.
In the field copy and paste the following URL and click the Save button at the bottom of the page.
https://PROJECT_ID.firebaseapp.com/__/auth/handler
To try out the complete service:
Navigate your browser to the URL provided by the deployment step above.
Click the Sign in with Google button and go through the authentication flow.
Add your vote!
It should look like this:
If you choose to continue developing these services, remember that they have restricted Identity and Access Management (IAM) access to the rest of Google Cloud and will need to be given additional IAM roles to access many other services.
If you created a new project for this tutorial, delete the project. If you used an existing project and wish to keep it without the changes added in this tutorial, delete resources created for the tutorial.
The easiest way to eliminate billing is to delete the project that you created for the tutorial.
To delete the project:
Delete the Cloud Run service you deployed in this tutorial:
gcloudrunservicesdeleteSERVICE-NAME
Where SERVICE-NAME is your chosen service name.
You can also delete Cloud Run services from the Google Cloud console.
Remove the gcloud default region configuration you added during tutorial setup:
gcloudconfigunsetrun/region
Remove the project configuration:
gcloud config unset project
Delete other Google Cloud resources created in this tutorial:
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-04-17 UTC.