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
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class SecureNativeHttpResponse
|
|
2
4
|
attr_reader :ok, :status_code, :body
|
|
3
5
|
attr_writer :ok, :status_code, :body
|
|
4
6
|
|
|
@@ -7,4 +9,4 @@ class HttpResponse
|
|
|
7
9
|
@status_code = status_code
|
|
8
10
|
@body = body
|
|
9
11
|
end
|
|
10
|
-
end
|
|
12
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
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
|
|
@@ -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
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
class RequestContext
|
|
2
|
-
attr_reader :cid, :vid, :fp, :ip, :remote_ip, :headers, :url, :
|
|
3
|
-
attr_writer :cid, :vid, :fp, :ip, :remote_ip, :headers, :url, :
|
|
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
|
|
4
6
|
|
|
5
|
-
def initialize(cid
|
|
7
|
+
def initialize(cid: nil, vid: nil, fp: nil, ip: nil, remote_ip: nil, headers: nil, url: nil, http_method: nil)
|
|
6
8
|
@cid = cid
|
|
7
9
|
@vid = vid
|
|
8
10
|
@fp = fp
|
|
@@ -10,6 +12,7 @@ class RequestContext
|
|
|
10
12
|
@remote_ip = remote_ip
|
|
11
13
|
@headers = headers
|
|
12
14
|
@url = url
|
|
13
|
-
@method =
|
|
15
|
+
@method = http_method
|
|
14
16
|
end
|
|
15
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,49 @@
|
|
|
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,11 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
class UserTraits
|
|
2
4
|
attr_reader :name, :email, :phone, :created_at
|
|
3
5
|
attr_writer :name, :email, :phone, :created_at
|
|
4
6
|
|
|
5
|
-
def initialize(name
|
|
7
|
+
def initialize(name: nil, email: nil, phone: nil, created_at: nil)
|
|
6
8
|
@name = name
|
|
7
9
|
@email = email
|
|
8
10
|
@created_at = created_at
|
|
9
11
|
@phone = phone
|
|
10
12
|
end
|
|
11
|
-
end
|
|
13
|
+
end
|
|
@@ -1,10 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
class VerifyResult
|
|
2
4
|
attr_reader :risk_level, :score, :triggers
|
|
3
5
|
attr_writer :risk_level, :score, :triggers
|
|
4
6
|
|
|
5
|
-
def initialize(risk_level
|
|
7
|
+
def initialize(risk_level: nil, score: nil, triggers: nil)
|
|
6
8
|
@risk_level = risk_level
|
|
7
9
|
@score = score
|
|
8
10
|
@triggers = triggers
|
|
9
11
|
end
|
|
10
|
-
|
|
12
|
+
|
|
13
|
+
def to_s
|
|
14
|
+
"risk_level: #{@risk_level}, score: #{@score}, triggers: #{@triggers}"
|
|
15
|
+
end
|
|
16
|
+
end
|
data/lib/securenative.rb
ADDED
|
@@ -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
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "resolv"
|
|
4
4
|
|
|
5
|
+
class IpUtils
|
|
5
6
|
def self.ip_address?(ip_address)
|
|
6
|
-
return true if
|
|
7
|
-
return true if
|
|
7
|
+
return true if ip_address =~ Resolv::IPv4::Regex
|
|
8
|
+
return true if ip_address =~ Resolv::IPv6::Regex
|
|
8
9
|
|
|
9
10
|
false
|
|
10
11
|
end
|
|
@@ -19,4 +20,4 @@ class IpUtils
|
|
|
19
20
|
def self.loop_back?(ip_address)
|
|
20
21
|
IPAddr.new(ip_address).loopback?
|
|
21
22
|
end
|
|
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.scan(/\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/)[0] unless x_forwarded_for.nil?
|
|
25
|
+
rescue NoMethodError
|
|
26
|
+
begin
|
|
27
|
+
x_forwarded_for = request['HTTP_X_FORWARDED_FOR']
|
|
28
|
+
return x_forwarded_for.scan(/\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/)[0] 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.scan(/\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/)[0] unless x_forwarded_for.nil?
|
|
36
|
+
rescue NoMethodError
|
|
37
|
+
begin
|
|
38
|
+
x_forwarded_for = request['REMOTE_ADDR']
|
|
39
|
+
return x_forwarded_for.scan(/\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/)[0] 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.scan(/\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/)[0] unless h.nil?
|
|
49
|
+
rescue NoMethodError
|
|
50
|
+
begin
|
|
51
|
+
h = request[header]
|
|
52
|
+
return h.scan(/\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/)[0] 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
|