Class: Webpush::VapidKey

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVapidKey

Returns a new instance of VapidKey.



33
34
35
36
# File 'lib/webpush/vapid_key.rb', line 33

def initialize
  @curve = OpenSSL::PKey::EC.new('prime256v1')
  @curve.generate_key
end

Instance Attribute Details

#:curveOpenSSL::PKey::EC (readonly)

the OpenSSL elliptic curve instance

Returns:

  • (OpenSSL::PKey::EC)

    the current value of :curve



7
8
9
# File 'lib/webpush/vapid_key.rb', line 7

def :curve
  @:curve
end

#curveObject (readonly)

Returns the value of attribute curve.



31
32
33
# File 'lib/webpush/vapid_key.rb', line 31

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

Returns:



11
12
13
14
15
16
17
# File 'lib/webpush/vapid_key.rb', line 11

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

Returns:



22
23
24
25
26
27
28
29
# File 'lib/webpush/vapid_key.rb', line 22

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_nameObject



67
68
69
# File 'lib/webpush/vapid_key.rb', line 67

def curve_name
  group.curve_name
end

#groupObject



71
72
73
# File 'lib/webpush/vapid_key.rb', line 71

def group
  curve.group
end

#inspectObject



87
88
89
# File 'lib/webpush/vapid_key.rb', line 87

def inspect
  "#<#{self.class}:#{object_id.to_s(16)} #{to_h.map { |k, v| ":#{k}=#{v}" }.join(' ')}>"
end

#private_keyString

Retrive the encoded elliptic curve private key for VAPID protocol

Returns:

  • (String)

    base64 urlsafe-encoded binary representation of 32-byte VAPID private key



55
56
57
# File 'lib/webpush/vapid_key.rb', line 55

def private_key
  encode64(curve.private_key.to_s(2))
end

#private_key=(key) ⇒ Object



63
64
65
# File 'lib/webpush/vapid_key.rb', line 63

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

#public_keyString

Retrieve the encoded elliptic curve public key for VAPID protocol

Returns:

  • (String)

    encoded binary representation of 65-byte VAPID public key



41
42
43
# File 'lib/webpush/vapid_key.rb', line 41

def public_key
  encode64(curve.public_key.to_bn.to_s(2))
end

#public_key=(key) ⇒ Object



59
60
61
# File 'lib/webpush/vapid_key.rb', line 59

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

#public_key_for_push_headerString

Retrieve the encoded elliptic curve public key suitable for the Web Push request

Returns:

  • (String)

    the encoded VAPID public key for us in ‘Encryption’ header



48
49
50
# File 'lib/webpush/vapid_key.rb', line 48

def public_key_for_push_header
  trim_encode64(curve.public_key.to_bn.to_s(2))
end

#to_hObject Also known as: to_hash



75
76
77
# File 'lib/webpush/vapid_key.rb', line 75

def to_h
  { public_key: public_key, private_key: private_key }
end

#to_pemObject



80
81
82
83
84
85
# File 'lib/webpush/vapid_key.rb', line 80

def to_pem
  public_key = OpenSSL::PKey::EC.new curve
  public_key.private_key = nil

  curve.to_pem + public_key.to_pem
end