RAILS Faye - 令牌认证
RAILS Faye - token authentication
我构建 Rails API 与各种平台客户端进行交互。在服务器端,实施了 faye 服务器,类似于:
Faye::RackAdapter.new(:mount => '/faye', :timeout => 25)
在服务器端,我想通过令牌添加身份验证。我使用 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
实际上它不像我那样工作,除了。当客户端传递正确的令牌时,一切似乎都很好,但是当他传递无效的令牌时,即使他从服务器返回错误消息,他仍然能够推送消息。我应该如何阻止客户端不会收到它们的消息(显然在服务器端)。
我找到了解决问题的方法。碰巧是因为我只验证了“/meta/subscribe”,而不是客户发布消息的其他渠道。现在,发布消息的第二个扩展方法应该如下所示:
def incoming(message, callback)
return callback.call(message) if message['channel'].start_with? '/meta/'
<here all logic>
end
我构建 Rails API 与各种平台客户端进行交互。在服务器端,实施了 faye 服务器,类似于:
Faye::RackAdapter.new(:mount => '/faye', :timeout => 25)
在服务器端,我想通过令牌添加身份验证。我使用 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
实际上它不像我那样工作,除了。当客户端传递正确的令牌时,一切似乎都很好,但是当他传递无效的令牌时,即使他从服务器返回错误消息,他仍然能够推送消息。我应该如何阻止客户端不会收到它们的消息(显然在服务器端)。
我找到了解决问题的方法。碰巧是因为我只验证了“/meta/subscribe”,而不是客户发布消息的其他渠道。现在,发布消息的第二个扩展方法应该如下所示:
def incoming(message, callback)
return callback.call(message) if message['channel'].start_with? '/meta/'
<here all logic>
end