2015-04-15 114 views
1

我在Hapi.js中實施身份驗證方案。Hapi身份驗證方案:設置自定義標頭

在我的authenticate函數中,我驗證了該請求並想設置自定義標題。但是因爲我必須使用reply.continue()完成身份驗證功能,所以我無法將任何標頭傳遞給響應。

如何將自定義標題傳遞給客戶端?

最小代碼:

var Boom = require('boom'), 
 
    Hoek = require('hoek'), 
 
    request = require('request'); 
 

 
exports.register = function(plugin, config, next) { 
 
    plugin.auth.scheme('myScheme', function(server, options) { 
 
    Hoek.assert(options, 'Missing auth strategy options'); 
 

 
    return { 
 
     authenticate: function(req, reply) { 
 
     request(
 
      'http://localhost/somewhere', 
 
      function(error, response, body) { 
 
      if (error) { 
 
       return reply(null, null, Boom.unauthorized(null, 'myScheme')); 
 
      } 
 

 
      options.validateFunc(
 
       body, 
 
       function(validateError, isValid, credentials) { 
 
       if (validateError || !isValid) { 
 
        return reply(
 
        Boom.unauthorized('Invalid cookie'), 
 
        null, 
 
        {credentials: credentials} 
 
       ); 
 
       } 
 

 
       // I want to add a custom header here     
 
       //.header('my-header', 'my-header-content') 
 

 
       return reply 
 
        .continue({ 
 
        credentials: credentials || body 
 
        })); 
 
       } 
 
      ); 
 
      } 
 
     ); 
 
     } 
 
    }; 
 
    }); 
 

 
    next(); 
 
}; 
 

 
exports.register.attributes = { 
 
    pkg: require('../package.json') 
 
};

回答

1

將溶液保存在插件數據的標題,並添加一個response功能,即得到所述認證之後調用和可用於添加頁眉到響應。

更新的代碼:

var Boom = require('boom'), 
 
    Hoek = require('hoek'), 
 
    request = require('request'); 
 

 
exports.register = function(plugin, config, next) { 
 
    plugin.auth.scheme('myScheme', function(server, options) { 
 
    Hoek.assert(options, 'Missing auth strategy options'); 
 

 
    return { 
 
     // add headers to the response. 
 
     response: function(request, reply) { 
 
     var pluginData = request.plugins['myScheme']; 
 

 
     if (pluginData && pluginData['my-header']) { 
 
      request.response.header('my-header', pluginData['my-header']); 
 
     } 
 

 
     reply.continue(); 
 
     }, 
 
     authenticate: function(req, reply) { 
 
     request(
 
      'http://localhost/somewhere', 
 
      function(error, response, body) { 
 
      if (error) { 
 
       return reply(null, null, Boom.unauthorized(null, 'myScheme')); 
 
      } 
 

 
      options.validateFunc(
 
       body, 
 
       function(validateError, isValid, credentials) { 
 
       if (validateError || !isValid) { 
 
        return reply(
 
        Boom.unauthorized('Invalid cookie'), 
 
        null, 
 
        {credentials: credentials} 
 
       ); 
 
       } 
 

 
       // save header in the plugin data 
 
       request.plugins['myScheme'] = { 
 
        'my-header': 'my-header-content' 
 
       }; 
 
     
 
       return reply 
 
        .continue({ 
 
        credentials: credentials || body 
 
        })); 
 
       } 
 
      ); 
 
      } 
 
     ); 
 
     } 
 
    }; 
 
    }); 
 

 
    next(); 
 
}; 
 

 
exports.register.attributes = { 
 
    pkg: require('../package.json') 
 
};