Sha256: acd33671c1a4ba2b2c66e87b2b4f5db470e37ed0476b37d4b925f1544184c095

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

module Thunderer
  # This class is an extension for the Faye::RackAdapter.
  # It is used inside of Thunderer.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 = Thunderer.subscription(:channel => message["subscription"], :timestamp => message["ext"]["thunderer_timestamp"])
      if message["ext"]["thunderer_signature"] != subscription[:signature]
        message["error"] = "Incorrect signature."
      elsif Thunderer.signature_expired? message["ext"]["thunderer_timestamp"].to_i
        message["error"] = "Signature has expired."
      end
    end

    # Ensures the secret token is correct before publishing.
    def authenticate_publish(message)
      if Thunderer.config[:secret_token].nil?
        raise Error, "No secret_token config set, ensure thunderer.yml is loaded properly."
      elsif message["ext"]["thunderer_secret_token"] != Thunderer.config[:secret_token]

        message["error"] = "Incorrect token."
      else
        message["ext"]["thunderer_secret_token"] = nil
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
thunderer-0.9.1 lib/thunderer/faye_extension.rb
thunderer-0.9.0 lib/thunderer/faye_extension.rb