Class: SmartHealthCardsTestKit::Utils::KeySet

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/smart_health_cards_test_kit/utils/key_set.rb

Overview

A set of keys used for signing or verifying HealthCards

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keys = nil) ⇒ KeySet

Create a new KeySet

Parameters:



26
27
28
29
# File 'lib/smart_health_cards_test_kit/utils/key_set.rb', line 26

def initialize(keys = nil)
  @key_map = {}
  add_keys(keys) unless keys.nil?
end

Class Method Details

.from_jwks(jwks) ⇒ SmartHealthCardsTestKit::Utils::KeySet

Create a KeySet from a JWKS

Parameters:

  • jwks (String) —

    the JWKS as a string

Returns:



17
18
19
20
21
# File 'lib/smart_health_cards_test_kit/utils/key_set.rb', line 17

def self.from_jwks(jwks)
  jwks = JSON.parse(jwks)
  keys = jwks['keys'].map { |jwk| SmartHealthCardsTestKit::Utils::Key.from_jwk(jwk) }
  KeySet.new(keys)
end

Instance Method Details

#add_keys(new_keys) ⇒ Object

Add keys to KeySet

Keys are added based on the key kid



58
59
60
61
62
63
64
# File 'lib/smart_health_cards_test_kit/utils/key_set.rb', line 58

def add_keys(new_keys)
  if new_keys.is_a? KeySet
    add_keys(new_keys.keys)
  else
    [*new_keys].each { |new_key| @key_map[new_key.kid] = new_key }
  end
end

#find_key(kid) ⇒ Payload::Key

Retrieves a key from the keyset with a kid that matches the parameter

Parameters:

  • kid (String) —

    a Base64 encoded kid from a JWS or Key

Returns:

  • (Payload::Key) —

    a key with a matching kid or nil if not found



49
50
51
# File 'lib/smart_health_cards_test_kit/utils/key_set.rb', line 49

def find_key(kid)
  @key_map[kid]
end

#include?(key) ⇒ Boolean

Check if key is included in the KeySet

Parameters:

Returns:

  • (Boolean)


83
84
85
# File 'lib/smart_health_cards_test_kit/utils/key_set.rb', line 83

def include?(key)
  !@key_map[key.kid].nil?
end

#keys ⇒ Array

The contained keys

Returns:

  • (Array)


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

def keys
  @key_map.values
end

#remove_keys(removed_keys) ⇒ Object

Remove keys from KeySet

Keys are remove based on the key kid



71
72
73
74
75
76
77
# File 'lib/smart_health_cards_test_kit/utils/key_set.rb', line 71

def remove_keys(removed_keys)
  if removed_keys.is_a? KeySet
    remove_keys(removed_keys.keys)
  else
    [*removed_keys].each { |removed_key| @key_map.delete(removed_key.kid) }
  end
end

#to_jwk ⇒ Object

Returns the keys as a JWK

Returns:

  • JSON string in JWK format



41
42
43
# File 'lib/smart_health_cards_test_kit/utils/key_set.rb', line 41

def to_jwk
  { keys: keys.map(&:to_jwk) }.to_json
end