3 回答

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超3個(gè)贊
這是我使用https://github.com/google/google-api-go-client庫(kù)完成的方法:
import (
"google.golang.org/api/oauth2/v2"
"net/http"
)
var httpClient = &http.Client{}
func verifyIdToken(idToken string) (*oauth2.Tokeninfo, error) {
oauth2Service, err := oauth2.New(httpClient)
tokenInfoCall := oauth2Service.Tokeninfo()
tokenInfoCall.IdToken(idToken)
tokenInfo, err := tokenInfoCall.Do()
if err != nil {
return nil, err
}
return tokenInfo, nil
}
oauth2.Tokeninfo 對(duì)象包含有關(guān)用戶(hù)的信息。請(qǐng)注意,這會(huì)調(diào)用https://www.googleapis.com/oauth2/v2/tokeninfo,我認(rèn)為所有 Google API 客戶(hù)端庫(kù)都會(huì)在幕后進(jìn)行此 http 調(diào)用。

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超4個(gè)贊
Google 的 idToken 實(shí)際上是 JWT 格式的,它是帶有簽名的緊湊且自包含的 JSON。
另見(jiàn):https : //jwt.io/introduction/
google-auth-library-nodejs 的 OAuth2Client.prototype.verifyIdToken 使用 Google 的公鑰驗(yàn)證 idtoken 并從 idtoken 中提取 ClaimSet,而無(wú)需調(diào)用 tokeninfo 端點(diǎn)。
我剛剛從 google-auth-library-nodejs 移植了 verifyIdToken 函數(shù),并為此創(chuàng)建了一個(gè)庫(kù):https : //github.com/futurenda/google-auth-id-token-verifier。
用法:
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)
}

TA貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超8個(gè)贊
它非常簡(jiǎn)單,并且具有單線解決方案。只需使用官方庫(kù):
go get google.golang.org/api/idtoken"
然后編寫(xiě)以下代碼:
payload, err := idtoken.Validate(context.Background(), request.IdToken, "your google client id")
if err != nil {
panic(err)
}
fmt.Print(payload.Claims)
然后你會(huì)得到這個(gè)輸出:
map[
aud:<Your web application client id>
azp:<Your android application client id>
email:<Authenticated user email>
email_verified:true
exp:<expire at>
family_name:<Authenticated user lastname>
given_name:<Authenticated user firstname>
iat:<issued at>
iss: <accounts.google.com or https://accounts.google.com>
locale:en
name:<Authenticated User fullname>
picture:<Authenticated User Photo URL>
sub: <Google Account ID [Use this to identify a id uniquely]>
]
- 3 回答
- 0 關(guān)注
- 365 瀏覽
添加回答
舉報(bào)