pubnub 4.5.0 → 4.6.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of pubnub might be problematic. Click here for more details.

Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/.pubnub.yml +53 -37
  3. data/CHANGELOG.md +10 -0
  4. data/Gemfile.lock +1 -1
  5. data/VERSION +1 -1
  6. data/fixtures/vcr_cassettes/examples/channel_members/001.yml +111 -0
  7. data/fixtures/vcr_cassettes/examples/channel_members/002.yml +111 -0
  8. data/fixtures/vcr_cassettes/examples/channel_members/003.yml +111 -0
  9. data/fixtures/vcr_cassettes/examples/channel_members/004.yml +147 -0
  10. data/fixtures/vcr_cassettes/examples/channel_members/005.yml +147 -0
  11. data/fixtures/vcr_cassettes/examples/channel_members/006.yml +147 -0
  12. data/fixtures/vcr_cassettes/examples/channel_metadata/001.yml +39 -0
  13. data/fixtures/vcr_cassettes/examples/channel_metadata/002.yml +39 -0
  14. data/fixtures/vcr_cassettes/examples/channel_metadata/003.yml +39 -0
  15. data/fixtures/vcr_cassettes/examples/channel_metadata/004.yml +73 -0
  16. data/fixtures/vcr_cassettes/examples/channel_metadata/005.yml +73 -0
  17. data/fixtures/vcr_cassettes/examples/channel_metadata/006.yml +73 -0
  18. data/fixtures/vcr_cassettes/examples/history/include_meta__encrypted.yml +7 -7
  19. data/fixtures/vcr_cassettes/examples/history/include_meta__encrypted_random_iv.yml +46 -0
  20. data/fixtures/vcr_cassettes/examples/history/include_token__encrypted.yml +6 -6
  21. data/fixtures/vcr_cassettes/examples/history/include_token__encrypted_random_iv.yml +46 -0
  22. data/fixtures/vcr_cassettes/examples/history/no_includes__encrypted.yml +6 -6
  23. data/fixtures/vcr_cassettes/examples/history/no_includes__encrypted_random_iv.yml +45 -0
  24. data/fixtures/vcr_cassettes/examples/memberships/001.yml +111 -0
  25. data/fixtures/vcr_cassettes/examples/memberships/002.yml +111 -0
  26. data/fixtures/vcr_cassettes/examples/memberships/003.yml +111 -0
  27. data/fixtures/vcr_cassettes/examples/memberships/004.yml +111 -0
  28. data/fixtures/vcr_cassettes/examples/memberships/005.yml +147 -0
  29. data/fixtures/vcr_cassettes/examples/memberships/006.yml +147 -0
  30. data/fixtures/vcr_cassettes/examples/memberships/007.yml +147 -0
  31. data/fixtures/vcr_cassettes/examples/memberships/008.yml +147 -0
  32. data/fixtures/vcr_cassettes/examples/uuid_metadata/001.yml +39 -0
  33. data/fixtures/vcr_cassettes/examples/uuid_metadata/002.yml +39 -0
  34. data/fixtures/vcr_cassettes/examples/uuid_metadata/003.yml +39 -0
  35. data/fixtures/vcr_cassettes/examples/uuid_metadata/004.yml +39 -0
  36. data/fixtures/vcr_cassettes/examples/uuid_metadata/005.yml +73 -0
  37. data/fixtures/vcr_cassettes/examples/uuid_metadata/006.yml +73 -0
  38. data/fixtures/vcr_cassettes/examples/uuid_metadata/007.yml +73 -0
  39. data/fixtures/vcr_cassettes/examples/uuid_metadata/008.yml +73 -0
  40. data/lib/pubnub/client.rb +4 -1
  41. data/lib/pubnub/client/helpers.rb +1 -1
  42. data/lib/pubnub/configuration.rb +1 -0
  43. data/lib/pubnub/constants.rb +1 -0
  44. data/lib/pubnub/crypto.rb +22 -8
  45. data/lib/pubnub/event.rb +7 -1
  46. data/lib/pubnub/events/history.rb +2 -1
  47. data/lib/pubnub/events/publish.rb +2 -2
  48. data/lib/pubnub/events/remove_channel_members.rb +1 -1
  49. data/lib/pubnub/events/remove_channel_metadata.rb +1 -1
  50. data/lib/pubnub/events/remove_memberships.rb +1 -1
  51. data/lib/pubnub/events/remove_uuid_metadata.rb +1 -1
  52. data/lib/pubnub/events/set_channel_members.rb +1 -1
  53. data/lib/pubnub/events/set_channel_metadata.rb +1 -1
  54. data/lib/pubnub/events/set_memberships.rb +1 -1
  55. data/lib/pubnub/events/set_uuid_metadata.rb +1 -1
  56. data/lib/pubnub/events/signal.rb +1 -1
  57. data/lib/pubnub/formatter.rb +3 -3
  58. data/lib/pubnub/subscribe_event/formatter.rb +4 -2
  59. data/lib/pubnub/version.rb +1 -1
  60. data/spec/examples/history_examples_spec.rb +87 -9
  61. data/spec/examples/publish_examples_spec.rb +864 -0
  62. data/spec/examples/remove_channel_members_examples_spec.rb +100 -0
  63. data/spec/examples/remove_channel_metadata_examples_spec.rb +89 -0
  64. data/spec/examples/remove_memberships_examples_spec.rb +121 -0
  65. data/spec/examples/remove_uuid_metadata_examples_spec.rb +107 -0
  66. data/spec/examples/set_channel_members_examples_spec.rb +99 -0
  67. data/spec/examples/set_channel_metadata_examples_spec.rb +93 -0
  68. data/spec/examples/set_memberships_examples_spec.rb +122 -0
  69. data/spec/examples/set_uuid_metadata_examples_spec.rb +114 -0
  70. data/spec/examples/subscribe_examples_1_spec.rb +2 -2
  71. data/spec/examples/subscribe_examples_2_spec.rb +4 -4
  72. data/spec/lib/events/subscribe_spec.rb +2 -0
  73. data/spec/lib/multiple_ciphers_spec.rb +16 -4
  74. data/spec/spec_helper.rb +1 -1
  75. metadata +50 -3
@@ -0,0 +1,73 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: patch
5
+ uri: http://ps.pndsn.com/v2/objects/sub-a-mock-key/uuids/ruby-test-uuid-client-one?auth=ruby-test-auth-client-one&include=custom&pnsdk=PubNub-Ruby/4.5.0&uuid=ruby-test-uuid-client-one
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"name":"magnum"}'
9
+ headers:
10
+ User-Agent:
11
+ - HTTPClient/1.0 (2.8.3, ruby 2.6.5 (2019-10-01))
12
+ Accept:
13
+ - "*/*"
14
+ Date:
15
+ - Wed, 03 Mar 2021 23:11:13 GMT
16
+ Content-Type:
17
+ - application/x-www-form-urlencoded
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ Date:
24
+ - Wed, 03 Mar 2021 23:11:13 GMT
25
+ Content-Type:
26
+ - application/json
27
+ Content-Length:
28
+ - '199'
29
+ Connection:
30
+ - keep-alive
31
+ Access-Control-Allow-Origin:
32
+ - "*"
33
+ Access-Control-Allow-Credentials:
34
+ - 'true'
35
+ body:
36
+ encoding: UTF-8
37
+ string: '{"status":200,"data":{"id":"ruby-test-uuid-client-one","name":"magnum","externalId":null,"profileUrl":null,"email":null,"custom":null,"updated":"2021-03-03T23:11:13.101616Z","eTag":"AaaUxtPTpNTufg"}}'
38
+ recorded_at: Wed, 03 Mar 2021 23:11:13 GMT
39
+ - request:
40
+ method: delete
41
+ uri: http://ps.pndsn.com/v2/objects/sub-a-mock-key/uuids/bob?auth=ruby-test-auth-client-one&pnsdk=PubNub-Ruby/4.5.0&uuid=ruby-test-uuid-client-one
42
+ body:
43
+ encoding: UTF-8
44
+ string: ''
45
+ headers:
46
+ User-Agent:
47
+ - HTTPClient/1.0 (2.8.3, ruby 2.6.5 (2019-10-01))
48
+ Accept:
49
+ - "*/*"
50
+ Date:
51
+ - Wed, 03 Mar 2021 23:11:13 GMT
52
+ response:
53
+ status:
54
+ code: 200
55
+ message: OK
56
+ headers:
57
+ Date:
58
+ - Wed, 03 Mar 2021 23:11:13 GMT
59
+ Content-Type:
60
+ - application/json
61
+ Content-Length:
62
+ - '26'
63
+ Connection:
64
+ - keep-alive
65
+ Access-Control-Allow-Origin:
66
+ - "*"
67
+ Access-Control-Allow-Credentials:
68
+ - 'true'
69
+ body:
70
+ encoding: UTF-8
71
+ string: '{"status":200,"data":null}'
72
+ recorded_at: Wed, 03 Mar 2021 23:11:13 GMT
73
+ recorded_with: VCR 6.0.0
@@ -0,0 +1,73 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: patch
5
+ uri: http://ps.pndsn.com/v2/objects/sub-a-mock-key/uuids/ruby-test-uuid-client-one?auth=ruby-test-auth-client-one&include=1&pnsdk=PubNub-Ruby/4.5.0&uuid=ruby-test-uuid-client-one
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"name":"magnum"}'
9
+ headers:
10
+ User-Agent:
11
+ - HTTPClient/1.0 (2.8.3, ruby 2.6.5 (2019-10-01))
12
+ Accept:
13
+ - "*/*"
14
+ Date:
15
+ - Wed, 03 Mar 2021 23:11:13 GMT
16
+ Content-Type:
17
+ - application/x-www-form-urlencoded
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ Date:
24
+ - Wed, 03 Mar 2021 23:11:13 GMT
25
+ Content-Type:
26
+ - application/json
27
+ Content-Length:
28
+ - '185'
29
+ Connection:
30
+ - keep-alive
31
+ Access-Control-Allow-Origin:
32
+ - "*"
33
+ Access-Control-Allow-Credentials:
34
+ - 'true'
35
+ body:
36
+ encoding: UTF-8
37
+ string: '{"status":200,"data":{"id":"ruby-test-uuid-client-one","name":"magnum","externalId":null,"profileUrl":null,"email":null,"updated":"2021-03-03T23:11:13.101616Z","eTag":"AaaUxtPTpNTufg"}}'
38
+ recorded_at: Wed, 03 Mar 2021 23:11:13 GMT
39
+ - request:
40
+ method: delete
41
+ uri: http://ps.pndsn.com/v2/objects/sub-a-mock-key/uuids/bob?auth=ruby-test-auth-client-one&pnsdk=PubNub-Ruby/4.5.0&uuid=ruby-test-uuid-client-one
42
+ body:
43
+ encoding: UTF-8
44
+ string: ''
45
+ headers:
46
+ User-Agent:
47
+ - HTTPClient/1.0 (2.8.3, ruby 2.6.5 (2019-10-01))
48
+ Accept:
49
+ - "*/*"
50
+ Date:
51
+ - Wed, 03 Mar 2021 23:11:13 GMT
52
+ response:
53
+ status:
54
+ code: 200
55
+ message: OK
56
+ headers:
57
+ Date:
58
+ - Wed, 03 Mar 2021 23:11:13 GMT
59
+ Content-Type:
60
+ - application/json
61
+ Content-Length:
62
+ - '26'
63
+ Connection:
64
+ - keep-alive
65
+ Access-Control-Allow-Origin:
66
+ - "*"
67
+ Access-Control-Allow-Credentials:
68
+ - 'true'
69
+ body:
70
+ encoding: UTF-8
71
+ string: '{"status":200,"data":null}'
72
+ recorded_at: Wed, 03 Mar 2021 23:11:13 GMT
73
+ recorded_with: VCR 6.0.0
data/lib/pubnub/client.rb CHANGED
@@ -119,6 +119,9 @@ module Pubnub
119
119
  # <dt>ssl</dt>
120
120
  # <dd><b>optional.</b> Your connection will use ssl if set to true.</dd>
121
121
  #
122
+ # <dt>random_iv</dt>
123
+ # <dd><b>optional.</b> Whether data should be encrypted / decrypted using random initialization vector.</dd>
124
+ #
122
125
  # <dt>heartbeat</dt>
123
126
  # <dd><b>optional.</b> Heartbeat interval, if not set heartbeat will not be running.</dd>
124
127
  #
@@ -378,7 +381,7 @@ module Pubnub
378
381
  def assign_defaults
379
382
  @env[:origin] = @env[:origins_pool].first if @env[:origins_pool]
380
383
  default_values.each do |k, v|
381
- @env[k] = v unless @env[k]
384
+ @env[k] = v unless @env.has_key?(k)
382
385
  end
383
386
  @env[:timetoken] = 0
384
387
  @env[:sequence_number_for_publish] = 0
@@ -34,7 +34,7 @@ module Pubnub
34
34
  end
35
35
 
36
36
  def clean_env
37
- @env.delete_if { |_, v| v.blank? } # nillify if blank
37
+ @env.delete_if { |_, v| v.blank? && v != false } # nillify if blank
38
38
  end
39
39
  end
40
40
  end
@@ -16,6 +16,7 @@ module Pubnub
16
16
  reconnect_interval: Pubnub::Constants::DEFAULT_RECONNECT_INTERVAL,
17
17
  region: Pubnub::Constants::DEFAULT_REGION,
18
18
  ssl: Pubnub::Constants::DEFAULT_SSL,
19
+ random_iv: Pubnub::Constants::DEFAULT_USE_RANDOM_IV,
19
20
  request_message_count_threshold: Pubnub::Constants::REQUEST_MESSAGE_COUNT_THRESHOLD }
20
21
  end
21
22
  end
@@ -19,6 +19,7 @@ module Pubnub
19
19
  PERIODIC_TIMER_INTERVAL = 0.25
20
20
  DEFAULT_TTL = 1440
21
21
  DEFAULT_REGION = '0'.freeze
22
+ DEFAULT_USE_RANDOM_IV = true
22
23
  DEFAULT_SSL = false
23
24
  REQUEST_MESSAGE_COUNT_THRESHOLD = 0
24
25
 
data/lib/pubnub/crypto.rb CHANGED
@@ -2,12 +2,12 @@
2
2
  module Pubnub
3
3
  # Internal Crypto class used for message encryption and decryption
4
4
  class Crypto
5
- def initialize(cipher_key)
5
+ def initialize(cipher_key, use_random_iv)
6
6
  @alg = 'AES-256-CBC'
7
7
  sha256_key = Digest::SHA256.hexdigest(cipher_key.to_s)
8
8
  @key = sha256_key.slice(0, 32)
9
-
10
- @iv = '0123456789012345'
9
+ @using_random_iv = use_random_iv
10
+ @iv = @using_random_iv == true ? random_iv : '0123456789012345'
11
11
  end
12
12
 
13
13
  def encrypt(message)
@@ -17,18 +17,26 @@ module Pubnub
17
17
  aes.iv = @iv
18
18
 
19
19
  json_message = message.to_json
20
- cipher = aes.update(json_message)
20
+ cipher = @using_random_iv == true ? @iv : ''
21
+ cipher << aes.update(json_message)
21
22
  cipher << aes.final
22
23
 
23
24
  Base64.strict_encode64(cipher)
24
25
  end
25
26
 
26
27
  def decrypt(cipher_text)
28
+ undecoded_text = Base64.decode64(cipher_text)
29
+ iv = @iv
30
+
31
+ if cipher_text.length > 16 && @using_random_iv == true
32
+ iv = undecoded_text.slice!(0..15)
33
+ end
34
+
27
35
  decode_cipher = OpenSSL::Cipher.new(@alg).decrypt
28
36
  decode_cipher.key = @key
29
- decode_cipher.iv = @iv
37
+ decode_cipher.iv = iv
30
38
 
31
- plain_text = decryption(cipher_text, decode_cipher)
39
+ plain_text = decryption(undecoded_text, decode_cipher)
32
40
  load_json(plain_text)
33
41
 
34
42
  Pubnub.logger.debug('Pubnub') { 'Finished decrypting' }
@@ -39,8 +47,7 @@ module Pubnub
39
47
  private
40
48
 
41
49
  def decryption(cipher_text, decode_cipher)
42
- undecoded_text = Base64.decode64(cipher_text)
43
- plain_text = decode_cipher.update(undecoded_text)
50
+ plain_text = decode_cipher.update(cipher_text)
44
51
  plain_text << decode_cipher.final
45
52
  rescue StandardError => e
46
53
  Pubnub.error('Pubnub') { "DECRYPTION ERROR #{e}" }
@@ -52,5 +59,12 @@ module Pubnub
52
59
  rescue JSON::ParserError
53
60
  JSON.load("[#{plain_text}]")[0]
54
61
  end
62
+
63
+ private
64
+
65
+ def random_iv
66
+ random_bytes = Random.new.bytes(16).unpack('NnnnnN')
67
+ format('%08x%04x%04x', *random_bytes)
68
+ end
55
69
  end
56
70
  end
data/lib/pubnub/event.rb CHANGED
@@ -157,7 +157,7 @@ module Pubnub
157
157
 
158
158
  def create_variables_from_options(options)
159
159
  variables = %w[channel channels message http_sync callback
160
- ssl cipher_key secret_key auth_key
160
+ ssl cipher_key random_iv secret_key auth_key
161
161
  publish_key subscribe_key timetoken
162
162
  open_timeout read_timeout idle_timeout heartbeat
163
163
  group action read write delete manage ttl presence start
@@ -197,6 +197,12 @@ module Pubnub
197
197
  ck.call(data)
198
198
  end
199
199
 
200
+ def compute_random_iv(data)
201
+ ck = @compute_random_iv || @random_iv || @app.env[:random_iv_selector] || @app.env[:random_iv].to_s
202
+ return ck unless ck.respond_to?(:call)
203
+ ck.call(data)
204
+ end
205
+
200
206
  def error_message(parsed_response)
201
207
  parsed_response['message']
202
208
  rescue StandardError
@@ -76,7 +76,8 @@ module Pubnub
76
76
 
77
77
  if (@cipher_key || @app.env[:cipher_key] || @cipher_key_selector || @app.env[:cipher_key_selector]) && messages
78
78
  cipher_key = compute_cipher_key(parsed_response)
79
- crypto = Crypto.new(cipher_key)
79
+ random_iv = compute_random_iv(parsed_response)
80
+ crypto = Crypto.new(cipher_key, random_iv)
80
81
  messages = messages.map { |message| decrypt_history(message, crypto) }
81
82
  end
82
83
 
@@ -27,7 +27,7 @@ module Pubnub
27
27
  Pubnub.logger.debug('Pubnub::Publish') { "Fired event #{self.class}" }
28
28
 
29
29
  if @compressed
30
- compressed_body = Formatter.format_message(@message, @cipher_key, false)
30
+ compressed_body = Formatter.format_message(@message, @cipher_key, @random_iv, false)
31
31
  response = send_request(compressed_body)
32
32
  else
33
33
  response = send_request
@@ -72,7 +72,7 @@ module Pubnub
72
72
  '0',
73
73
  Formatter.format_channel(@channel, true),
74
74
  '0',
75
- Formatter.format_message(@message, @cipher_key)
75
+ Formatter.format_message(@message, @cipher_key, @random_iv)
76
76
  ]
77
77
 
78
78
  rpath.pop if @compressed
@@ -42,7 +42,7 @@ module Pubnub
42
42
  { uuid: { id: member } }
43
43
  end
44
44
 
45
- body = Formatter.format_message({ delete: members }, @cipher_key, false)
45
+ body = Formatter.format_message({ delete: members }, "", @random_iv, false)
46
46
  response = send_request(body)
47
47
 
48
48
  envelopes = fire_callbacks(handle(response, uri))
@@ -17,7 +17,7 @@ module Pubnub
17
17
  def fire
18
18
  Pubnub.logger.debug('Pubnub::RemoveChannelMetadata') { "Fired event #{self.class}" }
19
19
 
20
- body = Formatter.format_message(@data, @cipher_key, false)
20
+ body = Formatter.format_message(@data, "", @random_iv, false)
21
21
  response = send_request(body)
22
22
 
23
23
  envelopes = fire_callbacks(handle(response, uri))
@@ -42,7 +42,7 @@ module Pubnub
42
42
  { channel: { id: membership } }
43
43
  end
44
44
 
45
- body = Formatter.format_message({ delete: memberships }, @cipher_key, false)
45
+ body = Formatter.format_message({ delete: memberships }, "", @random_iv, false)
46
46
  response = send_request(body)
47
47
 
48
48
  envelopes = fire_callbacks(handle(response, uri))
@@ -17,7 +17,7 @@ module Pubnub
17
17
  def fire
18
18
  Pubnub.logger.debug('Pubnub::RemoveUuidMetadata') { "Fired event #{self.class}" }
19
19
 
20
- body = Formatter.format_message(@data, @cipher_key, false)
20
+ body = Formatter.format_message(@data, "", @random_iv, false)
21
21
  response = send_request(body)
22
22
 
23
23
  envelopes = fire_callbacks(handle(response, uri))
@@ -45,7 +45,7 @@ module Pubnub
45
45
  member_object
46
46
  end
47
47
 
48
- body = Formatter.format_message({ set: members }, @cipher_key, false)
48
+ body = Formatter.format_message({ set: members }, "", @random_iv, false)
49
49
  response = send_request(body)
50
50
 
51
51
  envelopes = fire_callbacks(handle(response, uri))
@@ -27,7 +27,7 @@ module Pubnub
27
27
  def fire
28
28
  Pubnub.logger.debug('Pubnub::SetChannelMetadata') { "Fired event #{self.class}" }
29
29
 
30
- body = Formatter.format_message(@metadata, @cipher_key, false)
30
+ body = Formatter.format_message(@metadata, "", @random_iv, false)
31
31
  response = send_request(body)
32
32
 
33
33
  envelopes = fire_callbacks(handle(response, uri))
@@ -45,7 +45,7 @@ module Pubnub
45
45
  membership_object
46
46
  end
47
47
 
48
- body = Formatter.format_message({ set: memberships }, @cipher_key, false)
48
+ body = Formatter.format_message({ set: memberships }, "", @random_iv, false)
49
49
  response = send_request(body)
50
50
 
51
51
  envelopes = fire_callbacks(handle(response, uri))
@@ -28,7 +28,7 @@ module Pubnub
28
28
  def fire
29
29
  Pubnub.logger.debug('Pubnub::SetUuidMetadata') { "Fired event #{self.class}" }
30
30
 
31
- body = Formatter.format_message(@metadata, @cipher_key, false)
31
+ body = Formatter.format_message(@metadata, "", @random_iv, false)
32
32
  response = send_request(body)
33
33
 
34
34
  envelopes = fire_callbacks(handle(response, uri))
@@ -39,7 +39,7 @@ module Pubnub
39
39
  '0',
40
40
  Formatter.format_channel(@channel, true),
41
41
  '0',
42
- Formatter.format_message(@message, @cipher_key)
42
+ Formatter.format_message(@message, @cipher_key, @random_iv)
43
43
  ].join('/')
44
44
  end
45
45
 
@@ -42,9 +42,9 @@ module Pubnub
42
42
  end
43
43
 
44
44
  # Transforms message to json and encode it
45
- def format_message(message, cipher_key, uri_escape = true)
46
- if cipher_key
47
- pc = Pubnub::Crypto.new(cipher_key)
45
+ def format_message(message, cipher_key = "", use_random_iv = false, uri_escape = true)
46
+ if cipher_key && !cipher_key.empty?
47
+ pc = Pubnub::Crypto.new(cipher_key, use_random_iv)
48
48
  message = pc.encrypt(message).to_json
49
49
  message = Addressable::URI.escape(message) if uri_escape
50
50
  else
@@ -34,8 +34,10 @@ module Pubnub
34
34
 
35
35
  def decipher_payload(message)
36
36
  return message[:payload] if message[:channel].end_with?('-pnpres') || (@app.env[:cipher_key].nil? && @cipher_key.nil? && @cipher_key_selector.nil? && @env[:cipher_key_selector].nil?)
37
- cipher_key = compute_cipher_key(message.reject { |k, _v| k == :payload })
38
- crypto = Pubnub::Crypto.new(cipher_key)
37
+ data = message.reject { |k, _v| k == :payload }
38
+ cipher_key = compute_cipher_key(data)
39
+ random_iv = compute_random_iv(data)
40
+ crypto = Pubnub::Crypto.new(cipher_key, random_iv)
39
41
  JSON.parse(crypto.decrypt(message[:payload]), quirks_mode: true)
40
42
  rescue StandardError
41
43
  message[:payload]
@@ -1,4 +1,4 @@
1
1
  # Toplevel Pubnub module.
2
2
  module Pubnub
3
- VERSION = '4.5.0'.freeze
3
+ VERSION = '4.6.0'.freeze
4
4
  end
@@ -34,18 +34,20 @@ describe Pubnub::History do
34
34
  end
35
35
  end
36
36
 
37
- it "__channel___demo___count__10___start__nil___end__nil___reverse__false___http_sync__true___callback__nil___encrypted__true_" do
37
+ it "__channel___demo___count__10___start__nil___end__nil___reverse__false___http_sync__true___callback__nil___encrypted__true___random_iv__false_" do
38
38
  @pubnub = Pubnub.new(
39
39
  publish_key: "pub-a-mock-key",
40
40
  subscribe_key: "sub-a-mock-key",
41
41
  uuid: "ruby-test-uuid-client-one",
42
42
  auth_key: "ruby-test-auth-client-one",
43
43
  cipher_key: "super-secret-cipher-key",
44
+ random_iv: false
44
45
  )
45
46
  VCR.use_cassette("examples/history/no_includes__encrypted", :record => :none) do
46
- envelope = @pubnub.history(channel: :rubytest, count: 10, http_sync: true)
47
+ envelope = @pubnub.history(channel: :rubytest1, count: 10, http_sync: true)
47
48
  expect(envelope.is_a?(Pubnub::Envelope)).to eq true
48
49
  expect(envelope.error?).to eq false
50
+ expect(@pubnub.env[:random_iv]).to eq false
49
51
 
50
52
  expect(envelope.status[:code]).to eq(200)
51
53
  expect(envelope.status[:category]).to eq(:ack)
@@ -53,22 +55,24 @@ describe Pubnub::History do
53
55
 
54
56
  expect(envelope.result[:code]).to eq(200)
55
57
  expect(envelope.result[:operation]).to eq(:history)
56
- expect(envelope.result[:data]).to eq({:messages => [100], :end => 15965666789169854, :start => 15965666789169854})
58
+ expect(envelope.result[:data]).to eq({:messages => [100], :end => 16148147101366576, :start => 16148147101366576})
57
59
  end
58
60
  end
59
61
 
60
- it "__channel___demo___count__10___start__nil___end__nil___reverse__false___http_sync__true___callback__nil___include_meta__true___encrypted__true_" do
62
+ it "__channel___demo___count__10___start__nil___end__nil___reverse__false___http_sync__true___callback__nil___include_meta__true___encrypted__true___random_iv__false_" do
61
63
  @pubnub = Pubnub.new(
62
64
  publish_key: "pub-a-mock-key",
63
65
  subscribe_key: "sub-a-mock-key",
64
66
  uuid: "ruby-test-uuid-client-one",
65
67
  auth_key: "ruby-test-auth-client-one",
66
68
  cipher_key: "super-secret-cipher-key",
69
+ random_iv: false
67
70
  )
68
71
  VCR.use_cassette("examples/history/include_meta__encrypted", :record => :none) do
69
- envelope = @pubnub.history(channel: :rubytest, count: 10, include_meta: true, http_sync: true)
72
+ envelope = @pubnub.history(channel: :rubytest2, count: 10, include_meta: true, http_sync: true)
70
73
  expect(envelope.is_a?(Pubnub::Envelope)).to eq true
71
74
  expect(envelope.error?).to eq false
75
+ expect(@pubnub.env[:random_iv]).to eq false
72
76
 
73
77
  expect(envelope.status[:code]).to eq(200)
74
78
  expect(envelope.status[:category]).to eq(:ack)
@@ -76,22 +80,24 @@ describe Pubnub::History do
76
80
 
77
81
  expect(envelope.result[:code]).to eq(200)
78
82
  expect(envelope.result[:operation]).to eq(:history)
79
- expect(envelope.result[:data]).to eq({:messages => [{"message" => 100, "meta" => "", "timetoken" => 15965666789169854}], :end => 15965666789169854, :start => 15965666789169854})
83
+ expect(envelope.result[:data]).to eq({:messages => [{"message" => 100, "meta" => "", "timetoken" => 16148149695171741}], :end => 16148149695171741, :start => 16148149695171741})
80
84
  end
81
85
  end
82
86
 
83
- it "__channel___demo___count__10___start__nil___end__nil___reverse__false___http_sync__true___callback__nil___include_token__true___encrypted__true_" do
87
+ it "__channel___demo___count__10___start__nil___end__nil___reverse__false___http_sync__true___callback__nil___include_token__true___encrypted__true___random_iv__false_" do
84
88
  @pubnub = Pubnub.new(
85
89
  publish_key: "pub-a-mock-key",
86
90
  subscribe_key: "sub-a-mock-key",
87
91
  uuid: "ruby-test-uuid-client-one",
88
92
  auth_key: "ruby-test-auth-client-one",
89
93
  cipher_key: "super-secret-cipher-key",
94
+ random_iv: false
90
95
  )
91
96
  VCR.use_cassette("examples/history/include_token__encrypted", :record => :none) do
92
- envelope = @pubnub.history(channel: :rubytest, count: 10, include_token: true, http_sync: true)
97
+ envelope = @pubnub.history(channel: :rubytest3, count: 10, include_token: true, http_sync: true)
93
98
  expect(envelope.is_a?(Pubnub::Envelope)).to eq true
94
99
  expect(envelope.error?).to eq false
100
+ expect(@pubnub.env[:random_iv]).to eq false
95
101
 
96
102
  expect(envelope.status[:code]).to eq(200)
97
103
  expect(envelope.status[:category]).to eq(:ack)
@@ -99,7 +105,79 @@ describe Pubnub::History do
99
105
 
100
106
  expect(envelope.result[:code]).to eq(200)
101
107
  expect(envelope.result[:operation]).to eq(:history)
102
- expect(envelope.result[:data]).to eq({:messages => [{"message" => 100, "timetoken" => 15965666789169854}], :end => 15965666789169854, :start => 15965666789169854})
108
+ expect(envelope.result[:data]).to eq({:messages => [{"message" => 100, "timetoken" => 16148151241848372}], :end => 16148151241848372, :start => 16148151241848372})
109
+ end
110
+ end
111
+
112
+ it "__channel___demo___count__10___start__nil___end__nil___reverse__false___http_sync__true___callback__nil___encrypted__true___random_iv__true_" do
113
+ @pubnub = Pubnub.new(
114
+ publish_key: "pub-a-mock-key",
115
+ subscribe_key: "sub-a-mock-key",
116
+ uuid: "ruby-test-uuid-client-one",
117
+ auth_key: "ruby-test-auth-client-one",
118
+ cipher_key: "super-secret-cipher-key"
119
+ )
120
+ VCR.use_cassette("examples/history/no_includes__encrypted_random_iv", :record => :none) do
121
+ envelope = @pubnub.history(channel: :rubytest4, count: 10, http_sync: true)
122
+ expect(envelope.is_a?(Pubnub::Envelope)).to eq true
123
+ expect(envelope.error?).to eq false
124
+ expect(@pubnub.env[:random_iv]).to eq true
125
+
126
+ expect(envelope.status[:code]).to eq(200)
127
+ expect(envelope.status[:category]).to eq(:ack)
128
+ expect(envelope.status[:config]).to eq({:tls => false, :uuid => "ruby-test-uuid-client-one", :auth_key => "ruby-test-auth-client-one", :origin => "ps.pndsn.com"})
129
+
130
+ expect(envelope.result[:code]).to eq(200)
131
+ expect(envelope.result[:operation]).to eq(:history)
132
+ expect(envelope.result[:data]).to eq({:messages => [100], :end => 16148153795208310, :start => 16148153795208310})
133
+ end
134
+ end
135
+
136
+ it "__channel___demo___count__10___start__nil___end__nil___reverse__false___http_sync__true___callback__nil___include_meta__true___encrypted__true___random_iv__false_" do
137
+ @pubnub = Pubnub.new(
138
+ publish_key: "pub-a-mock-key",
139
+ subscribe_key: "sub-a-mock-key",
140
+ uuid: "ruby-test-uuid-client-one",
141
+ auth_key: "ruby-test-auth-client-one",
142
+ cipher_key: "super-secret-cipher-key"
143
+ )
144
+ VCR.use_cassette("examples/history/include_meta__encrypted_random_iv", :record => :none) do
145
+ envelope = @pubnub.history(channel: :rubytest5, count: 10, include_meta: true, http_sync: true)
146
+ expect(envelope.is_a?(Pubnub::Envelope)).to eq true
147
+ expect(envelope.error?).to eq false
148
+ expect(@pubnub.env[:random_iv]).to eq true
149
+
150
+ expect(envelope.status[:code]).to eq(200)
151
+ expect(envelope.status[:category]).to eq(:ack)
152
+ expect(envelope.status[:config]).to eq({:tls => false, :uuid => "ruby-test-uuid-client-one", :auth_key => "ruby-test-auth-client-one", :origin => "ps.pndsn.com"})
153
+
154
+ expect(envelope.result[:code]).to eq(200)
155
+ expect(envelope.result[:operation]).to eq(:history)
156
+ expect(envelope.result[:data]).to eq({:messages => [{"message" => 100, "meta" => "", "timetoken" => 16148154562721969}], :end => 16148154562721969, :start => 16148154562721969})
157
+ end
158
+ end
159
+
160
+ it "__channel___demo___count__10___start__nil___end__nil___reverse__false___http_sync__true___callback__nil___include_token__true___encrypted__true___random_iv__true_" do
161
+ @pubnub = Pubnub.new(
162
+ publish_key: "pub-a-mock-key",
163
+ subscribe_key: "sub-a-mock-key",
164
+ uuid: "ruby-test-uuid-client-one",
165
+ auth_key: "ruby-test-auth-client-one",
166
+ cipher_key: "super-secret-cipher-key"
167
+ )
168
+ VCR.use_cassette("examples/history/include_token__encrypted_random_iv", :record => :none) do
169
+ envelope = @pubnub.history(channel: :rubytest6, count: 10, include_token: true, http_sync: true)
170
+ expect(envelope.is_a?(Pubnub::Envelope)).to eq true
171
+ expect(envelope.error?).to eq false
172
+ expect(@pubnub.env[:random_iv]).to eq true
173
+
174
+ expect(envelope.status[:code]).to eq(200)
175
+ expect(envelope.status[:category]).to eq(:ack)
176
+ expect(envelope.status[:config]).to eq({:tls => false, :uuid => "ruby-test-uuid-client-one", :auth_key => "ruby-test-auth-client-one", :origin => "ps.pndsn.com"})
177
+
178
+ expect(envelope.result[:code]).to eq(200)
179
+ expect(envelope.result[:operation]).to eq(:history)
180
+ expect(envelope.result[:data]).to eq({:messages => [{"message" => 100, "timetoken" => 16148155532212500}], :end => 16148155532212500, :start => 16148155532212500})
103
181
  end
104
182
  end
105
183