Class: PusherFake::Channel::Private

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

Overview

A private channel.

Direct Known Subclasses

Presence

Constant Summary

Constants inherited from Public

PusherFake::Channel::Public::CACHE_CHANNEL_PREFIX

Instance Attribute Summary

Attributes inherited from Public

#connections, #name

Instance Method Summary collapse

Methods inherited from Public

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

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)

    Information for subscribed client.



13
14
15
16
17
18
19
# File 'lib/pusher-fake/channel/private.rb', line 13

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.



39
40
41
42
43
44
45
46
47
# File 'lib/pusher-fake/channel/private.rb', line 39

def authentication_for(id, data = nil)
  configuration = PusherFake.configuration

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

  "#{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.



27
28
29
30
# File 'lib/pusher-fake/channel/private.rb', line 27

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