在我的服务器中,我通过传递 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 theuid
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/