Skip to content

Instantly share code, notes, and snippets.

@nicosantangelo
Last active August 29, 2015 14:03
Show Gist options
  • Save nicosantangelo/cf747bbaa380bf41be28 to your computer and use it in GitHub Desktop.
Save nicosantangelo/cf747bbaa380bf41be28 to your computer and use it in GitHub Desktop.
Pirvate pub auth to be able to create a ruby client
class PrivatePubAuth
def initialize channel, secret
@config = {
timestamp: (Time.now.to_f * 1000).round,
secret_token: secret,
channel: channel
}
end
def outgoing(message, callback)
unless message['channel'] == '/meta/subscribe'
return callback.call(message)
end
set_private_pub_auth(message)
callback.call(message)
end
def set_private_pub_auth message
message['ext'] ||= {}
message['ext']['private_pub_timestamp'] = @config[:timestamp]
message['ext']['private_pub_signature'] = signature
message
end
private
def signature
Digest::SHA1.hexdigest([@config[:secret_token], @config[:channel], @config[:timestamp]].join)
end
end
require 'private_pub_auth'
require 'eventmachine'
# Server and secret_token can be found in private_pub.yml
server = 'http://localhost:9292/faye'
secret_token = 'secret'
client = Faye::Client.new(server)
# For each channel
channel = 'some/channel'
client.add_extension(PrivatePubAuth.new(channel, secret_token))
EM.run do
client.subscribe(channel) do |message|
puts 'Worked!'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment