【问题标题】:Hapi authentication scheme: Set custom headerHapi 认证方案:设置自定义标头
【发布时间】:2015-04-24 17:13:32
【问题描述】:

我正在 Hapi.js 中实现一个身份验证方案。

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

【问题讨论】:

    标签: javascript authentication hapijs


    【解决方案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')
    };

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-06
      • 1970-01-01
      • 2017-05-11
      • 1970-01-01
      • 2016-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多