pusher 2.0.2 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b567f2d8e33ab63ee101f1d463ee826ef9deb173509724a0df6996b73fac1249
4
- data.tar.gz: 9e0e443c7769c8520be0f9f72b2db5d95c9a9f221f0221df48b0eee0267f60a1
3
+ metadata.gz: 5666fd615b33c55a6d4edfb362d9efd756c08a86c8b71c5e5364998d9392c3ec
4
+ data.tar.gz: c868837eb799850564c54de4e419d90471e57c738c58fff813a1af89776a7fd5
5
5
  SHA512:
6
- metadata.gz: 9b9d18e469e431fd495fee4eadb33ed0f982fbc796033d036fb3366853a72634a88be4b77fd21bd4c1387b02f00b6f635fbc766bb783cb36987fb81415717fb4
7
- data.tar.gz: 762bb0188b7ee29aa1ff87d235383fc0ca1ded17b251171b091d44d621796faf06b176db4580e2cef6c44c44da8bc5296ec7c3867315faa87ff42a200558425f
6
+ metadata.gz: c8abe07a9ba76d5dbfa47881903c1e553b39163eef3df19357039ee368ae18ebe24bef42bb4be20d7a41e14527c2453893d3b92c84144c686673de045626abe4
7
+ data.tar.gz: 873f4bc20a57905565b80a037c59d560ad8fbb2b97bca6325bc2ff6a1fd092437e68855918a7931f2cc9ccdf6031c38a9190b8f561b7fd6561c6b9c935cbd0a0
data/CHANGELOG.md CHANGED
@@ -1,20 +1,28 @@
1
1
  # Changelog
2
2
 
3
+ ## 2.0.4
4
+
5
+ * [ADDED] Add `authenticate_user` method for user authentication flow
6
+
7
+ ## 2.0.3
8
+
9
+ * [FIXED] Corrected the channels limit when publishing events. Upped from 10 to 100.
10
+
3
11
  ## 2.0.2
4
12
 
5
- * [CHANGED] made encryption_master_key_base64 globally configurable
13
+ * [CHANGED] made encryption_master_key_base64 globally configurable
6
14
 
7
15
  ## 2.0.1
8
16
 
9
- * [CHANGED] Only include lib and essential docs in gem.
17
+ * [CHANGED] Only include lib and essential docs in gem.
10
18
 
11
19
  ## 2.0.0
12
20
 
13
- * [CHANGED] Use TLS by default.
14
- * [REMOVED] Support for Ruby 2.4 and 2.5.
15
- * [FIXED] Handle empty or nil configuration.
16
- * [REMOVED] Legacy Push Notification integration.
17
- * [ADDED] Stalebot and Github actions.
21
+ * [CHANGED] Use TLS by default.
22
+ * [REMOVED] Support for Ruby 2.4 and 2.5.
23
+ * [FIXED] Handle empty or nil configuration.
24
+ * [REMOVED] Legacy Push Notification integration.
25
+ * [ADDED] Stalebot and Github actions.
18
26
 
19
27
  ## 1.4.3
20
28
 
@@ -126,20 +126,9 @@ module Pusher
126
126
  # @raise [Pusher::Error] if socket_id or custom_string invalid
127
127
  #
128
128
  def authentication_string(socket_id, custom_string = nil)
129
- validate_socket_id(socket_id)
129
+ string_to_sign = [socket_id, name, custom_string].compact.map(&:to_s).join(':')
130
130
 
131
- unless custom_string.nil? || custom_string.kind_of?(String)
132
- raise Error, 'Custom argument must be a string'
133
- end
134
-
135
- string_to_sign = [socket_id, name, custom_string].
136
- compact.map(&:to_s).join(':')
137
- Pusher.logger.debug "Signing #{string_to_sign}"
138
- token = @client.authentication_token
139
- digest = OpenSSL::Digest::SHA256.new
140
- signature = OpenSSL::HMAC.hexdigest(digest, token.secret, string_to_sign)
141
-
142
- return "#{token.key}:#{signature}"
131
+ _authentication_string(socket_id, string_to_sign, @client.authentication_token, custom_string)
143
132
  end
144
133
 
145
134
  # Generate the expected response for an authentication endpoint.
@@ -185,10 +174,6 @@ module Pusher
185
174
 
186
175
  private
187
176
 
188
- def validate_socket_id(socket_id)
189
- unless socket_id && /\A\d+\.\d+\z/.match(socket_id)
190
- raise Pusher::Error, "Invalid socket ID #{socket_id.inspect}"
191
- end
192
- end
177
+ include Pusher::Utils
193
178
  end
194
179
  end
data/lib/pusher/client.rb CHANGED
@@ -321,7 +321,7 @@ module Pusher
321
321
 
322
322
 
323
323
  # Generate the expected response for an authentication endpoint.
324
- # See http://pusher.com/docs/authenticating_users for details.
324
+ # See https://pusher.com/docs/channels/server_api/authorizing-users for details.
325
325
  #
326
326
  # @example Private channels
327
327
  # render :json => Pusher.authenticate('private-my_channel', params[:socket_id])
@@ -355,6 +355,31 @@ module Pusher
355
355
  r
356
356
  end
357
357
 
358
+ # Generate the expected response for a user authentication endpoint.
359
+ # See https://pusher.com/docs/authenticating_users for details.
360
+ #
361
+ # @example
362
+ # user_data = { id: current_user.id.to_s, company_id: current_user.company_id }
363
+ # render :json => Pusher.authenticate_user(params[:socket_id], user_data)
364
+ #
365
+ # @param socket_id [String]
366
+ # @param user_data [Hash] user's properties (id is required and must be a string)
367
+ #
368
+ # @return [Hash]
369
+ #
370
+ # @raise [Pusher::Error] if socket_id or user_data is invalid
371
+ #
372
+ # @private Custom data is sent to server as JSON-encoded string
373
+ #
374
+ def authenticate_user(socket_id, user_data)
375
+ validate_user_data(user_data)
376
+
377
+ custom_data = MultiJson.encode(user_data)
378
+ auth = authentication_string(socket_id, custom_data)
379
+
380
+ { auth:, user_data: custom_data }
381
+ end
382
+
358
383
  # @private Construct a net/http http client
359
384
  def sync_http_client
360
385
  require 'httpclient'
@@ -399,9 +424,11 @@ module Pusher
399
424
 
400
425
  private
401
426
 
427
+ include Pusher::Utils
428
+
402
429
  def trigger_params(channels, event_name, data, params)
403
430
  channels = Array(channels).map(&:to_s)
404
- raise Pusher::Error, "Too many channels (#{channels.length}), max 10" if channels.length > 10
431
+ raise Pusher::Error, "Too many channels (#{channels.length}), max 100" if channels.length > 100
405
432
 
406
433
  encoded_data = if channels.any?{ |c| c.match(/^private-encrypted-/) } then
407
434
  raise Pusher::Error, "Cannot trigger to multiple channels if any are encrypted" if channels.length > 1
@@ -469,5 +496,33 @@ module Pusher
469
496
  $stderr.puts "You don't have rbnacl installed in your application. Please add it to your Gemfile and run bundle install"
470
497
  raise e
471
498
  end
499
+
500
+ # Compute authentication string required as part of the user authentication
501
+ # endpoint response. Generally the authenticate method should be used in
502
+ # preference to this one.
503
+ #
504
+ # @param socket_id [String] Each Pusher socket connection receives a
505
+ # unique socket_id. This is sent from pusher.js to your server when
506
+ # channel authentication is required.
507
+ # @param custom_string [String] Allows signing additional data
508
+ # @return [String]
509
+ #
510
+ # @raise [Pusher::Error] if socket_id or custom_string invalid
511
+ #
512
+ def authentication_string(socket_id, custom_string = nil)
513
+ string_to_sign = [socket_id, 'user', custom_string].compact.map(&:to_s).join('::')
514
+
515
+ _authentication_string(socket_id, string_to_sign, authentication_token, string_to_sign)
516
+ end
517
+
518
+ def validate_user_data(user_data)
519
+ return if user_data_valid?(user_data)
520
+
521
+ raise Pusher::Error, "Invalid user data #{user_data.inspect}"
522
+ end
523
+
524
+ def user_data_valid?(data)
525
+ data.is_a?(Hash) && data.key?(:id) && !data[:id].empty? && data[:id].is_a?(String)
526
+ end
472
527
  end
473
528
  end
@@ -0,0 +1,34 @@
1
+ module Pusher
2
+ module Utils
3
+ def validate_socket_id(socket_id)
4
+ unless socket_id && /\A\d+\.\d+\z/.match(socket_id)
5
+ raise Pusher::Error, "Invalid socket ID #{socket_id.inspect}"
6
+ end
7
+ end
8
+
9
+ # Compute authentication string required as part of the user authentication
10
+ # and subscription authorization endpoints responses.
11
+ # Generally the authenticate method should be used in preference to this one.
12
+ #
13
+ # @param socket_id [String] Each Pusher socket connection receives a
14
+ # unique socket_id. This is sent from pusher.js to your server when
15
+ # channel authentication is required.
16
+ # @param custom_string [String] Allows signing additional data
17
+ # @return [String]
18
+ #
19
+ # @raise [Pusher::Error] if socket_id or custom_string invalid
20
+ #
21
+ def _authentication_string(socket_id, string_to_sign, token, custom_string = nil)
22
+ validate_socket_id(socket_id)
23
+
24
+ raise Pusher::Error, 'Custom argument must be a string' unless custom_string.nil? || custom_string.is_a?(String)
25
+
26
+ Pusher.logger.debug "Signing #{string_to_sign}"
27
+
28
+ digest = OpenSSL::Digest.new('SHA256')
29
+ signature = OpenSSL::HMAC.hexdigest(digest, token.secret, string_to_sign)
30
+
31
+ "#{token.key}:#{signature}"
32
+ end
33
+ end
34
+ end
@@ -1,3 +1,3 @@
1
1
  module Pusher
2
- VERSION = '2.0.2'
2
+ VERSION = '2.0.4'
3
3
  end
data/lib/pusher.rb CHANGED
@@ -2,6 +2,7 @@ autoload 'Logger', 'logger'
2
2
  require 'uri'
3
3
  require 'forwardable'
4
4
 
5
+ require 'pusher/utils'
5
6
  require 'pusher/client'
6
7
 
7
8
  # Used for configuring API credentials and creating Channel objects
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pusher
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pusher
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-03 00:00:00.000000000 Z
11
+ date: 2026-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json
@@ -179,13 +179,14 @@ files:
179
179
  - lib/pusher/client.rb
180
180
  - lib/pusher/request.rb
181
181
  - lib/pusher/resource.rb
182
+ - lib/pusher/utils.rb
182
183
  - lib/pusher/version.rb
183
184
  - lib/pusher/webhook.rb
184
185
  homepage: http://github.com/pusher/pusher-http-ruby
185
186
  licenses:
186
187
  - MIT
187
188
  metadata: {}
188
- post_install_message:
189
+ post_install_message:
189
190
  rdoc_options: []
190
191
  require_paths:
191
192
  - lib
@@ -200,8 +201,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
200
201
  - !ruby/object:Gem::Version
201
202
  version: '0'
202
203
  requirements: []
203
- rubygems_version: 3.1.2
204
- signing_key:
204
+ rubygems_version: 3.4.20
205
+ signing_key:
205
206
  specification_version: 4
206
207
  summary: Pusher Channels API client
207
208
  test_files: []