未知的身份验证策略:hapi-auth-bearer-simple

时间:2017-10-04 16:49:44

标签: javascript node.js hapijs

我正在尝试使用hapi-auth-bearer-simple模块在我的应用上启用持票人令牌。但是,我收到了标题中显示的错误。

我正在尝试实现此模块以在我的应用程序中启用令牌授权但我收到的错误如下所述

e:\python_training\Training\Node\Test\Project\Backend\node_modules\hapi\node_modules\hoek\lib\index.js:723

我有一个路线文件

module.exports = [
    {
        method: 'GET',
        path: '/api/{_id?}',
        handler: function (request, reply) {
           Controller.control.get(request.params, function (err, success) {
                console.log(request.params);
                if (err) {  
                    reply(unifunc.sendError(err));
                } else {
                    reply(unifunc.sendSuccess(SuccessMsg,success)).code(200);
                }
            });
        },
        config: {
            description: 'desc',
            tags: ['api', 'oV'],
            validate: {
                headers: unifunc.authorizationHeaderObj,
                params: {
                    o_id: Joi.string().required().trim(),
                    _id: Joi.string().optional().trim()
                },
                failAction: unifunc.failActionFunction
            },
            auth: {
                strategy: 'bearer',
                scope: ['admin', 'user-{params.id}']
                },
            plugins: {
                'hapi-swagger': {
                    responseMessages: msgs
                }](url)

和我提到策略的控制器文件

var bearerSimple= require('hapi-auth-bearer-simple')
authorization = Authorization.auth; // This plugin has the logic to validate the token and return the error in case it fails and I am passing accesstoken as parameter in a function in that file
var getV = function(server, params, callbackRoute){  
    server.register(
        [{
            register: bearerSimple
        }], function(err){
     if(err){
         console.log("Failed to log the plugin",err);
         throw err;
     }
     server.auth.strategy('bearer', 'bearerAuth', {
        authorization : authorization
     });
    });
    console.log(params);
    async.series([
        function(cb){}
        ]}

完整的错误信息是:

Error: Unknown authentication strategy: bearer in path: /api/orders/{order_id}/vehicles/{_id?}
    at Object.exports.assert (e:\python_training\Training\Node\Test\Project\Backend\node_modules\hapi\node_modules\hoek\lib\index.js:723:11)
    at e:\python_training\Training\Node\Test\Project\Backend\node_modules\hapi\lib\auth.js:152:14
    at Array.forEach (native)
    at internals.Auth._setupRoute (e:\python_training\Training\Node\Test\Project\Backend\node_modules\hapi\lib\auth.js:149:24)
    at new module.exports.internals.Route (e:\python_training\Training\Node\Test\Project\Backend\node_modules\hapi\lib\route.js:142:47)
    at internals.Connection._addRoute (e:\python_training\Training\Node\Test\Project\Backend\node_modules\hapi\lib\connection.js:375:17)
    at internals.Connection._route (e:\python_training\Training\Node\Test\Project\Backend\node_modules\hapi\lib\connection.js:367:18)
    at wrappedRoute [as _route] (e:\python_training\Training\Node\Test\Project\Backend\node_modules\newrelic\lib\instrumentation\hapi.js:222:29)
    at internals.Plugin._apply (e:\python_training\Training\Node\Test\Project\Backend\node_modules\hapi\lib\plugin.js:460:14)
    at internals.Plugin.route 

有什么方法可以解决这个问题吗?

编辑:
我修改了server.js文件并从控制器文件中删除了策略

我将策略放在server.js

var validationFunction = Authorization.auth;
console.log(validationFunction);

server.register(
    [{
        register: bearerSimple
    }], function(err){
 if(err){
     console.log("Failed to log the plugin",err);
     throw err;
 }

 server.auth.strategy('bearer', 'bearerAuth', {
    validationFunction : validationFunction
 });
});

并且在授权文件中看起来像这样

function rauth(accessToken, cb) {
    var criteria = {accessToken: accessToken};
    var projection = {};
    var options = {limit: 1};
    Service.AdminService.getadmin(criteria, projection, options, function (err, data) {
        if (err) {
            cb(err);
        } else if (data && data.length > 0 && data[0]._id) {
            console.log(data);
            console.log(data.length);
            adminId = data[0]._id;
            cb()
        } else {
            cb(UniversalFunctions.CONFIG.APP_CONSTANTS.STATUS_MSG.ERROR.INVALID_ACCESS_TOKEN);
        }
    });

现在我收到了这个错误:

Error: options.validateFunc must be a valid function in bearerAuthentication scheme

我几天来一直在解决这个问题。有谁能建议这里可能出现什么问题?

我发现的唯一问题是在validateFunction中传递的回调函数的参数但我无法删除参数,因为这些参数正在另一个名为getadmin的函数中定义。有人可以为此建议解决方法吗?

1 个答案:

答案 0 :(得分:1)

本期https://github.com/Salesflare/hapi-auth-bearer-simple/issues/69已解决。 问题是一个错字,需要通过成功的授权传递更多信息。