1

我在 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);
                              });
                            }));

                        }
                      }
                    },
                ]);
    });
4

1 回答 1

0

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

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

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

于 2016-06-21T12:16:58.100 回答