Native - Easiest Implementation
Native - Easiest Implementation
Native - Easiest Implementation
In this article we will learn how to integrate the SDK to your native iOS/Android app.
Highlights:
Note that the Android implementation requires that your app use OkHttp which supports Interceptors.
Note that the iOS implementation requires that your app use URLSession or any third party library that based on it.
We will cover the following topics:
The SDK uses a custom URL protocol in order to manipulate your URL requests. The SDK creates a new URLSession object to load your URL requests. As a result, there are few limitations that you should be aware of before the integration:
urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping @Sendable (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) in your URLSessionDelegate, then this function will not be called and the default OS logic will apply in your URL request.URLSession object is configured with URLSessionConfiguration.default. Any custom configuration on your side will not apply’Application’s onCreate function on Android.AppDelegate’s didFinishLaunchingWithOptions function on iOS.example.com the SDK will also intercept calls to www.example.com and api.example.com.Here is an example of how it should be:
Let’s talk about what we have in the code here:
HSPolicy instance. This object is used to configure the SDK’s behavior. Here, we set the HSAutomaticInterceptorPolicy/interceptorType property to HSAutomaticInterceptorType/intercept. This will turn on the Automatic Interception feature of the SDK. In short, this feature allows the SDK to manipulate URL requests and handle their responses by itself. We also set our server’s domain. This will tell the SDK to intercept URL requests only for this domain.HumanSecurity/start(appId:policy:) function of the SDK. We provide the following parameters:
Application instance (Android only).In iOS, The Automatic Interception uses the URLSessionConfiguration.default to send your URL request. However, if your app uses a custom configuration, you should set your configuration in the policy - HSAutomaticInterceptorPolicy/urlSessionConfiguration.
If your app communicates with several servers that have different AppID, you can call the HumanSecurity/start(appIds:policy:) function which allow you to pass an array of AppIDs. You should specify the relevant AppID for each API call in the SDK.
The SDK adds its HTTP headers to you URL requests automatically. There is no need to manually add those headers.
The SDK handles the blocked request and present a challenge to the user automatically.
The SDK provides a custom error response directly to your request handler.
Here is an example of how it should be:
Using URLSession:
Using Alamofire:
Let’s talk about what we have in the code here:
HSInterceptor. This interceptor must be the last interceptor in the list.Please note that while your request handler is called, the SDK is presenting a challenge to the user.
Handle it as a failure.
Your app should handle the blocked request as a failure. However, you should consider that your app’s UI will be shown again after the challenge was solved/cancelled by the user. If the request was triggered by a user’s action, you should make it clear that the user may try again the same action.
You can set custom parameters in order to configure HUMAN’s backend with additional parameters.
Those parameters can be set with a dictionary (iOS) or an hash map (Android), with the key “custom_param[x]” where [x] is a number between 1-10.
You should call the HSBotDefender/setCustomParameters(parameters:forAppId:) only after the HumanSecurity/start(appId:policy:) function was already called.
Here is an example of how it should be:
In order to enable Account Defender, you should set the UserID of your current logged-in user in the SDK.
Here is an example of how it should be:
In order to allow Account Defender protect the user’s account, your app has to provide the SDK with outgoing URL requests.
While the SDK’s interceptor is enabled, the SDK is collecting your outgoing URL requests automatically. There is no need to manually call the SDK’s API.
You can set additional data in order to configure HUMAN’s backend with additional parameters.
Those parameters can be set with a dictionary (iOS) or an hash map (Android).
You should call the HSAccountDefender/setAdditionalData(parameters:forAppId:) only after the HumanSecurity/start(appId:policy:) function was already called.
Here is an example of how it should be: