Skip to content

Commit 50bffa5

Browse files
authored
Merge pull request #14 from securenative/dev
Dev
2 parents 27b3efe + f71c4e1 commit 50bffa5

23 files changed

Lines changed: 208 additions & 181 deletions

Gemfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ source "https://rubygems.org"
33
gemspec
44
gem "rspec"
55
gem "rake"
6-
gem "parseconfig"
76
gem "simplecov", :require => false, :group => :test
87
gem "codecov", :require => false, :group => :test
98
gem "webmock", :require => false, :group => :test

Gemfile.lock

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ GEM
7373
crack (0.4.3)
7474
safe_yaml (~> 1.0.0)
7575
crass (1.0.6)
76-
diff-lcs (1.3)
76+
diff-lcs (1.4.4)
7777
docile (1.3.2)
7878
dry-configurable (0.11.6)
7979
concurrent-ruby (~> 1.0)
@@ -174,7 +174,6 @@ GEM
174174
nio4r (2.5.2)
175175
nokogiri (1.10.10)
176176
mini_portile2 (~> 2.4.0)
177-
parseconfig (1.0.8)
178177
public_suffix (4.0.5)
179178
rack (2.2.3)
180179
rack-protection (2.0.8.1)
@@ -208,19 +207,19 @@ GEM
208207
rake (>= 0.8.7)
209208
thor (>= 0.20.3, < 2.0)
210209
rake (13.0.1)
211-
rspec (3.8.0)
212-
rspec-core (~> 3.8.0)
213-
rspec-expectations (~> 3.8.0)
214-
rspec-mocks (~> 3.8.0)
215-
rspec-core (3.8.2)
216-
rspec-support (~> 3.8.0)
217-
rspec-expectations (3.8.4)
210+
rspec (3.9.0)
211+
rspec-core (~> 3.9.0)
212+
rspec-expectations (~> 3.9.0)
213+
rspec-mocks (~> 3.9.0)
214+
rspec-core (3.9.2)
215+
rspec-support (~> 3.9.3)
216+
rspec-expectations (3.9.2)
218217
diff-lcs (>= 1.2.0, < 2.0)
219-
rspec-support (~> 3.8.0)
220-
rspec-mocks (3.8.1)
218+
rspec-support (~> 3.9.0)
219+
rspec-mocks (3.9.1)
221220
diff-lcs (>= 1.2.0, < 2.0)
222-
rspec-support (~> 3.8.0)
223-
rspec-support (3.8.2)
221+
rspec-support (~> 3.9.0)
222+
rspec-support (3.9.3)
224223
ruby2_keywords (0.0.2)
225224
safe_yaml (1.0.5)
226225
simplecov (0.19.0)
@@ -263,7 +262,6 @@ DEPENDENCIES
263262
bundler (~> 2.0)
264263
codecov
265264
hanami
266-
parseconfig
267265
rails
268266
rake
269267
rspec

README.md

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Or install it yourself as:
4747
To get your *API KEY*, login to your SecureNative account and go to project settings page:
4848

4949
### Option 1: Initialize via Config file
50-
SecureNative can automatically load your config from *securenative.cfg* file or from the file that is specified in your *SECURENATIVE_CONFIG_FILE* env variable:
50+
SecureNative can automatically load your config from *securenative.yml* file or from the file that is specified in your *SECURENATIVE_CONFIG_FILE* env variable:
5151

5252
```ruby
5353
require 'securenative'
@@ -69,7 +69,8 @@ securenative = SecureNative.init_with_api_key('YOUR_API_KEY')
6969
require 'securenative'
7070

7171

72-
securenative = SecureNative.init_with_options(SecureNative.config_builder(api_key = 'API_KEY', max_event = 10, log_level = 'ERROR'))
72+
options = ConfigurationBuilder.new(api_key: 'API_KEY', max_events: 10, log_level: 'ERROR')
73+
SecureNative.init_with_options(options)
7374
```
7475

7576
## Getting SecureNative instance
@@ -88,42 +89,46 @@ instance. Make sure you build event with the EventBuilder:
8889

8990
```ruby
9091
require 'securenative'
91-
require 'securenative/enums/event_types'
92-
require 'securenative/event_options_builder'
93-
require 'securenative/models/user_traits'
94-
require 'securenative/context/context_builder'
92+
require 'models/event_options'
93+
require 'enums/event_types'
94+
require 'models/user_traits'
9595

9696

97-
securenative = SecureNative.instance
98-
99-
context = securenative.context_builder(ip = '127.0.0.1', client_token = 'SECURED_CLIENT_TOKEN',
100-
headers = { 'user-agent' => 'Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405' })
101-
102-
event_options = EventOptions(event_type = EventTypes::LOG_IN,
103-
user_id = '1234', user_traits = UserTraits('Your Name', 'name@gmail.com', '+1234567890'),
104-
context = context, properties = {prop1 => 'CUSTOM_PARAM_VALUE', prop2 => true, prop3 => 3}).build
105-
106-
securenative.track(event_options)
97+
def track
98+
securenative = SecureNative.instance
99+
context = SecureNativeContext.new(client_token: '2a980d872b939c7e4f4378aa111a5eeffb22808b58b5372f658d34904ebd5b05fff0daab91921243ac08b72442a5b3992e402dc21df16aa7cc0e19f8bffa9d6cc59996d480d70aa22b857189403675d37fd144ebaf9dc697fed149b907678f2b1f964d73b332dc8ea7df63fcfc3c11f7bbb51ba2672652ca7d5d43f36a62e15db8b13dfd794a5eccfc5968ca514dd7cce59f2df2b9d8184d076eba808c81b311', ip: '127.0.0.1',
100+
headers: { 'user-agent' => 'Mozilla: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.3 Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/43.4' })
101+
102+
event_options = EventOptions.new(event: EventTypes::LOG_IN, user_id: '1234', context: context,
103+
user_traits: UserTraits.new(name: 'Your Name', email: 'name@gmail.com', phone: '+1234567890'),
104+
properties: { custom_param1: 'CUSTOM_PARAM_VALUE', custom_param2: true, custom_param3: 3 })
105+
106+
securenative.track(event_options)
107+
108+
@message = 'tracked'
109+
end
107110
```
108111

109112
You can also create request context from requests:
110113

111114
```ruby
112115
require 'securenative'
113-
require 'securenative/enums/event_types'
114-
require 'securenative/event_options_builder'
115-
require 'securenative/models/user_traits'
116+
require 'models/event_options'
117+
require 'enums/event_types'
118+
require 'models/user_traits'
116119

117120

118-
def track(request)
121+
def track
119122
securenative = SecureNative.instance
120-
context = SecureNative.context_builder.from_http_request(request).build
121-
122-
event_options = EventOptions(event_type = EventTypes::LOG_IN,
123-
user_id = '1234', user_traits = UserTraits('Your Name', 'name@gmail.com', '+1234567890'),
124-
context = context, properties = {prop1 => 'CUSTOM_PARAM_VALUE', prop2 => true, prop3 => 3}).build
123+
context = SecureNativeContext.from_http_request(request)
124+
125+
event_options = EventOptions.new(event: EventTypes::LOG_IN, user_id: '1234', context: context,
126+
user_traits: UserTraits.new(name: 'Your Name', email: 'name@gmail.com', phone: '+1234567890'),
127+
properties: { custom_param1: 'CUSTOM_PARAM_VALUE', custom_param2: true, custom_param3: 3 })
125128

126129
securenative.track(event_options)
130+
131+
@message = 'tracked'
127132
end
128133
```
129134

@@ -133,18 +138,18 @@ end
133138

134139
```ruby
135140
require 'securenative'
136-
require 'securenative/enums/event_types'
137-
require 'securenative/event_options_builder'
138-
require 'securenative/models/user_traits'
141+
require 'models/event_options'
142+
require 'enums/event_types'
143+
require 'models/user_traits'
139144

140145

141146
def verify(request)
142147
securenative = SecureNative.instance
143-
context = SecureNative.context_builder.from_http_request(request).build
148+
context = SecureNativeContext.from_http_request(request)
144149

145-
event_options = EventOptions(event_type = EventTypes::LOG_IN,
146-
user_id = '1234', user_traits = UserTraits('Your Name', 'name@gmail.com', '+1234567890'),
147-
context = context, properties = {prop1 => 'CUSTOM_PARAM_VALUE', prop2 => true, prop3 => 3}).build
150+
event_options = EventOptions.new(event: EventTypes::LOG_IN, user_id: '1234', context: context,
151+
user_traits: UserTraits.new(name: 'Your Name', email: 'name@gmail.com', phone: '+1234567890'),
152+
properties: { custom_param1: 'CUSTOM_PARAM_VALUE', custom_param2: true, custom_param3: 3 })
148153

149154
verify_result = securenative.verify(event_options)
150155
verify_result.risk_level # Low, Medium, High

lib/api_manager.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ def verify(event_options)
2424
event = SDKEvent.new(event_options, @options)
2525

2626
begin
27-
res = @event_manager.send_sync(event, ApiRoute::VERIFY, false).to_json
28-
return VerifyResult.new(risk_level: res['riskLevel'], score: res['score'], triggers: res['triggers'])
27+
res = @event_manager.send_sync(event, ApiRoute::VERIFY, false)
28+
ver_result = JSON.parse(res.body)
29+
return VerifyResult.new(risk_level: ver_result['riskLevel'], score: ver_result['score'], triggers: ver_result['triggers'])
2930
rescue StandardError => e
3031
SecureNativeLogger.debug("Failed to call verify; #{e}")
3132
end

lib/config/configuration_builder.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ def initialize(api_key: nil, api_url: 'https://api.securenative.com/collector/ap
2020
@fail_over_strategy = fail_over_strategy
2121
end
2222

23-
def self.default_config_builder
24-
ConfigurationBuilder.new
25-
end
26-
2723
def self.default_securenative_options
2824
SecureNativeOptions.new
2925
end
Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
# frozen_string_literal: true
22

3-
require 'parseconfig'
3+
require 'yaml'
4+
require 'config/configuration_builder'
45

56
class ConfigurationManager
6-
DEFAULT_CONFIG_FILE = 'securenative.cfg'
7+
DEFAULT_CONFIG_FILE = 'securenative.yml'
78
CUSTOM_CONFIG_FILE_ENV_NAME = 'SECURENATIVE_CONFIG_FILE'
89
@config = nil
910

1011
def self.read_resource_file(resource_path)
11-
@config = ParseConfig.new(resource_path)
12-
1312
properties = {}
14-
@config.get_groups.each do |group|
15-
group.each do |key, value|
16-
properties[key.upcase] = value
17-
end
13+
begin
14+
@config = YAML.load_file(resource_path)
15+
properties = @config unless @config.nil?
16+
rescue StandardError => e
17+
SecureNativeLogger.error("Could not parse config file #{resource_path}; #{e}")
1818
end
1919
properties
2020
end
@@ -24,32 +24,32 @@ def self._get_resource_path(env_name)
2424
end
2525

2626
def self.config_builder
27-
ConfigurationBuilder.default_config_builder
27+
ConfigurationBuilder.new
2828
end
2929

3030
def self._get_env_or_default(properties, key, default)
31-
return Env[key] if Env[key]
31+
return ENV[key] if ENV[key]
3232
return properties[key] if properties[key]
3333

3434
default
3535
end
3636

3737
def self.load_config
38-
options = ConfigurationBuilder().default_securenative_options
38+
options = ConfigurationBuilder.default_securenative_options
3939

4040
resource_path = DEFAULT_CONFIG_FILE
41-
resource_path = Env[CUSTOM_CONFIG_FILE_ENV_NAME] if Env[CUSTOM_CONFIG_FILE_ENV_NAME]
41+
resource_path = ENV[CUSTOM_CONFIG_FILE_ENV_NAME] unless ENV[CUSTOM_CONFIG_FILE_ENV_NAME].nil?
4242

4343
properties = read_resource_file(resource_path)
4444

45-
ConfigurationBuilder(_get_env_or_default(properties, 'SECURENATIVE_API_KEY', options.api_key),
46-
_get_env_or_default(properties, 'SECURENATIVE_API_URL', options.api_url),
47-
_get_env_or_default(properties, 'SECURENATIVE_INTERVAL', options.interval),
48-
_get_env_or_default(properties, 'SECURENATIVE_MAX_EVENTS', options.max_events),
49-
_get_env_or_default(properties, 'SECURENATIVE_TIMEOUT', options.timeout),
50-
_get_env_or_default(properties, 'SECURENATIVE_AUTO_SEND', options.auto_send),
51-
_get_env_or_default(properties, 'SECURENATIVE_DISABLE', options.disable),
52-
_get_env_or_default(properties, 'SECURENATIVE_LOG_LEVEL', options.log_level),
53-
_get_env_or_default(properties, 'SECURENATIVE_FAILOVER_STRATEGY', options.fail_over_strategy))
45+
ConfigurationBuilder.new(api_key: _get_env_or_default(properties, 'SECURENATIVE_API_KEY', options.api_key),
46+
api_url: _get_env_or_default(properties, 'SECURENATIVE_API_URL', options.api_url),
47+
interval: _get_env_or_default(properties, 'SECURENATIVE_INTERVAL', options.interval),
48+
max_events: _get_env_or_default(properties, 'SECURENATIVE_MAX_EVENTS', options.max_events),
49+
timeout: _get_env_or_default(properties, 'SECURENATIVE_TIMEOUT', options.timeout),
50+
auto_send: _get_env_or_default(properties, 'SECURENATIVE_AUTO_SEND', options.auto_send),
51+
disable: _get_env_or_default(properties, 'SECURENATIVE_DISABLE', options.disable),
52+
log_level: _get_env_or_default(properties, 'SECURENATIVE_LOG_LEVEL', options.log_level),
53+
fail_over_strategy: _get_env_or_default(properties, 'SECURENATIVE_FAILOVER_STRATEGY', options.fail_over_strategy))
5454
end
5555
end

lib/context/hanami_context.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
# frozen_string_literal: true
22

33
class HanamiContext
4+
SECURENATIVE_COOKIE = '_sn'
5+
46
def self.get_client_token(request)
57
begin
68
request.env[SECURENATIVE_COOKIE]
79
rescue StandardError
8-
nil
10+
begin
11+
request.cookies[SECURENATIVE_COOKIE]
12+
rescue StandardError
13+
nil
14+
end
915
end
1016
end
1117

@@ -27,7 +33,8 @@ def self.get_method(request)
2733

2834
def self.get_headers(request)
2935
begin
30-
request.headers.to_hash
36+
# Note: At the moment we're filtering out everything but user-agent since ruby's payload is way too big
37+
{ 'user-agent' => request.env['HTTP_USER_AGENT'] }
3138
rescue StandardError
3239
nil
3340
end

lib/context/rails_context.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
class RailsContext
4+
SECURENATIVE_COOKIE = '_sn'
5+
46
def self.get_client_token(request)
57
begin
68
request.cookies[SECURENATIVE_COOKIE]
@@ -33,7 +35,8 @@ def self.get_method(request)
3335

3436
def self.get_headers(request)
3537
begin
36-
request.headers.to_hash
38+
# Note: At the moment we're filtering out everything but user-agent since ruby's payload is way too big
39+
{ 'user-agent' => request.env['HTTP_USER_AGENT'] }
3740
rescue StandardError
3841
nil
3942
end

lib/context/sinatra_context.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,40 @@
11
# frozen_string_literal: true
22

33
class SinatraContext
4+
SECURENATIVE_COOKIE = '_sn'
5+
46
def self.get_client_token(request)
57
begin
68
request.env[SECURENATIVE_COOKIE]
79
rescue StandardError
8-
nil
10+
begin
11+
request.cookies[SECURENATIVE_COOKIE]
12+
rescue StandardError
13+
nil
14+
end
915
end
1016
end
1117

1218
def self.get_url(request)
1319
begin
14-
request.url
20+
request.env['REQUEST_URI']
1521
rescue StandardError
1622
nil
1723
end
1824
end
1925

2026
def self.get_method(request)
2127
begin
22-
request.method
28+
request.env['REQUEST_METHOD']
2329
rescue StandardError
2430
nil
2531
end
2632
end
2733

2834
def self.get_headers(request)
2935
begin
30-
request.headers.to_hash
36+
# Note: At the moment we're filtering out everything but user-agent since ruby's payload is way too big
37+
{ 'user-agent' => request.env['HTTP_USER_AGENT'] }
3138
rescue StandardError
3239
nil
3340
end

lib/event_manager.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def send_sync(event, resource_path, retry_sending)
6565
SecureNativeLogger.debug("Attempting to send event #{event}")
6666
res = @http_client.post(resource_path, EventManager.serialize(event).to_json)
6767

68-
if res.nil? || res.code != 200
68+
if res.nil? || res.code != '200'
6969
SecureNativeLogger.info("SecureNative failed to call endpoint #{resource_path} with event #{event}. adding back to queue")
7070
item = QueueItem.new(resource_path, EventManager.serialize(event).to_json, retry_sending)
7171
@queue.append(item)

0 commit comments

Comments
 (0)