RAILS Faye - 令牌认证

时间:2015-09-23 17:30:45

标签: ruby-on-rails authentication websocket token faye

我构建了与各种平台客户端交互的Rails API。在服务器端,实现了faye服务器,如:

Faye::RackAdapter.new(:mount => '/faye', :timeout => 25)

在服务器端,我想通过令牌添加autentication。我使用faye扩展名:

class ServerAuth
  def incoming(message, callback)
    # Let non-subscribe messages throughs
    unless message['channel'] == '/meta/subscribe'
      return callback.call(message)
    end
    # Get subscribed channel and auth token
    msg_token = message['ext'] && message['ext']['authToken']
    # Add an error if the tokens don't match
    if msg_token != '12345'
      message['error'] = 'Invalid subscription auth token'
    end
    # Call the server back now we're done
    callback.call(message)
  end
end

实际上除了我之外它并不起作用。当客户端传递正确的令牌时,一切似乎都可以,但是当他传递无效令牌时,即使他从服务器收到错误消息,他仍然能够推送消息。我应该如何阻止客户赢得这些消息的消息(显然在服务器端)。

1 个答案:

答案 0 :(得分:0)

我为我的问题找到了解决方案。它发生了,因为我只验证'/ meta / subscribe'而不是客户发布消息的其他chanells。现在,第二种推送消息的扩展方法应如下所示:

  def incoming(message, callback)
    return callback.call(message) if message['channel'].start_with? '/meta/'
    <here all logic>
  end