【问题标题】:Send REST calls from Node server to third party application using OAuth使用 OAuth 将 REST 调用从 Node 服务器发送到第三方应用程序
【发布时间】:2016-02-06 13:33:13
【问题描述】:

我正在实现一个处理聊天消息的服务器。在某些情况下,我想从 JIRA 实例访问数据。我正在使用 passport-atlassian-oauth 策略通过 JIRA 和 BearerStrategy 进行请求身份验证,但我的问题是,只有在用户授予“我的服务器”对 JIRA 的读写访问权限后,身份验证才在浏览器中有效。在许多指南中,他们只是在成功验证后调用 res.redirect('/successfulLogin') 或类似的东西,但我想对 JIRA 进行休息调用,处理数据并将其发送到我连接的客户端应用程序。

我该怎么做?

我对这一切完全陌生,一切都在我脑海中转来转去。我保存并可以访问用于身份验证的令牌,例如,当我在浏览器中导航到 .../test/access_token=?[token] 时,它可以工作。

passport.use(new BearerStrategy(
    function(token, done) {
      // Find user by token
      client.smembers('access_token:' + token, function(err, replies) {
        if (err) {
          return done(err);
        }
        // if user found
        // TODO: yet again, hard coded for one
        if (replies.length > 0) {
          console.log('SHOULD BE 1:', replies[0]);
          client.hgetall('users:' + replies[0], function(err, user) {
            if (err) {
              return done(err);
            }
            if (!user) {
              return done(null, false);
            }
            return done(null, user, {scope: 'all'});
          });
        }
      });
    }
  ));

如您所见,它仅针对一个用户进行了硬编码,而我将 Redis 用作“数据库”。

  passport.use(new AtlassianOAuthStrategy({
      applicationURL: 'http://localhost:2990/jira',
      callbackURL: '/auth/atlassian-oauth/callback',
      consumerKey: RsaPublicKey,
      consumerSecret: rsaPrivateKey,
      clientId: 'MyBot'
    },
    function(accessToken, tokenSecret, profile, done) {
      // Find user
      client.hgetall('users:1', function(err, user) {
        if(err) {
          return done(err);
        }
        // user not found
        if(!user) {
          // create new user, no worries!
          // TODO: HARD CODED FOR ONE USER
          client.hmset('users:1', 'id', profile.id, 'access_token', accessToken, function(err, res) {
            client.sadd('id:admin', '1');
            client.sadd('access_token:'+ accessToken, '1');
            client.hgetall(profile.id, function(err, user) {
              return done(null, user);
            });
          });
        } else {
          // Update access token!
          client.hmset(profile.id, 'access_token', accessToken, function() {
            client.sadd('access_token:' + accessToken, '1', function() {
              client.hgetall(profile.id, function(err, result) {
                return done(null, user);
              });
            });
          });

        }
      });
    }
  ));

剩下的就到这里了

  app.get('/auth/atlassian-oauth',
    passport.authenticate('atlassian-oauth', {session: false, scope: []}),
    function(req, res) {
      console.log('- Function: /auth/atlassian-oauth - should not be called)');
    });

  app.get('/auth/atlassian-oauth/callback',
    passport.authenticate('atlassian-oauth', {session: false, failureRedirect: '/login'}),
    function(req, res) {
      console.log('- Function: /auth/atlassian-oauth/callback - Authentication successful!', req.user.access_token);
      // Update access token!
      // Should I even do this? Shouldn't I already have the correct token?
      client.hmset('users:1', 'access_token', req.user.access_token, function() {
        client.sadd('access_token:' + req.user.access_token, '1', function() {
          client.hgetall('users:1', function(err, result) {
            res.redirect('/test?access_token=' + req.user.access_token);
          });
        });
      });
    });

既然您已经看到了一些相关的(只需告诉我,我会发布更多)代码,那么如何在不收到 401 的情况下向 JIRA 发送休息呼叫? :)

编辑:任何帮助表示赞赏!如果你能指出我正确的方向,你会让我非常高兴!

【问题讨论】:

    标签: javascript node.js express oauth connect


    【解决方案1】:

    好的。我想到了!首先,您要在 AtlassianOAuthStrategy 中将访问令牌和令牌秘密保存到您的数据库中。其次,为了向第三方服务发送 REST 调用,您可以使用http request with OAuth

    var request = require('request');
    
    var oauth = {
      signature_method : 'RSA-SHA1',
      consumer_key : RsaPublicKey,
      private_key : rsaPrivateKey,
      token : [get access_token from you db],
      token_secret : [get token_secret from you db]'
    };
    var url = 'http://localhost:2990/jira/rest/api/2/issue/' + id;
    request.get({url:url, oauth:oauth, json:true}, function (e, r, issue) {
      console.log(issue)
    });
    

    现在一切正常,我将开始重构并阅读更多文档,以使设计更漂亮并弄清楚如何正确使用 Redis :)

    【讨论】:

      猜你喜欢
      • 2018-02-24
      • 2011-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-11
      • 1970-01-01
      • 2021-03-12
      • 2020-11-06
      相关资源
      最近更新 更多