Module: Pusher::Utils

Included in:
Channel, Client
Defined in:
lib/pusher/utils.rb

Instance Method Summary collapse

Instance Method Details

#_authentication_string(socket_id, string_to_sign, token, custom_string = nil) ⇒ String

Compute authentication string required as part of the user authentication and subscription authorization endpoints responses. Generally the authenticate method should be used in preference to this one.

Parameters:

  • socket_id (String)

    Each Pusher socket connection receives a unique socket_id. This is sent from pusher.js to your server when channel authentication is required.

  • custom_string (String) (defaults to: nil)

    Allows signing additional data

Returns:

  • (String)

Raises:



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pusher/utils.rb', line 21

def _authentication_string(socket_id, string_to_sign, token, custom_string = nil)
  validate_socket_id(socket_id)

  raise Pusher::Error, 'Custom argument must be a string' unless custom_string.nil? || custom_string.is_a?(String)

  Pusher.logger.debug "Signing #{string_to_sign}"

  digest = OpenSSL::Digest.new('SHA256')
  signature = OpenSSL::HMAC.hexdigest(digest, token.secret, string_to_sign)

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

#validate_socket_id(socket_id) ⇒ Object



3
4
5
6
7
# File 'lib/pusher/utils.rb', line 3

def validate_socket_id(socket_id)
  unless socket_id && /\A\d+\.\d+\z/.match(socket_id)
    raise Pusher::Error, "Invalid socket ID #{socket_id.inspect}"
  end
end