From 20f40d9586b524b8c75e64887683931d6e11b078 Mon Sep 17 00:00:00 2001 From: Marco Colli Date: Sat, 26 Nov 2022 19:51:54 +0100 Subject: [PATCH] Fix vapid key to_pem and add tests --- .travis.yml | 1 - lib/web_push/vapid_key.rb | 11 ++++++++--- spec/web_push/vapid_key_spec.rb | 29 +++++++++++++++++++++++++++++ web-push.gemspec | 3 ++- 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4722497..7e4abce 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,6 @@ env: - CC_TEST_REPORTER_ID=155202524386dfebe0c3267a5c868b5417ff4cc2cde8ed301fb36b177d46a458 language: ruby rvm: - - 2.2 - 2.3 - 2.4 - 2.5 diff --git a/lib/web_push/vapid_key.rb b/lib/web_push/vapid_key.rb index 74ebf7d..c69f7aa 100644 --- a/lib/web_push/vapid_key.rb +++ b/lib/web_push/vapid_key.rb @@ -78,10 +78,15 @@ def to_h alias to_hash to_h def to_pem - public_key = OpenSSL::PKey::EC.new curve - public_key.private_key = nil + private_key_to_pem + public_key_to_pem + end + + def private_key_to_pem + curve.to_pem + end - curve.to_pem + public_key.to_pem + def public_key_to_pem + curve.public_to_pem end def inspect diff --git a/spec/web_push/vapid_key_spec.rb b/spec/web_push/vapid_key_spec.rb index 27be96b..a968e9a 100644 --- a/spec/web_push/vapid_key_spec.rb +++ b/spec/web_push/vapid_key_spec.rb @@ -43,6 +43,35 @@ expect(pem).to include('-----BEGIN PUBLIC KEY-----') end + it 'returns the correct public and private keys in pem format' do + public_key_base64 = 'BMA-wciFTkEq2waVGB2hg8cSyiRiMcsIvIYQb3LkLOmBheh3YC6NB2GtE9t6YgaXt428rp7bC9JjuPtAY9AQaR8=' + private_key_base64 = '4MwLvN1Cpxe43AV9fa4BiS-SPp51gWlhv9c6bb_XSJ4=' + key = WebPush::VapidKey.from_keys(public_key_base64, private_key_base64) + pem = key.to_pem + expected_pem = <<~PEM + -----BEGIN EC PRIVATE KEY----- + MHcCAQEEIODMC7zdQqcXuNwFfX2uAYkvkj6edYFpYb/XOm2/10ieoAoGCCqGSM49 + AwEHoUQDQgAEwD7ByIVOQSrbBpUYHaGDxxLKJGIxywi8hhBvcuQs6YGF6HdgLo0H + Ya0T23piBpe3jbyuntsL0mO4+0Bj0BBpHw== + -----END EC PRIVATE KEY----- + -----BEGIN PUBLIC KEY----- + MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEwD7ByIVOQSrbBpUYHaGDxxLKJGIx + ywi8hhBvcuQs6YGF6HdgLo0HYa0T23piBpe3jbyuntsL0mO4+0Bj0BBpHw== + -----END PUBLIC KEY----- + PEM + expect(pem).to eq(expected_pem) + end + + it 'can return the private key in pem format' do + pem = WebPush::VapidKey.new.private_key_to_pem + expect(pem).to include('-----BEGIN EC PRIVATE KEY-----') + end + + it 'can return the public key in pem format' do + pem = WebPush::VapidKey.new.public_key_to_pem + expect(pem).to include('-----BEGIN PUBLIC KEY-----') + end + it 'imports pem of public and private keys' do pem = WebPush::VapidKey.new.to_pem key = WebPush::VapidKey.from_pem pem diff --git a/web-push.gemspec b/web-push.gemspec index 8eec2f8..5d00211 100644 --- a/web-push.gemspec +++ b/web-push.gemspec @@ -11,10 +11,11 @@ Gem::Specification.new do |spec| spec.files = `git ls-files`.split("\n") - spec.required_ruby_version = '>= 2.2' + spec.required_ruby_version = '>= 2.3' spec.add_dependency 'hkdf', '~> 0.2' spec.add_dependency 'jwt', '~> 2.0' + spec.add_dependency 'openssl', '~> 2.2' spec.add_development_dependency 'bundler', '>= 1.17.3' spec.add_development_dependency 'pry'