简体   繁体   中英

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

Users are authenticated in the frontend via

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

after that I am calling a cloud function with the firebase callable:

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

This workks without any problems and I recieve the jwt token in my go function.

I tried many different variations to authenticate this token. The function which I am using now is idtoken.Validate() from the google.golang.org/api/idtoken pacakge.

My 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
}

but I keep getting "invalid value" when I send the token.

I also tried the oauth2 service:

oauth2Service, err := oauth2.NewService(context.Background())
tokenInfoCall := oauth2Service.Tokeninfo()
tokenInfoCall.IdToken(splitToken)
tokenInfo, err := tokenInfoCall.Do()

which didn't work either.

What do I have to do to validate the firebase jwt token in my go function?

Because you are using firebase service, you need to use this to verify your token:

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

Note that you should setup your function in the same project so the library will query correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM