Class: SyrupPay::JwsSerializer

Inherits:
Object
  • Object
show all
Includes:
JwsSupportAlgorithm
Defined in:
lib/jose/jws/jws.rb

Defined Under Namespace

Classes: InvalidJwsFormatError, UnSupportHeaderError

Constant Summary

Constants included from JwsSupportAlgorithm

SyrupPay::JwsSupportAlgorithm::ALG

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from JwsSupportAlgorithm

#alg?, #json_to_hash, #signature_algorithm?

Constructor Details

#initialize(key) ⇒ JwsSerializer

Returns a new instance of JwsSerializer.



33
34
35
# File 'lib/jose/jws/jws.rb', line 33

def initialize(key)
  @key = key
end

Instance Attribute Details

#claimsObject (readonly)

Returns the value of attribute claims.



31
32
33
# File 'lib/jose/jws/jws.rb', line 31

def claims
  @claims
end

#headerObject

Returns the value of attribute header.



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

def header
  @header
end

#keyObject (readonly)

Returns the value of attribute key.



31
32
33
# File 'lib/jose/jws/jws.rb', line 31

def key
  @key
end

Instance Method Details

#compactDeserialize(serialized_input) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/jose/jws/jws.rb', line 49

def compactDeserialize(serialized_input)
  validate_deserialize! serialized_input

  header_json, @claims, sign_value = split_deserialize serialized_input
  @header = json_to_hash(header_json)

  jws_alg = signature_algorithm? @header[:alg].try(:to_sym)
  jws_alg.verify!(@key, hmac_data, UrlSafeBase64.encode64(sign_value))

  @claims
end

#compactSerialize(header = {}, claims) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/jose/jws/jws.rb', line 37

def compactSerialize(header = {}, claims)
  @claims = claims
  @header = header.with_indifferent_access

  validate_header!

  jws_alg = signature_algorithm? @header[:alg].try(:to_sym)
  sign_value = jws_alg.sign(@key, hmac_data)

  [@header.to_json, claims, sign_value].collect { |parts| UrlSafeBase64.encode64(parts)}.join('.')
end