Class: ActionPushWeb::VapidKey

Inherits:
Object
  • Object
show all
Defined in:
lib/action_push_web/vapid_key.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(public_key, private_key) ⇒ VapidKey

Returns a new instance of VapidKey.



5
6
7
8
# File 'lib/action_push_web/vapid_key.rb', line 5

def initialize(public_key, private_key)
  @public_key = public_key
  @private_key = private_key
end

Instance Attribute Details

#private_keyObject (readonly)

Returns the value of attribute private_key.



3
4
5
# File 'lib/action_push_web/vapid_key.rb', line 3

def private_key
  @private_key
end

#public_keyObject (readonly)

Returns the value of attribute public_key.



3
4
5
# File 'lib/action_push_web/vapid_key.rb', line 3

def public_key
  @public_key
end

Instance Method Details

#ec_keyObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/action_push_web/vapid_key.rb', line 15

def ec_key
  @ec_key ||= begin
    group      = OpenSSL::PKey::EC::Group.new("prime256v1")
    public_point  = OpenSSL::PKey::EC::Point.new(group, decode_base64url_to_big_number(public_key))
    priv_bn    = decode_base64url_to_big_number(private_key)

    asn1 = OpenSSL::ASN1::Sequence([
      OpenSSL::ASN1::Integer(1),
      OpenSSL::ASN1::OctetString(priv_bn.to_s(2)),
      OpenSSL::ASN1::ObjectId("prime256v1", 0, :EXPLICIT),
      OpenSSL::ASN1::BitString(public_point.to_octet_string(:uncompressed), 1, :EXPLICIT)
    ])

    OpenSSL::PKey::EC.new(asn1.to_der)
  end
end

#public_key_for_push_headerObject

For request header (unpadded)



11
12
13
# File 'lib/action_push_web/vapid_key.rb', line 11

def public_key_for_push_header
  public_key.delete("=")
end