簡體   English   中英

如何在我的谷歌雲 function 中驗證 firebase jwt 令牌?

[英]How to validate the firebase jwt token in my google cloud function?

用戶通過在前端進行身份驗證

   const googleAuthProvider = new firebase.auth.GoogleAuthProvider();
                                firebase.auth().signInWithPopup(googleAuthProvider);

之后我用 firebase 調用雲 function:

 const nodeStateCall = functions.httpsCallable('myFunction');
        nodeStateCall().then(...);

這個工作沒有任何問題,我在我的 go function 中收到了 jwt 令牌。

我嘗試了許多不同的變體來驗證此令牌。 我現在使用的 function 是來自google.golang.org/api/idtoken pacakge 的idtoken.Validate()

我的function:

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 function 中的 firebase jwt 令牌?

因為您使用的是 firebase 服務,所以您需要使用它來驗證您的令牌:

https://firebase.google.com/docs/auth/admin/verify-id-tokens#verify_id_tokens_using_the_firebase_admin_sdk

請注意,您應該在同一個項目中設置 function,這樣庫才能正確查詢。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM