forked from stevegraham/slanger
/
subscription.rb
47 lines (36 loc) · 1.02 KB
/
subscription.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
module Slanger
class Subscription
attr_accessor :payload, :socket
def initialize socket, socket_id, msg
@payload = Payload.new socket, socket_id
delegate :send_message, to: :connection
@msg = msg
end
def handle
payload.send channel_id, 'pusher_internal:subscription_succeeded'
channel.subscribe { |m| send_message m }
end
private
def channel
Channel.from channel_id
end
def channel_id
@msg['data']['channel']
end
def token
to_sign = [connection.socket_id, channel_id].compact.join ':'
HMAC::SHA256.hexdigest(Slanger::Config.secret, to_sign)
end
def invalid_signature?
token != auth.split(':')[1]
end
def auth
@msg['data']['auth']
end
def handle_invalid_signature
message = "Invalid signature: Expected HMAC SHA256 hex digest of "
message << "#{payload.socket_id}:#{@msg['data']['channel']}, but got #{@msg['data']['auth']}"
payload.error({ message: message})
end
end
end