Skip to content

Commit

Permalink
reuse shared secret logic
Browse files Browse the repository at this point in the history
  • Loading branch information
callum-oakley committed Oct 20, 2020
1 parent f412a61 commit f19a52a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/pusher/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def shared_secret(encryption_master_key)
secret_string = @name + encryption_master_key
digest = OpenSSL::Digest::SHA256.new
digest << secret_string
Base64.strict_encode64(digest.digest)
digest.digest
end

private
Expand Down
8 changes: 5 additions & 3 deletions lib/pusher/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,9 @@ def authenticate(channel_name, socket_id, custom_data = nil)
channel_instance = channel(channel_name)
r = channel_instance.authenticate(socket_id, custom_data)
if channel_name.match(/^private-encrypted-/)
r[:shared_secret] = channel_instance.shared_secret(encryption_master_key)
r[:shared_secret] = Base64.strict_encode64(
channel_instance.shared_secret(encryption_master_key)
)
end
r
end
Expand Down Expand Up @@ -466,15 +468,15 @@ def encode_data(data)

# Encrypts a message with a key derived from the master key and channel
# name
def encrypt(channel, encoded_data)
def encrypt(channel_name, encoded_data)
raise ConfigurationError, :encryption_master_key unless @encryption_master_key

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

secret_box = RbNaCl::SecretBox.new(
RbNaCl::Hash.sha256(channel + @encryption_master_key)
channel(channel_name).shared_secret(@encryption_master_key)
)

nonce = RbNaCl::Random.random_bytes(secret_box.nonce_bytes)
Expand Down
4 changes: 3 additions & 1 deletion spec/channel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ def authentication_string(*data)
it 'should return a shared_secret based on the channel name and encryption master key' do
key = '3W1pfB/Etr+ZIlfMWwZP3gz8jEeCt4s2pe6Vpr+2c3M='
shared_secret = @channel.shared_secret(key)
expect(shared_secret).to eq("6zeEp/chneRPS1cbK/hGeG860UhHomxSN6hTgzwT20I=")
expect(Base64.strict_encode64(shared_secret)).to eq(
"6zeEp/chneRPS1cbK/hGeG860UhHomxSN6hTgzwT20I="
)
end

it 'should return nil if missing encryption master key' do
Expand Down
4 changes: 2 additions & 2 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@
describe 'can set encryption_master_key_base64' do
it "sets encryption_master_key" do
@client.encryption_master_key_base64 =
Base64.encode64(encryption_master_key)
Base64.strict_encode64(encryption_master_key)

expect(@client.encryption_master_key).to eq(encryption_master_key)
end
Expand All @@ -191,7 +191,7 @@
@client.key = '12345678900000001'
@client.secret = '12345678900000001'
@client.encryption_master_key_base64 =
Base64.encode64(encryption_master_key)
Base64.strict_encode64(encryption_master_key)
end

describe '#[]' do
Expand Down

0 comments on commit f19a52a

Please sign in to comment.