Sha256: 8b4f05ed7d5c069080779801e9f07d44bc1ef41caf8001a1507f9d3cb7520fe4

Contents?: true

Size: 1.48 KB

Versions: 4

Compression:

Stored size: 1.48 KB

Contents

module Danthes
  # This class is an extension for the Faye::RackAdapter.
  # It is used inside of Danthes.faye_app.
  class FayeExtension
    # Callback to handle incoming Faye messages. This authenticates both
    # subscribe and publish calls.
    def incoming(message, callback)
      if message['channel'] == '/meta/subscribe'
        authenticate_subscribe(message)
      elsif message['channel'] !~ %r{^/meta/}
        authenticate_publish(message)
      end
      callback.call(message)
    end

    private

    # Ensure the subscription signature is correct and that it has not expired.
    def authenticate_subscribe(message)
      subscription = Danthes.subscription(channel: message['subscription'],
                                          timestamp: message['ext']['danthes_timestamp'])
      if message['ext']['danthes_signature'] != subscription[:signature]
        message['error'] = 'Incorrect signature.'
      elsif Danthes.signature_expired? message['ext']['danthes_timestamp'].to_i
        message['error'] = 'Signature has expired.'
      end
    end

    # Ensures the secret token is correct before publishing.
    def authenticate_publish(message)
      if Danthes.config[:secret_token].nil?
        fail Error, 'No secret_token config set, ensure danthes.yml is loaded properly.'
      elsif message['ext']['danthes_token'] != Danthes.config[:secret_token]
        message['error'] = 'Incorrect token.'
      else
        message['ext']['danthes_token'] = nil
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
edmond-danthes-2.1.1 lib/danthes/faye_extension.rb
edmond-danthes-2.1.0 lib/danthes/faye_extension.rb
danthes-2.0.1 lib/danthes/faye_extension.rb
danthes-2.0.0 lib/danthes/faye_extension.rb