class Auric::Vault::Door

Constants

PRODUCTIONURLS
SANDBOXURLS
VERSION

Attributes

config_id[RW]
error[R]
mtid[RW]
production[RW]
secret[RW]
segment[RW]
success[R]

Public Class Methods

new(args) click to toggle source
# File lib/auric/vault/door.rb, line 17
def initialize(args)
  required_args = [:secret, :mtid, :config_id]
  required_args.each do |arg|
    raise ArgumentError, "Required argument: (#{arg}) not provided" unless args.include?(arg)
  end
  @secret = args[:secret]
  @mtid = args[:mtid]
  @config_id = args[:config_id]
  @production = args[:production] || false
  @segment = args[:segment]
  @success = false
  @error = nil
  if @production
    @url = PRODUCTIONURLS
  else
    @url = SANDBOXURLS
  end
end

Public Instance Methods

decrypt(token) click to toggle source
# File lib/auric/vault/door.rb, line 46
def decrypt(token)
  json = get_data('decrypt', token)
  if @success
    return json['result']['plaintextValue']
  else
    @error = json['error']
    raise DecryptionError, @error
  end
end
encrypt(data) click to toggle source
# File lib/auric/vault/door.rb, line 36
def encrypt(data)
  json = post_data('encrypt', data)
  if @success
    return json['result']['token']
  else
    @error = json['error']
    raise EncryptionError, @error
  end
end

Private Instance Methods

build_get_message(method, token) click to toggle source
# File lib/auric/vault/door.rb, line 74
def build_get_message(method, token)
  {
    'params'=>
    [{
      'mtid'=> @mtid,
      'configurationId'=> @config_id,
      'utcTimestamp'=> Time.now.to_i.to_s,
      'token'=> token
    }],
    'method'=> method
  }
end
build_post_message(method, plaintext_value) click to toggle source
# File lib/auric/vault/door.rb, line 58
def build_post_message(method, plaintext_value)
  {
    'params'=>
    [{
      'mtid'=> @mtid,
      'configurationId'=> @config_id,
      'utcTimestamp'=> Time.now.to_i.to_s,
      'retention'=> 'big-year',
      'segment'=> @segment,
      'last4'=> '',
      'plaintextValue'=> plaintext_value
    }],
    'method'=> method
  }
end
call_auric(method, data) click to toggle source
# File lib/auric/vault/door.rb, line 92
def call_auric(method, data)
  signature = figure_hexdigest_for_auth(data)
  begin
  HTTParty.post(
    @url[0],
    {
      :body => data.to_json,
      headers: { 'X-VAULT-HMAC' => signature }
    }
  )
  rescue
    HTTParty.post(
      @url[1],
      {
        :body => data.to_json,
        headers: { 'X-VAULT-HMAC' => signature }
      }
    )
  end
end
figure_hexdigest_for_auth(message_body) click to toggle source
# File lib/auric/vault/door.rb, line 87
def figure_hexdigest_for_auth(message_body)
  digest = OpenSSL::Digest.new('sha512')
  OpenSSL::HMAC.hexdigest(digest, @secret, message_body.to_json)
end
get_data(method, data) click to toggle source
# File lib/auric/vault/door.rb, line 125
def get_data(method, data)
  message_body = build_get_message(method, data)
  response = call_auric(method, message_body)
  json_response = JSON.parse(response.parsed_response)
  if json_response['result']['lastActionSucceeded'] == 1
    @success = true
  else
    @success = false
  end
  json_response
end
post_data(method, data) click to toggle source
# File lib/auric/vault/door.rb, line 113
def post_data(method, data)
  message_body = build_post_message(method, data)
  response = call_auric(method, message_body)
  json_response = JSON.parse(response.parsed_response)
  if json_response['result']['lastActionSucceeded'] == 1
    @success = true
  else
    @success = false
  end
  json_response
end