Class: PusherFake::Channel::Private

Inherits:
Public
  • Object
show all
Defined in:
lib/pusher-fake/channel/private.rb

Direct Known Subclasses

Presence

Instance Attribute Summary

Attributes inherited from Public

#connections, #name

Instance Method Summary collapse

Methods inherited from Public

#emit, #includes?, #initialize, #remove, #subscription_data

Constructor Details

This class inherits a constructor from PusherFake::Channel::Public

Instance Method Details

#add(connection, options = {}) ⇒ Object

Add the connection to the channel if they are authorized.

Parameters:

  • connection (Connection)

    The connection to add.

  • options (Hash) (defaults to: {})

    The options for the channel.

Options Hash (options):

  • :auth (String)

    The authentication string.

  • :channel_data (Hash)

    The ID and information for the subscribed client.



10
11
12
13
14
15
16
# File 'lib/pusher-fake/channel/private.rb', line 10

def add(connection, options = {})
  if authorized?(connection, options)
    subscription_succeeded(connection, options)
  else
    connection.emit("pusher_internal:subscription_error", {}, name)
  end
end

#authentication_for(id, data = nil) ⇒ String

Generate an authentication string from the channel based on the connection ID provided.

Parameters:

  • id (String)

    The connection ID.

  • data (String) (defaults to: nil)

    Custom channel data.

Returns:

  • (String)

    The authentication string.



35
36
37
38
39
40
41
42
# File 'lib/pusher-fake/channel/private.rb', line 35

def authentication_for(id, data = nil)
  configuration = PusherFake.configuration
  string        = [id, name, data].compact.map(&:to_s).join(":")
  digest        = OpenSSL::Digest::SHA256.new
  signature     = OpenSSL::HMAC.hexdigest(digest, configuration.secret, string)

  "#{configuration.key}:#{signature}"
end

#authorized?(connection, options) ⇒ Boolean

Determine if the connection is authorized for the channel.

Parameters:

  • connection (Connection)

    The connection to authorize.

  • options (Hash)

Options Hash (options):

  • :auth (String)

    The authentication string.

Returns:

  • (Boolean)

    true if authorized, false otherwise.



24
25
26
# File 'lib/pusher-fake/channel/private.rb', line 24

def authorized?(connection, options)
  authentication_for(connection.id, options[:channel_data]) == options[:auth]
end