Class: Prevoty::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/prevoty/client.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil, base = nil) ⇒ Client

Create a new Client

Parameters:

  • api_key (String) (defaults to: nil)

    the api_key

  • base (String) (defaults to: nil)

    the base url to request



16
17
18
19
# 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

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



10
11
12
# File 'lib/prevoty/client.rb', line 10

def api_key
  @api_key
end

#baseObject

Returns the value of attribute base.



10
11
12
# File 'lib/prevoty/client.rb', line 10

def base
  @base
end

Instance Method Details

#analyze_query(query, config_key) ⇒ QueryAnalysis

Analyze an SQL query

Parameters:

  • query (String)

    query to analyze

  • config_key (String)

    configuration to analyze with

Returns:



240
241
242
243
244
245
246
247
248
249
250
251
252
# 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_infoAPIKeyInfo

Get information about the current api key

Returns:



38
39
40
41
42
43
44
45
46
47
48
49
# 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) ⇒ FilterContent

Filter a full query string from a request

Parameters:

  • input (String)

    query string to filter

  • configuration_key (String)

    configuration to filter content with

Returns:



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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) ⇒ DecryptResult

Decrypt encrypted data

Parameters:

Returns:



334
335
336
337
338
339
340
341
342
343
344
345
346
# 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) ⇒ DeleteToken

Delete a persisted

Parameters:

  • user_identifier (String)

    unique identifier for a user (eg. session id)

  • action (String)

    action the token is being deleted for

  • token (String)

    token to delete

Returns:



222
223
224
225
226
227
228
229
230
231
232
233
234
# 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) ⇒ DeleteToken

Delete a timed token

Parameters:

  • user_identifier (String)

    unique identifier for a user (eg. session id)

  • action (String)

    action the token is being deleted for

  • token (String)

    token to delete

Returns:



166
167
168
169
170
171
172
173
174
175
176
177
178
# 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) ⇒ RSASignature

Sign data using ECDSA

Parameters:

  • payload (String)

    the data to sign

  • func (Integer)

    the hash function to sign with. Constants can be found in Hash

  • private_key (ECDSAPrivateKey)

    private key to sign with

Returns:



413
414
415
416
# 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) ⇒ EncryptResult

Encrypt input with a specified algorithm

Parameters:

Returns:



316
317
318
319
320
321
322
323
324
325
326
327
328
# 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) ⇒ FilterContent

Filter content through the prevoty engine

Parameters:

  • input (String)

    content to be filtered

  • configuration_key (String)

    configuration to filter the content with

Returns:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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) ⇒ ECDSAPrivateKey

Generate a keypair using ECDSA

Parameters:

Returns:



370
371
372
373
374
375
376
377
378
379
380
381
382
# 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) ⇒ GenerateToken

Generate a persisted token

Parameters:

  • user_identifier (String)

    unique identifier for a user (eg. session id)

  • action (String)

    action the token is being generated for

Returns:



184
185
186
187
188
189
190
191
192
193
194
195
196
# 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) ⇒ RSAPrivateKey

Generate a keypair using RSA

Parameters:

  • keysize (Integer)

    number of bits for the keysize

Returns:



352
353
354
355
356
357
358
359
360
361
362
363
364
# 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) ⇒ GenerateToken

Generate a timed CSRF token

Parameters:

  • user_identifier (String)

    unique identifier for a user (eg. session id)

  • action (String)

    action the token is being generated for

  • ttl (Integer)

    time in seconds the token is valid for (min: 0, max: 86400)

Returns:



128
129
130
131
132
133
134
135
136
137
138
139
140
# 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) ⇒ HashResult

Note:

Constants for hash functions are specified in Hash

Hash input with a specified algorithm

Parameters:

  • input (String)

    input to hash

  • function (Integer)

    hash function to use

Returns:



296
297
298
299
300
301
302
303
304
305
306
307
308
# 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) ⇒ Array

Monitor content

Parameters:

  • input (Array)

    content to perform analysis on

Returns:

  • (Array)

    array of content that has been analyzed



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# 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) ⇒ Array

Monitor an SQL query

Parameters:

  • query (Array)

    array of queries to monitor

Returns:

  • (Array)

    array of analysis results



257
258
259
260
261
262
263
264
265
266
267
268
269
270
# 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) ⇒ RSASignature

Sign data using RSA PKCS

Parameters:

  • payload (String)

    the data to sign

  • func (Integer)

    the hash function to sign with. Constants can be found in Hash

  • private_key (RSAPrivateKey)

    private key to sign with

Returns:



402
403
404
405
# 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) ⇒ RSASignature

Sign data using RSA PSS

Parameters:

  • payload (String)

    the data to sign

  • func (Integer)

    the hash function to sign with. Constants can be found in Hash

  • private_key (RSAPrivateKey)

    private key to sign with

  • options (Integer)

    RSA PSS options. Constants can be found in Prevoty::Crypto::PSSSaltOptions

Returns:



391
392
393
394
# 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) ⇒ InputValidation

Validate input with a pattern

Parameters:

  • pattern (Integer, String)

    pattern to validate with. For built-ins constants can be found in Pattern

  • input (String)

    input to validate

Returns:



276
277
278
279
280
281
282
283
284
285
286
287
288
# 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) ⇒ ValidateToken

Validate a persisted token

Parameters:

  • user_identifier (String)

    unique identifier for a user (eg. session id)

  • action (String)

    action the token is being validated for

  • token (String)

    token to be validated

Returns:



203
204
205
206
207
208
209
210
211
212
213
214
215
# 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) ⇒ ValidateToken

Validate a timed CSRF token

Parameters:

  • user_identifier (String)

    unique identifier for a user (eg. session id)

  • action (String)

    action the token is being validated for

  • token (String)

    token to be validated

Returns:



147
148
149
150
151
152
153
154
155
156
157
158
159
# 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_keytrue, false

Verify that the supplied API key is valid

Returns:

  • (true, false)


23
24
25
26
27
28
29
30
31
32
33
34
# 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) ⇒ true, false

Verify that the specified content configuration key is valid

Parameters:

  • configuration_key (String)

    content configuration key

Returns:

  • (true, false)


54
55
56
57
58
59
60
61
62
63
64
65
# 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) ⇒ SignatureVerify

Verify ECDSA signature

Parameters:

  • payload (String)

    the data to verify

  • func (Integer)

    the hash function to sign with. Constants can be found in Hash

  • public_key (ECDSAPublicKey)

    public key to verify signature with

  • signature (String)

    signature to verify

Returns:



450
451
452
453
# 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) ⇒ SignatureVerify

Verify RSA PSS signature

Parameters:

  • payload (String)

    the data to verify

  • func (Integer)

    the hash function to sign with. Constants can be found in Hash

  • public_key (RSAPublicKey)

    public key to verify signature with

  • signature (String)

    signature to verify

Returns:



438
439
440
441
# 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) ⇒ SignatureVerify

Verify RSA PSS signature

Parameters:

  • payload (String)

    the data to verify

  • func (Integer)

    the hash function to sign with. Constants can be found in Hash

  • public_key (RSAPublicKey)

    public key to verify signature with

  • signature (String)

    signature to verify

  • options (Integer)

    RSA PSS options. Constants can be found in Prevoty::Crypto::PSSSaltOptions

Returns:



426
427
428
429
# 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