Class: Webpush::VapidKey
- Inherits:
-
Object
- Object
- Webpush::VapidKey
- Defined in:
- lib/webpush/vapid_key.rb
Overview
Class for abstracting the generation and encoding of elliptic curve public and private keys for use with the VAPID protocol
Instance Attribute Summary collapse
-
#:curve ⇒ OpenSSL::PKey::EC
readonly
the OpenSSL elliptic curve instance.
-
#curve ⇒ Object
readonly
Returns the value of attribute curve.
Class Method Summary collapse
-
.from_keys(public_key, private_key) ⇒ Webpush::VapidKey
Create a VapidKey instance from encoded elliptic curve public and private keys.
-
.from_pem(pem) ⇒ Webpush::VapidKey
Create a VapidKey instance from pem encoded elliptic curve public and private keys.
Instance Method Summary collapse
- #curve_name ⇒ Object
- #group ⇒ Object
-
#initialize ⇒ VapidKey
constructor
A new instance of VapidKey.
- #inspect ⇒ Object
-
#private_key ⇒ String
Retrive the encoded elliptic curve private key for VAPID protocol.
- #private_key=(key) ⇒ Object
-
#public_key ⇒ String
Retrieve the encoded elliptic curve public key for VAPID protocol.
- #public_key=(key) ⇒ Object
-
#public_key_for_push_header ⇒ String
Retrieve the encoded elliptic curve public key suitable for the Web Push request.
- #to_h ⇒ Object (also: #to_hash)
- #to_pem ⇒ Object
Constructor Details
#initialize ⇒ VapidKey
Returns a new instance of VapidKey.
31 32 33 34 |
# File 'lib/webpush/vapid_key.rb', line 31 def initialize @curve = OpenSSL::PKey::EC.new('prime256v1') @curve.generate_key end |
Instance Attribute Details
#:curve ⇒ OpenSSL::PKey::EC (readonly)
the OpenSSL elliptic curve instance
5 6 7 |
# File 'lib/webpush/vapid_key.rb', line 5
def :curve
@:curve
end
|
#curve ⇒ Object (readonly)
Returns the value of attribute curve.
29 30 31 |
# File 'lib/webpush/vapid_key.rb', line 29 def curve @curve end |
Class Method Details
.from_keys(public_key, private_key) ⇒ Webpush::VapidKey
Create a VapidKey instance from encoded elliptic curve public and private keys
9 10 11 12 13 14 15 |
# File 'lib/webpush/vapid_key.rb', line 9 def self.from_keys(public_key, private_key) key = new key.public_key = public_key key.private_key = private_key key end |
.from_pem(pem) ⇒ Webpush::VapidKey
Create a VapidKey instance from pem encoded elliptic curve public and private keys
20 21 22 23 24 25 26 27 |
# File 'lib/webpush/vapid_key.rb', line 20 def self.from_pem(pem) key = new src = OpenSSL::PKey.read pem key.curve.public_key = src.public_key key.curve.private_key = src.private_key key end |
Instance Method Details
#curve_name ⇒ Object
65 66 67 |
# File 'lib/webpush/vapid_key.rb', line 65 def curve_name group.curve_name end |
#group ⇒ Object
69 70 71 |
# File 'lib/webpush/vapid_key.rb', line 69 def group curve.group end |
#inspect ⇒ Object
85 86 87 |
# File 'lib/webpush/vapid_key.rb', line 85 def inspect "#<#{self.class}:#{object_id.to_s(16)} #{to_h.map { |k, v| ":#{k}=#{v}" }.join(" ")}>" end |
#private_key ⇒ String
Retrive the encoded elliptic curve private key for VAPID protocol
53 54 55 |
# File 'lib/webpush/vapid_key.rb', line 53 def private_key encode64(curve.private_key.to_s(2)) end |
#private_key=(key) ⇒ Object
61 62 63 |
# File 'lib/webpush/vapid_key.rb', line 61 def private_key=(key) curve.private_key = to_big_num(key) end |
#public_key ⇒ String
Retrieve the encoded elliptic curve public key for VAPID protocol
39 40 41 |
# File 'lib/webpush/vapid_key.rb', line 39 def public_key encode64(curve.public_key.to_bn.to_s(2)) end |
#public_key=(key) ⇒ Object
57 58 59 |
# File 'lib/webpush/vapid_key.rb', line 57 def public_key=(key) curve.public_key = OpenSSL::PKey::EC::Point.new(group, to_big_num(key)) end |
#public_key_for_push_header ⇒ String
Retrieve the encoded elliptic curve public key suitable for the Web Push request
46 47 48 |
# File 'lib/webpush/vapid_key.rb', line 46 def public_key_for_push_header trim_encode64(curve.public_key.to_bn.to_s(2)) end |
#to_h ⇒ Object Also known as: to_hash
73 74 75 |
# File 'lib/webpush/vapid_key.rb', line 73 def to_h { public_key: public_key, private_key: private_key } end |
#to_pem ⇒ Object
78 79 80 81 82 83 |
# File 'lib/webpush/vapid_key.rb', line 78 def to_pem public_key = OpenSSL::PKey::EC.new curve public_key.private_key = nil curve.to_pem + public_key.to_pem end |