如何检查服务器上的客户端进行身份validation?
情况:
目前,我通过URL传递UID到服务器。
然后我使用UID检查数据库中是否存在该UID:
码:
router.get("/profile/:uid", function(req, res, next){ var uid = req.params.uid; var userRef = admin.database().ref("users/"+uid); userRef.once('value', function(snapshot){ if (snapshot != null) {
问题:
这意味着任何人都可以访问任何人的个人资料通过构build一个url并将其粘贴到search栏中,并包括该用户的UID。
题:
如何检查用户是否在服务器上进行了身份validation,而没有出现这种安全漏洞?
您需要传递身份validation令牌而不是uid。身份validation过程后,您将从Firebase获取uid,而不是来自用户。
使用Firebase Admin SDKvalidationID令牌
卷筒纸
firebase.auth().currentUser.getToken(/* forceRefresh */ true).then(function(idToken) { // Send token to your backend via HTTPS // ... }).catch(function(error) { // Handle error });
服务器(NodeJs)
// idToken comes from the client app (shown above) admin.auth().verifyIdToken(idToken) .then(function(decodedToken) { var uid = decodedToken.uid; // here you can use the uid of the user without the fear of Forgery }).catch(function(error) { // Handle error });
来源: https : //firebase.google.com/docs/auth/admin/verify-id-tokens
您可以定义一个“身份validation”中间件,并将其安装在您要授权的任何路由上:
var authenticate = function(req, res, next) { var token = req.cookies.token; if ( token ) { firebase .auth() .verifyIdToken(token) .then(function(decodedToken) { var uid = decodedToken.sub; if ( uid === req.params.uid ) { next() } else { console.log('This page is personal'); } }) .catch(function(err) { res.redirect('/login'); }); } else { res.redirect('/login'); } };
并使用它如下所示:
router.get("/profile/:uid", authenticate, function(req, res, next){ var uid = req.params.uid; var userRef = admin.database().ref("users/"+uid); userRef.once('value', function(snapshot){ if (snapshot != null) {