securenative 0.1.5
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 +7 -0
- data/.gitignore +38 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +37 -0
- data/LICENSE +21 -0
- data/README.md +102 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/securenative.rb +39 -0
- data/lib/securenative/config.rb +9 -0
- data/lib/securenative/event_manager.rb +88 -0
- data/lib/securenative/event_options.rb +86 -0
- data/lib/securenative/event_type.rb +21 -0
- data/lib/securenative/http_client.rb +20 -0
- data/lib/securenative/secure_native_sdk.rb +62 -0
- data/lib/securenative/securenative_options.rb +17 -0
- data/lib/securenative/sn_exception.rb +5 -0
- data/lib/securenative/utils.rb +41 -0
- data/securenative.gemspec +28 -0
- metadata +91 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b39e9c3c819d0191bcf78f5813681f6679b82c5a864eb04f7bdd05d754b01643
|
4
|
+
data.tar.gz: a21bcbf70729dace6553c7853154f6a76d341d894a9398cce5c298e9201e8ed7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e4e85d1e8119c75f2e31dfdf74231a20bfc0b5af77b30cca2886a2860a0f7354d28956fcb8aa9a155b3a68c4fe383de9789d9d751ee11111f34646e9aa01db70
|
7
|
+
data.tar.gz: fc5a540361c33c0570d7be6769418cc2b58ed484e22ca59697508c5f67aca673674c7dd505f53d23ddf0022335a26dc04c95bc93cc1507e174f0af423b1c791e
|
data/.gitignore
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
.idea
|
13
|
+
# Used by dotenv library to load environment variables.
|
14
|
+
# .env
|
15
|
+
|
16
|
+
# Ignore Byebug command history file.
|
17
|
+
.byebug_history
|
18
|
+
|
19
|
+
## Documentation cache and generated files:
|
20
|
+
/.yardoc/
|
21
|
+
/_yardoc/
|
22
|
+
/doc/
|
23
|
+
/rdoc/
|
24
|
+
|
25
|
+
## Environment normalization:
|
26
|
+
/.bundle/
|
27
|
+
/vendor/bundle
|
28
|
+
/lib/bundler/man/
|
29
|
+
|
30
|
+
# for a library or gem, you might want to ignore these files since the code is
|
31
|
+
# intended to run in multiple environments; otherwise, check them in:
|
32
|
+
# Gemfile.lock
|
33
|
+
# .ruby-version
|
34
|
+
# .ruby-gemset
|
35
|
+
|
36
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
37
|
+
.rvmrc
|
38
|
+
.DS_Store
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
securenative (0.1.5)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.3)
|
10
|
+
httpclient (2.8.3)
|
11
|
+
rake (12.3.3)
|
12
|
+
rspec (3.8.0)
|
13
|
+
rspec-core (~> 3.8.0)
|
14
|
+
rspec-expectations (~> 3.8.0)
|
15
|
+
rspec-mocks (~> 3.8.0)
|
16
|
+
rspec-core (3.8.2)
|
17
|
+
rspec-support (~> 3.8.0)
|
18
|
+
rspec-expectations (3.8.4)
|
19
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
20
|
+
rspec-support (~> 3.8.0)
|
21
|
+
rspec-mocks (3.8.1)
|
22
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
23
|
+
rspec-support (~> 3.8.0)
|
24
|
+
rspec-support (3.8.2)
|
25
|
+
|
26
|
+
PLATFORMS
|
27
|
+
ruby
|
28
|
+
|
29
|
+
DEPENDENCIES
|
30
|
+
bundler (~> 2.0)
|
31
|
+
httpclient
|
32
|
+
rake
|
33
|
+
rspec
|
34
|
+
securenative!
|
35
|
+
|
36
|
+
BUNDLED WITH
|
37
|
+
2.1.0.pre.1
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2019 SecureNative
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,102 @@
|
|
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
|
5
|
+
|
6
|
+
Add this line to your application's Gemfile:
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
gem 'securenative'
|
10
|
+
```
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install securenative
|
19
|
+
|
20
|
+
## 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.
|
22
|
+
```ruby
|
23
|
+
require 'securenative'
|
24
|
+
|
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
|
34
|
+
```
|
35
|
+
|
36
|
+
## Tracking events
|
37
|
+
Once the SDK has been initialized, you can start sending new events with the `track` function:
|
38
|
+
```ruby
|
39
|
+
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"))
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
58
|
+
## Verification events
|
59
|
+
Once the SDK has been initialized, you can start sending new events with the `verify` function:
|
60
|
+
```ruby
|
61
|
+
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
|
79
|
+
end
|
80
|
+
```
|
81
|
+
|
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.
|
84
|
+
```ruby
|
85
|
+
require 'securenative'
|
86
|
+
|
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
|
+
|
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
|
101
|
+
end
|
102
|
+
```
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "securenative"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/securenative.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require_relative 'securenative/sn_exception'
|
2
|
+
require_relative 'securenative/secure_native_sdk'
|
3
|
+
|
4
|
+
$securenative = nil
|
5
|
+
|
6
|
+
module SecureNative
|
7
|
+
def self.init(api_key, options: SecureNativeOptions.new)
|
8
|
+
if $securenative == nil
|
9
|
+
$securenative = SecureNativeSDK.new(api_key, options: options)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.track(event)
|
14
|
+
sdk = _get_or_throw
|
15
|
+
sdk.track(event)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.verify(event)
|
19
|
+
sdk = _get_or_throw
|
20
|
+
sdk.verify(event)
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.verify_webhook(hmac_header, body)
|
24
|
+
sdk = _get_or_throw
|
25
|
+
sdk.verify_webhook(hmac_header = hmac_header, body = body)
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.flush
|
29
|
+
sdk = _get_or_throw
|
30
|
+
sdk.flush
|
31
|
+
end
|
32
|
+
|
33
|
+
def self._get_or_throw
|
34
|
+
if $securenative == nil
|
35
|
+
raise SecureNativeSDKException.new
|
36
|
+
end
|
37
|
+
$securenative
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
module Config
|
2
|
+
SDK_VERSION = '0.1.5'
|
3
|
+
MAX_ALLOWED_PARAMS = 6
|
4
|
+
API_URL_PROD = "https://api.securenative.com/collector/api/v1"
|
5
|
+
API_URL_STG = "https://api.securenative-stg.com/collector/api/v1"
|
6
|
+
TRACK_EVENT = "/track"
|
7
|
+
VERIFY_EVENT = "/verify"
|
8
|
+
FLOW_EVENT = "/flow"
|
9
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require_relative 'securenative_options'
|
2
|
+
require_relative 'http_client'
|
3
|
+
require_relative 'sn_exception'
|
4
|
+
require 'json'
|
5
|
+
require 'thread'
|
6
|
+
|
7
|
+
class QueueItem
|
8
|
+
def initialize(url, body)
|
9
|
+
@url = url
|
10
|
+
@body = body
|
11
|
+
end
|
12
|
+
|
13
|
+
attr_reader :url
|
14
|
+
attr_reader :body
|
15
|
+
end
|
16
|
+
|
17
|
+
class EventManager
|
18
|
+
def initialize(api_key, options: SecureNativeOptions.new, http_client: HttpClient.new)
|
19
|
+
if api_key == nil
|
20
|
+
raise SecureNativeSDKException.new
|
21
|
+
end
|
22
|
+
|
23
|
+
@api_key = api_key
|
24
|
+
@options = options
|
25
|
+
@http_client = http_client
|
26
|
+
@queue = Queue.new
|
27
|
+
|
28
|
+
if @options.auto_send
|
29
|
+
interval_seconds = [(@options.interval / 1000).floor, 1].max
|
30
|
+
Thread.new do
|
31
|
+
loop do
|
32
|
+
flush
|
33
|
+
sleep(interval_seconds)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def send_async(event, path)
|
40
|
+
q_item = QueueItem.new(build_url(path), event)
|
41
|
+
@queue.push(q_item)
|
42
|
+
end
|
43
|
+
|
44
|
+
def send_sync(event, path)
|
45
|
+
@http_client.post(
|
46
|
+
build_url(path),
|
47
|
+
@api_key,
|
48
|
+
event.to_hash.to_json
|
49
|
+
)
|
50
|
+
end
|
51
|
+
|
52
|
+
def flush
|
53
|
+
if is_queue_full
|
54
|
+
i = @options.max_events - 1
|
55
|
+
while i >= 0
|
56
|
+
item = @queue.pop
|
57
|
+
@http_client.post(
|
58
|
+
build_url(item.url),
|
59
|
+
@api_key,
|
60
|
+
item.body.to_hash.to_json
|
61
|
+
)
|
62
|
+
end
|
63
|
+
else
|
64
|
+
q = Array.new(@queue.size) {@queue.pop}
|
65
|
+
q.each do |item|
|
66
|
+
@http_client.post(
|
67
|
+
build_url(item.url),
|
68
|
+
@api_key,
|
69
|
+
item.body
|
70
|
+
)
|
71
|
+
@queue = Queue.new
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
def build_url(path)
|
79
|
+
@options.api_url + path
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def is_queue_full
|
85
|
+
@queue.length > @options.max_events
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
|
3
|
+
class User
|
4
|
+
def initialize(user_id: "", user_email: "", user_name: "")
|
5
|
+
@user_id = user_id
|
6
|
+
@user_email = user_email
|
7
|
+
@user_name = user_name
|
8
|
+
end
|
9
|
+
|
10
|
+
attr_reader :user_id
|
11
|
+
attr_reader :user_email
|
12
|
+
attr_reader :user_name
|
13
|
+
end
|
14
|
+
|
15
|
+
class Event
|
16
|
+
def initialize(event_type, user: User(), ip: "127.0.0.1", remote_ip: "127.0.0.1", user_agent: "unknown", sn_cookie: nil, params: nil)
|
17
|
+
@event_type = event_type
|
18
|
+
@user = user
|
19
|
+
@ip = ip
|
20
|
+
@remote_ip = remote_ip
|
21
|
+
@user_agent = user_agent
|
22
|
+
@cid = ""
|
23
|
+
@fp = ""
|
24
|
+
|
25
|
+
if params
|
26
|
+
unless params.length > 0 && params[0].instance_of?(CustomParam)
|
27
|
+
raise ArgumentError("custom params should be a list of CustomParams")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
@params = params
|
31
|
+
|
32
|
+
if sn_cookie
|
33
|
+
@cid, @cid = Utils.parse_cookie(sn_cookie)
|
34
|
+
end
|
35
|
+
@vid = SecureRandom.uuid
|
36
|
+
@ts = Time.now.getutc.to_i
|
37
|
+
end
|
38
|
+
|
39
|
+
def to_hash
|
40
|
+
p = Array.new
|
41
|
+
if @params
|
42
|
+
@params.each do |param|
|
43
|
+
p << {:key => param.key, :value => param.value}
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
{
|
48
|
+
:eventType => @event_type,
|
49
|
+
:user => {
|
50
|
+
:id => @user.user_id,
|
51
|
+
:email => @user.user_email,
|
52
|
+
:name => @user.user_name
|
53
|
+
},
|
54
|
+
:remoteIP => @remote_ip,
|
55
|
+
:ip => @ip,
|
56
|
+
:cid => @cid,
|
57
|
+
:fp => @fp,
|
58
|
+
:ts => @ts,
|
59
|
+
:vid => @vid,
|
60
|
+
:userAgent => @user_agent,
|
61
|
+
:device => Hash.new,
|
62
|
+
:params => p
|
63
|
+
}
|
64
|
+
end
|
65
|
+
|
66
|
+
attr_reader :cid
|
67
|
+
attr_reader :params
|
68
|
+
attr_reader :user_agent
|
69
|
+
attr_reader :user
|
70
|
+
attr_reader :remote_ip
|
71
|
+
attr_reader :event_type
|
72
|
+
attr_reader :fp
|
73
|
+
attr_reader :ip
|
74
|
+
attr_reader :ts
|
75
|
+
attr_reader :vid
|
76
|
+
end
|
77
|
+
|
78
|
+
class CustomParam
|
79
|
+
def initialize(key, value)
|
80
|
+
@key = key
|
81
|
+
@value = value
|
82
|
+
end
|
83
|
+
|
84
|
+
attr_reader :key
|
85
|
+
attr_reader :value
|
86
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module EventType
|
2
|
+
LOG_IN = "sn.user.login"
|
3
|
+
LOG_IN_CHALLENGE = "sn.user.login.challenge"
|
4
|
+
LOG_IN_FAILURE = "sn.user.login.failure"
|
5
|
+
LOG_OUT = "sn.user.logout"
|
6
|
+
SIGN_UP = "sn.user.signup"
|
7
|
+
AUTH_CHALLENGE = "sn.user.auth.challenge"
|
8
|
+
AUTH_CHALLENGE_SUCCESS = "sn.user.auth.challenge.success"
|
9
|
+
AUTH_CHALLENGE_FAILURE = "sn.user.auth.challenge.failure"
|
10
|
+
TWO_FACTOR_DISABLE = "sn.user.2fa.disable"
|
11
|
+
EMAIL_UPDATE = "sn.user.email.update"
|
12
|
+
PASSWORD_RESET = "sn.user.password.reset"
|
13
|
+
PASSWORD_RESET_SUCCESS = "sn.user.password.reset.success"
|
14
|
+
PASSWORD_UPDATE = "sn.user.password.update"
|
15
|
+
PASSWORD_RESET_FAILURE = "sn.user.password.reset.failure"
|
16
|
+
USER_INVITE = "sn.user.invite"
|
17
|
+
ROLE_UPDATE = "sn.user.role.update"
|
18
|
+
PROFILE_UPDATE = "sn.user.profile.update"
|
19
|
+
PAGE_VIEW = "sn.user.page.view"
|
20
|
+
VERIFY = "sn.verify"
|
21
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'httpclient'
|
2
|
+
|
3
|
+
class HttpClient
|
4
|
+
def initialize
|
5
|
+
@client = HTTPClient.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def headers(api_key)
|
9
|
+
{
|
10
|
+
"Content-Type" => 'application/json',
|
11
|
+
"User-Agent" => 'SecureNative-ruby',
|
12
|
+
"Sn-Version" => Config::SDK_VERSION,
|
13
|
+
"Authorization" => api_key
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
def post(url, api_key, body)
|
18
|
+
@client.post(url, body, self.headers(api_key))
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require_relative 'event_manager'
|
2
|
+
require_relative 'config'
|
3
|
+
require_relative 'sn_exception'
|
4
|
+
require_relative 'utils'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
class SecureNativeSDK
|
8
|
+
def initialize(api_key, options: SecureNativeOptions.new)
|
9
|
+
if api_key == nil
|
10
|
+
raise SecureNativeSDKException.new
|
11
|
+
end
|
12
|
+
|
13
|
+
@api_key = api_key
|
14
|
+
@options = options
|
15
|
+
@event_manager = EventManager.new(@api_key, options: @options)
|
16
|
+
end
|
17
|
+
|
18
|
+
def api_key
|
19
|
+
@api_key
|
20
|
+
end
|
21
|
+
|
22
|
+
def version
|
23
|
+
Config::SDK_VERSION
|
24
|
+
end
|
25
|
+
|
26
|
+
def track(event)
|
27
|
+
validate_event(event)
|
28
|
+
@event_manager.send_async(event, Config::TRACK_EVENT)
|
29
|
+
end
|
30
|
+
|
31
|
+
def verify(event)
|
32
|
+
validate_event(event)
|
33
|
+
res = @event_manager.send_sync(event, Config::VERIFY_EVENT)
|
34
|
+
if res.status_code == 200
|
35
|
+
return JSON.parse(res.body)
|
36
|
+
end
|
37
|
+
nil
|
38
|
+
end
|
39
|
+
|
40
|
+
def flow(event) # Note: For future purposes
|
41
|
+
validate_event(event)
|
42
|
+
@event_manager.send_async(event, Config::FLOW_EVENT)
|
43
|
+
end
|
44
|
+
|
45
|
+
def verify_webhook(hmac_header, body)
|
46
|
+
Utils.verify_signature(@api_key, body, hmac_header)
|
47
|
+
end
|
48
|
+
|
49
|
+
def flush
|
50
|
+
@event_manager.flush
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def validate_event(event)
|
56
|
+
unless event.params.nil?
|
57
|
+
if event.params.length > Config::MAX_ALLOWED_PARAMS
|
58
|
+
event.params = event.params[0, Config::MAX_ALLOWED_PARAMS]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require_relative 'config'
|
2
|
+
|
3
|
+
class SecureNativeOptions
|
4
|
+
def initialize(api_url: Config::API_URL_PROD, interval: 1000, max_events: 1000, timeout: 1500, auto_send: true)
|
5
|
+
@timeout = timeout
|
6
|
+
@max_events = max_events
|
7
|
+
@api_url = api_url
|
8
|
+
@interval = interval
|
9
|
+
@auto_send = auto_send
|
10
|
+
end
|
11
|
+
|
12
|
+
attr_reader :timeout
|
13
|
+
attr_reader :max_events
|
14
|
+
attr_reader :api_url
|
15
|
+
attr_reader :interval
|
16
|
+
attr_reader :auto_send
|
17
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require "base64"
|
2
|
+
require "json"
|
3
|
+
require 'openssl'
|
4
|
+
|
5
|
+
module Utils
|
6
|
+
def self.verify_signature(secret, text_body, header_signature)
|
7
|
+
begin
|
8
|
+
key = secret.encode('utf-8')
|
9
|
+
body = text_body.encode('utf-8')
|
10
|
+
calculated_signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha512'), key, body)
|
11
|
+
calculated_signature.eql? header_signature
|
12
|
+
rescue Exception
|
13
|
+
return false
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.parse_cookie(cookie = nil)
|
18
|
+
fp = ""
|
19
|
+
cid = ""
|
20
|
+
unless cookie
|
21
|
+
return fp, cid
|
22
|
+
end
|
23
|
+
|
24
|
+
begin
|
25
|
+
decoded_cookie = Base64.decode64(cookie)
|
26
|
+
unless decoded_cookie
|
27
|
+
decoded_cookie = "{}"
|
28
|
+
end
|
29
|
+
jsonified = JSON.generate(decoded_cookie)
|
30
|
+
if jsonified["fp"]
|
31
|
+
fp = jsonified["fp"]
|
32
|
+
end
|
33
|
+
if jsonified["cid"]
|
34
|
+
cid = jsonified["cid"]
|
35
|
+
end
|
36
|
+
rescue Exception
|
37
|
+
ensure
|
38
|
+
return fp, cid
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "securenative/config"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "securenative"
|
7
|
+
spec.version = Config::SDK_VERSION
|
8
|
+
spec.authors = ["SecureNative"]
|
9
|
+
spec.email = ["support@securenative.com"]
|
10
|
+
|
11
|
+
spec.summary = %q{SecureNative SDK for ruby}
|
12
|
+
spec.homepage = "https://www.securenative.com"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: securenative
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.5
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- SecureNative
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-08-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- support@securenative.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- Gemfile.lock
|
51
|
+
- LICENSE
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- bin/console
|
55
|
+
- bin/setup
|
56
|
+
- lib/securenative.rb
|
57
|
+
- lib/securenative/config.rb
|
58
|
+
- lib/securenative/event_manager.rb
|
59
|
+
- lib/securenative/event_options.rb
|
60
|
+
- lib/securenative/event_type.rb
|
61
|
+
- lib/securenative/http_client.rb
|
62
|
+
- lib/securenative/secure_native_sdk.rb
|
63
|
+
- lib/securenative/securenative_options.rb
|
64
|
+
- lib/securenative/sn_exception.rb
|
65
|
+
- lib/securenative/utils.rb
|
66
|
+
- securenative.gemspec
|
67
|
+
homepage: https://www.securenative.com
|
68
|
+
licenses:
|
69
|
+
- MIT
|
70
|
+
metadata:
|
71
|
+
homepage_uri: https://www.securenative.com
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options: []
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
requirements: []
|
87
|
+
rubygems_version: 3.0.4
|
88
|
+
signing_key:
|
89
|
+
specification_version: 4
|
90
|
+
summary: SecureNative SDK for ruby
|
91
|
+
test_files: []
|