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 Apr 16, 2021
1 parent 061ff9a commit f3fea74
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
11 changes: 8 additions & 3 deletions lib/webpush/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/webpush/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 webpush.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

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 f3fea74

Please sign in to comment.