Class: Evervault::Utils::ValidationUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/evervault/utils/validation_utils.rb

Class Method Summary collapse

Class Method Details

.validate_app_uuid_and_api_key(app_uuid, api_key) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/evervault/utils/validation_utils.rb', line 8

def self.validate_app_uuid_and_api_key(app_uuid, api_key)
  if app_uuid.nil?
    raise Evervault::Errors::EvervaultError,
          'No App ID provided. The App ID can be retrieved in the Evervault dashboard (App Settings).'
  end
  if api_key.nil?
    raise Evervault::Errors::EvervaultError,
          'The provided App ID is invalid. The App ID can be retrieved in the Evervault dashboard (App Settings).'
  end
  return unless api_key.start_with?('ev:key')

  # Scoped API key
  app_uuid_hash = Digest::SHA512.base64digest(app_uuid)[0, 6]
  app_uuid_hash_from_api_key = api_key.split(':')[4]
  return unless app_uuid_hash != app_uuid_hash_from_api_key

  raise Evervault::Errors::EvervaultError,
        "The API key is not valid for app #{app_uuid}. Make sure to use an API key belonging to the ap'\
  'p #{app_uuid}."
end