Skip to content

Commit

Permalink
Fix vapid key to_pem and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
collimarco committed Nov 26, 2022
1 parent ef1ebb3 commit 20f40d9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ env:
- CC_TEST_REPORTER_ID=155202524386dfebe0c3267a5c868b5417ff4cc2cde8ed301fb36b177d46a458
language: ruby
rvm:
- 2.2
- 2.3
- 2.4
- 2.5
Expand Down
11 changes: 8 additions & 3 deletions lib/web_push/vapid_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions spec/web_push/vapid_key_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion web-push.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 20f40d9

Please sign in to comment.