Skip to content

Commit 682e43f

Browse files
authored
Merge pull request #23 from securenative/dev
Version 0.1.29
2 parents ad2bf48 + af303df commit 682e43f

114 files changed

Lines changed: 857 additions & 2521 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
securenative (0.1.28)
4+
securenative (0.1.29)
55

66
GEM
77
remote: https://rubygems.org/
@@ -208,7 +208,7 @@ GEM
208208
rspec-core (~> 3.9.0)
209209
rspec-expectations (~> 3.9.0)
210210
rspec-mocks (~> 3.9.0)
211-
rspec-core (3.9.2)
211+
rspec-core (3.9.3)
212212
rspec-support (~> 3.9.3)
213213
rspec-expectations (3.9.2)
214214
diff-lcs (>= 1.2.0, < 2.0)

README.md

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -50,36 +50,36 @@ To get your *API KEY*, login to your SecureNative account and go to project sett
5050
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
53-
require 'securenative/securenative'
53+
require 'securenative'
5454

5555

56-
secureative = SecureNative::SecureNative.init
56+
secureative = SecureNative::Client.init
5757
```
5858
### Option 2: Initialize via API Key
5959

6060
```ruby
61-
require 'securenative/sdk'
61+
require 'securenative'
6262

6363

64-
securenative = SecureNative::SecureNative.init_with_api_key('YOUR_API_KEY')
64+
securenative = SecureNative::Client.init_with_api_key('YOUR_API_KEY')
6565
```
6666

6767
### Option 3: Initialize via ConfigurationBuilder
6868
```ruby
69-
require 'securenative/sdk'
69+
require 'securenative'
7070

7171

72-
options = SecureNative::ConfigurationBuilder.new(api_key: 'API_KEY', max_events: 10, log_level: 'ERROR')
73-
SecureNative::SecureNative.init_with_options(options)
72+
options = SecureNative::Config::ConfigurationBuilder.new(api_key: 'API_KEY', max_events: 10, log_level: 'ERROR')
73+
SecureNative::Client.init_with_options(options)
7474
```
7575

7676
## Getting SecureNative instance
7777
Once initialized, sdk will create a singleton instance which you can get:
7878
```ruby
79-
require 'securenative/sdk'
79+
require 'securenative'
8080

8181

82-
secureNative = SecureNative::SecureNative.instance
82+
secureNative = SecureNative::Client.instance
8383
```
8484

8585
## Tracking events
@@ -88,15 +88,12 @@ Once the SDK has been initialized, tracking requests sent through the SDK
8888
instance. Make sure you build event with the EventBuilder:
8989

9090
```ruby
91-
require 'securenative/sdk'
92-
require 'securenative/models/event_options'
93-
require 'securenative/enums/event_types'
94-
require 'securenative/models/user_traits'
91+
require 'securenative'
9592

9693

9794
def track
98-
securenative = SecureNative::SecureNative.instance
99-
context = SecureNative::SecureNativeContext.new(client_token: 'SECURED_CLIENT_TOKEN', ip: '127.0.0.1',
95+
securenative = SecureNative::Client.instance
96+
context = SecureNative::Context.new(client_token: 'SECURED_CLIENT_TOKEN', ip: '127.0.0.1',
10097
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' })
10198

10299
event_options = SecureNative::EventOptions.new(event: SecureNative::EventTypes::LOG_IN, user_id: '1234', context: context,
@@ -112,15 +109,12 @@ end
112109
You can also create request securenative.context from requests:
113110

114111
```ruby
115-
require 'securenative/sdk'
116-
require 'securenative/models/event_options'
117-
require 'securenative/enums/event_types'
118-
require 'securenative/models/user_traits'
112+
require 'securenative'
119113

120114

121115
def track(request)
122-
securenative = SecureNative::SecureNative.instance
123-
context = SecureNative::SecureNativeContext.from_http_request(request)
116+
securenative = SecureNative::Client.instance
117+
context = SecureNative::Context.from_http_request(request)
124118

125119
event_options = SecureNative::EventOptions.new(event: SecureNative::EventTypes::LOG_IN, user_id: '1234', context: context,
126120
user_traits: SecureNative::UserTraits.new(name: 'Your Name', email: 'name@gmail.com', phone: '+1234567890'),
@@ -137,15 +131,12 @@ end
137131
**Example**
138132

139133
```ruby
140-
require 'securenative/sdk'
141-
require 'securenative/models/event_options'
142-
require 'securenative/enums/event_types'
143-
require 'securenative/models/user_traits'
134+
require 'securenative'
144135

145136

146137
def verify(request)
147-
securenative = SecureNative::SecureNative.instance
148-
context = SecureNative::SecureNativeContext.from_http_request(request)
138+
securenative = SecureNative::Client.instance
139+
context = SecureNative::Context.from_http_request(request)
149140

150141
event_options = SecureNative::EventOptions.new(event: SecureNative::EventTypes::LOG_IN, user_id: '1234', context: context,
151142
user_traits: SecureNative::UserTraits.new(name: 'Your Name', email: 'name@gmail.com', phone: '+1234567890'),
@@ -163,11 +154,11 @@ end
163154
Apply our filter to verify the request is from us, for example:
164155

165156
```ruby
166-
require 'securenative/sdk'
157+
require 'securenative'
167158

168159

169160
def webhook_endpoint(request)
170-
securenative = SecureNative::SecureNative.instance
161+
securenative = SecureNative::Client.instance
171162

172163
# Checks if request is verified
173164
is_verified = securenative.verify_request_payload(request)
@@ -190,9 +181,9 @@ Initialize sdk as showed above.
190181
### Options 2: Using ConfigurationBuilder
191182
192183
```ruby
193-
require 'securenative/sdk'
184+
require 'securenative'
194185

195-
options = SecureNative::SecureNativeOptions.new(api_key: 'API_KEY', max_events: 10, log_level: 'ERROR', proxy_headers: ['CF-Connecting-IP'])
186+
options = SecureNative::Options.new(api_key: 'API_KEY', max_events: 10, log_level: 'ERROR', proxy_headers: ['CF-Connecting-IP'])
196187

197-
SecureNative::SecureNative.init_with_options(options)
188+
SecureNative::Client.init_with_options(options)
198189
```

lib/securenative.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# frozen_string_literal: true
2+
3+
require 'securenative/failover_strategy'
4+
require 'securenative/enums/risk_level'
5+
require 'securenative/enums/api_route'
6+
require 'securenative/event_types'
7+
require 'securenative/enums/risk_level'
8+
require 'securenative/config/configuration_builder'
9+
require 'securenative/config/configuration_manager'
10+
require 'securenative/options'
11+
require 'securenative/utils/request_utils'
12+
require 'securenative/utils/version_utils'
13+
require 'securenative/utils/encryption_utils'
14+
require 'securenative/utils/signature_utils'
15+
require 'securenative/utils/date_utils'
16+
require 'securenative/utils/utils'
17+
require 'securenative/utils/log'
18+
require 'securenative/utils/ip_utils'
19+
require 'securenative/frameworks/hanami'
20+
require 'securenative/frameworks/sinatra'
21+
require 'securenative/frameworks/rails'
22+
require 'securenative/context'
23+
require 'securenative/event_options'
24+
require 'securenative/user_traits'
25+
require 'securenative/request_context'
26+
require 'securenative/client_token'
27+
require 'securenative/sdk_event'
28+
require 'securenative/verify_result'
29+
require 'securenative/errors/invalid_options_error'
30+
require 'securenative/errors/sdk_Illegal_state_error'
31+
require 'securenative/errors/config_error'
32+
require 'securenative/errors/sdk_error'
33+
require 'securenative/errors/http_error'
34+
require 'securenative/http_client'
35+
require 'securenative/event_manager'
36+
require 'securenative/api_manager'
37+
require 'securenative/client'
38+
require 'securenative/version'
39+
40+
require 'yaml'
41+
require 'net/http'
42+
require 'uri'
43+
require 'json'
44+
require 'securerandom'
45+
require 'openssl'
46+
require 'digest'
47+
require 'base64'
48+
require 'resolv'
49+
require 'logger'

lib/securenative/api_manager.rb

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

3-
require 'securenative/models/sdk_event'
4-
require 'securenative/enums/failover_strategy'
5-
require 'securenative/enums/risk_level'
6-
require 'securenative/enums/api_route'
7-
require 'securenative/models/verify_result'
8-
require 'json'
9-
103
module SecureNative
114
class ApiManager
125
def initialize(event_manager, securenative_options)
@@ -15,27 +8,27 @@ def initialize(event_manager, securenative_options)
158
end
169

1710
def track(event_options)
18-
SecureNativeLogger.debug('Track event call')
19-
event = SDKEvent.new(event_options, @options)
20-
@event_manager.send_async(event, ApiRoute::TRACK)
11+
SecureNative::Log.debug('Track event call')
12+
event = SecureNative::SDKEvent.new(event_options, @options)
13+
@event_manager.send_async(event, SecureNative::Enums::ApiRoute::TRACK)
2114
end
2215

2316
def verify(event_options)
24-
SecureNativeLogger.debug('Verify event call')
25-
event = SDKEvent.new(event_options, @options)
17+
SecureNative::Log.debug('Verify event call')
18+
event = SecureNative::SDKEvent.new(event_options, @options)
2619

2720
begin
28-
res = @event_manager.send_sync(event, ApiRoute::VERIFY, false)
21+
res = @event_manager.send_sync(event, SecureNative::Enums::ApiRoute::VERIFY)
2922
ver_result = JSON.parse(res.body)
3023
return VerifyResult.new(risk_level: ver_result['riskLevel'], score: ver_result['score'], triggers: ver_result['triggers'])
3124
rescue StandardError => e
32-
SecureNativeLogger.debug("Failed to call verify; #{e}")
25+
SecureNative::Log.debug("Failed to call verify; #{e}")
3326
end
34-
if @options.fail_over_strategy == FailOverStrategy::FAIL_OPEN
35-
return VerifyResult.new(risk_level: RiskLevel::LOW, score: 0, triggers: nil)
27+
if @options.fail_over_strategy == SecureNative::FailOverStrategy::FAIL_OPEN
28+
return SecureNative::VerifyResult.new(risk_level: SecureNative::Enums::RiskLevel::LOW, score: 0, triggers: nil)
3629
end
3730

38-
VerifyResult.new(risk_level: RiskLevel::HIGH, score: 1, triggers: nil)
31+
VerifyResult.new(risk_level: SecureNative::Enums::RiskLevel::HIGH, score: 1, triggers: nil)
3932
end
4033
end
4134
end

lib/securenative/client.rb

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# frozen_string_literal: true
2+
3+
module SecureNative
4+
class Client
5+
attr_reader :options
6+
7+
def initialize(options)
8+
@securenative = nil
9+
if SecureNative::Utils::Utils.null_or_empty?(options.api_key)
10+
raise SecureNativeSDKError, 'You must pass your SecureNative api key'
11+
end
12+
13+
@options = options
14+
@event_manager = EventManager.new(@options)
15+
16+
@api_manager = SecureNative::ApiManager.new(@event_manager, @options)
17+
SecureNative::Log.init_logger(@options.log_level)
18+
end
19+
20+
def self.init_with_options(options)
21+
if @securenative.nil?
22+
@securenative = SecureNative::Client.new(options)
23+
@securenative
24+
else
25+
SecureNative::Log.debug('This SDK was already initialized.')
26+
raise SecureNativeSDKError, 'This SDK was already initialized.'
27+
end
28+
end
29+
30+
def self.init_with_api_key(api_key)
31+
if SecureNative::Utils::Utils.null_or_empty?(api_key)
32+
raise SecureNativeConfigError, 'You must pass your SecureNative api key'
33+
end
34+
35+
if @securenative.nil?
36+
options = SecureNative::Config::ConfigurationBuilder.new(api_key: api_key)
37+
@securenative = SecureNative::Client.new(options)
38+
@securenative
39+
else
40+
SecureNative::Log.debug('This SDK was already initialized.')
41+
raise SecureNativeSDKError, 'This SDK was already initialized.'
42+
end
43+
end
44+
45+
def self.init
46+
options = SecureNative::Config::ConfigurationManager.load_config
47+
init_with_options(options)
48+
end
49+
50+
def self.instance
51+
raise SecureNativeSDKIllegalStateError if @securenative.nil?
52+
53+
@securenative
54+
end
55+
56+
def track(event_options)
57+
@api_manager.track(event_options)
58+
end
59+
60+
def verify(event_options)
61+
@api_manager.verify(event_options)
62+
end
63+
64+
def self._flush
65+
@securenative = nil
66+
end
67+
68+
def verify_request_payload(request)
69+
request_signature = request.header[SignatureUtils.SIGNATURE_HEADER]
70+
body = request.body
71+
72+
SignatureUtils.valid_signature?(@options.api_key, body, request_signature)
73+
end
74+
end
75+
end
Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
# frozen_string_literal: true
22

3-
require 'securenative/enums/failover_strategy'
4-
53
module SecureNative
6-
class ConfigurationBuilder
7-
attr_reader :api_key, :api_url, :interval, :max_events, :timeout, :auto_send, :disable, :log_level, :fail_over_strategy, :proxy_headers
8-
attr_writer :api_key, :api_url, :interval, :max_events, :timeout, :auto_send, :disable, :log_level, :fail_over_strategy, :proxy_headers
4+
module Config
5+
class ConfigurationBuilder
6+
attr_reader :api_key, :api_url, :interval, :max_events, :timeout, :auto_send, :disable, :log_level, :fail_over_strategy, :proxy_headers
7+
attr_writer :api_key, :api_url, :interval, :max_events, :timeout, :auto_send, :disable, :log_level, :fail_over_strategy, :proxy_headers
98

10-
def initialize(api_key: nil, api_url: 'https://api.securenative.com/collector/api/v1', interval: 1000,
11-
max_events: 1000, timeout: 1500, auto_send: true, disable: false, log_level: 'FATAL',
12-
fail_over_strategy: FailOverStrategy::FAIL_OPEN, proxy_headers: nil)
13-
@api_key = api_key
14-
@api_url = api_url
15-
@interval = interval
16-
@max_events = max_events
17-
@timeout = timeout
18-
@auto_send = auto_send
19-
@disable = disable
20-
@log_level = log_level
21-
@fail_over_strategy = fail_over_strategy
22-
@proxy_headers = proxy_headers
23-
end
9+
def initialize(api_key: nil, api_url: 'https://api.securenative.com/collector/api/v1', interval: 1000,
10+
max_events: 1000, timeout: 1500, auto_send: true, disable: false, log_level: 'FATAL',
11+
fail_over_strategy: SecureNative::FailOverStrategy::FAIL_OPEN, proxy_headers: nil)
12+
@api_key = api_key
13+
@api_url = api_url
14+
@interval = interval
15+
@max_events = max_events
16+
@timeout = timeout
17+
@auto_send = auto_send
18+
@disable = disable
19+
@log_level = log_level
20+
@fail_over_strategy = fail_over_strategy
21+
@proxy_headers = proxy_headers
22+
end
2423

25-
def self.default_securenative_options
26-
SecureNativeOptions.new
24+
def self.default_securenative_options
25+
Options.new
26+
end
2727
end
2828
end
29-
end
29+
end

0 commit comments

Comments
 (0)