【问题标题】:How to properly use passport-oauth2 for using Twitter Oauth2 auth service?如何正确使用 passport-oauth2 来使用 Twitter Oauth2 身份验证服务?
【发布时间】:2016-01-18 06:58:20
【问题描述】:

由于 passport-twitter 仍然适用于 OAuth v1,我决定尝试使用 passport-oauth2 来完成 Twitter 登录,而无需我的应用程序会话。

这是我尝试过的:

passport.use(new OAuth2Strategy({
    authorizationURL: 'https://api.twitter.com/oauth/authenticate',
    tokenURL: 'https://api.twitter.com/oauth/access_token',
    clientID: process.env.TWITTER_FINALCUT_CONSUMER_KEY,
    clientSecret: process.env.TWITTER_FINALCUT_CONSUMER_SECRET,
    callbackURL: 'http://localhost:9248/auth/login/redirect/tw'
    },
    function (access_token,refreshToken,profile,done) {
        console.log(accessToken,Object.keys(profile));
        return done(null, profile, {tokens: {accessToken: accessToken, refreshToken: refreshToken}});
}));

但是,当到达启动身份验证过程的 url 时,我被重定向到这个 Twitter 屏幕。我不知道我在做什么有什么问题。

有什么建议吗?

【问题讨论】:

  • 你是怎么解决的?
  • OAuth 2.0 身份验证在使用状态时需要会话支持。如果未提供状态,则会引发错误提示 AuthorizationError: You need to pass the "state" parameter at OAuth2Strategy.authenticate 。因此,passport 不支持使用 oauth2 或其他策略(如 passport-linkedin-oauth2)的无会话身份验证。

标签: twitter oauth oauth-2.0 passport.js


【解决方案1】:

基本上该策略与例如 https://github.com/Slotos/passport-reddit/blob/master/lib/passport-reddit/strategy.js

问题不在于 Passport JS 本身,而在于底层模块“node-oauth”。因此,请特别参阅上述策略中的“为了遵守我们正在诉诸于压倒一切”的评论。

我希望它在模块中得到修复,所以我在这里评论:https://github.com/ciaranj/node-oauth/issues/300

解决此问题后,我想将其作为 OAuth2 策略直接贡献给 twitter 策略。

推特的首要步骤基本上是[现在,直到上述解决]:

var querystring = require('querystring');
var OAuth2 = require('oauth').OAuth2;
OAuth2.prototype.getOAuthAccessToken = function(code, params, callback) {
  var params= params || {};
  params['client_id'] = this._clientId;
  params['client_secret'] = this._clientSecret;
  var codeParam = (params.grant_type === 'refresh_token') ? 'refresh_token' : 'code';
  params[codeParam]= code;

  var post_data= querystring.stringify( params );
  var post_headers= {
    'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
  };
  if (params.hasOwnProperty('headers') && typeof params.headers === 'object') {
    for (var key in params.headers) {
      post_headers[key] = params.headers[key];
    }
  }

  this._request("POST", this._getAccessTokenUrl() || 'https://api.twitter.com/oauth2/token' /* TODO */, post_headers, post_data, null, function(error, data, response) {
    if( error )  callback(error);
    else {
      var results;
      try {
        // As of http://tools.ietf.org/html/draft-ietf-oauth-v2-07
        // responses should be in JSON
        results= JSON.parse( data );
      }
      catch(e) {
        // .... However both Facebook + Github currently use rev05 of the spec
        // and neither seem to specify a content-type correctly in their response headers :(
        // clients of these services will suffer a *minor* performance cost of the exception
        // being thrown
        results= querystring.parse( data );
      }
      var access_token= results["access_token"];
      var refresh_token= results["refresh_token"];
      delete results["refresh_token"];
      callback(null, access_token, refresh_token, results); // callback results =-=
    }
  });
}

并且可以在策略中用作

var s64 = new Buffer(
  [encodeURIComponent(process.env.CONSUMER_KEY),':',
  encodeURIComponent(process.env.CONSUMER_SECRET)].join('')
).toString('base64');
OAuth2.prototype.getOAuthAccessToken('', {
  grant_type: 'client_credentials',
  headers: {
    Authorization: ['Basic', s64].join(' ')
  }
},
function(e, access_token, refresh_token, res) {
  console.log(e, access_token, refresh_token, res);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-24
    • 1970-01-01
    • 2016-01-27
    • 2014-12-04
    • 2019-11-01
    • 2020-06-24
    • 2014-09-04
    • 2018-11-22
    相关资源
    最近更新 更多