class Prevoty::Client

The Client is used to call specific methods that represent the various endpoints of the Prevoty API.

Attributes

api_key[RW]
base[RW]

Public Class Methods

new(api_key=nil, base=nil) click to toggle source

Create a new Client @param api_key [String] the api_key @param base [String] the base url to request @return [Client] a new Client

# File lib/prevoty/client.rb, line 16
def initialize(api_key=nil, base=nil)
  @api_key = api_key
  @base = base ||= 'https://api.prevoty.com'
end

Public Instance Methods

analyze_query(query, config_key) click to toggle source

Analyze an SQL query @param query [String] query to analyze @param config_key [String] configuration to analyze with @return [QueryAnalysis] analysis of the query

# File lib/prevoty/client.rb, line 240
def analyze_query(query, config_key)
  params = {api_key: @api_key, query: query, config_key: config_key}

  response = HTTParty.post("#{@base}/1/query/parse", query: params)
  case response.code
  when 200 then return QueryAnalysis.new(JSON.parse(response.body))
  when 400 then raise BadInputParameter
  when 403 then raise BadAPIKey
  when 500 then raise InternalError
  when 507 then raise AccountQuotaExceeded
  else raise Exception
  end
end
api_key_info() click to toggle source

Get information about the current api key @return [APIKeyInfo] api key info

# File lib/prevoty/client.rb, line 38
def api_key_info
  params = {api_key: @api_key}

  response = HTTParty.get("#{@base}/1/key/info", query: params)
  case response.code
  when 200 then APIKeyInfo.new(JSON.parse(response.body))
  when 400 then raise BadInputParameter
  when 403 then raise BadAPIKey
  when 500 then raise InternalError
  else raise Exception
  end
end
bulk_filter(input, configuration_key) click to toggle source

Filter a full query string from a request @param input [String] query string to filter @param configuration_key [String] configuration to filter content with @return [FilterContent] filtered response from the api

# File lib/prevoty/client.rb, line 90
def bulk_filter(input, configuration_key)
  params = {api_key: @api_key, rule_key: configuration_key, input: input}

  response = HTTParty.post("#{@base}/1/xss/bulkfilter", query: params)
  case response.code
  when 200 then return FilterContent.new(JSON.parse(response.body))
  when 400 then raise BadInputParameter
  when 403 then raise BadAPIKey
  when 413 then raise RequestTooLarge
  when 500 then raise InternalError
  when 507 then raise AccountQuotaExceeded
  else raise Exception
  end
end
decrypt(result) click to toggle source

Decrypt encrypted data

@param result [EncryptResult] data to decrypt @return [DecryptResult] decrypted data

# File lib/prevoty/client.rb, line 334
def decrypt(result)
  params = {api_key: @api_key, obj: result.to_json}

  response = HTTParty.post("#{@base}/1/crypto/decrypt", query: params)
  case response.code
  when 200 then return DecryptResult.new(JSON.parse(response.body))
  when 400 then raise BadInputParameter
  when 403 then raise BadAPIKey
  when 500 then raise InternalError
  when 507 then raise AccountQuotaExceeded
  else raise Exception
  end
end
delete_persisted_token(user_identifier, action, token) click to toggle source

Delete a persisted @param user_identifier [String] unique identifier for a user (eg. session id) @param action [String] action the token is being deleted for @param token [String] token to delete @return [DeleteToken] result of deleting the token

# File lib/prevoty/client.rb, line 222
def delete_persisted_token(user_identifier, action, token)
  params = {api_key: @api_key, user_identifier: user_identifier, action: action, token: token}

  response = HTTParty.get("#{@base}/1/token/persisted/delete", query: params)
  case response.code
  when 200 then return DeleteToken.new(JSON.parse(response.body))
  when 400 then raise BadInputParameter
  when 403 then raise BadAPIKey
  when 500 then raise InternalError
  when 507 then raise AccountQuotaExceeded
  else raise Exception
  end
end
delete_timed_token(user_identifier, action, token) click to toggle source

Delete a timed token @param user_identifier [String] unique identifier for a user (eg. session id) @param action [String] action the token is being deleted for @param token [String] token to delete @return [DeleteToken] result of deleting the token

# File lib/prevoty/client.rb, line 166
def delete_timed_token(user_identifier, action, token)
  params = {api_key: @api_key, user_identifier: user_identifier, action: action, token: token}

  response = HTTParty.get("#{@base}/1/token/timed/delete", query: params)
  case response.code
  when 200 then return DeleteToken.new(JSON.parse(response.body))
  when 400 then raise BadInputParameter
  when 403 then raise BadAPIKey
  when 500 then raise InternalError
  when 507 then raise AccountQuotaExceeded
  else raise Exception
  end
end
ecdsa_signature(payload, func, private_key) click to toggle source

Sign data using ECDSA

@param payload [String] the data to sign @param func [Integer] the hash function to sign with. Constants can be found in {Prevoty::Hash} @param private_key [ECDSAPrivateKey] private key to sign with @return [RSASignature] signature of data

# File lib/prevoty/client.rb, line 413
def ecdsa_signature(payload, func, private_key)
  params = {api_key: @api_key, algorithm: Prevoty::Crypto::KeyAlgorithms::ECDSA, hash: func, key: private_key.to_json, payload: payload}
  return call_ecdsa_signature(params)
end
encrypt(input, algorithm, mode) click to toggle source

Encrypt input with a specified algorithm

@param input [String] into to encrypt @param algorithm [Integer] algorithm to encrypt with. Constants are specified in {Prevoty::Crypto::Algorithms} @param mode [Integer] mode to use. Constants are specified in {Prevoty::Crypto::Modes} @return [EncryptResult] encrypted data

# File lib/prevoty/client.rb, line 316
def encrypt(input, algorithm, mode)
  params = {api_key: @api_key, payload: input, algorithm: algorithm, mode: mode}

  response = HTTParty.post("#{@base}/1/crypto/encrypt", query: params)
  case response.code
  when 200 then return EncryptResult.new(JSON.parse(response.body))
  when 400 then raise BadInputParameter
  when 403 then raise BadAPIKey
  when 500 then raise InternalError
  when 507 then raise AccountQuotaExceeded
  else raise Exception
  end
end
filter_content(input, configuration_key) click to toggle source

Filter content through the prevoty engine @param input [String] content to be filtered @param configuration_key [String] configuration to filter the content with @return [FilterContent] filtered response from the api

# File lib/prevoty/client.rb, line 71
def filter_content(input, configuration_key)
  params = {api_key: @api_key, rule_key: configuration_key, input: input}

  response = HTTParty.post("#{@base}/1/xss/filter", query: params)
  case response.code
  when 200 then return FilterContent.new(JSON.parse(response.body))
  when 400 then raise BadInputParameter
  when 403 then raise BadAPIKey
  when 413 then raise RequestTooLarge
  when 500 then raise InternalError
  when 507 then raise AccountQuotaExceeded
  else raise Exception
  end
end
generate_ecdsa_keypair(curve) click to toggle source

Generate a keypair using ECDSA

@param curve [Integer] curve to use. Constants exist in {Prevoty::Crypto::Curves} @return [ECDSAPrivateKey] private key

# File lib/prevoty/client.rb, line 370
def generate_ecdsa_keypair(curve)
  params = {api_key: @api_key, algorithm: Prevoty::Crypto::KeyAlgorithms::ECDSA, meta: curve}

  response = HTTParty.post("#{@base}/1/crypto/genkeypair", query: params)
  case response.code
  when 200 then return ECDSAPrivateKey.new(JSON.parse(response.body))
  when 400 then raise BadInputParameter
  when 403 then raise BadAPIKey
  when 500 then raise InternalError
  when 507 then raise AccountQuotaExceeded
  else raise Exception
  end
end
generate_persisted_token(user_identifier, action) click to toggle source

Generate a persisted token @param user_identifier [String] unique identifier for a user (eg. session id) @param action [String] action the token is being generated for @return [GenerateToken] generated token

# File lib/prevoty/client.rb, line 184
def generate_persisted_token(user_identifier, action)
  params = {api_key: @api_key, user_identifier: user_identifier, action: action}

  response = HTTParty.get("#{@base}/1/token/persisted/generate", query: params)
  case response.code
  when 200 then return GenerateToken.new(JSON.parse(response.body))
  when 400 then raise BadInputParameter
  when 403 then raise BadAPIKey
  when 500 then raise InternalError
  when 507 then raise AccountQuotaExceeded
  else raise Exception
  end
end
generate_rsa_keypair(keysize) click to toggle source

Generate a keypair using RSA

@param keysize [Integer] number of bits for the keysize @return [RSAPrivateKey] private key

# File lib/prevoty/client.rb, line 352
def generate_rsa_keypair(keysize)
  params = {api_key: @api_key, algorithm: Prevoty::Crypto::KeyAlgorithms::RSA_PKCS, meta: keysize}

  response = HTTParty.post("#{@base}/1/crypto/genkeypair", query: params)
  case response.code
  when 200 then return RSAPrivateKey.new(JSON.parse(response.body))
  when 400 then raise BadInputParameter
  when 403 then raise BadAPIKey
  when 500 then raise InternalError
  when 507 then raise AccountQuotaExceeded
  else raise Exception
  end
end
generate_timed_token(user_identifier, action, ttl) click to toggle source

Generate a timed CSRF token @param user_identifier [String] unique identifier for a user (eg. session id) @param action [String] action the token is being generated for @param ttl [Integer] time in seconds the token is valid for (min: 0, max: 86400) @return [GenerateToken] generated token

# File lib/prevoty/client.rb, line 128
def generate_timed_token(user_identifier, action, ttl)
  params = {api_key: @api_key, user_identifier: user_identifier, action: action, ttl: ttl}

  response = HTTParty.get("#{@base}/1/token/timed/generate", query: params)
  case response.code
  when 200 then return GenerateToken.new(JSON.parse(response.body))
  when 400 then raise BadInputParameter
  when 403 then raise BadAPIKey
  when 500 then raise InternalError
  when 507 then raise AccountQuotaExceeded
  else raise Exception
  end
end
hash(input, function) click to toggle source

Hash input with a specified algorithm

@note Constants for hash functions are specified in {Prevoty::Hash} @param input [String] input to hash @param function [Integer] hash function to use @return [HashResult] hashed input

# File lib/prevoty/client.rb, line 296
def hash(input, function)
  params = {api_key: @api_key, payload: input, function: function}

  response = HTTParty.post("#{@base}/1/crypto/hash", query: params)
  case response.code
  when 200 then return HashResult.new(JSON.parse(response.body))
  when 400 then raise BadInputParameter
  when 403 then raise BadAPIKey
  when 500 then raise InternalError
  when 507 then raise AccountQuotaExceeded
  else raise Exception
  end
end
monitor_content(input) click to toggle source

Monitor content @param input [Array] content to perform analysis on @return [Array] array of content that has been analyzed

# File lib/prevoty/client.rb, line 108
def monitor_content(input)
  params = {api_key: @api_key, input: JSON.dump(input)}

  response = HTTParty.post("#{@base}/1/xss/monitor", body: params)
  case response.code
  when 200 then return JSON.parse(response.body).map {|record| MonitorContent.new(record)}
  when 400 then raise BadInputParameter
  when 403 then raise BadAPIKey
  when 413 then raise RequestTooLarge
  when 500 then raise InternalError
  when 507 then raise AccountQuotaExceeded
  else raise Exception
  end
end
monitor_query(query) click to toggle source

Monitor an SQL query @param query [Array] array of queries to monitor @return [Array] array of analysis results

# File lib/prevoty/client.rb, line 257
def monitor_query(query)
  params = {api_key: @api_key, inputs: JSON.dump(query)}

  response = HTTParty.post("#{@base}/1/query/monitor", body: params)
  case response.code
  when 200 then return JSON.parse(response.body).map {|record| MonitorQuery.new(record)}
  when 400 then raise BadInputParameter
  when 403 then raise BadAPIKey
  when 413 then raise RequestTooLarge
  when 500 then raise InternalError
  when 507 then raise AccountQuotaExceeded
  else raise Exception
  end
end
rsa_pkcs_signature(payload, func, private_key) click to toggle source

Sign data using RSA PKCS

@param payload [String] the data to sign @param func [Integer] the hash function to sign with. Constants can be found in {Prevoty::Hash} @param private_key [RSAPrivateKey] private key to sign with @return [RSASignature] signature of data

# File lib/prevoty/client.rb, line 402
def rsa_pkcs_signature(payload, func, private_key)
  params = {api_key: @api_key, algorithm: Prevoty::Crypto::KeyAlgorithms::RSA_PKCS, hash: func, key: private_key.to_json, payload: payload}
  return call_rsa_signature(params)
end
rsa_pss_signature(payload, func, private_key, options) click to toggle source

Sign data using RSA PSS

@param payload [String] the data to sign @param func [Integer] the hash function to sign with. Constants can be found in {Prevoty::Hash} @param private_key [RSAPrivateKey] private key to sign with @param options [Integer] RSA PSS options. Constants can be found in {Prevoty::Crypto::PSSSaltOptions} @return [RSASignature] signature of data

# File lib/prevoty/client.rb, line 391
def rsa_pss_signature(payload, func, private_key, options)
  params = {api_key: @api_key, algorithm: Prevoty::Crypto::KeyAlgorithms::RSA_PSS, hash: func, key: private_key.to_json, payload: payload, opt: options}
  return call_rsa_signature(params)
end
validate_pattern(pattern, input) click to toggle source

Validate input with a pattern @param pattern [Integer, String] pattern to validate with. For built-ins constants can be found in {Prevoty::Pattern} @param input [String] input to validate @return [InputValidation] pattern validation result

# File lib/prevoty/client.rb, line 276
def validate_pattern(pattern, input)
  params = {api_key: @api_key, input: input}

  response = HTTParty.get("#{@base}/1/pattern/#{pattern}", query: params)
  case response.code
  when 200 then return InputValidation.new(JSON.parse(response.body))
  when 400 then raise BadInputParameter
  when 403 then raise BadAPIKey
  when 500 then raise InternalError
  when 507 then raise AccountQuotaExceeded
  else raise Exception
  end
end
validate_persisted_token(user_identifier, action, token) click to toggle source

Validate a persisted token @param user_identifier [String] unique identifier for a user (eg. session id) @param action [String] action the token is being validated for @param token [String] token to be validated @return [ValidateToken] result of validating

# File lib/prevoty/client.rb, line 203
def validate_persisted_token(user_identifier, action, token)
  params = {api_key: @api_key, user_identifier: user_identifier, action: action, token: token}

  response = HTTParty.get("#{@base}/1/token/persisted/validate", query: params)
  case response.code
  when 200 then return ValidateToken.new(JSON.parse(response.body))
  when 400 then raise BadInputParameter
  when 403 then raise BadAPIKey
  when 500 then raise InternalError
  when 507 then raise AccountQuotaExceeded
  else raise Exception
  end
end
validate_timed_token(user_identifier, action, token) click to toggle source

Validate a timed CSRF token @param user_identifier [String] unique identifier for a user (eg. session id) @param action [String] action the token is being validated for @param token [String] token to be validated @return [ValidateToken] result of validating

# File lib/prevoty/client.rb, line 147
def validate_timed_token(user_identifier, action, token)
  params = {api_key: @api_key, user_identifier: user_identifier, action: action, token: token}

  response = HTTParty.get("#{@base}/1/token/timed/validate", query: params)
  case response.code
  when 200 then return ValidateToken.new(JSON.parse(response.body))
  when 400 then raise BadInputParameter
  when 403 then raise BadAPIKey
  when 500 then raise InternalError
  when 507 then raise AccountQuotaExceeded
  else raise Exception
  end
end
verify_api_key() click to toggle source

Verify that the supplied API key is valid @return [true, false]

# File lib/prevoty/client.rb, line 23
def verify_api_key
  params = {api_key: @api_key}

  response = HTTParty.get("#{@base}/1/key/verify", query: params)
  case response.code
  when 200 then return true
  when 400 then raise BadInputParameter
  when 403 then raise BadAPIKey
  when 500 then raise InternalError
  else false
  end
end
verify_content_configuration(configuration_key) click to toggle source

Verify that the specified content configuration key is valid @param configuration_key [String] content configuration key @return [true, false]

# File lib/prevoty/client.rb, line 54
def verify_content_configuration(configuration_key)
  params = {api_key: @api_key, rule_key: configuration_key}

  response = HTTParty.get("#{@base}/1/rule/verify", query: params)
  case response.code
  when 200 then return true
  when 400 then raise BadInputParameter
  when 403 then raise BadAPIKey
  when 500 then raise InternalError
  else raise Exception
  end
end
verify_ecdsa_signature(payload, func, public_key, signature) click to toggle source

Verify ECDSA signature

@param payload [String] the data to verify @param func [Integer] the hash function to sign with. Constants can be found in {Prevoty::Hash} @param public_key [ECDSAPublicKey] public key to verify signature with @param signature [String] signature to verify @return [SignatureVerify] signature of data

# File lib/prevoty/client.rb, line 450
def verify_ecdsa_signature(payload, func, public_key, signature)
  params = {api_key: @api_key, algorithm: Prevoty::Crypto::KeyAlgorithms::ECDSA, hash: func, key: public_key.to_json, sig: signature.to_json, payload: payload}
  return call_verify_signature(params)
end
verify_rsa_pkcs_signature(payload, func, public_key, signature) click to toggle source

Verify RSA PSS signature

@param payload [String] the data to verify @param func [Integer] the hash function to sign with. Constants can be found in {Prevoty::Hash} @param public_key [RSAPublicKey] public key to verify signature with @param signature [String] signature to verify @return [SignatureVerify] signature of data

# File lib/prevoty/client.rb, line 438
def verify_rsa_pkcs_signature(payload, func, public_key, signature)
  params = {api_key: @api_key, algorithm: Prevoty::Crypto::KeyAlgorithms::RSA_PKCS, hash: func, key: public_key.to_json, sig: signature.to_json, payload: payload}
  return call_verify_signature(params)
end
verify_rsa_pss_signature(payload, func, public_key, signature, options) click to toggle source

Verify RSA PSS signature

@param payload [String] the data to verify @param func [Integer] the hash function to sign with. Constants can be found in {Prevoty::Hash} @param public_key [RSAPublicKey] public key to verify signature with @param signature [String] signature to verify @param options [Integer] RSA PSS options. Constants can be found in {Prevoty::Crypto::PSSSaltOptions} @return [SignatureVerify] signature of data

# File lib/prevoty/client.rb, line 426
def verify_rsa_pss_signature(payload, func, public_key, signature, options)
  params = {api_key: @api_key, algorithm: Prevoty::Crypto::KeyAlgorithms::RSA_PSS, hash: func, key: public_key.to_json, sig: signature.to_json, payload: payload, opt: options}
  return call_verify_signature(params)
end

Private Instance Methods

call_ecdsa_signature(params) click to toggle source
# File lib/prevoty/client.rb, line 468
def call_ecdsa_signature(params)
  response = HTTParty.post("#{@base}/1/crypto/sign", query: params)
  case response.code
  when 200 then return ECDSASignature.new(JSON.parse(response.body))
  when 400 then raise BadInputParameter
  when 403 then raise BadAPIKey
  when 500 then raise InternalError
  when 507 then raise AccountQuotaExceeded
  else raise Exception
  end
end
call_rsa_signature(params) click to toggle source
# File lib/prevoty/client.rb, line 456
def call_rsa_signature(params)
  response = HTTParty.post("#{@base}/1/crypto/sign", query: params)
  case response.code
  when 200 then return RSASignature.new(JSON.parse(response.body))
  when 400 then raise BadInputParameter
  when 403 then raise BadAPIKey
  when 500 then raise InternalError
  when 507 then raise AccountQuotaExceeded
  else raise Exception
  end
end
call_verify_signature(params) click to toggle source
# File lib/prevoty/client.rb, line 480
def call_verify_signature(params)
  response = HTTParty.post("#{@base}/1/crypto/verify", query: params)
  case response.code
  when 200 then return SignatureVerify.new(JSON.parse(response.body))
  when 400 then raise BadInputParameter
  when 403 then raise BadAPIKey
  when 500 then raise InternalError
  when 507 then raise AccountQuotaExceeded
  else raise Exception
  end
end