securenative 0.1.17 → 0.1.23
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.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +5 -2
- data/.github/workflows/test.yml +5 -2
- data/.gitignore +3 -1
- data/Gemfile +6 -4
- data/Gemfile.lock +234 -25
- data/README.md +37 -32
- data/lib/api_manager.rb +39 -0
- data/lib/config/configuration_builder.rb +27 -0
- data/lib/config/configuration_manager.rb +56 -0
- data/lib/config/securenative_options.rb +23 -0
- data/lib/context/hanami_context.rb +42 -0
- data/lib/context/rails_context.rb +44 -0
- data/lib/context/securenative_context.rb +67 -0
- data/lib/context/sinatra_context.rb +42 -0
- data/lib/enums/api_route.rb +6 -0
- data/lib/enums/event_types.rb +23 -0
- data/lib/enums/failover_strategy.rb +6 -0
- data/lib/enums/risk_level.rb +7 -0
- data/lib/errors/securenative_config_error.rb +4 -0
- data/lib/errors/securenative_http_error.rb +4 -0
- data/lib/errors/securenative_invalid_options_error.rb +4 -0
- data/lib/errors/securenative_invalid_uri_error.rb +4 -0
- data/lib/errors/securenative_parse_error.rb +4 -0
- data/lib/errors/securenative_sdk_Illegal_state_error.rb +4 -0
- data/lib/errors/securenative_sdk_error.rb +4 -0
- data/lib/event_manager.rb +157 -0
- data/lib/{securenative/http/http_response.rb → http/secure_native_http_response.rb} +4 -2
- data/lib/http/securenative_http_client.rb +50 -0
- data/lib/{securenative/models → models}/client_token.rb +2 -0
- data/lib/{securenative/models → models}/device.rb +3 -1
- data/lib/models/event_options.rb +37 -0
- data/lib/{securenative/models → models}/request_context.rb +7 -4
- data/lib/models/request_options.rb +12 -0
- data/lib/models/sdk_event.rb +49 -0
- data/lib/{securenative/models → models}/user_traits.rb +4 -2
- data/lib/{securenative/models → models}/verify_result.rb +8 -2
- data/lib/securenative.rb +83 -0
- data/lib/utils/date_utils.rb +9 -0
- data/lib/utils/encryption_utils.rb +49 -0
- data/lib/{securenative/utils → utils}/ip_utils.rb +7 -6
- data/lib/utils/request_utils.rb +69 -0
- data/lib/{securenative/logger.rb → utils/secure_native_logger.rb} +5 -3
- data/lib/{securenative/utils → utils}/signature_utils.rb +4 -2
- data/lib/utils/utils.rb +9 -0
- data/lib/utils/version_utils.rb +11 -0
- data/out/production/securenative-ruby/api_manager.rb +39 -0
- data/out/production/securenative-ruby/config/configuration_builder.rb +27 -0
- data/out/production/securenative-ruby/config/configuration_manager.rb +56 -0
- data/out/production/securenative-ruby/config/securenative_options.rb +23 -0
- data/out/production/securenative-ruby/context/hanami_context.rb +42 -0
- data/out/production/securenative-ruby/context/rails_context.rb +44 -0
- data/out/production/securenative-ruby/context/securenative_context.rb +67 -0
- data/out/production/securenative-ruby/context/sinatra_context.rb +42 -0
- data/out/production/securenative-ruby/enums/api_route.rb +6 -0
- data/out/production/securenative-ruby/enums/event_types.rb +23 -0
- data/out/production/securenative-ruby/enums/failover_strategy.rb +6 -0
- data/out/production/securenative-ruby/enums/risk_level.rb +7 -0
- data/out/production/securenative-ruby/errors/securenative_config_error.rb +4 -0
- data/out/production/securenative-ruby/errors/securenative_http_error.rb +4 -0
- data/out/production/securenative-ruby/errors/securenative_invalid_options_error.rb +4 -0
- data/out/production/securenative-ruby/errors/securenative_invalid_uri_error.rb +4 -0
- data/out/production/securenative-ruby/errors/securenative_parse_error.rb +4 -0
- data/out/production/securenative-ruby/errors/securenative_sdk_Illegal_state_error.rb +4 -0
- data/out/production/securenative-ruby/errors/securenative_sdk_error.rb +4 -0
- data/out/production/securenative-ruby/event_manager.rb +157 -0
- data/out/production/securenative-ruby/http/secure_native_http_response.rb +12 -0
- data/out/production/securenative-ruby/http/securenative_http_client.rb +50 -0
- data/out/production/securenative-ruby/models/client_token.rb +12 -0
- data/out/production/securenative-ruby/models/device.rb +10 -0
- data/out/production/securenative-ruby/models/event_options.rb +37 -0
- data/out/production/securenative-ruby/models/request_context.rb +18 -0
- data/out/production/securenative-ruby/models/request_options.rb +12 -0
- data/out/production/securenative-ruby/models/sdk_event.rb +49 -0
- data/out/production/securenative-ruby/models/user_traits.rb +13 -0
- data/out/production/securenative-ruby/models/verify_result.rb +16 -0
- data/out/production/securenative-ruby/securenative.rb +83 -0
- data/out/production/securenative-ruby/utils/date_utils.rb +9 -0
- data/out/production/securenative-ruby/utils/encryption_utils.rb +49 -0
- data/out/production/securenative-ruby/utils/ip_utils.rb +23 -0
- data/out/production/securenative-ruby/utils/request_utils.rb +69 -0
- data/out/production/securenative-ruby/utils/secure_native_logger.rb +44 -0
- data/out/production/securenative-ruby/utils/signature_utils.rb +16 -0
- data/out/production/securenative-ruby/utils/utils.rb +9 -0
- data/out/production/securenative-ruby/utils/version_utils.rb +11 -0
- data/out/test/securenative-ruby/spec_api_manager.rb +87 -0
- data/out/test/securenative-ruby/spec_context_builder.rb +87 -0
- data/out/test/securenative-ruby/spec_date_utils.rb +13 -0
- data/out/test/securenative-ruby/spec_encryption_utils.rb +26 -0
- data/out/test/securenative-ruby/spec_event_manager.rb +93 -0
- data/out/test/securenative-ruby/spec_helper.rb +28 -0
- data/out/test/securenative-ruby/spec_ip_utils.rb +41 -0
- data/out/test/securenative-ruby/spec_request_utils.rb +25 -0
- data/out/test/securenative-ruby/spec_sdk_event.rb +24 -0
- data/out/test/securenative-ruby/spec_securenative.rb +61 -0
- data/out/test/securenative-ruby/spec_securenative_http_client.rb +31 -0
- data/out/test/securenative-ruby/spec_signature_utils.rb +18 -0
- data/out/test/securenative-ruby/spec_version_util.rb +10 -0
- data/securenative.gemspec +2 -2
- metadata +93 -43
- data/.travis.yml +0 -6
- data/VERSION +0 -1
- data/lib/securenative/api_manager.rb +0 -30
- data/lib/securenative/config/configuration_builder.rb +0 -26
- data/lib/securenative/config/configuration_manager.rb +0 -53
- data/lib/securenative/config/securenative_options.rb +0 -18
- data/lib/securenative/context/context_builder.rb +0 -59
- data/lib/securenative/context/securenative_context.rb +0 -14
- data/lib/securenative/enums/api_route.rb +0 -4
- data/lib/securenative/enums/event_types.rb +0 -21
- data/lib/securenative/enums/failover_strategy.rb +0 -4
- data/lib/securenative/enums/risk_level.rb +0 -5
- data/lib/securenative/event_manager.rb +0 -150
- data/lib/securenative/event_options_builder.rb +0 -30
- data/lib/securenative/exceptions/securenative_config_exception.rb +0 -2
- data/lib/securenative/exceptions/securenative_http_exception.rb +0 -2
- data/lib/securenative/exceptions/securenative_invalid_options_exception.rb +0 -2
- data/lib/securenative/exceptions/securenative_invalid_uri_exception.rb +0 -2
- data/lib/securenative/exceptions/securenative_parse_exception.rb +0 -2
- data/lib/securenative/exceptions/securenative_sdk_Illegal_state_exception.rb +0 -2
- data/lib/securenative/exceptions/securenative_sdk_exception.rb +0 -2
- data/lib/securenative/http/securenative_http_client.rb +0 -30
- data/lib/securenative/models/event_options.rb +0 -13
- data/lib/securenative/models/request_options.rb +0 -10
- data/lib/securenative/models/sdk_event.rb +0 -25
- data/lib/securenative/securenative.iml +0 -9
- data/lib/securenative/securenative.rb +0 -82
- data/lib/securenative/utils/date_utils.rb +0 -7
- data/lib/securenative/utils/encryption_utils.rb +0 -38
- data/lib/securenative/utils/request_utils.rb +0 -21
- data/lib/securenative/utils/utils.rb +0 -9
- data/lib/securenative/utils/version_utils.rb +0 -10
@@ -0,0 +1,83 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'utils/secure_native_logger'
|
4
|
+
require 'utils/signature_utils'
|
5
|
+
require 'utils/utils'
|
6
|
+
require 'errors/securenative_sdk_error'
|
7
|
+
require 'errors/securenative_sdk_Illegal_state_error'
|
8
|
+
require 'errors/securenative_config_error'
|
9
|
+
require 'enums/failover_strategy'
|
10
|
+
require 'config/configuration_builder'
|
11
|
+
require 'config/configuration_manager'
|
12
|
+
require 'event_manager'
|
13
|
+
require 'api_manager'
|
14
|
+
|
15
|
+
class SecureNative
|
16
|
+
attr_reader :options
|
17
|
+
|
18
|
+
def initialize(options)
|
19
|
+
@securenative = nil
|
20
|
+
raise SecureNativeSDKError, 'You must pass your SecureNative api key' if Utils.null_or_empty?(options.api_key)
|
21
|
+
|
22
|
+
@options = options
|
23
|
+
@event_manager = EventManager.new(@options)
|
24
|
+
|
25
|
+
@event_manager.start_event_persist unless @options.api_url.nil?
|
26
|
+
|
27
|
+
@api_manager = ApiManager.new(@event_manager, @options)
|
28
|
+
SecureNativeLogger.init_logger(@options.log_level)
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.init_with_options(options)
|
32
|
+
if @securenative.nil?
|
33
|
+
@securenative = SecureNative.new(options)
|
34
|
+
@securenative
|
35
|
+
else
|
36
|
+
SecureNativeLogger.debug('This SDK was already initialized.')
|
37
|
+
raise SecureNativeSDKError, 'This SDK was already initialized.'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.init_with_api_key(api_key)
|
42
|
+
raise SecureNativeConfigError, 'You must pass your SecureNative api key' if Utils.null_or_empty?(api_key)
|
43
|
+
|
44
|
+
if @securenative.nil?
|
45
|
+
options = ConfigurationBuilder.new(api_key: api_key)
|
46
|
+
@securenative = SecureNative.new(options)
|
47
|
+
@securenative
|
48
|
+
else
|
49
|
+
SecureNativeLogger.debug('This SDK was already initialized.')
|
50
|
+
raise SecureNativeSDKError, 'This SDK was already initialized.'
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.init
|
55
|
+
options = ConfigurationManager.load_config
|
56
|
+
init_with_options(options)
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.instance
|
60
|
+
raise SecureNativeSDKIllegalStateError if @securenative.nil?
|
61
|
+
|
62
|
+
@securenative
|
63
|
+
end
|
64
|
+
|
65
|
+
def track(event_options)
|
66
|
+
@api_manager.track(event_options)
|
67
|
+
end
|
68
|
+
|
69
|
+
def verify(event_options)
|
70
|
+
@api_manager.verify(event_options)
|
71
|
+
end
|
72
|
+
|
73
|
+
def self._flush
|
74
|
+
@securenative = nil
|
75
|
+
end
|
76
|
+
|
77
|
+
def verify_request_payload(request)
|
78
|
+
request_signature = request.header[SignatureUtils.SIGNATURE_HEADER]
|
79
|
+
body = request.body
|
80
|
+
|
81
|
+
SignatureUtils.valid_signature?(@options.api_key, body, request_signature)
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'openssl'
|
4
|
+
require 'digest'
|
5
|
+
require 'base64'
|
6
|
+
require 'models/client_token'
|
7
|
+
|
8
|
+
class EncryptionUtils
|
9
|
+
def self.padding_key(key, length)
|
10
|
+
if key.length == length
|
11
|
+
key
|
12
|
+
else
|
13
|
+
if key.length > length
|
14
|
+
key.slice(0, length)
|
15
|
+
else
|
16
|
+
(length - key.length).times { key << '0' }
|
17
|
+
key
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.encrypt(plain_text, secret_key)
|
23
|
+
begin
|
24
|
+
cipher = OpenSSL::Cipher.new('aes-256-cbc')
|
25
|
+
cipher.encrypt
|
26
|
+
iv = cipher.random_iv
|
27
|
+
cipher.key = padding_key(secret_key, 32)
|
28
|
+
encrypted = cipher.update(plain_text) + cipher.final
|
29
|
+
(iv + encrypted).unpack1('H*')
|
30
|
+
rescue StandardError
|
31
|
+
''
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.decrypt(cipher_text, secret_key)
|
36
|
+
begin
|
37
|
+
cipher = OpenSSL::Cipher.new('aes-256-cbc')
|
38
|
+
cipher.decrypt
|
39
|
+
raw_data = [cipher_text].pack('H*')
|
40
|
+
cipher.iv = raw_data.slice(0, 16)
|
41
|
+
cipher.key = padding_key(secret_key, 32)
|
42
|
+
decrypted = JSON.parse(cipher.update(raw_data.slice(16, raw_data.length)) + cipher.final)
|
43
|
+
|
44
|
+
return ClientToken.new(decrypted['cid'], decrypted['vid'], decrypted['fp'])
|
45
|
+
rescue StandardError
|
46
|
+
ClientToken.new('', '','')
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "resolv"
|
4
|
+
|
5
|
+
class IpUtils
|
6
|
+
def self.ip_address?(ip_address)
|
7
|
+
return true if ip_address =~ Resolv::IPv4::Regex
|
8
|
+
return true if ip_address =~ Resolv::IPv6::Regex
|
9
|
+
|
10
|
+
false
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.valid_public_ip?(ip_address)
|
14
|
+
ip = IPAddr.new(ip_address)
|
15
|
+
return false if ip.loopback? || ip.private? || ip.link_local? || ip.untrusted? || ip.tainted?
|
16
|
+
|
17
|
+
true
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.loop_back?(ip_address)
|
21
|
+
IPAddr.new(ip_address).loopback?
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class RequestUtils
|
4
|
+
SECURENATIVE_COOKIE = '_sn'
|
5
|
+
SECURENATIVE_HEADER = 'x-securenative'
|
6
|
+
|
7
|
+
def self.get_secure_header_from_request(headers)
|
8
|
+
begin
|
9
|
+
return headers[SECURENATIVE_HEADER] unless headers.nil?
|
10
|
+
rescue StandardError
|
11
|
+
[]
|
12
|
+
end
|
13
|
+
[]
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.get_client_ip_from_request(request, options = nil)
|
17
|
+
begin
|
18
|
+
return request.ip unless request.ip.nil?
|
19
|
+
rescue NoMethodError
|
20
|
+
end
|
21
|
+
|
22
|
+
begin
|
23
|
+
x_forwarded_for = request.env['HTTP_X_FORWARDED_FOR']
|
24
|
+
return x_forwarded_for unless x_forwarded_for.nil?
|
25
|
+
rescue NoMethodError
|
26
|
+
begin
|
27
|
+
x_forwarded_for = request['HTTP_X_FORWARDED_FOR']
|
28
|
+
return x_forwarded_for unless x_forwarded_for.nil?
|
29
|
+
rescue NoMethodError
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
begin
|
34
|
+
x_forwarded_for = request.env['REMOTE_ADDR']
|
35
|
+
return x_forwarded_for unless x_forwarded_for.nil?
|
36
|
+
rescue NoMethodError
|
37
|
+
begin
|
38
|
+
x_forwarded_for = request['REMOTE_ADDR']
|
39
|
+
return x_forwarded_for unless x_forwarded_for.nil?
|
40
|
+
rescue NoMethodError
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
unless options.nil?
|
45
|
+
for header in options.proxy_headers do
|
46
|
+
begin
|
47
|
+
h = request.env[header]
|
48
|
+
return h unless h.nil?
|
49
|
+
rescue NoMethodError
|
50
|
+
begin
|
51
|
+
h = request[header]
|
52
|
+
return h unless h.nil?
|
53
|
+
rescue NoMethodError
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
''
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.get_remote_ip_from_request(request)
|
63
|
+
begin
|
64
|
+
request.remote_ip
|
65
|
+
rescue NoMethodError
|
66
|
+
''
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'logger'
|
4
|
+
|
5
|
+
class SecureNativeLogger
|
6
|
+
@logger = Logger.new(STDOUT)
|
7
|
+
|
8
|
+
def self.init_logger(level = 'DEBUG')
|
9
|
+
@logger.level = case level
|
10
|
+
when 'WARN'
|
11
|
+
Logger::WARN
|
12
|
+
when 'DEBUG'
|
13
|
+
Logger::DEBUG
|
14
|
+
when 'ERROR'
|
15
|
+
Logger::ERROR
|
16
|
+
when 'FATAL'
|
17
|
+
Logger::FATAL
|
18
|
+
when 'INFO'
|
19
|
+
Logger::INFO
|
20
|
+
else
|
21
|
+
Logger::FATAL
|
22
|
+
end
|
23
|
+
|
24
|
+
@logger.formatter = proc do |severity, datetime, progname, msg|
|
25
|
+
"[#{datetime}] #{severity} (#{progname}): #{msg}\n"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.info(msg)
|
30
|
+
@logger.info(msg)
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.debug(msg)
|
34
|
+
@logger.debug(msg)
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.warning(msg)
|
38
|
+
@logger.warning(msg)
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.error(msg)
|
42
|
+
@logger.error(msg)
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'openssl'
|
4
|
+
|
5
|
+
class SignatureUtils
|
6
|
+
SIGNATURE_HEADER = 'x-securenative'
|
7
|
+
|
8
|
+
def self.valid_signature?(api_key, payload, header_signature)
|
9
|
+
key = api_key.encode('utf-8')
|
10
|
+
body = payload.encode('utf-8')
|
11
|
+
calculated_signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha512'), key, body)
|
12
|
+
calculated_signature.eql? header_signature
|
13
|
+
rescue StandardError
|
14
|
+
false
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'api_manager'
|
4
|
+
require 'webmock/rspec'
|
5
|
+
require 'config/configuration_builder'
|
6
|
+
require 'errors/securenative_invalid_options_error'
|
7
|
+
require 'models/event_options'
|
8
|
+
require 'models/verify_result'
|
9
|
+
require 'models/user_traits'
|
10
|
+
require 'enums/event_types'
|
11
|
+
require 'enums/risk_level'
|
12
|
+
require 'event_manager'
|
13
|
+
require 'rspec'
|
14
|
+
|
15
|
+
RSpec.describe ApiManager do
|
16
|
+
it 'tracks an event' do
|
17
|
+
options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', auto_send: true, interval: 10, api_url: 'https://api.securenative-stg.com/collector/api/v1')
|
18
|
+
|
19
|
+
stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/track').to_return(status: 200)
|
20
|
+
event_manager = EventManager.new(options)
|
21
|
+
event_manager.start_event_persist
|
22
|
+
api_manager = ApiManager.new(event_manager, options)
|
23
|
+
event_options = EventOptions.new(event: EventTypes::LOG_IN, user_id: 'USER_ID',
|
24
|
+
user_traits: UserTraits.new(name: 'USER_NAME', email: 'USER_EMAIL', phone: '+1234567890'),
|
25
|
+
properties: { prop1: 'CUSTOM_PARAM_VALUE', prop2: true, prop3: 3 })
|
26
|
+
|
27
|
+
begin
|
28
|
+
res = api_manager.track(event_options)
|
29
|
+
ensure
|
30
|
+
event_manager.stop_event_persist
|
31
|
+
end
|
32
|
+
|
33
|
+
expect(res).to_not be_nil
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'uses invalid options' do
|
37
|
+
options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', auto_send: true, interval: 10, api_url: 'https://api.securenative-stg.com/collector/api/v1')
|
38
|
+
|
39
|
+
properties = {}
|
40
|
+
(0..12).each do |i|
|
41
|
+
properties[i] = i
|
42
|
+
end
|
43
|
+
|
44
|
+
stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/track').to_return(status: 200)
|
45
|
+
event_manager = EventManager.new(options)
|
46
|
+
event_manager.start_event_persist
|
47
|
+
api_manager = ApiManager.new(event_manager, options)
|
48
|
+
|
49
|
+
begin
|
50
|
+
expect { api_manager.track(EventOptions.new(event: EventTypes::LOG_IN, properties: properties)) }
|
51
|
+
.to raise_error(SecureNativeInvalidOptionsError)
|
52
|
+
ensure
|
53
|
+
event_manager.stop_event_persist
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'verifies an event' do
|
58
|
+
options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', api_url: 'https://api.securenative-stg.com/collector/api/v1')
|
59
|
+
|
60
|
+
stub_request(:post, "https://api.securenative-stg.com/collector/api/v1/verify").
|
61
|
+
with(
|
62
|
+
headers: {
|
63
|
+
'Accept'=>'*/*',
|
64
|
+
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
|
65
|
+
'Authorization'=>'YOUR_API_KEY',
|
66
|
+
'Content-Type'=>'application/json',
|
67
|
+
'Sn-Version'=>'0.1.22',
|
68
|
+
'User-Agent'=>'SecureNative-ruby'
|
69
|
+
}).
|
70
|
+
to_return(status: 200, body: "", headers: {})
|
71
|
+
|
72
|
+
|
73
|
+
event_manager = EventManager.new(options)
|
74
|
+
event_manager.start_event_persist
|
75
|
+
api_manager = ApiManager.new(event_manager, options)
|
76
|
+
event_options = EventOptions.new(event: EventTypes::LOG_IN, user_id: 'USER_ID',
|
77
|
+
user_traits: UserTraits.new(name: 'USER_NAME', email: 'USER_EMAIL', phone: '+1234567890'),
|
78
|
+
properties: { prop1: 'CUSTOM_PARAM_VALUE', prop2: true, prop3: 3 })
|
79
|
+
|
80
|
+
result = api_manager.verify(event_options)
|
81
|
+
|
82
|
+
expect(result).not_to be_nil
|
83
|
+
expect(result.risk_level).to eq('low')
|
84
|
+
expect(result.score).to eq(0)
|
85
|
+
expect(result.triggers).to eq(nil)
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'context/securenative_context'
|
4
|
+
require 'webmock/rspec'
|
5
|
+
require 'rails'
|
6
|
+
require 'hanami'
|
7
|
+
require 'sinatra'
|
8
|
+
require 'rspec'
|
9
|
+
|
10
|
+
RSpec.describe SecureNativeContext do
|
11
|
+
it 'creates context from ruby default request' do
|
12
|
+
stub_request(:any, 'www.example.com')
|
13
|
+
.to_return(status: 200,
|
14
|
+
headers: { '_sn': '71532c1fad2c7f56118f7969e401f3cf080239140d208e7934e6a530818c37e544a0c2330a487bcc6fe4f662a57f265a3ed9f37871e80529128a5e4f2ca02db0fb975ded401398f698f19bb0cafd68a239c6caff99f6f105286ab695eaf3477365bdef524f5d70d9be1d1d474506b433aed05d7ed9a435eeca357de57817b37c638b6bb417ffb101eaf856987615a77a' })
|
15
|
+
|
16
|
+
request = Net::HTTP.get_response('www.example.com', '/')
|
17
|
+
context = SecureNativeContext.from_http_request(request)
|
18
|
+
|
19
|
+
expect(context.ip).to eq('')
|
20
|
+
expect(context.http_method).to eq('')
|
21
|
+
expect(context.url).to eq('')
|
22
|
+
expect(context.remote_ip).to eq('')
|
23
|
+
expect(context.headers['-sn']).to eq(['71532c1fad2c7f56118f7969e401f3cf080239140d208e7934e6a530818c37e544a0c2330a487bcc6fe4f662a57f265a3ed9f37871e80529128a5e4f2ca02db0fb975ded401398f698f19bb0cafd68a239c6caff99f6f105286ab695eaf3477365bdef524f5d70d9be1d1d474506b433aed05d7ed9a435eeca357de57817b37c638b6bb417ffb101eaf856987615a77a'])
|
24
|
+
expect(context.body).to eq('')
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'creates context from rails request' do
|
28
|
+
request = ActionDispatch::Request.new(nil)
|
29
|
+
context = SecureNativeContext.from_http_request(request)
|
30
|
+
|
31
|
+
expect(context.ip).to eq('')
|
32
|
+
expect(context.http_method).to eq('')
|
33
|
+
expect(context.url).to eq('')
|
34
|
+
expect(context.remote_ip).to eq('')
|
35
|
+
expect(context.headers).to eq([])
|
36
|
+
expect(context.body).to eq('')
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'creates context from sinatra request' do
|
40
|
+
request = Sinatra::Request.new(nil)
|
41
|
+
context = SecureNativeContext.from_http_request(request)
|
42
|
+
|
43
|
+
expect(context.ip).to eq('')
|
44
|
+
expect(context.http_method).to eq('')
|
45
|
+
expect(context.url).to eq('')
|
46
|
+
expect(context.remote_ip).to eq('')
|
47
|
+
expect(context.headers).to eq([])
|
48
|
+
expect(context.body).to eq('')
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'creates context from hanami request' do
|
52
|
+
request = Hanami::Action::Request
|
53
|
+
context = SecureNativeContext.from_http_request(request)
|
54
|
+
|
55
|
+
expect(context.ip).to eq('')
|
56
|
+
expect(context.http_method).to eq('')
|
57
|
+
expect(context.url).to eq('')
|
58
|
+
expect(context.remote_ip).to eq('')
|
59
|
+
expect(context.headers).to eq([])
|
60
|
+
expect(context.body).to eq('')
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'creates default context builder' do
|
64
|
+
context = SecureNativeContext.default_context_builder
|
65
|
+
|
66
|
+
expect(context.client_token).to eq('')
|
67
|
+
expect(context.ip).to eq('')
|
68
|
+
expect(context.http_method).to eq('')
|
69
|
+
expect(context.url).to eq('')
|
70
|
+
expect(context.remote_ip).to eq('')
|
71
|
+
expect(context.headers).to be_nil
|
72
|
+
expect(context.body).to eq('')
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'creates custom context with context builder' do
|
76
|
+
context = SecureNativeContext.new(client_token: 'SECRET_TOKEN', ip: '10.0.0.0', remote_ip: '10.0.0.0',
|
77
|
+
headers: { 'header' => 'value1' }, url: '/some-url', http_method: 'Get', body: nil)
|
78
|
+
|
79
|
+
expect(context.url).to eq('/some-url')
|
80
|
+
expect(context.client_token).to eq('SECRET_TOKEN')
|
81
|
+
expect(context.ip).to eq('10.0.0.0')
|
82
|
+
expect(context.body).to be_nil
|
83
|
+
expect(context.http_method).to eq('Get')
|
84
|
+
expect(context.remote_ip).to eq('10.0.0.0')
|
85
|
+
expect(context.headers).to eq({ 'header' => 'value1' })
|
86
|
+
end
|
87
|
+
end
|