如何在node.js通行证中通过twitter登录后使用访问令牌对用户进行身份验证

时间:2013-10-03 07:27:25

标签: node.js authentication oauth-2.0 access-token passport.js

我需要在我的node.js应用程序上构建基于令牌的身份验证,用户可以使用他的facebook或twitter凭据登录我的应用程序,并使用访问令牌获取资源。 this post建议通过脸书或推特或其他方式进行身份验证后,在每次请求时使用访问令牌,并且根本不需要会话 例如

GET / api / v1 / somefunction?token ='abcedf'

  1. 客户端从响应中获取访问令牌。
  2. 客户端使用token参数调用某个服务器api。
  3. 所以下面的代码是通过twitter授权用户,如果我的应用程序找不到我的用户信息,则将用户信息存储到数据库中。

    passport.use(new TwitterStrategy({
          consumerKey: config.twitter.clientID,
          consumerSecret: config.twitter.clientSecret,
          callbackURL: config.twitter.callbackURL
        },
        function(token, tokenSecret, profile, done) {
            console.log('TwitterStrategy   /auth/twitter.............',profile.id, profile.displayName, profile.username, profile.emails[0], profile._json.avatar_url);   
            userModel.findUserByQuery({ 'social.twitter.id': profile.id }, function (err, user) {
              if (!user) {
                console.log('twitter user not found'.red);
                userModel.createNewUser( {  username:profile.username,  
                                            email:profile.emails[0].value, 
                                            img:profile._json.avatar_url, 
                                            fullname:profile.displayName,
                                            password:profile._json.avatar_url,
                                            social:{twitter:{id:profile.id,avatar:profile._json.avatar_url, name:profile.username,token:accessToken} }}, 
                function(err,data){
                    if(err) return done(err);
                    else if(!data)  return done(null, false, { message: 'can not create your profile in database' });           
                    else {
                       console.log('save the new twitter user into database'.green, data._id);
                       return done(err, user);
                    }               
                })          
            } else {
              console.log('twitter user  found'.green);
              return done(err, user);
            }
          })                
        }
    ))
    

    但是,我有两个问题,

    1。如何将访问令牌发送到客户端以获取以下请求

    在代码中,在通过twitter验证后,我获得了访问令牌并将此令牌发送到浏览器上的客户端,因为令牌嵌入在url参数中,我尝试了代码 res.redirect('/ users / profile?token = blabla'),但在客户端浏览器中,网址仍然显示为'/ users / profile'而不是'/ users / profile?标记=布拉布拉'

    2。一旦通过twitter验证,带有令牌的以下请求将在本地通过我的应用程序(我将令牌存储在数据库中,并比较以下令牌以进行验证)或仍然使用twitter API验证?

    如果在第一种情况下,那么我应该将令牌存储到数据库中,以便在以下请求中将令牌中的以下请求与我的应用进行比较?那是对的

1 个答案:

答案 0 :(得分:2)

我也在努力做到这一点,并且发现了这个相当相关的答案:Call Bitbucket REST API using 2leg oauth token

我只是不知道如何用护照做到这一点?特别是,如何从护照认证会话中获取ouath实例,如oauth.get(...),如下所述:https://github.com/ciaranj/node-oauth

更新:Jared(passportjs的作者)在下面的google群组帖子中解释了这是错误的做法,并建议使用https://github.com/mikeal/request

这就是我的工作方式:

var oauth = {
  consumer_key: process.env.BB_CONSUMER_KEY,
  consumer_secret: process.env.BB_CONSUMER_SECRET
};

passport.use(new BitbucketStrategy({
  ...
  function(token, tokenSecret, profile, done) {
    oauth = extend(oauth, {
      token: token,
      token_secret: tokenSecret
  });
}

注意,在群集环境中,每个用户可能需要保留上述令牌。

稍后,要访问api,请执行:

request.get({url: 'protected end point', oauth: oauth})

希望它会帮助别人!