Home > Backend Development > Golang > How to Validate Google Sign-In ID Tokens in Go Using the Official Library?

How to Validate Google Sign-In ID Tokens in Go Using the Official Library?

Mary-Kate Olsen
Release: 2024-11-07 08:23:03
Original
273 people have browsed it

How to Validate Google Sign-In ID Tokens in Go Using the Official Library?

Validating Google Sign-In ID Tokens in Go: Using the Official Library

In the Go backend, validating Google sign-in ID tokens is straightforward with the official idtoken library.

To start, install the library:

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

Then, you can validate the token with a single line of code:

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

Replace tokenString with the ID token received from the client and audience with your web application's client ID. The payload object will contain the validated claims, such as:

  • aud: Your web application client ID
  • azp: Your Android application client ID
  • email: Authenticated user email
  • email_verified: Email verification status
  • exp: Token expiration timestamp
  • family_name: Authenticated user's last name
  • given_name: Authenticated user's first name
  • iat: Token issuance timestamp
  • iss: Issuer (expected to be either accounts.google.com or https://accounts.google.com)
  • locale: User's preferred language
  • name: Authenticated user's full name
  • picture: User's profile picture URL
  • sub: Unique Google Account ID

This method provides a simple and efficient way to validate ID tokens without introducing potential latency or network errors associated with using the token info endpoint.

The above is the detailed content of How to Validate Google Sign-In ID Tokens in Go Using the Official Library?. 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