Skip to content

Commit

Permalink
Add yardoc comments for VapidKey class and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
rossta committed Oct 14, 2016
1 parent 783209f commit 8a92191
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
6 changes: 4 additions & 2 deletions lib/webpush.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ def payload_send(message: "", endpoint:, p256dh: "", auth: "", vapid: {}, **opti
).perform
end

# public_key: vapid_key.public_key.to_bn.to_s(2)
# private_key: vapid_key.private_key.to_s(2)
# Generate a VapidKey instance to obtain base64 encoded public and private keys
# suitable for VAPID protocol JSON web token signing
#
# @return [Webpush::VapidKey] a new VapidKey instance
def generate_key
VapidKey.new
end
Expand Down
27 changes: 18 additions & 9 deletions lib/webpush/vapid_key.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
module Webpush
# Class for abstracting the generation and encoding of elliptic curve public and private keys for use with the VAPID protocol
#
# @attr_reader [OpenSSL::PKey::EC] :curve the OpenSSL elliptic curve instance
class VapidKey
# Create a VapidKey instance from encoded elliptic curve public and private keys
#
# @return [Webpush::VapidKey] a VapidKey instance for the given public and private keys
def self.from_keys(public_key, private_key)
key = new
key.public_key = public_key
Expand All @@ -15,30 +21,33 @@ def initialize
@curve.generate_key
end

# Retrieve the encoded EC public key for server-side storage
# @return encoded binary representaion of 65-byte VAPID public key
# Retrieve the encoded elliptic curve public key for VAPID protocol
#
# @return [String] encoded binary representation of 65-byte VAPID public key
def public_key
encode64(curve.public_key.to_bn.to_s(2))
end

# Retrieve EC public key for Web Push
# @return the encoded VAPID public key suitable for Web Push transport
# Retrieve the encoded elliptic curve public key for 'Encryption' header
#
# @return [String] the encoded VAPID public key suitable for Web Push transport
def public_key_for_push_header
trim_encode64(curve.public_key.to_bn.to_s(2))
end

# Convenience
# @return base64 urlsafe-encoded binary representaion of 32-byte VAPID private key
# Retrive the encoded elliptic curve private key for VAPID protocol
#
# @return [String] base64 urlsafe-encoded binary representation of 32-byte VAPID private key
def private_key
Base64.urlsafe_encode64(curve.private_key.to_s(2))
encode64(curve.private_key.to_s(2))
end

def public_key=(key)
@curve.public_key = OpenSSL::PKey::EC::Point.new(group, to_big_num(key))
curve.public_key = OpenSSL::PKey::EC::Point.new(group, to_big_num(key))
end

def private_key=(key)
@curve.private_key = to_big_num(key)
curve.private_key = to_big_num(key)
end

def curve_name
Expand Down

0 comments on commit 8a92191

Please sign in to comment.