class McAPI::Encryption::OpenAPIInterceptor

Service class that provide interceptor facilities for OpenApi swagger client

Public Class Methods

install_field_level_encryption(swagger_client, config) click to toggle source

Install the field level encryption in the OpenAPI HTTP client adding encryption/decryption capabilities for the request/response payload.

@param [Object] swagger_client OpenAPI service client (it can be generated by the swagger code generator) @param [Object] config configuration object describing which field to enable encryption/decryption

# File lib/mcapi/encryption/openapi_interceptor.rb, line 20
def install_field_level_encryption(swagger_client, config)
  fle = McAPI::Encryption::FieldLevelEncryption.new(config)
  # Hooking ApiClient#call_api
  hook_call_api fle
  # Hooking ApiClient#deserialize
  hook_deserialize fle
  McAPI::Encryption::OpenAPIInterceptor.init_call_api swagger_client
  McAPI::Encryption::OpenAPIInterceptor.init_deserialize swagger_client
end

Private Class Methods

hook_call_api(fle) click to toggle source
Calls superclass method
# File lib/mcapi/encryption/openapi_interceptor.rb, line 32
def hook_call_api(fle)
  self.class.send :define_method, :init_call_api do |client|
    client.define_singleton_method(:call_api) do |http_method, path, opts|
      if opts && opts[:body]
        encrypted = fle.encrypt(path, opts[:header_params], opts[:body])
        opts[:body] = JSON.generate(encrypted[:body])
      end
      # noinspection RubySuperCallWithoutSuperclassInspection
      super(http_method, path, opts)
    end
  end
end
hook_deserialize(fle) click to toggle source
Calls superclass method
# File lib/mcapi/encryption/openapi_interceptor.rb, line 45
def hook_deserialize(fle)
  self.class.send :define_method, :init_deserialize do |client|
    client.define_singleton_method(:deserialize) do |response, return_type|
      if response && response.body
        endpoint = response.request.base_url.sub client.config.base_url, ''
        to_decrypt = { headers: McAPI::Utils.parse_header(response.options[:response_headers]),
                       request: { url: endpoint },
                       body: JSON.parse(response.body) }
        decrypted = fle.decrypt(JSON.generate(to_decrypt, symbolize_names: false))
        body = JSON.generate(JSON.parse(decrypted)['body'])
        response.options[:response_body] = JSON.generate(JSON.parse(body))
      end
      # noinspection RubySuperCallWithoutSuperclassInspection
      super(response, return_type)
    end
  end
end