securenative 0.1.5 → 0.1.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +49 -0
  3. data/.github/workflows/publish.yml +60 -0
  4. data/.github/workflows/test.yml +48 -0
  5. data/.gitignore +3 -1
  6. data/.rakeTasks +7 -0
  7. data/.rspec +3 -0
  8. data/Gemfile +6 -1
  9. data/Gemfile.lock +254 -17
  10. data/README.md +140 -67
  11. data/Rakefile +5 -1
  12. data/lib/api_manager.rb +39 -0
  13. data/lib/config/configuration_builder.rb +26 -0
  14. data/lib/config/configuration_manager.rb +55 -0
  15. data/lib/config/securenative_options.rb +22 -0
  16. data/lib/context/hanami_context.rb +42 -0
  17. data/lib/context/rails_context.rb +44 -0
  18. data/lib/context/securenative_context.rb +67 -0
  19. data/lib/context/sinatra_context.rb +42 -0
  20. data/lib/enums/api_route.rb +6 -0
  21. data/lib/enums/event_types.rb +23 -0
  22. data/lib/enums/failover_strategy.rb +6 -0
  23. data/lib/enums/risk_level.rb +7 -0
  24. data/lib/errors/securenative_config_error.rb +4 -0
  25. data/lib/errors/securenative_http_error.rb +4 -0
  26. data/lib/errors/securenative_invalid_options_error.rb +4 -0
  27. data/lib/errors/securenative_invalid_uri_error.rb +4 -0
  28. data/lib/errors/securenative_parse_error.rb +4 -0
  29. data/lib/errors/securenative_sdk_Illegal_state_error.rb +4 -0
  30. data/lib/errors/securenative_sdk_error.rb +4 -0
  31. data/lib/event_manager.rb +157 -0
  32. data/lib/http/secure_native_http_response.rb +12 -0
  33. data/lib/http/securenative_http_client.rb +50 -0
  34. data/lib/models/client_token.rb +12 -0
  35. data/lib/models/device.rb +10 -0
  36. data/lib/models/event_options.rb +37 -0
  37. data/lib/models/request_context.rb +18 -0
  38. data/lib/models/request_options.rb +12 -0
  39. data/lib/models/sdk_event.rb +40 -0
  40. data/lib/models/user_traits.rb +13 -0
  41. data/lib/models/verify_result.rb +16 -0
  42. data/lib/securenative.rb +69 -25
  43. data/lib/utils/date_utils.rb +9 -0
  44. data/lib/utils/encryption_utils.rb +49 -0
  45. data/lib/utils/ip_utils.rb +23 -0
  46. data/lib/utils/request_utils.rb +54 -0
  47. data/lib/utils/secure_native_logger.rb +44 -0
  48. data/lib/utils/signature_utils.rb +16 -0
  49. data/lib/utils/utils.rb +9 -0
  50. data/lib/utils/version_utils.rb +11 -0
  51. data/out/production/securenative-ruby/api_manager.rb +31 -0
  52. data/out/production/securenative-ruby/config/configuration_builder.rb +30 -0
  53. data/out/production/securenative-ruby/config/configuration_manager.rb +55 -0
  54. data/out/production/securenative-ruby/config/securenative_options.rb +20 -0
  55. data/out/production/securenative-ruby/context/securenative_context.rb +40 -0
  56. data/out/production/securenative-ruby/enums/api_route.rb +6 -0
  57. data/out/production/securenative-ruby/enums/event_types.rb +23 -0
  58. data/out/production/securenative-ruby/enums/failover_strategy.rb +6 -0
  59. data/out/production/securenative-ruby/enums/risk_level.rb +7 -0
  60. data/out/production/securenative-ruby/errors/securenative_config_error.rb +4 -0
  61. data/out/production/securenative-ruby/errors/securenative_http_error.rb +4 -0
  62. data/out/production/securenative-ruby/errors/securenative_invalid_options_error.rb +4 -0
  63. data/out/production/securenative-ruby/errors/securenative_invalid_uri_error.rb +4 -0
  64. data/out/production/securenative-ruby/errors/securenative_parse_error.rb +4 -0
  65. data/out/production/securenative-ruby/errors/securenative_sdk_Illegal_state_error.rb +4 -0
  66. data/out/production/securenative-ruby/errors/securenative_sdk_error.rb +4 -0
  67. data/out/production/securenative-ruby/event_manager.rb +156 -0
  68. data/out/production/securenative-ruby/event_options.rb +32 -0
  69. data/out/production/securenative-ruby/http/http_response.rb +12 -0
  70. data/out/production/securenative-ruby/http/securenative_http_client.rb +32 -0
  71. data/out/production/securenative-ruby/models/client_token.rb +12 -0
  72. data/out/production/securenative-ruby/models/device.rb +10 -0
  73. data/out/production/securenative-ruby/models/event_options.rb +15 -0
  74. data/out/production/securenative-ruby/models/request_context.rb +18 -0
  75. data/out/production/securenative-ruby/models/request_options.rb +12 -0
  76. data/out/production/securenative-ruby/models/sdk_event.rb +33 -0
  77. data/out/production/securenative-ruby/models/user_traits.rb +13 -0
  78. data/out/production/securenative-ruby/models/verify_result.rb +12 -0
  79. data/out/production/securenative-ruby/securenative.rb +91 -0
  80. data/out/production/securenative-ruby/utils/date_utils.rb +9 -0
  81. data/out/production/securenative-ruby/utils/encryption_utils.rb +35 -0
  82. data/out/production/securenative-ruby/utils/ip_utils.rb +23 -0
  83. data/out/production/securenative-ruby/utils/request_utils.rb +23 -0
  84. data/out/production/securenative-ruby/utils/secure_native_logger.rb +44 -0
  85. data/out/production/securenative-ruby/utils/signature_utils.rb +16 -0
  86. data/out/production/securenative-ruby/utils/utils.rb +9 -0
  87. data/out/production/securenative-ruby/utils/version_utils.rb +12 -0
  88. data/out/test/securenative-ruby/spec_api_manager.rb +81 -0
  89. data/out/test/securenative-ruby/spec_context_builder.rb +69 -0
  90. data/out/test/securenative-ruby/spec_date_utils.rb +13 -0
  91. data/out/test/securenative-ruby/spec_encryption_utils.rb +26 -0
  92. data/out/test/securenative-ruby/spec_event_manager.rb +59 -0
  93. data/out/test/securenative-ruby/spec_helper.rb +20 -0
  94. data/out/test/securenative-ruby/spec_ip_utils.rb +41 -0
  95. data/out/test/securenative-ruby/spec_securenative.rb +65 -0
  96. data/out/test/securenative-ruby/spec_securenative_http_client.rb +23 -0
  97. data/out/test/securenative-ruby/spec_signature_utils.rb +18 -0
  98. data/securenative.gemspec +4 -4
  99. metadata +96 -15
  100. data/lib/securenative/config.rb +0 -9
  101. data/lib/securenative/event_manager.rb +0 -88
  102. data/lib/securenative/event_options.rb +0 -86
  103. data/lib/securenative/event_type.rb +0 -21
  104. data/lib/securenative/http_client.rb +0 -20
  105. data/lib/securenative/secure_native_sdk.rb +0 -62
  106. data/lib/securenative/securenative_options.rb +0 -17
  107. data/lib/securenative/sn_exception.rb +0 -5
  108. data/lib/securenative/utils.rb +0 -41
data/README.md CHANGED
@@ -1,7 +1,32 @@
1
- # SecureNative
2
- **[SecureNative](https://www.securenative.com/) is rethinking-security-as-a-service, disrupting the cyber security space and the way enterprises consume and implement security solutions.**
3
-
4
- ## Installation
1
+ <p align="center">
2
+ <a href="https://www.securenative.com"><img src="https://user-images.githubusercontent.com/45174009/77826512-f023ed80-7120-11ea-80e0-58aacde0a84e.png" alt="SecureNative Logo"/></a>
3
+ </p>
4
+
5
+ <p align="center">
6
+ <b>A Cloud-Native Security Monitoring and Protection for Modern Applications</b>
7
+ </p>
8
+ <p align="center">
9
+ <a href="https://github.com/securenative/securenative-ruby">
10
+ <img alt="Github Actions" src="https://github.com/securenative/securenative-ruby/workflows/CI/badge.svg">
11
+ </a>
12
+ <a href="https://codecov.io/gh/securenative/securenative-ruby">
13
+ <img src="https://codecov.io/gh/securenative/securenative-ruby/branch/master/graph/badge.svg" />
14
+ </a>
15
+ <a href="https://badge.fury.io/rb/securenative"><img src="https://badge.fury.io/rb/securenative.svg" alt="Gem Version" height="18"></a>
16
+ </p>
17
+ <p align="center">
18
+ <a href="https://docs.securenative.com">Documentation</a> |
19
+ <a href="https://docs.securenative.com/quick-start">Quick Start</a> |
20
+ <a href="https://blog.securenative.com">Blog</a> |
21
+ <a href="">Chat with us on Slack!</a>
22
+ </p>
23
+ <hr/>
24
+
25
+
26
+ [SecureNative](https://www.securenative.com/) performs user monitoring by analyzing user interactions with your application and various factors such as network, devices, locations and access patterns to stop and prevent account takeover attacks.
27
+
28
+
29
+ ## Install the SDK
5
30
 
6
31
  Add this line to your application's Gemfile:
7
32
 
@@ -9,94 +34,142 @@ Add this line to your application's Gemfile:
9
34
  gem 'securenative'
10
35
  ```
11
36
 
12
- And then execute:
37
+ Then execute:
13
38
 
14
- $ bundle
39
+ $ bundle install
15
40
 
16
41
  Or install it yourself as:
17
42
 
18
43
  $ gem install securenative
19
44
 
20
45
  ## Initialize the SDK
21
- Retrieve your API key from settings page of your SecureNative account and use the following in your code to initialize and use the sdk.
46
+
47
+ To get your *API KEY*, login to your SecureNative account and go to project settings page:
48
+
49
+ ### Option 1: Initialize via Config file
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:
51
+
22
52
  ```ruby
23
53
  require 'securenative'
24
54
 
25
- # Do some cool stuff here
26
- # ...
27
- # ...
28
-
29
- begin
30
- SecureNative.init('YOUR_API_KEY') # Should be called before any other call to securenative
31
- rescue SecureNativeSDKException => e
32
- # Do some error handling
33
- end
55
+
56
+ secureative = SecureNative.init
57
+ ```
58
+ ### Option 2: Initialize via API Key
59
+
60
+ ```ruby
61
+ require 'securenative'
62
+
63
+
64
+ securenative = SecureNative.init_with_api_key('YOUR_API_KEY')
65
+ ```
66
+
67
+ ### Option 3: Initialize via ConfigurationBuilder
68
+ ```ruby
69
+ require 'securenative'
70
+
71
+
72
+ options = ConfigurationBuilder.new(api_key: 'API_KEY', max_events: 10, log_level: 'ERROR')
73
+ SecureNative.init_with_options(options)
74
+ ```
75
+
76
+ ## Getting SecureNative instance
77
+ Once initialized, sdk will create a singleton instance which you can get:
78
+ ```ruby
79
+ require 'securenative'
80
+
81
+
82
+ secureNative = SecureNative.instance
34
83
  ```
35
84
 
36
85
  ## Tracking events
37
- Once the SDK has been initialized, you can start sending new events with the `track` function:
86
+
87
+ Once the SDK has been initialized, tracking requests sent through the SDK
88
+ instance. Make sure you build event with the EventBuilder:
89
+
90
+ ```ruby
91
+ require 'securenative'
92
+ require 'models/event_options'
93
+ require 'enums/event_types'
94
+ require 'models/user_traits'
95
+
96
+
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
110
+ ```
111
+
112
+ You can also create request context from requests:
113
+
38
114
  ```ruby
39
115
  require 'securenative'
40
- require 'securenative/event_type'
41
-
42
-
43
- def login
44
- # Do some cool stuff here
45
- # ...
46
- # ...
47
-
48
- SecureNative.track(Event.new(
49
- event_type = EventTypes::LOG_IN,
50
- user: User.new("1", "Jon Snow", "jon@snow.com"),
51
- ip: "1.2.3.4",
52
- remote_ip: "5.6.7.8",
53
- user_agent: "Mozilla/5.0 (Linux; U; Android 4.4.2; zh-cn; GT-I9500 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko)Version/4.0 MQQBrowser/5.0 QQ-URL-Manager Mobile Safari/537.36",
54
- sn_cookie: "cookie"))
116
+ require 'models/event_options'
117
+ require 'enums/event_types'
118
+ require 'models/user_traits'
119
+
120
+
121
+ def track
122
+ securenative = SecureNative.instance
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 })
128
+
129
+ securenative.track(event_options)
130
+
131
+ @message = 'tracked'
55
132
  end
56
133
  ```
57
134
 
58
- ## Verification events
59
- Once the SDK has been initialized, you can start sending new events with the `verify` function:
135
+ ## Verify events
136
+
137
+ **Example**
138
+
60
139
  ```ruby
61
140
  require 'securenative'
62
- require 'securenative/event_type'
63
-
64
-
65
- def reset_password
66
- res = SecureNative.verify(Event.new(
67
- event_type = EventTypes::PASSWORD_RESET,
68
- user: User.new("1", "Jon Snow", "jon@snow.com"),
69
- ip: "1.2.3.4",
70
- remote_ip: "5.6.7.8",
71
- user_agent: "Mozilla/5.0 (Linux; U; Android 4.4.2; zh-cn; GT-I9500 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko)Version/4.0 MQQBrowser/5.0 QQ-URL-Manager Mobile Safari/537.36",
72
- sn_cookie: "cookie"))
73
-
74
- if res['riskLevel'] == 'high'
75
- return 'Cannot change password'
76
- else res['riskLevel'] == 'medium'
77
- return 'MFA'
78
- end
141
+ require 'models/event_options'
142
+ require 'enums/event_types'
143
+ require 'models/user_traits'
144
+
145
+
146
+ def verify(request)
147
+ securenative = SecureNative.instance
148
+ context = SecureNativeContext.from_http_request(request)
149
+
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 })
153
+
154
+ verify_result = securenative.verify(event_options)
155
+ verify_result.risk_level # Low, Medium, High
156
+ verify_result.score # Risk score: 0 -1 (0 - Very Low, 1 - Very High)
157
+ verify_result.triggers # ["TOR", "New IP", "New City"]
79
158
  end
80
159
  ```
81
160
 
82
- ## Using webhooks
83
- You can use the SDK to verify incoming webhooks from SecureNative, just call the `veriy_webhook` function which return a boolean which indicates if the webhook came from Secure Native servers.
161
+ ## Webhook signature verification
162
+
163
+ Apply our filter to verify the request is from us, for example:
164
+
84
165
  ```ruby
85
166
  require 'securenative'
86
167
 
87
- begin
88
- SecureNative.init('YOUR_API_KEY') # Should be called before any other call to securenative
89
- rescue SecureNativeSDKException => e
90
- # Do some error handling
91
- end
92
168
 
93
- def handle_some_code(headers, body)
94
- sig_header = headers["X-SecureNative"]
95
- if SecureNative.verify_webhook(sig_header, body)
96
- # Handle the webhook
97
- level = body['riskLevel']
98
- else
99
- # This request wasn't sent from Secure Native servers, you can dismiss/investigate it
100
- end
169
+ def webhook_endpoint(request)
170
+ securenative = SecureNative.instance
171
+
172
+ # Checks if request is verified
173
+ is_verified = securenative.verify_request_payload(request)
101
174
  end
102
- ```
175
+ ```
data/Rakefile CHANGED
@@ -1,2 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
- task :default => :spec
2
+ task :default => :spec
3
+
4
+ require 'rspec/core/rake_task'
5
+ task :default => :spec
6
+ RSpec::Core::RakeTask.new
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'models/sdk_event'
4
+ require 'enums/failover_strategy'
5
+ require 'enums/risk_level'
6
+ require 'enums/api_route'
7
+ require 'models/verify_result'
8
+ require 'json'
9
+
10
+ class ApiManager
11
+ def initialize(event_manager, securenative_options)
12
+ @event_manager = event_manager
13
+ @options = securenative_options
14
+ end
15
+
16
+ def track(event_options)
17
+ SecureNativeLogger.debug('Track event call')
18
+ event = SDKEvent.new(event_options, @options)
19
+ @event_manager.send_async(event, ApiRoute::TRACK)
20
+ end
21
+
22
+ def verify(event_options)
23
+ SecureNativeLogger.debug('Verify event call')
24
+ event = SDKEvent.new(event_options, @options)
25
+
26
+ begin
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'])
30
+ rescue StandardError => e
31
+ SecureNativeLogger.debug("Failed to call verify; #{e}")
32
+ end
33
+ if @options.fail_over_strategy == FailOverStrategy::FAIL_OPEN
34
+ return VerifyResult.new(risk_level: RiskLevel::LOW, score: 0, triggers: nil)
35
+ end
36
+
37
+ VerifyResult.new(risk_level: RiskLevel::HIGH, score: 1, triggers: nil)
38
+ end
39
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'enums/failover_strategy'
4
+
5
+ class ConfigurationBuilder
6
+ attr_reader :api_key, :api_url, :interval, :max_events, :timeout, :auto_send, :disable, :log_level, :fail_over_strategy
7
+ attr_writer :api_key, :api_url, :interval, :max_events, :timeout, :auto_send, :disable, :log_level, :fail_over_strategy
8
+
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: FailOverStrategy::FAIL_OPEN)
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
+ end
22
+
23
+ def self.default_securenative_options
24
+ SecureNativeOptions.new
25
+ end
26
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+ require 'config/configuration_builder'
5
+
6
+ class ConfigurationManager
7
+ DEFAULT_CONFIG_FILE = 'securenative.yml'
8
+ CUSTOM_CONFIG_FILE_ENV_NAME = 'SECURENATIVE_CONFIG_FILE'
9
+ @config = nil
10
+
11
+ def self.read_resource_file(resource_path)
12
+ properties = {}
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}")
18
+ end
19
+ properties
20
+ end
21
+
22
+ def self._get_resource_path(env_name)
23
+ Env.fetch(env_name, ENV[DEFAULT_CONFIG_FILE])
24
+ end
25
+
26
+ def self.config_builder
27
+ ConfigurationBuilder.new
28
+ end
29
+
30
+ def self._get_env_or_default(properties, key, default)
31
+ return ENV[key] if ENV[key]
32
+ return properties[key] if properties[key]
33
+
34
+ default
35
+ end
36
+
37
+ def self.load_config
38
+ options = ConfigurationBuilder.default_securenative_options
39
+
40
+ resource_path = DEFAULT_CONFIG_FILE
41
+ resource_path = ENV[CUSTOM_CONFIG_FILE_ENV_NAME] unless ENV[CUSTOM_CONFIG_FILE_ENV_NAME].nil?
42
+
43
+ properties = read_resource_file(resource_path)
44
+
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))
54
+ end
55
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'enums/failover_strategy'
4
+
5
+ class SecureNativeOptions
6
+ attr_reader :api_key, :api_url, :interval, :max_events, :timeout, :auto_send, :disable, :log_level, :fail_over_strategy
7
+ attr_writer :api_key, :api_url, :interval, :max_events, :timeout, :auto_send, :disable, :log_level, :fail_over_strategy
8
+
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: FailOverStrategy::FAIL_OPEN)
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
+ end
22
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ class HanamiContext
4
+ SECURENATIVE_COOKIE = '_sn'
5
+
6
+ def self.get_client_token(request)
7
+ begin
8
+ request.env[SECURENATIVE_COOKIE]
9
+ rescue StandardError
10
+ begin
11
+ request.cookies[SECURENATIVE_COOKIE]
12
+ rescue StandardError
13
+ nil
14
+ end
15
+ end
16
+ end
17
+
18
+ def self.get_url(request)
19
+ begin
20
+ request.env['REQUEST_PATH']
21
+ rescue StandardError
22
+ nil
23
+ end
24
+ end
25
+
26
+ def self.get_method(request)
27
+ begin
28
+ request.request_method
29
+ rescue StandardError
30
+ nil
31
+ end
32
+ end
33
+
34
+ def self.get_headers(request)
35
+ begin
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'] }
38
+ rescue StandardError
39
+ nil
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ class RailsContext
4
+ SECURENATIVE_COOKIE = '_sn'
5
+
6
+ def self.get_client_token(request)
7
+ begin
8
+ request.cookies[SECURENATIVE_COOKIE]
9
+ rescue StandardError
10
+ nil
11
+ end
12
+ end
13
+
14
+ def self.get_url(request)
15
+ begin
16
+ # Rails >= 3.x
17
+ request.fullpath
18
+ rescue StandardError
19
+ begin
20
+ # Rails < 3.x & Sinatra
21
+ request.url if url.nil?
22
+ rescue StandardError
23
+ nil
24
+ end
25
+ end
26
+ end
27
+
28
+ def self.get_method(request)
29
+ begin
30
+ request.method
31
+ rescue StandardError
32
+ nil
33
+ end
34
+ end
35
+
36
+ def self.get_headers(request)
37
+ begin
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'] }
40
+ rescue StandardError
41
+ nil
42
+ end
43
+ end
44
+ end