Class: HealthCards::JWS
- Inherits:
-
Object
- Object
- HealthCards::JWS
- Extended by:
- Encoding
- Defined in:
- lib/health_cards/jws.rb
Overview
Create JWS from a payload
Instance Attribute Summary collapse
-
#header ⇒ Object
Returns the value of attribute header.
-
#key ⇒ Object
Returns the value of attribute key.
-
#payload ⇒ Object
Returns the value of attribute payload.
-
#public_key ⇒ Object
Returns the value of attribute public_key.
-
#signature ⇒ String
The signature component of the card.
Class Method Summary collapse
-
.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.
Instance Method Summary collapse
-
#initialize(header: nil, payload: nil, signature: nil, key: nil, public_key: nil) ⇒ JWS
constructor
Create a new JWS.
-
#kid ⇒ String
The kid value from the JWS header, used to identify the key to use to verify.
-
#to_s ⇒ String
Export the card to a JWS String.
-
#verify ⇒ Boolean
Verify the digital signature on the jws.
Methods included from Encoding
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
#header ⇒ Object
Returns the value of attribute header.
32 33 34 |
# File 'lib/health_cards/jws.rb', line 32 def header @header end |
#key ⇒ Object
Returns the value of attribute key.
30 31 32 |
# File 'lib/health_cards/jws.rb', line 30 def key @key end |
#payload ⇒ Object
Returns the value of attribute payload.
30 31 32 |
# File 'lib/health_cards/jws.rb', line 30 def payload @payload end |
#public_key ⇒ Object
Returns the value of attribute public_key.
30 31 32 |
# File 'lib/health_cards/jws.rb', line 30 def public_key @public_key end |
#signature ⇒ String
The signature component of the card
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
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
#kid ⇒ String
The kid value from the JWS header, used to identify the key to use to verify
47 48 49 |
# File 'lib/health_cards/jws.rb', line 47 def kid header['kid'] end |
#to_s ⇒ String
Export the card to a JWS String
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 |
#verify ⇒ Boolean
Verify the digital signature on the jws
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 |