Class: McAPI::Encryption::JweEncryption

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

Overview

Performs JWE encryption on HTTP payloads.

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ JweEncryption

Create a new instance with the provided configuration

Parameters:

  • config (Hash)

    Configuration object



18
19
20
21
# File 'lib/mcapi/encryption/jwe_encryption.rb', line 18

def initialize(config)
  @config = config
  @crypto = McAPI::Encryption::JweCrypto.new(config)
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



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mcapi/encryption/jwe_encryption.rb', line 53

def decrypt(response)
  response = JSON.parse(response)
  config = McAPI::Utils.config?(response['request']['url'], @config)
  body_map = response
  if config
    body_map = config['toDecrypt'].map do |v|
      decrypt_with_body(v, response['body'])
    end
  end
  response['body'] = McAPI::Utils.compute_body(config['toDecrypt'], body_map) { response['body'] } unless config.nil?
  JSON.generate(response)
end

#encrypt(endpoint, 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|nil)

    HTTP header

  • body (String, Hash)

    HTTP body

Returns:

  • (Hash)

    Hash with two keys:



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mcapi/encryption/jwe_encryption.rb', line 34

def encrypt(endpoint, body)
  body = JSON.parse(body) if body.is_a?(String)
  config = McAPI::Utils.config?(endpoint, @config)
  body_map = body
  if config
    body_map = config['toEncrypt'].map do |v|
      encrypt_with_body(v, body)
    end
  end
  { body: config ? McAPI::Utils.compute_body(config['toEncrypt'], body_map) { body.json } : body.json }
end