Class: HealthCards::JWS

Inherits:
Object
  • Object
show all
Extended by:
Encoding
Defined in:
lib/health_cards/jws.rb

Overview

Create JWS from a payload

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Encoding

decode, encode

Constructor Details

#initialize(header: nil, payload: nil, signature: nil, key: nil, public_key: nil) ⇒ JWS

Create a new JWS



36
37
38
39
40
41
42
43
# File 'lib/health_cards/jws.rb', line 36

def initialize(header: nil, payload: nil, signature: nil, key: nil, public_key: nil)
  # Not using accessors because they reset the signature which requires both a key and a payload
  @header = header
  @payload = payload
  @signature = signature if signature
  @key = key
  @public_key = public_key || key&.public_key
end

Instance Attribute Details

#headerObject

Returns the value of attribute header.



32
33
34
# File 'lib/health_cards/jws.rb', line 32

def header
  @header
end

#keyObject

Returns the value of attribute key.



30
31
32
# File 'lib/health_cards/jws.rb', line 30

def key
  @key
end

#payloadObject

Returns the value of attribute payload.



30
31
32
# File 'lib/health_cards/jws.rb', line 30

def payload
  @payload
end

#public_keyObject

Returns the value of attribute public_key.



30
31
32
# File 'lib/health_cards/jws.rb', line 30

def public_key
  @public_key
end

#signatureString

The signature component of the card

Returns:

  • (String)

    the unencoded signature

Raises:



85
86
87
88
89
90
91
# File 'lib/health_cards/jws.rb', line 85

def signature
  return @signature if @signature

  raise MissingPrivateKeyError unless key

  @signature ||= key.sign(jws_signing_input)
end

Class Method Details

.from_jws(jws, public_key: nil, key: nil) ⇒ HealthCards::JWS

Creates a JWS from a String representation, or returns the HealthCards::JWS object that was passed in

Parameters:

Returns:



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/health_cards/jws.rb', line 15

def from_jws(jws, public_key: nil, key: nil)
  return jws if jws.is_a?(HealthCards::JWS) && public_key.nil? && key.nil?

  unless jws.is_a?(HealthCards::JWS) || jws.is_a?(String)
    raise ArgumentError,
          'Expected either a HealthCards::JWS or String'
  end

  header, payload, signature = jws.to_s.split('.').map { |entry| decode(entry) }
  header = JSON.parse(header)
  JWS.new(header: header, payload: payload, signature: signature,
          public_key: public_key, key: key)
end

Instance Method Details

#kidString

The kid value from the JWS header, used to identify the key to use to verify

Returns:

  • (String)


47
48
49
# File 'lib/health_cards/jws.rb', line 47

def kid
  header['kid']
end

#to_sString

Export the card to a JWS String

Returns:

  • (String)

    the JWS



95
96
97
# File 'lib/health_cards/jws.rb', line 95

def to_s
  [JSON.generate(header), payload, signature].map { |entry| JWS.encode(entry) }.join('.')
end

#verifyBoolean

Verify the digital signature on the jws

Returns:

  • (Boolean)

Raises:



102
103
104
105
106
# File 'lib/health_cards/jws.rb', line 102

def verify
  raise MissingPublicKeyError unless public_key

  public_key.verify(jws_signing_input, signature)
end