Validating Google Sign In Id Token In Go
Solution 1:
This is how I've done it using https://github.com/google/google-api-go-client library:
import (
"google.golang.org/api/oauth2/v2""net/http"
)
var httpClient = &http.Client{}
funcverifyIdToken(idToken string) (*oauth2.Tokeninfo, error) {
oauth2Service, err := oauth2.New(httpClient)
tokenInfoCall := oauth2Service.Tokeninfo()
tokenInfoCall.IdToken(idToken)
tokenInfo, err := tokenInfoCall.Do()
if err != nil {
returnnil, err
}
return tokenInfo, nil
}
oauth2.Tokeninfo object has info about the user. Note that this makes a call to https://www.googleapis.com/oauth2/v2/tokeninfo and I think that all Google API Client Libraries make this http call under the hood.
Solution 2:
It's very easy and has a one-liner solution. Just use the Official library:
go get google.golang.org/api/idtoken"
and then write this code:
payload, err := idtoken.Validate(context.Background(), request.IdToken, "your google client id")
if err != nil {
panic(err)
}
fmt.Print(payload.Claims)
Then you will get this output:
map[
aud:<Yourwebapplicationclientid>
azp:<Yourandroidapplicationclientid>
email:<Authenticateduseremail>
email_verified:true
exp:<expireat>
family_name:<Authenticateduserlastname>
given_name:<Authenticateduserfirstname>
iat:<issuedat>
iss: <accounts.google.comorhttps://accounts.google.com>
locale:en
name:<AuthenticatedUserfullname>
picture:<AuthenticatedUserPhotoURL>
sub: <GoogleAccountID [Usethistoidentifyaiduniquely]>
]
Solution 3:
Google's idToken is actually in JWT format, which is compact and self-contained JSON with signature.
See also: https://jwt.io/introduction/
google-auth-library-nodejs's OAuth2Client.prototype.verifyIdToken verify the idtoken using Google's public key and extract ClaimSet from the idtoken without calling the tokeninfo endpoint.
I just ported the verifyIdToken function from google-auth-library-nodejs, and created a library for this: https://github.com/futurenda/google-auth-id-token-verifier.
Usage:
import (
"github.com/futurenda/google-auth-id-token-verifier"
)
v := googleAuthIDTokenVerifier.Verifier{}
aud := "xxxxxx-yyyyyyy.apps.googleusercontent.com"
err := v.VerifyIDToken(TOKEN, []string{
aud,
})
if err == nil {
claimSet, err := googleAuthIDTokenVerifier.Decode(TOKEN)
// claimSet.Iss,claimSet.Email ... (See claimset.go)
}
Solution 4:
import (
"google.golang.org/api/idtoken"
)
var token string// this comes from your web or mobile app maybeconst googleClientId = ""// from credentials in the Google dev console
tokenValidator, err := idtoken.NewValidator(context.Background())
if err != nil {
// handle error, stop execution
}
payload, err := tokenValidator.Validate(context.Background(), token, googleClientId)
if err != nil {
// handle error, stop execution
}
email := payload.Claims["email"]
name := payload.Claims["name"]
// and so on...You may need to provide your Google credentials to your application: https://cloud.google.com/docs/authentication/production
Post a Comment for "Validating Google Sign In Id Token In Go"