Class: MastercardCoreSdk::Util::JweUtil

Inherits:
Object
  • Object
show all
Extended by:
Converters, Core, Exceptions
Defined in:
lib/mastercard_core_sdk/util/jwe_util.rb

Overview

JweUtil class provides the utility to decrypt encrypted payload object.

Constant Summary collapse

@@logger =
Logging.logger[self]

Class Method Summary collapse

Class Method Details

.decrypt_and_verify(encrypted_jwe_payload, signature, private_key, public_key, response_type) ⇒ Object

Decrypt encrypted payload, verify signature and convert it to response type.

Parameters:

  • encrypted_jwe_payload (String)

    the encrypted payload input.

  • signature (String)

    the signature to be verified.

  • private_key (OpenSSL::PKey::RSA)

    the private key for decrypting encrypted payload.

  • public_key (OpenSSL::PKey::RSA)

    the Masterpass public key to verify signature.

  • response_type

    the response type for conversion after decryption and verification of signature.

Returns:

  • (Object)

    the decrypted payload converted as per the response_type.

Raises:

  • (SDKConversionError)

    if error on converting the decrypted payload into response type.



61
62
63
64
65
# File 'lib/mastercard_core_sdk/util/jwe_util.rb', line 61

def decrypt_and_verify(encrypted_jwe_payload, signature, private_key, public_key, response_type) 
  decrypted_payload = jwe_decrypt_payload(encrypted_jwe_payload, private_key)
  raise SDKValidationError.new(ERR_MSG_VERIFY_SIGNATURE) if !verify_signature(decrypted_payload, signature, public_key)
  return convert_to_response_type(decrypted_payload, response_type)          
end

.get_jwe_decrypted_payload(encrypted_jwe_payload, private_key, response_type) ⇒ Object

Decrypt the encrypted payload and converts it to response type.

Parameters:

  • encrypted_jwe_payload (String)

    the encrypted payload input.

  • private_key (OpenSSL::PKey::RSA)

    the private key for decrypting encrypted payload.

  • response_type

    the response type for conversion after decryption.

Returns:

  • (Object)

    the decrypted payload converted as per the response_type.

Raises:

  • (SDKConversionError)

    if decrypted payload could not be converted into response_type.



25
26
27
28
# File 'lib/mastercard_core_sdk/util/jwe_util.rb', line 25

def get_jwe_decrypted_payload(encrypted_jwe_payload, private_key, response_type) 
  decrypted_payload = jwe_decrypt_payload(encrypted_jwe_payload, private_key)
  return convert_to_response_type(decrypted_payload, response_type)
end

.jwe_decrypt_payload(encrypted_jwe_payload, private_key) ⇒ Object

Decrypt the encrypted payload with the private key.

Parameters:

  • encrypted_jwe_payload (String)

    the encrypted payload input.

  • private_key (OpenSSL::PKey::RSA)

    the private key for decrypting encrypted payload.

Raises:

  • (SDKValidationError)

    if encrypted_jwe_payload, private_key is not provided or decryption fails.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mastercard_core_sdk/util/jwe_util.rb', line 34

def jwe_decrypt_payload(encrypted_jwe_payload, private_key)
  if(encrypted_jwe_payload.nil?)
     @@logger.error EMPTY_JWE_PAYLOAD_ERR
     raise SDKValidationError.new(EMPTY_JWE_PAYLOAD_ERR) 
  end
      
  if(private_key.nil?)
     @@logger.error EMPTY_JWE_PRIVATE_KEY_ERR
     raise SDKValidationError.new(EMPTY_JWE_PRIVATE_KEY_ERR) 
  end
  
  begin
     return JWE.decrypt(encrypted_jwe_payload, private_key)
  rescue StandardError => error
     @@logger.error error.message
     raise SDKValidationError.new(ERR_MSG_DECRYPTION) 
  end
end

.verify_signature(decrypted_payload, signature, public_key) ⇒ Boolean

Verify the signature, with the digest, an instance of OpenSSL::Digest, provided to re-compute the message digest of the original data.

Parameters:

  • decrypted_payload (String)

    the payload decrypted with JWE.

  • signature (String)

    the signature to be verified.

  • public_key (OpenSSL::PKey::RSA)

    the Masterpass public key to verify signature.

Returns:

  • (Boolean)

    true if signature is valid, false otherwise.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/mastercard_core_sdk/util/jwe_util.rb', line 72

def verify_signature(decrypted_payload, signature, public_key)
  if(decrypted_payload.nil?)
     @@logger.error EMPTY_JWE_DECRYPT_PAYLOAD_ERR
     raise SDKValidationError.new(EMPTY_JWE_DECRYPT_PAYLOAD_ERR) 
  end
  
  if(signature.nil?)
     @@logger.error EMPTY_SIGNATURE_ERR
     raise SDKValidationError.new(EMPTY_SIGNATURE_ERR) 
  end
  
  if(public_key.nil?)
     @@logger.error EMPTY_PUBLIC_KEY_ERR
     raise SDKValidationError.new(EMPTY_PUBLIC_KEY_ERR) 
  end
  
  digest = OpenSSL::Digest::SHA256.new
  return public_key.verify(digest, Base64.decode64(signature), decrypted_payload)
end