Skip to content

Commit 27b3efe

Browse files
author
Inbal Tako
committed
Fix alignment bugs
1 parent 14c9c0b commit 27b3efe

14 files changed

Lines changed: 74 additions & 58 deletions

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
securenative (0.1.20)
4+
securenative (0.1.21)
55

66
GEM
77
remote: https://rubygems.org/

VERSION

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/api_manager.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
require 'models/sdk_event'
44
require 'enums/failover_strategy'
5+
require 'enums/risk_level'
6+
require 'enums/api_route'
7+
require 'models/verify_result'
58
require 'json'
69

710
class ApiManager
@@ -21,7 +24,7 @@ def verify(event_options)
2124
event = SDKEvent.new(event_options, @options)
2225

2326
begin
24-
res = JSON.parse(@event_manager.send_sync(event, ApiRoute::VERIFY, false))
27+
res = @event_manager.send_sync(event, ApiRoute::VERIFY, false).to_json
2528
return VerifyResult.new(risk_level: res['riskLevel'], score: res['score'], triggers: res['triggers'])
2629
rescue StandardError => e
2730
SecureNativeLogger.debug("Failed to call verify; #{e}")

lib/context/securenative_context.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class SecureNativeContext
1212

1313
SECURENATIVE_COOKIE = '_sn'
1414

15-
def initialize(client_token: nil, ip: nil, remote_ip: nil, headers: nil, url: nil, http_method: nil, body: nil)
15+
def initialize(client_token: '', ip: '', remote_ip: '', headers: nil, url: '', http_method: '', body: '')
1616
@client_token = client_token
1717
@ip = ip
1818
@remote_ip = remote_ip

lib/event_manager.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require 'config/securenative_options'
55
require 'http/securenative_http_client'
66
require 'errors/securenative_sdk_error'
7+
require 'errors/securenative_http_error'
78

89
class QueueItem
910
attr_reader :url, :body, :retry_sending
@@ -64,7 +65,7 @@ def send_sync(event, resource_path, retry_sending)
6465
SecureNativeLogger.debug("Attempting to send event #{event}")
6566
res = @http_client.post(resource_path, EventManager.serialize(event).to_json)
6667

67-
if res.code != 200
68+
if res.nil? || res.code != 200
6869
SecureNativeLogger.info("SecureNative failed to call endpoint #{resource_path} with event #{event}. adding back to queue")
6970
item = QueueItem.new(resource_path, EventManager.serialize(event).to_json, retry_sending)
7071
@queue.append(item)
@@ -81,9 +82,9 @@ def run
8182
@queue.each do |item|
8283
begin
8384
res = @http_client.post(item.url, item.body)
84-
if res.status_code == 401
85+
if res.code == '401'
8586
item.retry_sending = false
86-
elsif res.status_code != 200
87+
elsif res.code != '200'
8788
raise SecureNativeHttpError, res.status_code
8889
end
8990
SecureNativeLogger.debug("Event successfully sent; #{item.body}")
@@ -145,7 +146,7 @@ def self.serialize(obj)
145146
fp: obj.request.fp,
146147
ip: obj.request.ip,
147148
remoteIp: obj.request.remote_ip,
148-
http_method: obj.request.http_method,
149+
method: obj.request.http_method || '',
149150
url: obj.request.url,
150151
headers: obj.request.headers
151152
},

lib/http/securenative_http_client.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
require 'net/http'
44
require 'uri'
55
require 'json'
6+
require 'utils/version_utils'
7+
require 'utils/secure_native_logger'
68

79
class SecureNativeHttpClient
810
AUTHORIZATION_HEADER = 'Authorization'
@@ -30,9 +32,19 @@ def post(path, body)
3032
headers = _headers
3133

3234
client = Net::HTTP.new(uri.host, uri.port)
35+
client.use_ssl = true
36+
client.verify_mode = OpenSSL::SSL::VERIFY_NONE
37+
3338
request = Net::HTTP::Post.new(uri.request_uri, headers)
34-
request.body = body.to_json
39+
request.body = body
3540

36-
client.request(request)
41+
res = nil
42+
begin
43+
res = client.request(request)
44+
rescue StandardError => e
45+
SecureNativeLogger.error("Failed to send request; #{e}")
46+
return res
47+
end
48+
res
3749
end
3850
end

lib/models/sdk_event.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
require 'context/securenative_context'
44
require 'utils/encryption_utils'
5+
require 'utils/date_utils'
6+
require 'models/request_context'
57

68
class SDKEvent
79
attr_reader :context, :rid, :event_type, :user_id, :user_traits, :request, :timestamp, :properties
@@ -20,9 +22,11 @@ def initialize(event_options, securenative_options)
2022
@event_type = event_options.event
2123
@user_id = event_options.user_id
2224
@user_traits = event_options.user_traits
23-
@request = RequestContext(client_token ? client_token.cid : '', client_token ? client_token.vid : '',
24-
client_token ? client_token.fp : '', @context.ip,
25-
@context.remote_ip, @context.headers, @context.url, @context.http_method)
25+
@request = RequestContext.new(cid: client_token ? client_token.cid : '', vid: client_token ? client_token.vid : '',
26+
fp: client_token ? client_token.fp : '', ip: @context.ip,
27+
remote_ip: @context.remote_ip, headers: @context.headers,
28+
url: @context.url, http_method: @context.http_method)
29+
2630

2731
@timestamp = DateUtils.to_timestamp(event_options.timestamp)
2832
@properties = event_options.properties

lib/securenative.rb

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,6 @@ def self.instance
6161
@securenative
6262
end
6363

64-
def self.config_builder(api_key: nil, api_url: 'https://api.securenative.com/collector/api/v1', interval: 1000,
65-
max_events: 1000, timeout: 1500, auto_send: true, disable: false, log_level: 'FATAL',
66-
fail_over_strategy: FailOverStrategy::FAIL_OPEN)
67-
ConfigurationBuilder.new(api_key: api_key, api_url: api_url, interval: interval, max_events: max_events,
68-
timeout: timeout, auto_send: auto_send, disable: disable, log_level: log_level,
69-
fail_over_strategy: fail_over_strategy)
70-
end
71-
72-
def self.context_builder(client_token: nil, ip: nil, remote_ip: nil, headers: nil, url: nil, http_method: nil, body: nil)
73-
SecureNativeContext.new(client_token: client_token, ip: ip, remote_ip: remote_ip, headers: headers,
74-
url: url, http_method: http_method, body: body)
75-
end
76-
7764
def track(event_options)
7865
@api_manager.track(event_options)
7966
end

lib/utils/date_utils.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class DateUtils
44
def self.to_timestamp(date)
5-
return Time.now.strftime('%Y-%m-%dT%H:%M:%S%Z') if date.nil?
5+
return Time.now.utc.iso8601 if date.nil?
66

77
Time.parse(date).iso8601
88
end

lib/utils/encryption_utils.rb

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,44 @@
11
# frozen_string_literal: true
22

33
require 'openssl'
4+
require 'models/client_token'
45

56
class EncryptionUtils
67
BLOCK_SIZE = 16
78
KEY_SIZE = 32
89

910
def self.encrypt(text, cipher_key)
10-
cipher = OpenSSL::Cipher::AES.new(KEY_SIZE, :CBC).encrypt
11-
cipher.padding = 0
11+
begin
12+
cipher = OpenSSL::Cipher::AES.new(KEY_SIZE, :CBC).encrypt
13+
cipher.padding = 0
1214

13-
if text.size % BLOCK_SIZE != 0
14-
return nil
15-
end
15+
if text.size % BLOCK_SIZE != 0
16+
return nil
17+
end
1618

17-
cipher_key = Digest::SHA1.hexdigest cipher_key
18-
cipher.key = cipher_key.slice(0, BLOCK_SIZE)
19-
s = cipher.update(text) + cipher.final
19+
cipher_key = Digest::SHA1.hexdigest cipher_key
20+
cipher.key = cipher_key.slice(0, BLOCK_SIZE)
21+
s = cipher.update(text) + cipher.final
2022

21-
s.unpack('H*')[0].upcase
23+
s.unpack('H*')[0].upcase
24+
rescue StandardError
25+
''
26+
end
2227
end
2328

2429
def self.decrypt(encrypted, cipher_key)
25-
cipher = OpenSSL::Cipher::AES.new(KEY_SIZE, :CBC).decrypt
26-
cipher.padding = 0
27-
28-
cipher_key = Digest::SHA1.hexdigest cipher_key
29-
cipher.key = cipher_key.slice(0, BLOCK_SIZE)
30-
s = [encrypted].pack('H*').unpack('C*').pack('c*')
31-
32-
rv = cipher.update(s) + cipher.final
33-
rv.strip
30+
begin
31+
cipher = OpenSSL::Cipher::AES.new(KEY_SIZE, :CBC).decrypt
32+
cipher.padding = 0
33+
34+
cipher_key = Digest::SHA1.hexdigest cipher_key
35+
cipher.key = cipher_key.slice(0, BLOCK_SIZE)
36+
s = [encrypted].pack('H*').unpack('C*').pack('c*')
37+
38+
rv = cipher.update(s) + cipher.final
39+
rv.strip
40+
rescue StandardError
41+
ClientToken.new('', '', '')
42+
end
3443
end
3544
end

0 commit comments

Comments
 (0)