Skip to content

Commit d52fc8c

Browse files
committed
Add Pusher.authenticate for authing on channels
This brings the API inline with `Pusher.trigger`, rather than having to use the now deprecated `Pusher[name].authenticate(...)` style. The APIs aren't quite 1-1 as `Pusher.authenticate` only takes a single channel name, rather than 1-N channel names. That can come in a further PR.
1 parent 419787f commit d52fc8c

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,15 @@ It's possible to use the gem to authenticate subscription requests to private or
178178
### Private channels
179179

180180
``` ruby
181-
Pusher['private-my_channel'].authenticate(params[:socket_id])
181+
Pusher.authenticate('private-my_channel', params[:socket_id])
182182
```
183183

184184
### Presence channels
185185

186186
These work in a very similar way, but require a unique identifier for the user being authenticated, and optionally some attributes that are provided to clients via presence events:
187187

188188
``` ruby
189-
Pusher['presence-my_channel'].authenticate(params[:socket_id], {
189+
Pusher.authenticate('presence-my_channel', params[:socket_id], {
190190
user_id: 'user_id',
191191
user_info: {} # optional
192192
})

lib/pusher/client.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,33 @@ def trigger_async(channels, event_name, data, params = {})
235235
post_async('/events', trigger_params(channels, event_name, data, params))
236236
end
237237

238+
# Generate the expected response for an authentication endpoint.
239+
# See http://pusher.com/docs/authenticating_users for details.
240+
#
241+
# @example Private channels
242+
# render :json => Pusher.authenticate('private-my_channel', params[:socket_id])
243+
#
244+
# @example Presence channels
245+
# render :json => Pusher.authenticate('presence-my_channel', params[:socket_id], {
246+
# :user_id => current_user.id, # => required
247+
# :user_info => { # => optional - for example
248+
# :name => current_user.name,
249+
# :email => current_user.email
250+
# }
251+
# })
252+
#
253+
# @param socket_id [String]
254+
# @param custom_data [Hash] used for example by private channels
255+
#
256+
# @return [Hash]
257+
#
258+
# @private Custom data is sent to server as JSON-encoded string
259+
#
260+
def authenticate(channel_name, socket_id, custom_data = nil)
261+
channel_instance = channel(channel_name)
262+
channel_instance.authenticate(socket_id, custom_data)
263+
end
264+
238265
# @private Construct a net/http http client
239266
def sync_http_client
240267
@client ||= begin

spec/client_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,24 @@
128128
end
129129
end
130130

131+
describe '#authenticate' do
132+
before :each do
133+
@custom_data = {:uid => 123, :info => {:name => 'Foo'}}
134+
end
135+
136+
it 'should return a hash with signature including custom data and data as json string' do
137+
allow(MultiJson).to receive(:encode).with(@custom_data).and_return 'a json string'
138+
139+
response = @client.authenticate('test_channel', '1.1', @custom_data)
140+
141+
expect(response).to eq({
142+
:auth => "12345678900000001:#{hmac(@client.secret, "1.1:test_channel:a json string")}",
143+
:channel_data => 'a json string'
144+
})
145+
end
146+
147+
end
148+
131149
describe '#trigger' do
132150
before :each do
133151
@api_path = %r{/apps/20/events}

0 commit comments

Comments
 (0)