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
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'models/event_options'
4
+ require 'models/user_traits'
5
+ require 'errors/securenative_invalid_options_error'
6
+
7
+ class EventOptions
8
+ attr_reader :event, :user_id, :user_traits, :context, :properties, :timestamp
9
+ attr_writer :event, :user_id, :user_traits, :context, :properties, :timestamp
10
+
11
+ MAX_PROPERTIES_SIZE = 10
12
+
13
+ def initialize(event: nil, user_id: nil, user_traits: nil, user_name: nil, email: nil, phone: nil, created_at: nil, context: nil, properties: nil, timestamp: nil)
14
+ if !properties.nil? && properties.length > MAX_PROPERTIES_SIZE
15
+ raise SecureNativeInvalidOptionsError, "You can have only up to #{MAX_PROPERTIES_SIZE} custom properties"
16
+ end
17
+
18
+ if user_traits.nil?
19
+ if user_name && email && phone && created_at
20
+ user_traits = UserTraits(user_name, email, phone, created_at)
21
+ elsif user_name && email && phone
22
+ user_traits = UserTraits(user_name, email, phone)
23
+ elsif user_name && email
24
+ user_traits = UserTraits(user_name, email)
25
+ else
26
+ user_traits = UserTraits.new
27
+ end
28
+ end
29
+
30
+ @event = event
31
+ @user_id = user_id
32
+ @user_traits = user_traits
33
+ @context = context
34
+ @properties = properties
35
+ @timestamp = timestamp
36
+ end
37
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ class RequestContext
4
+ attr_reader :cid, :vid, :fp, :ip, :remote_ip, :headers, :url, :http_method
5
+ attr_writer :cid, :vid, :fp, :ip, :remote_ip, :headers, :url, :http_method
6
+
7
+ def initialize(cid: nil, vid: nil, fp: nil, ip: nil, remote_ip: nil, headers: nil, url: nil, http_method: nil)
8
+ @cid = cid
9
+ @vid = vid
10
+ @fp = fp
11
+ @ip = ip
12
+ @remote_ip = remote_ip
13
+ @headers = headers
14
+ @url = url
15
+ @method = http_method
16
+ end
17
+ end
18
+
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ class RequestOptions
4
+ attr_reader :url, :body, :retry_sending
5
+ attr_writer :url, :body, :retry_sending
6
+
7
+ def initialize(url, body, retry_sending)
8
+ @url = url
9
+ @body = body
10
+ @retry_sending = retry_sending
11
+ end
12
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'context/securenative_context'
4
+ require 'utils/encryption_utils'
5
+ require 'utils/date_utils'
6
+ require 'models/request_context'
7
+ require 'securerandom'
8
+
9
+ class SDKEvent
10
+ attr_reader :context, :rid, :event_type, :user_id, :user_traits, :request, :timestamp, :properties
11
+ attr_writer :context, :rid, :event_type, :user_id, :user_traits, :request, :timestamp, :properties
12
+
13
+ def initialize(event_options, securenative_options)
14
+ @context = if !event_options.context.nil?
15
+ event_options.context
16
+ else
17
+ SecureNativeContext.default_context_builder
18
+ end
19
+
20
+ client_token = EncryptionUtils.decrypt(@context.client_token, securenative_options.api_key)
21
+
22
+ @rid = SecureRandom.uuid.to_str
23
+ @event_type = event_options.event
24
+ @user_id = event_options.user_id
25
+ @user_traits = event_options.user_traits
26
+ @request = RequestContext.new(cid: client_token ? client_token.cid : '', vid: client_token ? client_token.vid : '',
27
+ fp: client_token ? client_token.fp : '', ip: @context.ip,
28
+ remote_ip: @context.remote_ip, headers: @context.headers,
29
+ url: @context.url, http_method: @context.http_method)
30
+
31
+
32
+ @timestamp = DateUtils.to_timestamp(event_options.timestamp)
33
+ @properties = event_options.properties
34
+ end
35
+
36
+ def to_s
37
+ "context: #{@context}, rid: #{@rid}, event_type: #{@event_type}, user_id: #{@user_id},
38
+ user_traits: #{@user_traits}, request: #{@request}, timestamp: #{@timestamp}, properties: #{@properties}"
39
+ end
40
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ class UserTraits
4
+ attr_reader :name, :email, :phone, :created_at
5
+ attr_writer :name, :email, :phone, :created_at
6
+
7
+ def initialize(name: nil, email: nil, phone: nil, created_at: nil)
8
+ @name = name
9
+ @email = email
10
+ @created_at = created_at
11
+ @phone = phone
12
+ end
13
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ class VerifyResult
4
+ attr_reader :risk_level, :score, :triggers
5
+ attr_writer :risk_level, :score, :triggers
6
+
7
+ def initialize(risk_level: nil, score: nil, triggers: nil)
8
+ @risk_level = risk_level
9
+ @score = score
10
+ @triggers = triggers
11
+ end
12
+
13
+ def to_s
14
+ "risk_level: #{@risk_level}, score: #{@score}, triggers: #{@triggers}"
15
+ end
16
+ end
@@ -1,39 +1,83 @@
1
- require_relative 'securenative/sn_exception'
2
- require_relative 'securenative/secure_native_sdk'
1
+ # frozen_string_literal: true
3
2
 
4
- $securenative = nil
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'
5
14
 
6
- module SecureNative
7
- def self.init(api_key, options: SecureNativeOptions.new)
8
- if $securenative == nil
9
- $securenative = SecureNativeSDK.new(api_key, options: options)
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.'
10
38
  end
11
39
  end
12
40
 
13
- def self.track(event)
14
- sdk = _get_or_throw
15
- sdk.track(event)
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
16
52
  end
17
53
 
18
- def self.verify(event)
19
- sdk = _get_or_throw
20
- sdk.verify(event)
54
+ def self.init
55
+ options = ConfigurationManager.load_config
56
+ init_with_options(options)
21
57
  end
22
58
 
23
- def self.verify_webhook(hmac_header, body)
24
- sdk = _get_or_throw
25
- sdk.verify_webhook(hmac_header = hmac_header, body = body)
59
+ def self.instance
60
+ raise SecureNativeSDKIllegalStateError if @securenative.nil?
61
+
62
+ @securenative
26
63
  end
27
64
 
28
- def self.flush
29
- sdk = _get_or_throw
30
- sdk.flush
65
+ def track(event_options)
66
+ @api_manager.track(event_options)
31
67
  end
32
68
 
33
- def self._get_or_throw
34
- if $securenative == nil
35
- raise SecureNativeSDKException.new
36
- end
37
- $securenative
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)
38
82
  end
39
- end
83
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DateUtils
4
+ def self.to_timestamp(date)
5
+ return Time.now.utc.iso8601 if date.nil?
6
+
7
+ Time.parse(date).iso8601
8
+ end
9
+ 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,54 @@
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)
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
+ ''
45
+ end
46
+
47
+ def self.get_remote_ip_from_request(request)
48
+ begin
49
+ request.remote_ip
50
+ rescue NoMethodError
51
+ ''
52
+ end
53
+ end
54
+ 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