用户在前端通过
进行身份验证 const googleAuthProvider = new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithPopup(googleAuthProvider);
之后,我使用 firebase callable 调用云函数:
const nodeStateCall = functions.httpsCallable('myFunction');
nodeStateCall().then(...);
这工作没有任何问题,我在我的 go 函数中收到了 jwt 令牌。
我尝试了许多不同的变体来验证此令牌。我现在使用的功能是来自
idtoken.Validate()
pacakge 的 google.golang.org/api/idtoken
。
我的功能:
func verifyIdToken(idToken string)(tokenInfo *idtoken.Payload, err error) {
log.Print("running verify Token")
splitToken := strings.Split(idToken, "Bearer ")[1]
log.Print("split Token:"+splitToken)
tokenInfo, err = idtoken.Validate(context.Background(),splitToken,"MYAUDIENCE")
if err != nil {
log.Print("error from tokenInfoCall.Do()")
log.Print(err)
return nil, err
}
log.Print("Finished verify token.")
return tokenInfo, nil
}
但是当我发送令牌时,我不断收到“无效值”。
我还尝试了oauth2服务:
oauth2Service, err := oauth2.NewService(context.Background())
tokenInfoCall := oauth2Service.Tokeninfo()
tokenInfoCall.IdToken(splitToken)
tokenInfo, err := tokenInfoCall.Do()
这也不起作用。
我需要做什么来验证 go 函数中的 firebase jwt 令牌?
因为您使用的是firebase服务,所以需要使用它来验证您的令牌:
请注意,您应该在同一项目中设置函数,以便库能够正确查询。
首先使用
检查您的令牌curl https://oauth2.googleapis.com/tokeninfo?id_token=<token-from-signin>
查看您收到的错误(如果显示“无效令牌”)。下面可能是解决方案。
从登录中解码 JWT 令牌(使用此 url 进行解码 https://jwt.io/ )并确保
iss 和 aud 与 openapi 定义中的 x-google-issuer 和 x-google-audiences 匹配。