Package: securenative
A Cloud-Native Security Monitoring and Protection for Modern ApplicationsDocumentation|Quick Start|Blog|Chat with us on Slack!SecureNativeperforms 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 SDKWhen using PyPi, run the following:pipinstallsecurenativeInitialize the SDKTo get yourAPI KEY, login to your SecureNative account and go to project settings page:Option 1: Initialize via Config fileSecureNative can automatically load your config fromsecurenative.inifile or from the file that is specified in yourSECURENATIVE_CONFIG_FILEenv variable:fromsecurenative.securenativeimportSecureNative# 1. Config file path is given by environment variablesecurenative=SecureNative.init()# 2. Config file path is specified directlysecurenative=SecureNative.init('path/to/securenative.ini')Option 2: Initialize via API Keyfromsecurenative.securenativeimportSecureNativesecurenative=SecureNative.init_with_api_key("YOUR_API_KEY")Option 3: Initialize via ConfigurationBuilderfromsecurenative.securenativeimportSecureNativefromsecurenative.config.securenative_optionsimportSecureNativeOptionsoptions=SecureNativeOptions(api_key="YOUR_API_KEY",max_events=10,log_level="ERROR")securenative=SecureNative.init_with_options(options)Getting SecureNative instanceOnce initialized, sdk will create a singleton instance which you can get:fromsecurenative.securenativeimportSecureNativesecurenative=SecureNative.get_instance()Tracking eventsOnce the SDK has been initialized, tracking requests sent through the SDK
instance. Make sure you build event with the EventBuilder:fromsecurenative.securenativeimportSecureNativefromsecurenative.context.securenative_contextimportSecureNativeContextfromsecurenative.models.event_optionsimportEventOptionsfromsecurenative.enums.event_typesimportEventTypesfromsecurenative.models.user_traitsimportUserTraitssecurenative=SecureNative.get_instance()context=SecureNativeContext(client_token="SECURE_CLIENT_TOKEN",ip="127.0.0.1",headers={"user-agent":"Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405"})event_options=EventOptions(event=EventTypes.LOG_IN,user_id="1234",user_traits=UserTraits("Your Name","name@gmail.com","+1234567890"),context=context,properties={"custom_param1":"CUSTOM_PARAM_VALUE","custom_param2":True,"custom_param3":3})securenative.track(event_options)You can also create request context from requests:fromsecurenative.securenativeimportSecureNativefromsecurenative.models.event_optionsimportEventOptionsfromsecurenative.enums.event_typesimportEventTypesfromsecurenative.models.user_traitsimportUserTraitsdeftrack(request):securenative=SecureNative.get_instance()context=securenative.from_http_request(request)event_options=EventOptions(event=EventTypes.LOG_IN,user_id="1234",user_traits=UserTraits("Your Name","name@gmail.com","+1234567890"),context=context,properties={"custom_param1":"CUSTOM_PARAM_VALUE","custom_param2":True,"custom_param3":3})securenative.track(event_options)Verify eventsExamplefromsecurenative.securenativeimportSecureNativefromsecurenative.models.event_optionsimportEventOptionsfromsecurenative.enums.event_typesimportEventTypesfromsecurenative.models.user_traitsimportUserTraitsdefverify(request):securenative=SecureNative.get_instance()context=securenative.from_http_request(request)event_options=EventOptions(event=EventTypes.LOG_IN,user_id="1234",user_traits=UserTraits("Your Name","name@gmail.com","+1234567890"),context=context,properties={"custom_param1":"CUSTOM_PARAM_VALUE","custom_param2":True,"custom_param3":3})verify_result=securenative.verify(event_options)verify_result.risk_level# Low, Medium, Highverify_result.score# Risk score: 0 -1 (0 - Very Low, 1 - Very High)verify_result.triggers# ["TOR", "New IP", "New City"]Webhook signature verificationApply our filter to verify the request is from us, for example:fromsecurenative.securenativeimportSecureNativedefwebhook_endpoint(request):securenative=SecureNative.get_instance()# Checks if request is verifiedis_verified=securenative.verify_request_payload(request)Extract proxy headers from cloud providersYou 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 fileSECURENATIVE_API_KEY:"YOUR_API_KEY"SECURENATIVE_PROXY_HEADERS:["CF-Connecting-IP"]Initialize sdk as shown above.Options 2: Using ConfigurationBuilderfromsecurenative.securenativeimportSecureNativefromsecurenative.config.securenative_optionsimportSecureNativeOptionsoptions=SecureNativeOptions(api_key="YOUR_API_KEY",max_events=10,log_level="ERROR",proxy_headers=['CF-Connecting-IP'])securenative=SecureNative.init_with_options(options)Remove PII Data From HeadersBy default, SecureNative SDK remove any known pii headers from the received request.
We also support using custom pii headers and regex matching via configuration, for example:Option 1: Using config fileSECURENATIVE_API_KEY:"YOUR_API_KEY"SECURENATIVE_PII_HEADERS:["apiKey"]Initialize sdk as shown above.Options 2: Using ConfigurationBuilderfromsecurenative.securenativeimportSecureNativefromsecurenative.config.securenative_optionsimportSecureNativeOptionsoptions=SecureNativeOptions(api_key="YOUR_API_KEY",max_events=10,log_level="ERROR",pii_regex_pattern='((?i)(http_auth_)(\w+)?)')securenative=SecureNative.init_with_options(options)