Class: SmartHealthCardsTestKit::Utils::JWS

Inherits:
Object
  • Object
show all
Extended by:
Encoding
Defined in:
lib/smart_health_cards_test_kit/utils/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



40
41
42
43
44
45
46
47
# File 'lib/smart_health_cards_test_kit/utils/jws.rb', line 40

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

#header ⇒ Object

Returns the value of attribute header.



36
37
38
# File 'lib/smart_health_cards_test_kit/utils/jws.rb', line 36

def header
  @header
end

#key ⇒ Object

Returns the value of attribute key.



34
35
36
# File 'lib/smart_health_cards_test_kit/utils/jws.rb', line 34

def key
  @key
end

#payload ⇒ Object

Returns the value of attribute payload.



34
35
36
# File 'lib/smart_health_cards_test_kit/utils/jws.rb', line 34

def payload
  @payload
end

#public_key ⇒ Object

Returns the value of attribute public_key.



34
35
36
# File 'lib/smart_health_cards_test_kit/utils/jws.rb', line 34

def public_key
  @public_key
end

#signature ⇒ String

The signature component of the card

Returns:

  • (String) —

    the unencoded signature

Raises:

  • (MissingPrivateKeyError)


89
90
91
92
93
94
95
# File 'lib/smart_health_cards_test_kit/utils/jws.rb', line 89

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) ⇒ SmartHealthCardsTestKit::Utils::JWS

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

Parameters:

Returns:



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/smart_health_cards_test_kit/utils/jws.rb', line 19

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

  unless jws.is_a?(SmartHealthCardsTestKit::Utils::JWS) || jws.is_a?(String)
    raise ArgumentError,
          'Expected either a SmartHealthCardsTestKit::Utils::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

#kid ⇒ String

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

Returns:

  • (String)


51
52
53
# File 'lib/smart_health_cards_test_kit/utils/jws.rb', line 51

def kid
  header['kid']
end

#to_s ⇒ String

Export the card to a JWS String

Returns:

  • (String) —

    the JWS



99
100
101
# File 'lib/smart_health_cards_test_kit/utils/jws.rb', line 99

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

#verify ⇒ Object

Verify the digital signature on the jws

@return [Boolean]

Raises:

  • (MissingPublicKeyError)


106
107
108
109
110
# File 'lib/smart_health_cards_test_kit/utils/jws.rb', line 106

def verify
  raise MissingPublicKeyError unless public_key

  public_key.verify(jws_signing_input, signature)
end