Class: McAPI::Encryption::FieldLevelEncryption

Inherits:
Object
  • Object
show all
Defined in:
lib/mcapi/encryption/field_level_encryption.rb

Overview

Performs field level encryption on HTTP payloads.

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ FieldLevelEncryption

Create a new instance with the provided configuration

Parameters:

  • config (Object)

    Configuration object



18
19
20
21
22
23
24
# File 'lib/mcapi/encryption/field_level_encryption.rb', line 18

def initialize(config)
  @config = config
  @crypto = McAPI::Encryption::Crypto.new(config)
  @is_with_header = config['ivHeaderName'] && config['encryptedKeyHeaderName']
  @encryption_response_properties = [@config['ivFieldName'], @config['encryptedKeyFieldName'],
                                     @config['publicKeyFingerprintFieldName'], @config['oaepHashingAlgorithmFieldName']]
end

Instance Method Details

#decrypt(response) ⇒ Object

Decrypt part of the HTTP response using the given config

Parameters:

  • response (Object)

    object as obtained from the http client

Returns:

  • (Object)

    response object with decrypted fields



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mcapi/encryption/field_level_encryption.rb', line 62

def decrypt(response)
  response = JSON.parse(response)
  config = config?(response['request']['url'])
  if config
    if !@is_with_header
      config['toDecrypt'].each do |v|
        decrypt_with_body(v, response['body'])
      end
    else
      config['toDecrypt'].each do |v|
        elem = elem_from_path(v['obj'], response['body'])
        decrypt_with_header(v, elem, response) if elem[:node][v['element']]
      end
    end
  end
  JSON.generate(response)
end

#encrypt(endpoint, header, body) ⇒ Hash

Encrypt parts of a HTTP request using the given config

  • :header header with encrypted value (if configured with header)

  • :body encrypted body

Parameters:

  • endpoint (String)

    HTTP URL for the current call

  • header (Object)

    HTTP header

  • body (String, Hash)

    HTTP body

Returns:

  • (Hash)

    Hash with two keys:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mcapi/encryption/field_level_encryption.rb', line 37

def encrypt(endpoint, header, body)
  body = JSON.parse(body) if body.is_a?(String)
  config = config?(endpoint)
  if config
    if !@is_with_header
      config['toEncrypt'].each do |v|
        encrypt_with_body(v, body)
      end
    else
      enc_params = @crypto.new_encryption_params
      config['toEncrypt'].each do |v|
        body = encrypt_with_header(v, enc_params, header, body)
      end
    end
  end
  { header: header, body: body.json }
end