Class: Pushkin::Faye::Authentication

Inherits:
Object
  • Object
show all
Defined in:
lib/pushkin/faye/authentication.rb

Instance Method Summary collapse

Instance Method Details

#authenticate_publish(message) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/pushkin/faye/authentication.rb', line 30

def authenticate_publish(message)
  if Pushkin.secret_token.nil?
    raise AuthenticationError.new("No secret_token config set")
  elsif message["ext"]["pushkin_token"] != Pushkin.secret_token
    message["error"] = "Incorrect token."
  else
    message["ext"]["pushkin_token"] = nil
  end
end

#authenticate_subscribe(message) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pushkin/faye/authentication.rb', line 15

def authenticate_subscribe(message)
  signature = message["ext"]["pushkin_signature"]
  timestamp = message["ext"]["pushkin_timestamp"]

  subscription = Pushkin::Subscription.new \
    channel: message["subscription"],
    timestamp: timestamp

  if signature != subscription.signature
    message["error"] = "Incorrect signature."
  elsif subscription.signature_expired?
    message["error"] = "Signature has expired."
  end
end

#incoming(message, callback) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/pushkin/faye/authentication.rb', line 6

def incoming(message, callback)
  if message["channel"] == '/meta/subscribe'
    authenticate_subscribe(message)
  elsif message["channel"] !~ %r{^/meta/}
    authenticate_publish(message)
  end
  callback.call(message)
end