Class: Eligible::Lockbox

Inherits:
APIResource show all
Defined in:
lib/eligible/lockbox.rb

Instance Attribute Summary

Attributes inherited from EligibleObject

#api_key, #eligible_id

Class Method Summary collapse

Methods inherited from APIResource

api_url, class_name, endpoint_name, require_param, required_param_validation, rest_api_params, send_request, url

Methods inherited from EligibleObject

#[], #[]=, construct_from, #each, #error, #initialize, #keys, #refresh_from, #to_hash, #to_json, #values

Constructor Details

This class inherits a constructor from Eligible::EligibleObject

Class Method Details

.all(params, opts = {}) ⇒ Object



10
11
12
# File 'lib/eligible/lockbox.rb', line 10

def self.all(params, opts = {})
  send_request :get, api_url('lockboxes'), params, **opts
end

.decrypt_data(data, encrypted_data_key, private_key) ⇒ Object



25
26
27
28
29
30
# File 'lib/eligible/lockbox.rb', line 25

def self.decrypt_data(data, encrypted_data_key, private_key)
  pkey = OpenSSL::PKey::RSA.new(private_key)
  aes_key = pkey.private_decrypt(Base64.decode64(encrypted_data_key))
  sha_key = Digest::SHA256.hexdigest(aes_key)
  Encryptor.decrypt(value: Base64.decode64(data), key: sha_key, insecure_mode: true)
end

.delete_private_key!(params) ⇒ Object



20
21
22
23
# File 'lib/eligible/lockbox.rb', line 20

def self.delete_private_key!(params)
  params.delete('private_key')
  params.delete(:private_key)
end

.extract_private_key(params) ⇒ Object



14
15
16
17
18
# File 'lib/eligible/lockbox.rb', line 14

def self.extract_private_key(params)
  private_key = Util.value(params, :private_key)
  fail ArgumentError, "Private key is required for decryption" if private_key.nil?
  private_key
end

.get(params, opts = {}) ⇒ Object



6
7
8
# File 'lib/eligible/lockbox.rb', line 6

def self.get(params, opts = {})
  send_request :get, api_url('lockboxes', params, :lockbox_id), params, **opts.merge(required_params: [:lockbox_id])
end

.get_and_decrypt_from_lockbox(params, opts = {}) ⇒ Object



32
33
34
35
36
37
# File 'lib/eligible/lockbox.rb', line 32

def self.get_and_decrypt_from_lockbox(params, opts = {})
  private_key = extract_private_key(params)
  delete_private_key!(params)
  req = get(params, **opts).to_hash
  decrypt_data(req[:encrypted_data], req[:encrypted_key], private_key)
end