简体   繁体   English

在 Google App Engine NodeJS 服务器上验证用户请求的 Firebase 身份验证令牌?

[英]Verify the Firebase authentication token of a user's request on an Google App Engine NodeJS server?

So I've found this piece of doc on how to integrate Firebase authentication with Google App Engine , but it was written for Python, and it seems old, as I've found some inconsistencies in the code.所以我找到了这篇关于如何将 Firebase 身份验证与 Google App Engine 集成的文档,但它是为 Python 编写的,而且看起来很旧,因为我发现代码中存在一些不一致的地方。 I didn't find a corresponding tutorial for NodeJS.我没有找到 NodeJS 的相应教程。

From I understood, the sequence of the process should be the following:据我了解,该过程的顺序应如下所示:

STEP 1第1步

  • User logs in through client code (firebase JS SDK) and I get its JWT userIdToken by using user.getIdToken()用户通过客户端代码(firebase JS SDK)登录,我使用user.getIdToken()获取其 JWT userIdToken

STEP 2第2步

  • Fetch my GAE server URL with the following header:使用以下标头获取我的 GAE 服务器 URL:
headers: {
  'Authorization': 'Bearer ' + userIdToken
}

Then the tutorial indicates that I should use a Google Auth library, in order to validate the JWT userIdToken .然后教程指出我应该使用 Google Auth 库,以验证 JWT userIdToken

Before the client can access server data, your server must verify the token is signed by Firebase.在客户端可以访问服务器数据之前,您的服务器必须验证令牌是否由 Firebase 签名。 You can verify this token using the Google Authentication Library for Python .您可以使用Google Authentication Library for Python验证此令牌。 Use the authentication library's verify_firebase_token function to verify the bearer token and extract the claims:使用身份验证库的verify_firebase_token函数来验证不记名令牌并提取声明:

Therefore, for a NodeJS server, I should use the following library, correct?因此,对于 NodeJS 服务器,我应该使用以下库,对吗?

google-auth-library谷歌授权库

Or instead, can I use firebase-admin to validate the userIdToken as indicated in the following doc?或者,我可以使用userIdToken firebase-admin来验证userIdToken如下面的文档所示吗?

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

I guess that the firebase-admin seems to be the way to go on this case.我想firebase-admin似乎是处理这种情况的方法。 But if I choose that path, should I still pass the token using the 'Authorization': 'Bearer ' header?但是,如果我选择该路径,我是否仍应使用'Authorization': 'Bearer '标头传递令牌? Or is there a better way of handling this?或者有更好的处理方法吗?

You can pass the ID token to your backend any way you want.您可以以任何方式将 ID 令牌传递给后端。 It's customary and standard to use the Authorization header as you see in the documentation, but not required.正如您在文档中看到的那样,使用 Authorization 标头是习惯和标准的,但不是必需的。 The code examples should make it clear that what you really just need is to pass that token to be verified with the Firebase Admin SDK.代码示例应该清楚地表明您真正需要的是传递该令牌以使用 Firebase Admin SDK 进行验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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