Last active
August 29, 2015 14:03
-
-
Save nicosantangelo/cf747bbaa380bf41be28 to your computer and use it in GitHub Desktop.
Pirvate pub auth to be able to create a ruby client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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