doushen4719 2018-01-09 08:05
浏览 84

如何验证JWT?

I'm unsure of the examples in Google's documentation. How do I verify a JWT produced by Firebase, in a Google App engine flexible service?

main.go:

// ...

func main () {
    InitializeAppWithServiceAccount()
    go lib.GetStockData()
    http.HandleFunc("/_ah/someendPoint", SomeHandler)
}

func InitializeAppWithServiceAccount() *firebase.App {
    // [START initialize_app_service_account]
    opt := option.WithCredentialsFile("keystore/someapp-firebase-adminsdk-1ts1k-1fbbbad63f.json")
    app, err := firebase.NewApp(context.Background(), nil, opt)
    if err != nil {
        log.Fatalf("error initializing app: %v
", err)
    }
    return app
}


func someHandler(w http.ResponseWriter, r *http.Request) {
    // Set content type:
    w.Header().Set("Content-Type", "application/json")

    if r.Header != nil {
        ReqToken := r.Header.Get("Authorization")
        splitToken := strings.Split(ReqToken, "Bearer")
        ReqToken = splitToken[1]
        fmt.Println(ReqToken) // Correctly prints the JWT
        // Verify JWT
        // If it's invalid, return?
        // verifyIDToken(??, reqToken)

        enc := json.NewEncoder(w)
        err := enc.Encode(somedata)
        fmt.Println("request made")
        if err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
            return
        }
    }
    http.Error(w, "Unauthorized", http.StatusUnauthorized)
}

According to their documentation, you can use the following function to verify an ID token? But what do I pass in as app? The documentation doesn't really say.

func verifyIDToken(app *firebase.App, idToken string) *auth.Token {
    // [START verify_id_token]
    client, err := app.Auth(context.Background())
    if err != nil {
        log.Fatalf("error getting Auth client: %v
", err)
    }

    token, err := client.VerifyIDToken(idToken)
    if err != nil {
        log.Fatalf("error verifying ID token: %v
", err)
    }

    log.Printf("Verified ID token: %v
", token)
    // [END verify_id_token]

    return token
}

Obviously, idToken is my token from the handler. But what is app *firebase.App and how would I pass it in to the function from the handler itself?

  • 写回答

1条回答 默认 最新

  • dou2347 2018-02-16 22:45
    关注

    You're already initializing a firebase.App in your InitializeAppWithServiceAccount() function. You just need to pass the return value into your handler.

    conf := firebase.Config{
        ProjectID: "my-project-id",
    }
    app, err := firebase.NewApp(context.Background(), &conf)
    if err != nil {
        log.Fatalln(err)
    }
    handler := func(w http.ResponseWriter, r *http.Request) {
        client, err := app.Auth(context.Background())
        if err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
        }
    
        token := getTokenFromReq(r)
        t, err := client.VerifyIDToken(token)
        if err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
        }
        w.Write([]byte("token verified"))
    }
    

    In this example I'm not using a service account (which is probably what you want as well). And since this is on GAE, make sure to pass the GAE context instead of the background context.

    评论

报告相同问题?

悬赏问题

  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。