Skip to content

Latest commit

 

History

History
189 lines (127 loc) · 6.01 KB

README.md

File metadata and controls

189 lines (127 loc) · 6.01 KB

SecureNative Logo

A Cloud-Native Security Monitoring and Protection for Modern Applications

Github Actions Gem Version

Documentation | Quick Start | Blog | Chat with us on Slack!


SecureNative performs user monitoring by analyzing user interactions with your application and various factors such as network, devices, locations and access patterns to stop and prevent account takeover attacks.

Install the SDK

Add this line to your application's Gemfile:

gem 'securenative'

Then execute:

$ bundle install

Or install it yourself as:

$ gem install securenative

Initialize the SDK

To get your API KEY, login to your SecureNative account and go to project settings page:

Option 1: Initialize via Config file

SecureNative can automatically load your config from securenative.yml file or from the file that is specified in your SECURENATIVE_CONFIG_FILE env variable:

require 'securenative'


secureative =  SecureNative::Client.init

Option 2: Initialize via API Key

require 'securenative'


securenative =  SecureNative::Client.init_with_api_key('YOUR_API_KEY')

Option 3: Initialize via ConfigurationBuilder

require 'securenative'


options = SecureNative::Config::ConfigurationBuilder.new(api_key: 'API_KEY', max_events: 10, log_level: 'ERROR')
SecureNative::Client.init_with_options(options)                                 

Getting SecureNative instance

Once initialized, sdk will create a singleton instance which you can get:

require 'securenative'


secureNative = SecureNative::Client.instance

Tracking events

Once the SDK has been initialized, tracking requests sent through the SDK instance. Make sure you build event with the EventBuilder:

require 'securenative'


def track
   securenative = SecureNative::Client.instance
   context = SecureNative::Context.new(client_token: 'SECURED_CLIENT_TOKEN', ip: '127.0.0.1',
                                      headers: { 'user-agent' => 'Mozilla: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.3 Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/43.4' })
   
   event_options = SecureNative::EventOptions.new(event: SecureNative::EventTypes::LOG_IN, user_id: '1234', context: context,
                                    user_traits: SecureNative::UserTraits.new(name: 'Your Name', email: 'name@gmail.com', phone: '+1234567890'),
                                    properties: { custom_param1: 'CUSTOM_PARAM_VALUE', custom_param2: true, custom_param3: 3 })
   
   securenative.track(event_options)
   
   @message = 'tracked'
end

You can also create request securenative.context from requests:

require 'securenative'


def track(request)
    securenative = SecureNative::Client.instance
    context = securenative.from_http_request(request)
    
    event_options = SecureNative::EventOptions.new(event: SecureNative::EventTypes::LOG_IN, user_id: '1234', context: context,
                                     user_traits: SecureNative::UserTraits.new(name: 'Your Name', email: 'name@gmail.com', phone: '+1234567890'),
                                     properties: { custom_param1: 'CUSTOM_PARAM_VALUE', custom_param2: true, custom_param3: 3 })
    
    securenative.track(event_options)
    
    @message = 'tracked'
end

Verify events

Example

require 'securenative'


def verify(request)
    securenative = SecureNative::Client.instance
    context = securenative.from_http_request(request)

    event_options = SecureNative::EventOptions.new(event: SecureNative::EventTypes::LOG_IN, user_id: '1234', context: context,
                                         user_traits: SecureNative::UserTraits.new(name: 'Your Name', email: 'name@gmail.com', phone: '+1234567890'),
                                         properties: { custom_param1: 'CUSTOM_PARAM_VALUE', custom_param2: true, custom_param3: 3 })
    
    verify_result = securenative.verify(event_options)
    verify_result.risk_level  # Low, Medium, High
    verify_result.score  # Risk score: 0 -1 (0 - Very Low, 1 - Very High)
    verify_result.triggers  # ["TOR", "New IP", "New City"]
end

Webhook signature verification

Apply our filter to verify the request is from us, for example:

require 'securenative'


def webhook_endpoint(request)
    securenative = SecureNative::Client.instance
    
    # Checks if request is verified
    is_verified = securenative.verify_request_payload(request)
end

Extract proxy headers from cloud providers

You can specify custom header keys to allow extraction of client ip from different providers. This example demonstrates the usage of proxy headers for ip extraction from Cloudflare.

Option 1: Using config file

SECURENATIVE_API_KEY: YOUR_API_KEY
SECURENATIVE_PROXY_HEADERS: ["CF-Connecting-IP"]

Initialize sdk as shown above.

Options 2: Using ConfigurationBuilder

require 'securenative'

options = SecureNative::Options.new(api_key: 'API_KEY', max_events: 10, log_level: 'ERROR', proxy_headers: ['CF-Connecting-IP'])

SecureNative::Client.init_with_options(options)