How to Verify Google Sign-In ID Tokens in Go?

Susan Sarandon
Release: 2024-11-07 14:18:02
Original
874 people have browsed it

How to Verify Google Sign-In ID Tokens in Go?

Verifying Google Sign-In ID Tokens in Go

In your quest to validate Google sign-in ID tokens using a Go backend server project, you've come across a challenge: locating an equivalent function to Java and Python's Google API Client Library examples. While the library has implementations for various languages, the Go documentation seems to omit a specific function for ID token verification.

A Go-to Solution

Fortunately, the solution in Go is straightforward and concise. Use the official Google API Client Library for Go:

go get google.golang.org/api/idtoken
Copy after login

Once the library is installed, the following code snippet provides a one-liner verification:

payload, err := idtoken.Validate(context.Background(), tokenString, audience)
if err != nil {
    panic(err)
}
fmt.Print(payload.Claims)
Copy after login

Replace tokenString with the actual ID token you wish to validate, and audience with your application's client ID.

Sample Output

Upon successful validation, you'll receive a payload object containing the following claims:

  • aud: Your web application's client ID
  • azp: Your Android application's client ID
  • email: Authenticated user's email
  • email_verified: True or false
  • exp: Token expiration timestamp
  • family_name: Authenticated user's last name
  • given_name: Authenticated user's first name
  • iat: Token issued at timestamp
  • iss: Issuer of the token (either accounts.google.com or https://accounts.google.com)
  • locale: Locale of the authenticated user
  • name: Authenticated user's full name
  • picture: Authenticated user's profile picture URL
  • sub: Google unique account ID for the authenticated user

By utilizing this method, you can efficiently verify Google sign-in ID tokens in your Go backend project, providing a secure authentication mechanism while minimizing latency and network issues associated with using the token info endpoint.

The above is the detailed content of How to Verify Google Sign-In ID Tokens in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!