【问题标题】:How to implement oauth2orize in hapijs如何在 hapijs 中实现 oauth2orize
【发布时间】:2016-10-22 19:41:02
【问题描述】:

我在 hapijs 中实现了 oauth2orize。但是当我调用 api 时,什么也没有发生。该函数进入 oauth2orize 模块的 code.js 文件并挂在两者之间。请建议我如何在 hapjs 中实现 oauth2orize。 hapi-oauth2orize 也无法正常工作,因为移民和 hapi-oauth2orize 插件会引发选项错误。

const Hapi = require('hapi');
const server = new Hapi.Server();
const oauth2orize = require('oauth2orize');
var oauth = oauth2orize.createServer();

server.connection({ 
    host: 'localhost', 
    port: 8000 
});

    server.register([{
        register: require('hapi-mongodb'),
        options: dbOpts
    }], function (err) {
        if (err) {
            console.error(err);
            throw err;
        }
        server.start();

        server.route([
                    {
                      method: 'GET',
                      path: '/oauth/authorizegrant',
                      config: {
                        auth: false,
                        handler: function(request, reply) {
                            var clientId = request.query.client_id,
                                redirectUrl = request.query.redirect_uri,
                                resType = request.query.response_type,
                                state = request.query.state;
                            oauth.grant(oauth2orize.grant.code(function(clientId,redirectUrl,resType,state,callback) {
                              // Create a new authorization code
                                  console.log('client', client);
                                  var db = request.server.plugins['hapi-mongodb'].db;
                                  var code = new Code({
                                    value: uid(16),
                                    clientId: client._id,
                                    redirectUri: redirectUri,
                                    userId: user._id
                                  });

                              // Save the auth code and check for errors
                            db.collection('codes').insert(code, function(err) {
                                if (err) { console.log('err*********', err); return callback(err); }

                                callback(null, code.value);
                              });
                            }));

                        }
                      }
                    },
                ]);
    });

【问题讨论】:

    标签: node.js hapijs oauth2 oauth2orize


    【解决方案1】:

    您需要更改传递给 oauth.grant 函数的参数,回调应该被移除并替换为 hapi 的回复函数。一个简单的 sn-p 将是

    if (err) {
        return reply(err);
    }
    return reply(code.value);
    

    我会在插件repo 中提出问题,因为这是在 hapi 和 oauth2orize 之间进行交互的最佳方式。

    【讨论】:

    • 感谢您的回复。我是 hapijs 和 oauth2orize 的新手。我按照你做了代码,但它不起作用。此外,hapi-oauth2orize 已过时并且会引发插件错误。 oauth.grant(oauth2orize.grant.code(function(clientId, redirectUrl, resType, state, reply){ }
    • 删除回调并在函数体内使用回调的地方使用回复,同时在 github 上为模块提交问题。它可能由于程序错误而抛出。您需要编写所有代码吗?
    • 我删除了回调,但没有任何效果。是的,我已经提出来了。
    • 是的,你已经打开了 3 个不同的问题,它们中的代码是错误的,这就是它不起作用的原因。
    • 但我实现的代码与 oauth2orize 框架中给出的代码相同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-29
    • 2017-09-30
    • 1970-01-01
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多