securenative 0.1.28 → 0.1.33

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +2 -2
  3. data/README.md +23 -32
  4. data/lib/securenative.rb +49 -0
  5. data/lib/securenative/api_manager.rb +10 -17
  6. data/lib/securenative/client.rb +79 -0
  7. data/lib/securenative/{models/client_token.rb → client_token.rb} +0 -0
  8. data/lib/securenative/config/configuration_builder.rb +22 -22
  9. data/lib/securenative/config/configuration_manager.rb +42 -43
  10. data/lib/securenative/context.rb +65 -0
  11. data/lib/securenative/{models/device.rb → device.rb} +0 -0
  12. data/lib/securenative/enums/api_route.rb +5 -3
  13. data/lib/securenative/enums/risk_level.rb +7 -5
  14. data/{out/production/securenative-ruby/errors/securenative_config_error.rb → lib/securenative/errors/config_error.rb} +0 -0
  15. data/{out/production/securenative-ruby/errors/securenative_http_error.rb → lib/securenative/errors/http_error.rb} +0 -0
  16. data/{out/production/securenative-ruby/errors/securenative_invalid_options_error.rb → lib/securenative/errors/invalid_options_error.rb} +0 -0
  17. data/lib/securenative/errors/{securenative_invalid_uri_error.rb → invalid_uri_error.rb} +0 -0
  18. data/lib/securenative/errors/{securenative_parse_error.rb → parse_error.rb} +0 -2
  19. data/{out/production/securenative-ruby/errors/securenative_sdk_Illegal_state_error.rb → lib/securenative/errors/sdk_Illegal_state_error.rb} +0 -0
  20. data/{out/production/securenative-ruby/errors/securenative_sdk_error.rb → lib/securenative/errors/sdk_error.rb} +0 -0
  21. data/lib/securenative/event_manager.rb +125 -127
  22. data/lib/securenative/{models/event_options.rb → event_options.rb} +3 -7
  23. data/lib/securenative/{enums/event_types.rb → event_types.rb} +0 -0
  24. data/lib/securenative/{enums/failover_strategy.rb → failover_strategy.rb} +0 -0
  25. data/lib/securenative/frameworks/hanami.rb +46 -0
  26. data/lib/securenative/frameworks/rails.rb +48 -0
  27. data/lib/securenative/frameworks/sinatra.rb +46 -0
  28. data/lib/securenative/{http/securenative_http_client.rb → http_client.rb} +7 -12
  29. data/lib/securenative/{http/secure_native_http_response.rb → http_response.rb} +1 -1
  30. data/lib/securenative/{config/securenative_options.rb → options.rb} +1 -3
  31. data/lib/securenative/{models/request_context.rb → request_context.rb} +0 -0
  32. data/lib/securenative/{models/request_options.rb → request_options.rb} +0 -0
  33. data/lib/securenative/{models/sdk_event.rb → sdk_event.rb} +4 -11
  34. data/lib/securenative/{models/user_traits.rb → user_traits.rb} +0 -0
  35. data/lib/securenative/utils/date_utils.rb +6 -4
  36. data/lib/securenative/utils/encryption_utils.rb +34 -37
  37. data/lib/securenative/utils/ip_utils.rb +15 -15
  38. data/lib/securenative/utils/{secure_native_logger.rb → log.rb} +1 -1
  39. data/lib/securenative/utils/request_utils.rb +66 -47
  40. data/lib/securenative/utils/signature_utils.rb +12 -12
  41. data/lib/securenative/utils/utils.rb +6 -4
  42. data/lib/securenative/utils/version_utils.rb +9 -7
  43. data/lib/securenative/{models/verify_result.rb → verify_result.rb} +0 -0
  44. data/lib/securenative/{errors/securenative_sdk_error.rb → version.rb} +1 -2
  45. data/securenative.gemspec +5 -2
  46. data/{out/test/securenative-ruby → spec}/spec_helper.rb +0 -0
  47. metadata +35 -83
  48. data/lib/securenative/context/hanami_context.rb +0 -44
  49. data/lib/securenative/context/rails_context.rb +0 -46
  50. data/lib/securenative/context/securenative_context.rb +0 -69
  51. data/lib/securenative/context/sinatra_context.rb +0 -44
  52. data/lib/securenative/errors/securenative_config_error.rb +0 -6
  53. data/lib/securenative/errors/securenative_http_error.rb +0 -6
  54. data/lib/securenative/errors/securenative_invalid_options_error.rb +0 -6
  55. data/lib/securenative/errors/securenative_sdk_Illegal_state_error.rb +0 -6
  56. data/lib/securenative/sdk.rb +0 -85
  57. data/out/production/securenative-ruby/api_manager.rb +0 -39
  58. data/out/production/securenative-ruby/config/configuration_builder.rb +0 -27
  59. data/out/production/securenative-ruby/config/configuration_manager.rb +0 -56
  60. data/out/production/securenative-ruby/config/securenative_options.rb +0 -23
  61. data/out/production/securenative-ruby/context/hanami_context.rb +0 -42
  62. data/out/production/securenative-ruby/context/rails_context.rb +0 -44
  63. data/out/production/securenative-ruby/context/securenative_context.rb +0 -67
  64. data/out/production/securenative-ruby/context/sinatra_context.rb +0 -42
  65. data/out/production/securenative-ruby/enums/api_route.rb +0 -6
  66. data/out/production/securenative-ruby/enums/event_types.rb +0 -23
  67. data/out/production/securenative-ruby/enums/failover_strategy.rb +0 -6
  68. data/out/production/securenative-ruby/enums/risk_level.rb +0 -7
  69. data/out/production/securenative-ruby/errors/securenative_invalid_uri_error.rb +0 -4
  70. data/out/production/securenative-ruby/errors/securenative_parse_error.rb +0 -4
  71. data/out/production/securenative-ruby/event_manager.rb +0 -157
  72. data/out/production/securenative-ruby/http/secure_native_http_response.rb +0 -12
  73. data/out/production/securenative-ruby/http/securenative_http_client.rb +0 -50
  74. data/out/production/securenative-ruby/models/client_token.rb +0 -12
  75. data/out/production/securenative-ruby/models/device.rb +0 -10
  76. data/out/production/securenative-ruby/models/event_options.rb +0 -37
  77. data/out/production/securenative-ruby/models/request_context.rb +0 -18
  78. data/out/production/securenative-ruby/models/request_options.rb +0 -12
  79. data/out/production/securenative-ruby/models/sdk_event.rb +0 -49
  80. data/out/production/securenative-ruby/models/user_traits.rb +0 -13
  81. data/out/production/securenative-ruby/models/verify_result.rb +0 -16
  82. data/out/production/securenative-ruby/securenative.rb +0 -83
  83. data/out/production/securenative-ruby/utils/date_utils.rb +0 -9
  84. data/out/production/securenative-ruby/utils/encryption_utils.rb +0 -49
  85. data/out/production/securenative-ruby/utils/ip_utils.rb +0 -23
  86. data/out/production/securenative-ruby/utils/request_utils.rb +0 -69
  87. data/out/production/securenative-ruby/utils/secure_native_logger.rb +0 -44
  88. data/out/production/securenative-ruby/utils/signature_utils.rb +0 -16
  89. data/out/production/securenative-ruby/utils/utils.rb +0 -9
  90. data/out/production/securenative-ruby/utils/version_utils.rb +0 -11
  91. data/out/test/securenative-ruby/spec_api_manager.rb +0 -87
  92. data/out/test/securenative-ruby/spec_context_builder.rb +0 -87
  93. data/out/test/securenative-ruby/spec_date_utils.rb +0 -13
  94. data/out/test/securenative-ruby/spec_encryption_utils.rb +0 -26
  95. data/out/test/securenative-ruby/spec_event_manager.rb +0 -93
  96. data/out/test/securenative-ruby/spec_ip_utils.rb +0 -41
  97. data/out/test/securenative-ruby/spec_request_utils.rb +0 -25
  98. data/out/test/securenative-ruby/spec_sdk_event.rb +0 -24
  99. data/out/test/securenative-ruby/spec_securenative.rb +0 -61
  100. data/out/test/securenative-ruby/spec_securenative_http_client.rb +0 -31
  101. data/out/test/securenative-ruby/spec_signature_utils.rb +0 -18
  102. data/out/test/securenative-ruby/spec_version_util.rb +0 -10
@@ -1,6 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ApiRoute
4
- TRACK = 'track'
5
- VERIFY = 'verify'
6
- end
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module EventTypes
4
- LOG_IN = 'sn.user.login'
5
- LOG_IN_CHALLENGE = 'sn.user.login.challenge'
6
- LOG_IN_FAILURE = 'sn.user.login.failure'
7
- LOG_OUT = 'sn.user.logout'
8
- SIGN_UP = 'sn.user.signup'
9
- AUTH_CHALLENGE = 'sn.user.auth.challenge'
10
- AUTH_CHALLENGE_SUCCESS = 'sn.user.auth.challenge.success'
11
- AUTH_CHALLENGE_FAILURE = 'sn.user.auth.challenge.failure'
12
- TWO_FACTOR_DISABLE = 'sn.user.2fa.disable'
13
- EMAIL_UPDATE = 'sn.user.email.update'
14
- PASSWORD_REST = 'sn.user.password.reset'
15
- PASSWORD_REST_SUCCESS = 'sn.user.password.reset.success'
16
- PASSWORD_UPDATE = 'sn.user.password.update'
17
- PASSWORD_REST_FAILURE = 'sn.user.password.reset.failure'
18
- USER_INVITE = 'sn.user.invite'
19
- ROLE_UPDATE = 'sn.user.role.update'
20
- PROFILE_UPDATE = 'sn.user.profile.update'
21
- PAGE_VIEW = 'sn.user.page.view'
22
- VERIFY = 'sn.verify'
23
- end
@@ -1,6 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module FailOverStrategy
4
- FAIL_OPEN = 'fail-open'
5
- FAIL_CLOSED = 'fail-closed'
6
- end
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RiskLevel
4
- LOW = 'low'
5
- MEDIUM = 'medium'
6
- HIGH = 'high'
7
- end
@@ -1,4 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class SecureNativeInvalidUriError < StandardError
4
- end
@@ -1,4 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class SecureNativeParseError < StandardError
4
- end
@@ -1,157 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'utils/secure_native_logger'
4
- require 'config/securenative_options'
5
- require 'http/securenative_http_client'
6
- require 'errors/securenative_sdk_error'
7
- require 'errors/securenative_http_error'
8
-
9
- class QueueItem
10
- attr_reader :url, :body, :retry_sending
11
- attr_writer :url, :body, :retry_sending
12
-
13
- def initialize(url, body, retry_sending)
14
- @url = url
15
- @body = body
16
- @retry = retry_sending
17
- end
18
- end
19
-
20
- class EventManager
21
- def initialize(options = SecureNativeOptions.new, http_client = nil)
22
- if options.api_key.nil?
23
- raise SecureNativeSDKError, 'API key cannot be None, please get your API key from SecureNative console.'
24
- end
25
-
26
- @http_client = if http_client.nil?
27
- SecureNativeHttpClient.new(options)
28
- else
29
- http_client
30
- end
31
-
32
- @queue = []
33
- @semaphore = Mutex.new
34
- @interval = options.interval
35
- @options = options
36
- @send_enabled = false
37
- @attempt = 0
38
- @coefficients = [1, 1, 2, 3, 5, 8, 13]
39
-
40
- @thread = Thread.new { run }
41
- end
42
-
43
- def send_async(event, resource_path)
44
- if @options.disable
45
- SecureNativeLogger.warning('SDK is disabled. no operation will be performed')
46
- return
47
- end
48
-
49
- item = QueueItem.new(resource_path, EventManager.serialize(event).to_json, false)
50
- @queue.append(item)
51
- end
52
-
53
- def flush
54
- @queue.each do |item|
55
- @http_client.post(item.url, item.body)
56
- end
57
- end
58
-
59
- def send_sync(event, resource_path, retry_sending)
60
- if @options.disable
61
- SecureNativeLogger.warning('SDK is disabled. no operation will be performed')
62
- return
63
- end
64
-
65
- SecureNativeLogger.debug("Attempting to send event #{event}")
66
- res = @http_client.post(resource_path, EventManager.serialize(event).to_json)
67
-
68
- if res.nil? || res.code != '200'
69
- SecureNativeLogger.info("SecureNative failed to call endpoint #{resource_path} with event #{event}. adding back to queue")
70
- item = QueueItem.new(resource_path, EventManager.serialize(event).to_json, retry_sending)
71
- @queue.append(item)
72
- end
73
-
74
- res
75
- end
76
-
77
- def run
78
- loop do
79
- @semaphore.synchronize do
80
- next unless !@queue.empty? && @send_enabled
81
-
82
- @queue.each do |item|
83
- begin
84
- res = @http_client.post(item.url, item.body)
85
- if res.code == '401'
86
- item.retry_sending = false
87
- elsif res.code != '200'
88
- raise SecureNativeHttpError, res.status_code
89
- end
90
- SecureNativeLogger.debug("Event successfully sent; #{item.body}")
91
- return res
92
- rescue StandardError => e
93
- SecureNativeLogger.error("Failed to send event; #{e}")
94
- if item.retry_sending
95
- @attempt = 0 if @coefficients.length == @attempt + 1
96
-
97
- back_off = @coefficients[@attempt] * @options.interval
98
- SecureNativeLogger.debug("Automatic back-off of #{back_off}")
99
- @send_enabled = false
100
- sleep back_off
101
- @send_enabled = true
102
- end
103
- end
104
- end
105
- end
106
- sleep @interval / 1000
107
- end
108
- end
109
-
110
- def start_event_persist
111
- SecureNativeLogger.debug('Starting automatic event persistence')
112
- if @options.auto_send || @send_enabled
113
- @send_enabled = true
114
- else
115
- SecureNativeLogger.debug('Automatic event persistence is disabled, you should persist events manually')
116
- end
117
- end
118
-
119
- def stop_event_persist
120
- if @send_enabled
121
- SecureNativeLogger.debug('Attempting to stop automatic event persistence')
122
- begin
123
- flush
124
- @thread&.stop?
125
- SecureNativeLogger.debug('Stopped event persistence')
126
- rescue StandardError => e
127
- SecureNativeLogger.error("Could not stop event scheduler; #{e}")
128
- end
129
- end
130
- end
131
-
132
- def self.serialize(obj)
133
- {
134
- rid: obj.rid,
135
- eventType: obj.event_type,
136
- userId: obj.user_id,
137
- userTraits: {
138
- name: obj.user_traits.name,
139
- email: obj.user_traits.email,
140
- phone: obj.user_traits.phone,
141
- createdAt: obj.user_traits.created_at
142
- },
143
- request: {
144
- cid: obj.request.cid,
145
- vid: obj.request.vid,
146
- fp: obj.request.fp,
147
- ip: obj.request.ip,
148
- remoteIp: obj.request.remote_ip,
149
- method: obj.request.http_method || '',
150
- url: obj.request.url,
151
- headers: obj.request.headers
152
- },
153
- timestamp: obj.timestamp,
154
- properties: obj.properties
155
- }
156
- end
157
- end
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class SecureNativeHttpResponse
4
- attr_reader :ok, :status_code, :body
5
- attr_writer :ok, :status_code, :body
6
-
7
- def initialize(ok, status_code, body)
8
- @ok = ok
9
- @status_code = status_code
10
- @body = body
11
- end
12
- end
@@ -1,50 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'net/http'
4
- require 'uri'
5
- require 'json'
6
- require 'utils/version_utils'
7
- require 'utils/secure_native_logger'
8
-
9
- class SecureNativeHttpClient
10
- AUTHORIZATION_HEADER = 'Authorization'
11
- VERSION_HEADER = 'SN-Version'
12
- USER_AGENT_HEADER = 'User-Agent'
13
- USER_AGENT_HEADER_VALUE = 'SecureNative-ruby'
14
- CONTENT_TYPE_HEADER = 'Content-Type'
15
- CONTENT_TYPE_HEADER_VALUE = 'application/json'
16
-
17
- def initialize(securenative_options)
18
- @options = securenative_options
19
- end
20
-
21
- def _headers
22
- {
23
- CONTENT_TYPE_HEADER => CONTENT_TYPE_HEADER_VALUE,
24
- USER_AGENT_HEADER => USER_AGENT_HEADER_VALUE,
25
- VERSION_HEADER => VersionUtils.version,
26
- AUTHORIZATION_HEADER => @options.api_key
27
- }
28
- end
29
-
30
- def post(path, body)
31
- uri = URI.parse("#{@options.api_url}/#{path}")
32
- headers = _headers
33
-
34
- client = Net::HTTP.new(uri.host, uri.port)
35
- client.use_ssl = true
36
- client.verify_mode = OpenSSL::SSL::VERIFY_NONE
37
-
38
- request = Net::HTTP::Post.new(uri.request_uri, headers)
39
- request.body = body
40
-
41
- res = nil
42
- begin
43
- res = client.request(request)
44
- rescue StandardError => e
45
- SecureNativeLogger.error("Failed to send request; #{e}")
46
- return res
47
- end
48
- res
49
- end
50
- end
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class ClientToken
4
- attr_reader :cid, :vid, :fp
5
- attr_writer :cid, :vid, :fp
6
-
7
- def initialize(cid, vid, fp)
8
- @cid = cid
9
- @vid = vid
10
- @fp = fp
11
- end
12
- end
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Device
4
- attr_reader :device_id
5
- attr_writer :device_id
6
-
7
- def initialize(device_id)
8
- @device_id = device_id
9
- end
10
- end
@@ -1,37 +0,0 @@
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
@@ -1,18 +0,0 @@
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
-
@@ -1,12 +0,0 @@
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
@@ -1,49 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'context/securenative_context'
4
- require 'errors/securenative_invalid_options_error'
5
- require 'utils/encryption_utils'
6
- require 'utils/date_utils'
7
- require 'models/request_context'
8
- require 'securerandom'
9
-
10
- class SDKEvent
11
- attr_reader :context, :rid, :event_type, :user_id, :user_traits, :request, :timestamp, :properties
12
- attr_writer :context, :rid, :event_type, :user_id, :user_traits, :request, :timestamp, :properties
13
-
14
- def initialize(event_options, securenative_options)
15
- if event_options.user_id.nil? || event_options.user_id.length <= 0 || event_options.user_id == ''
16
- raise SecureNativeInvalidOptionsError.new, 'Invalid event structure; User Id is missing'
17
- end
18
-
19
- if event_options.event.nil? || event_options.event.length <= 0 || event_options.event == ''
20
- raise SecureNativeInvalidOptionsError.new, 'Invalid event structure; Event Type is missing'
21
- end
22
-
23
- @context = if !event_options.context.nil?
24
- event_options.context
25
- else
26
- SecureNativeContext.default_context_builder
27
- end
28
-
29
- client_token = EncryptionUtils.decrypt(@context.client_token, securenative_options.api_key)
30
-
31
- @rid = SecureRandom.uuid.to_str
32
- @event_type = event_options.event
33
- @user_id = event_options.user_id
34
- @user_traits = event_options.user_traits
35
- @request = RequestContext.new(cid: client_token ? client_token.cid : '', vid: client_token ? client_token.vid : '',
36
- fp: client_token ? client_token.fp : '', ip: @context.ip,
37
- remote_ip: @context.remote_ip, headers: @context.headers,
38
- url: @context.url, http_method: @context.http_method)
39
-
40
-
41
- @timestamp = DateUtils.to_timestamp(event_options.timestamp)
42
- @properties = event_options.properties
43
- end
44
-
45
- def to_s
46
- "context: #{@context}, rid: #{@rid}, event_type: #{@event_type}, user_id: #{@user_id},
47
- user_traits: #{@user_traits}, request: #{@request}, timestamp: #{@timestamp}, properties: #{@properties}"
48
- end
49
- end
@@ -1,13 +0,0 @@
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
@@ -1,16 +0,0 @@
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