Golang Google OIDC Usage
This document acquires and analyzes Google OIDC-based tokens using Golang.
1. OIDC Configuration
Configuration is required to obtain OIDC-based ID tokens and OAuth-based access tokens from Google Cloud Platform.
![[Figure 1] Project Creation](/blog-software/docs/programming/golang-google-oidc/images/project-create.png)
[Figure 1] Project Creation
As shown in [Figure 1], access https://console.developers.google.com to create a project.
![[Figure 2] OAuth Addition](/blog-software/docs/programming/golang-google-oidc/images/oauth-add.png)
[Figure 2] OAuth Addition
As shown in [Figure 2], go to the “APIs & Services” section and select “Add OAuth Client ID” to add OAuth authentication.
![[Figure 3] OAuth Client ID Creation](/blog-software/docs/programming/golang-google-oidc/images/oauth-clientid-create.png)
[Figure 3] OAuth Client ID Creation
As shown in [Figure 3], create a Client ID of “Web Application” type. The “Name” can be set arbitrarily. For “Redirect URI”, specify “/auth/google/callback”, which is the path that will be processed in the example code. After creation is complete, check the Client ID and Client Secret.
2. App Code
| |
[Code 1] shows part of a Golang app that obtains ID tokens and access tokens using Google OIDC. The full app code can be found in the following repository:
The operation process is as follows:
- When a user accesses the “/” path of the Golang app, the Golang app redirects the user to the Google authentication/authorization web page.
- When the user’s authentication and authorization process is complete on the Google authentication/authorization web page, the Google authentication/authorization web page redirects the user back to the “/auth/google/callback” path of the Golang app. In this case, an authorization code is also passed as a URL query.
- When the user accesses the “/auth/google/callback” path of the Golang app, the Golang app obtains the authorization code from the URL, then obtains and outputs ID tokens and access tokens through the obtained authorization code.
Line-by-line explanations of [Code 1] are as follows:
- Line 16 : Scope sets the range of user information included in ID token values.
- Lines 21, 41 : State is a temporary string to prevent CSRF attacks on users. State is generated and stored in cookies before authentication/authorization, and after redirect, it is checked whether the State in the URL matches the State in cookies.
- Lines 26, 78 : Nonce is a string used to verify whether ID tokens are valid. ID tokens are generated to include nonce and stored in cookies, and after redirect, it is checked whether the nonce in the obtained ID token matches the nonce in cookies.
- Line 52 : Authorization code exists in the “code” query of the URL.
3. Google Authentication/Authorization
![[Figure 4] Google Authentication](/blog-software/docs/programming/golang-google-oidc/images/google-authn.png)
[Figure 4] Google Authentication
| |
[Figure 3] is the Google authentication screen that is accessed when redirected after accessing the “/” path of the Golang app. [Text 1] shows the URL used when accessing the Google authentication screen. You can see that Client ID, Nonce, Callback URL (Redirect URL), Scope, and State information are included in the URL as queries.
4. ID Token, Access Token
| |
[Text 2] shows an example of a redirect URL. You can see that the “code” query contains the authorization code, and the “scope” query contains scope information.
| |
[Text 3] shows examples of ID token claims and access tokens.