是否可以安全地登录passport.js中的profile.id?

时间:2019-07-28 19:03:14

标签: javascript node.js authentication passport.js google-oauth

我正试图允许用户使用Google等oauth提供程序登录并创建帐户。

我尝试使用passport.js,在每个示例中,doc被用来使用profile.id登录用户。当我访问Google网站时,他们说,我引用:

  

警告:请勿在后端服务器上接受简单的用户ID,例如可以通过GoogleUser.getId()方法获得的用户ID。修改后的客户端应用程序可以向服务器发送任意用户ID以模拟用户,因此,您必须使用可验证的ID令牌安全地获取服务器端已登录用户的用户ID。

我不确定Passport.js是否正在执行此操作,或者是否应该对其进行处理。另外,每次我登录到同一帐户时,护照返回的accessToken都不相同,只有profile.id相同。

const Strategy = new GoogleStrategy(
{
  clientID: config.GOOGLE_CLIENT_ID,
  clientSecret: config.GOOGLE_CLIENT_SECRET,
  callbackURL: <url handler>
 },
  (accessToken, refreshToken, profile, done) => {
    handleOAuth(profile, done);
  }
);



const handleOAuth = (profile, accessToken, done) => {
Auths.findOne(
{
  _id: profile.id
},
(err, oldAuth) => {
  if (oldAuth) {
    userObject.user.findOne(
      {
        _id: oldAuth.id
      },
      (err, oldUser) => {
        if (err || !oldUser)
          return console.log("Found auth with incorrect id " + oldAuth._id);
        console.log(
          oldUser.username + " has logged in using Google OAuth2"
        );
        done(null, oldUser);
      }
    );
  } else {
    var _newUser = userObject.createAccount(
      profile,
      accessToken,
      finishCreatingOAuth,
      done
    );
  }
}
);
};

0 个答案:

没有答案