Skip to content

Commit f19a52a

Browse files
committed
reuse shared secret logic
1 parent f412a61 commit f19a52a

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

lib/pusher/channel.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def shared_secret(encryption_master_key)
180180
secret_string = @name + encryption_master_key
181181
digest = OpenSSL::Digest::SHA256.new
182182
digest << secret_string
183-
Base64.strict_encode64(digest.digest)
183+
digest.digest
184184
end
185185

186186
private

lib/pusher/client.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,9 @@ def authenticate(channel_name, socket_id, custom_data = nil)
377377
channel_instance = channel(channel_name)
378378
r = channel_instance.authenticate(socket_id, custom_data)
379379
if channel_name.match(/^private-encrypted-/)
380-
r[:shared_secret] = channel_instance.shared_secret(encryption_master_key)
380+
r[:shared_secret] = Base64.strict_encode64(
381+
channel_instance.shared_secret(encryption_master_key)
382+
)
381383
end
382384
r
383385
end
@@ -466,15 +468,15 @@ def encode_data(data)
466468

467469
# Encrypts a message with a key derived from the master key and channel
468470
# name
469-
def encrypt(channel, encoded_data)
471+
def encrypt(channel_name, encoded_data)
470472
raise ConfigurationError, :encryption_master_key unless @encryption_master_key
471473

472474
# Only now load rbnacl, so that people that aren't using it don't need to
473475
# install libsodium
474476
require_rbnacl
475477

476478
secret_box = RbNaCl::SecretBox.new(
477-
RbNaCl::Hash.sha256(channel + @encryption_master_key)
479+
channel(channel_name).shared_secret(@encryption_master_key)
478480
)
479481

480482
nonce = RbNaCl::Random.random_bytes(secret_box.nonce_bytes)

spec/channel_spec.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ def authentication_string(*data)
176176
it 'should return a shared_secret based on the channel name and encryption master key' do
177177
key = '3W1pfB/Etr+ZIlfMWwZP3gz8jEeCt4s2pe6Vpr+2c3M='
178178
shared_secret = @channel.shared_secret(key)
179-
expect(shared_secret).to eq("6zeEp/chneRPS1cbK/hGeG860UhHomxSN6hTgzwT20I=")
179+
expect(Base64.strict_encode64(shared_secret)).to eq(
180+
"6zeEp/chneRPS1cbK/hGeG860UhHomxSN6hTgzwT20I="
181+
)
180182
end
181183

182184
it 'should return nil if missing encryption master key' do

spec/client_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@
179179
describe 'can set encryption_master_key_base64' do
180180
it "sets encryption_master_key" do
181181
@client.encryption_master_key_base64 =
182-
Base64.encode64(encryption_master_key)
182+
Base64.strict_encode64(encryption_master_key)
183183

184184
expect(@client.encryption_master_key).to eq(encryption_master_key)
185185
end
@@ -191,7 +191,7 @@
191191
@client.key = '12345678900000001'
192192
@client.secret = '12345678900000001'
193193
@client.encryption_master_key_base64 =
194-
Base64.encode64(encryption_master_key)
194+
Base64.strict_encode64(encryption_master_key)
195195
end
196196

197197
describe '#[]' do

0 commit comments

Comments
 (0)