Class: SyrupPay::JweSerializer

Inherits:
Object
  • Object
show all
Includes:
JweSupportAlgorithm
Defined in:
lib/jose/jwe/jwe.rb

Defined Under Namespace

Classes: InvalidJweFormatError, UnSupportHeaderError

Constant Summary

Constants included from JweSupportAlgorithm

SyrupPay::JweSupportAlgorithm::ALG, SyrupPay::JweSupportAlgorithm::ENC

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from JweSupportAlgorithm

#alg?, #enc?, #encryption_algorithm?, #json_to_hash, #keywrap_algorithm?

Constructor Details

#initialize(key) ⇒ JweSerializer

Returns a new instance of JweSerializer.



48
49
50
# File 'lib/jose/jwe/jwe.rb', line 48

def initialize(key)
  @key = key
end

Instance Attribute Details

#cek=(value) ⇒ Object (writeonly)

Sets the attribute cek

Parameters:

  • value

    the value to set the attribute cek to.



46
47
48
# File 'lib/jose/jwe/jwe.rb', line 46

def cek=(value)
  @cek = value
end

#headerObject

Returns the value of attribute header.



44
45
46
# File 'lib/jose/jwe/jwe.rb', line 44

def header
  @header
end

#iv=(value) ⇒ Object (writeonly)

Sets the attribute iv

Parameters:

  • value

    the value to set the attribute iv to.



46
47
48
# File 'lib/jose/jwe/jwe.rb', line 46

def iv=(value)
  @iv = value
end

#keyObject (readonly)

Returns the value of attribute key.



45
46
47
# File 'lib/jose/jwe/jwe.rb', line 45

def key
  @key
end

#payloadObject (readonly)

Returns the value of attribute payload.



45
46
47
# File 'lib/jose/jwe/jwe.rb', line 45

def payload
  @payload
end

Instance Method Details

#compactDeserialize(serialized_input) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/jose/jwe/jwe.rb', line 73

def compactDeserialize(serialized_input)
  validate_deserialize! serialized_input
  header_json, wrapped_key, @iv, cipher_text, at = split_deserialize serialized_input
  @header = json_to_hash(header_json)

  jwe_alg = keywrap_algorithm? @header[:alg].try(:to_sym)
  jwe_enc = encryption_algorithm? @header[:enc].try(:to_sym)

  @cek = jwe_alg.decryption(@key, wrapped_key)

  aad = additional_authenticated_data
  jwe_enc.verify_and_decrypt(@cek, @iv, cipher_text, aad, UrlSafeBase64.encode64(at))
end

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/jose/jwe/jwe.rb', line 52

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

  validate_header!

  jwe_alg = keywrap_algorithm? @header[:alg].try(:to_sym)
  jwe_enc = encryption_algorithm? @header[:enc].try(:to_sym)

  cek_generator = jwe_enc.content_encryption_generator
  cek_generator.user_encryption_key = @cek
  @cek, wrapped_key = jwe_alg.encryption(@key, cek_generator)

  aad = additional_authenticated_data
  cipher_text, at, @iv = jwe_enc.encrypt_and_sign(@cek, @iv, @payload, aad)

  [@header.to_json, wrapped_key, @iv, cipher_text, at].collect do |parts|
    UrlSafeBase64.encode64(parts)
  end.join('.')
end