Class: Pubnub::PAM

Inherits:
SingleEvent show all
Defined in:
lib/pubnub/pam.rb

Overview

PAM module holds shared functionality for all PAM requests

Direct Known Subclasses

Audit, Grant, Revoke

Instance Attribute Summary

Attributes inherited from Event

#callback, #channel, #fresh_clone, #given_options, #group, #idle_timeout, #open_timeout, #origin, #presence_callback, #read_timeout, #ssl, #state, #wildcard_channel

Instance Method Summary collapse

Methods inherited from Event

#finalized?, #fire, #send_request, #sync?, #uri

Constructor Details

#initialize(options, app) ⇒ PAM

Returns a new instance of PAM.



5
6
7
8
9
10
11
12
13
# File 'lib/pubnub/pam.rb', line 5

def initialize(options, app)
  super

  @auth_key = options[:auth_key]

  @channel += format_channels(options[:presence]).map do |c|
    c + '-pnpres'
  end if options[:presence].present?
end

Instance Method Details

#current_operationObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/pubnub/pam.rb', line 43

def current_operation
  case @event
  when :audit
    Pubnub::Constants::OPERATION_AUDIT
  when :grant
    Pubnub::Constants::OPERATION_GRANT
  when :revoke
    Pubnub::Constants::OPERATION_REVOKE
  end
end

#error_envelope(parsed_response, error, req_res_objects) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/pubnub/pam.rb', line 100

def error_envelope(parsed_response, error, req_res_objects)
  ErrorEnvelope.new(
    event: @event,
    event_options: @given_options,
    timetoken: nil,
    status: {
      code: req_res_objects[:response].code,
      client_request: req_res_objects[:request],
      server_response: req_res_objects[:response],
      category: (error ? Pubnub::Constants::STATUS_NON_JSON_RESPONSE : Pubnub::Constants::ERROR),
      error: true,
      auto_retried: false,

      current_timetoken: nil,
      last_timetoken: nil,
      subscribed_channels: nil,
      subscribed_channel_groups: nil,

      data: nil,

      config: get_config
    },
    result: {
      code: req_res_objects[:response].code,
      operation: current_operation,
      client_request: req_res_objects[:request],
      server_response: req_res_objects[:response],

      data: {
        message: error_message(parsed_response)
      }
    }
  )
end

#format_envelopes(response, request) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/pubnub/pam.rb', line 54

def format_envelopes(response, request)
  parsed_response, error = Formatter.parse_json(response.body)

  error = response if parsed_response && response.code.to_i != 200

  if error
    error_envelope(parsed_response, error, request: uri, response: response)
  else
    valid_envelope(parsed_response, request: request, response: response)
  end
end

#parameters(set_signature = false) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/pubnub/pam.rb', line 28

def parameters(set_signature = false)
  params = super()
  params.merge!('channel-group' => @group.join(',')) unless @group.blank?
  params.merge!(timestamp: @timestamp)
  params.merge!(channel: @channel.join(',')) unless @channel.first.blank?
  params.merge!(signature: signature) unless set_signature
  params
end

#signatureObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pubnub/pam.rb', line 15

def signature
  message = [
    @subscribe_key,
    @publish_key,
    @event,
    variables_for_signature
  ].join("\n")
  Base64.urlsafe_encode64(
    OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'),
                         @secret_key.to_s, message)
  ).strip
end

#valid_envelope(parsed_response, req_res_objects) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/pubnub/pam.rb', line 66

def valid_envelope(parsed_response, req_res_objects)
  Pubnub::Envelope.new(
    event: @event,
    event_options: @given_options,
    timetoken: nil,
    status: {
      code: req_res_objects[:response].code,
      client_request: req_res_objects[:request],
      server_response: req_res_objects[:response],
      category: Pubnub::Constants::STATUS_ACK,
      error: false,
      auto_retried: false,

      current_timetoken: nil,
      last_timetoken: nil,
      subscribed_channels: nil,
      subscribed_channel_groups: nil,

      data: nil,

      config: get_config

    },
    result: {
      code: req_res_objects[:response].code,
      operation: current_operation,
      client_request: req_res_objects[:request],
      server_response: req_res_objects[:response],

      data: parsed_response['payload']
    }
  )
end

#variables_for_signatureObject



37
38
39
40
41
# File 'lib/pubnub/pam.rb', line 37

def variables_for_signature
  parameters(true).map do |k, v|
    "#{k}=#{CGI.escape(v.to_s).gsub('+', '%20')}"
  end.sort.join('&')
end