class HealthCards::KeySet

A set of keys used for signing or verifying HealthCards

Public Class Methods

from_jwks(jwks) click to toggle source

Create a KeySet from a JWKS

@param jwks [String] the JWKS as a string @return [HealthCards::KeySet]

# File lib/health_cards/key_set.rb, line 16
def self.from_jwks(jwks)
  jwks = JSON.parse(jwks)
  keys = jwks['keys'].map { |jwk| HealthCards::Key.from_jwk(jwk) }
  KeySet.new(keys)
end
new(keys = nil) click to toggle source

Create a new KeySet

@param keys [HealthCards::Key, Array<HealthCards::Key>, nil] the initial keys

# File lib/health_cards/key_set.rb, line 25
def initialize(keys = nil)
  @key_map = {}
  add_keys(keys) unless keys.nil?
end

Public Instance Methods

add_keys(new_keys) click to toggle source

Add keys to KeySet

Keys are added based on the key kid

@param new_keys [HealthCards::Key, Array<HealthCards::Key>, HealthCards::KeySet] the initial keys

# File lib/health_cards/key_set.rb, line 57
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) click to toggle source

Retrieves a key from the keyset with a kid that matches the parameter @param kid [String] a Base64 encoded kid from a JWS or Key @return [HealthCard::Key] a key with a matching kid or nil if not found

# File lib/health_cards/key_set.rb, line 48
def find_key(kid)
  @key_map[kid]
end
include?(key) click to toggle source

Check if key is included in the KeySet

@param key [HealthCards::Key] @return [Boolean]

# File lib/health_cards/key_set.rb, line 82
def include?(key)
  !@key_map[key.kid].nil?
end
keys() click to toggle source

The contained keys

@return [Array]

# File lib/health_cards/key_set.rb, line 33
def keys
  @key_map.values
end
remove_keys(removed_keys) click to toggle source

Remove keys from KeySet

Keys are remove based on the key kid

@param new_keys [HealthCards::Key, Array<HealthCards::Key>, HealthCards::KeySet] the initial keys

# File lib/health_cards/key_set.rb, line 70
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() click to toggle source

Returns the keys as a JWK

@return JSON string in JWK format

# File lib/health_cards/key_set.rb, line 40
def to_jwk
  { keys: keys.map(&:to_jwk) }.to_json
end