node.js - Firebase 管理员 : Is it necessary to check if uid decoded on the server (based on client ID token) matches the uid from the client?

标签 node.js firebase express firebase-authentication firebase-admin

在我的服务器中,我通过传递 Firebase uid 作为参数(即 GET/users/:uid)来访问特定于用户的数据。我还从 Firebase 客户端生成一个 ID token ,并在服务器上解码该 token 以获取 uid。然后我检查这两个 uid 是否相同。

根据 Firebase 处理 token 的方式,这是否有必要?或者 Firebase 能否有效确定请求的 uid 参数(使用 Auth.auth().getUserId 生成)和 token (使用 Auth.auth().generateIdToken 生成) ()) 并非来自同一用户?

注意:我没有使用 Firebase 数据库,因此我没有数据库权限带来的额外安全性。

最佳答案

Firebase 管理员文档建议使用以下方法来验证服务器上当前登录的用户。

If your Firebase client app communicates with a custom backend server, you might need to identify the currently signed-in user on that server. To do so securely, after a successful sign-in, send the user's ID token to your server using HTTPS. Then, on the server, verify the integrity and authenticity of the ID token and retrieve the uid from it. You can use the uid transmitted in this way to securely identify the currently signed-in user on your server.

因此,您只需要在客户端获取用户的ID Token,然后使用HTTPS发送到服务器即可。这足以安全地验证用户的身份。

服务器代码(node.js)

// idToken comes from the client app
admin.auth().verifyIdToken(idToken)
  .then(function(decodedToken) {
    let uid = decodedToken.uid;
    // ...
  }).catch(function(error) {
    // Handle error
  });

请参阅 Firebase 管理文档:Verify ID Tokens

关于node.js - Firebase 管理员 : Is it necessary to check if uid decoded on the server (based on client ID token) matches the uid from the client?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58209057/

相关文章:

javascript - 如果用户存在于 mongo 上并创建用户,如何使用 nest js 抛出异常?

javascript - 无法让浏览器启动从 express 下载

swift - 使用 swift 忽略 FirebaseDB 中的记录

javascript - Firebase 实时数据库规则 - 允许多个用户访问列出所有消息

javascript - NodeJS 从 AWS S3 存储桶下载文件

node.js - 我如何使用 multer 上传视频并强制格式为 mp4

node.js - 如果文件已打开/正在写入,如何检查 Node.js?

javascript - Nodejs 启动一个新的调用堆栈

firebase - 如何通过 Bitbucket Pipeline 部署 Firebase?

database - passportjs 和 passportlocal 添加用户 "Error: failed to serialize user into session"