【问题标题】:faye authentication with tokenfaye 使用令牌进行身份验证
【发布时间】:2014-06-06 20:07:23
【问题描述】:

我正在使用 faye 消息系统,我想添加身份验证!我浏览了网站并按照教程进行操作。

在客户端我有一个外发消息的扩展:

var droneFaye = new faye.Client("/faye", {
            timeout: 120
        });
var USER_TOKEN = 'ifd63cylqwsyaq9c2ptzywjujgtfpxs';

    droneFaye.addExtension({
        outgoing: function(message, callback) {
            if (message.channel !== '/meta/subscribe')
                return callback(message);

            message.ext = message.ext || {};
            message.ext.token = USER_TOKEN;
            console.log(message);
            callback(message);
        }
    });

在服务器上:

var token = 'ifd63cylqwsyaq9c2ptzywjujgtfpxs'

    var serverAuth = {
    incoming: function(message, callback) {
        // Let non-subscribe messages through

        if (message.channel !== '/meta/subscribe')
            return callback(message);

        // Get subscribed channel and auth token
        var msgToken = message.ext && message.ext.token;

        // Find the right token for the channel
        if (token !== msgToken) {
            message.error = 'Invalid subscription auth token';
        }
        console.log(message);
        callback(message);
    }
};

var adapter = new faye.NodeAdapter({
    mount:'/faye',
    timeout:45
});
adapter.addExtension(serverAuth);
adapter.attach(httpServer);

我在服务器上有这样的订阅:

adapter.getClient().subscribe("/drone/move", function(cmd) {});

好的!因此,当我启动服务器并且没有客户端时,它已经调用了订阅的扩展,它将在控制台上输出:

{ channel: '/meta/subscribe',
  clientId: '2isdeb0ds77zl0lh82ob1kqu29y1cajnv80',
  subscription: '/drone/move',
  id: '2',
  error: 'Invalid subscription auth token' }

一旦客户端连接到服务器,它将再次调用扩展并输出:

{ channel: '/meta/subscribe',
  clientId: '3kechs0c7smpc05z5o7d0a8qcd301d8zi41',
  subscription: '/drone/move',
  id: '3',
  ext: { userId: 18787, token: 'ifd63cylqwsyaq9c2ptzywjujgtfpxs' } }

所以这看起来不错!但是即使有正确的令牌并且没有错误消息,其他消息也不会到达服务器!

仅供参考。如果您向消息对象添加带有值的错误键,它不会将消息传递给它的订阅...它应该是这样的!..

此外,当我在扩展程序中注释掉 message.error 时,它可以正常工作,但当然没有身份验证。

那么有谁知道为什么服务器调用扩展,即使没有客户端,第二为什么即使消息对象没有错误,faye 也不将消息提供给它的订阅?

谢谢!

【问题讨论】:

    标签: javascript node.js websocket faye


    【解决方案1】:

    问题也被问到了,answered here:

    这是由对adapter.getClient().subscribe() 的调用引起的。

    [...] 服务器端客户端 (<adapter>.getClient()) 返回到服务器的连接,因此与客户端执行相同的扩展(传入、传出)。

    [...] 附加一个传出扩展,您会看到它正在响应服务器客户端。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-11
      • 2012-12-23
      • 2014-06-08
      • 2018-08-29
      相关资源
      最近更新 更多