Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V4 update #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
70 changes: 45 additions & 25 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,25 +1,45 @@
iOS/PerimeterX_SDK_Demo/PerimeterX_SDK_Demo.xcworkspace/xcuserdata
iOS/PerimeterX_SDK_Demo/PerimeterX_SDK_Demo.xcodeproj/xcuserdata
iOS/PerimeterX_SDK_Demo/PerimeterX_SDK_Demo.xcodeproj/project.xcworkspace/xcuserdata/
iOS/PerimeterX_SDK_Demo/Pods
Android/PerimeterX_SDK_Demo/*.iml
Android/PerimeterX_SDK_Demo/.gradle
Android/PerimeterX_SDK_Demo/local.properties
Android/PerimeterX_SDK_Demo/.idea
Android/PerimeterX_SDK_Demo/.DS_Store
Android/PerimeterX_SDK_Demo/build
Android/PerimeterX_SDK_Demo/captures
Android/PerimeterX_SDK_Demo/.externalNativeBuild
Android/PerimeterX_SDK_Demo/.cxx
Android/PerimeterX_SDK_Demo/local.properties
Android/PerimeterX_SDK_Demo/app/build
ReactNative/PerimeterX_SDK_Demo/ios/PerimeterX_SDK_Demo.xcworkspace/xcuserdata
ReactNative/PerimeterX_SDK_Demo/ios/PerimeterX_SDK_Demo.xcodeproj/xcuserdata
ReactNative/PerimeterX_SDK_Demo/ios/PerimeterX_SDK_Demo.xcodeproj/project.xcworkspace/xcuserdata/
ReactNative/PerimeterX_SDK_Demo/ios/Pods
ReactNative/PerimeterX_SDK_Demo/ios/build
ReactNative/PerimeterX_SDK_Demo/android/.gradle
ReactNative/PerimeterX_SDK_Demo/android/local.properties
ReactNative/PerimeterX_SDK_Demo/android/.idea
ReactNative/PerimeterX_SDK_Demo/android/app/build
ReactNative/PerimeterX_SDK_Demo/android/app/libs
iOS/HUMAN_SDK_Demo/HUMAN_SDK_Demo.xcworkspace/xcuserdata
iOS/HUMAN_SDK_Demo/HUMAN_SDK_Demo.xcodeproj/xcuserdata
iOS/HUMAN_SDK_Demo/HUMAN_SDK_Demo.xcodeproj/project.xcworkspace/xcuserdata/
iOS/HUMAN_SDK_Demo/Pods
Android/HUMAN_SDK_Demo/*.iml
Android/HUMAN_SDK_Demo/.gradle
Android/HUMAN_SDK_Demo/local.properties
Android/HUMAN_SDK_Demo/.idea
Android/HUMAN_SDK_Demo/.DS_Store
Android/HUMAN_SDK_Demo/build
Android/HUMAN_SDK_Demo/captures
Android/HUMAN_SDK_Demo/.externalNativeBuild
Android/HUMAN_SDK_Demo/.cxx
Android/HUMAN_SDK_Demo/local.properties
Android/HUMAN_SDK_Demo/app/build
ReactNative/HUMAN_Demo/ios/HUMAN_Demo.xcworkspace/xcuserdata
ReactNative/HUMAN_Demo/ios/HUMAN_Demo.xcodeproj/xcuserdata
ReactNative/HUMAN_Demo/ios/HUMAN_Demo.xcodeproj/project.xcworkspace/xcuserdata/
ReactNative/HUMAN_Demo/ios/Pods
ReactNative/HUMAN_Demo/ios/build
ReactNative/HUMAN_Demo/android/.gradle
ReactNative/HUMAN_Demo/android/local.properties
ReactNative/HUMAN_Demo/android/.idea
ReactNative/HUMAN_Demo/android/app/build
ReactNative/HUMAN_Demo/android/app/libs
Flutter/build
Flutter/HUMAN_Demo/build
Flutter/HUMAN_Demo/ios/Pods
Flutter/HUMAN_Demo/ios/HUMAN_Demo.xcworkspace/xcuserdata
Flutter/HUMAN_Demo/ios/HUMAN_Demo.xcodeproj/xcuserdata
Flutter/HUMAN_Demo/ios/HUMAN_Demo.xcodeproj/project.xcworkspace/xcuserdata/
Flutter/HUMAN_Demo/android/.gradle
Flutter/HUMAN_Demo/android/local.properties
Flutter/HUMAN_Demo/android/.idea
Flutter/HUMAN_Demo/android/app/build
Flutter/HUMAN_Demo/android/app/libs
Ionic/MyApp/ios/App/build
Ionic/MyApp/ios/App/Pods
Ionic/MyApp/ios/App/App.xcworkspace/xcuserdata
Ionic/MyApp/ios/App/App.xcodeproj/xcuserdata
Ionic/MyApp/ios/App/App.xcodeproj/project.xcworkspace/xcuserdata/
Ionic/MyApp/android/app/build
visionOS/HumanDemo/HumanDemo.xcodeproj/xcuserdata
visionOS/HumanDemo/HumanDemo.xcodeproj/project.xcworkspace/xcuserdata/
visionOS/HumanDemo/Packages
54 changes: 54 additions & 0 deletions Android/HUMAN_SDK_Demo/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
plugins {
id 'com.android.application'
id 'kotlin-android'
}

android {
namespace 'com.humansecurity.android_sdk_demo'

compileSdkVersion 35
buildToolsVersion "35.0.0"

defaultConfig {
applicationId "com.humansecurity.android_sdk_demo"
minSdkVersion 21
targetSdkVersion 35
versionCode 1
versionName "4.0.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = '17'
}
}

dependencies {

testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.2.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'

// HUMAN SDK - regular integration
implementation 'com.humansecurity:sdk:4.1.0'

// HUMAN SDK - manual integration
// implementation files('libs/HUMAN-release.aar')
// implementation 'androidx.core:core-ktx:1.15.0' // any version (select 1.10.1 if your app targets API 33)
// implementation 'androidx.lifecycle:lifecycle-process:2.8.7' // min v2.6.0
// implementation 'androidx.datastore:datastore-preferences:1.1.1' // any version (select 1.0.0 if your app targets API 33)
// implementation 'com.google.android.material:material:1.12.0' // min v1.6.0
// implementation 'com.fasterxml.uuid:java-uuid-generator:4.3.0' // min v3.0.0
// implementation 'io.ktor:ktor-client-okhttp:3.0.3' // min v3.0.0
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.perimeterx.android_sdk_demo">
package="com.humansecurity.android_sdk_demo">

<!-- PerimeterX -->
<!-- HUMAN -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".MainApplication"
android:name="com.humansecurity.human_sdk_demo.MainApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.PerimeterX_SDK_Demo">
android:theme="@style/Theme.HUMAN_SDK_Demo">
<activity
android:name=".LoginActivity"
android:name="com.humansecurity.human_sdk_demo.LoginActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package com.perimeterx.android_sdk_demo

import com.perimeterx.mobile_sdk.PerimeterX
import com.perimeterx.mobile_sdk.main.PXInterceptor
package com.humansecurity.human_sdk_demo

object APIDataManager {

// properties

private const val serverUrl = "https://sample-ios.pxchk.net/"
const val loginUrl = serverUrl + "login"
const val productsUrl = serverUrl + "products"
private const val ktorExample = false

suspend fun sendLoginRequest(email: String, password: String) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.humansecurity.human_sdk_demo

import android.app.Application
import com.humansecurity.mobile_sdk.HumanSecurity
import com.humansecurity.mobile_sdk.main.HSBotDefenderDelegate
import com.humansecurity.mobile_sdk.main.policy.HSAutomaticInterceptorType
import com.humansecurity.mobile_sdk.main.policy.HSPolicy
import com.humansecurity.mobile_sdk.main.policy.HSStorageMethod

object HumanManager: HSBotDefenderDelegate {

// HumanDelegate

override fun botDefenderRequestBlocked(url: String?, appId: String) {
println("Request Blocked")
}

override fun botDefenderChallengeSolved(appId: String) {
println("Challenge Solved")
}

override fun botDefenderChallengeCancelled(appId: String) {
println("Challenge Cancelled")
}

override fun botDefenderChallengeRendered(appId: String) {
println("Challenge Rendered")
}

override fun botDefenderChallengeRenderFailed(appId: String) {
println("Challenge Render Failed")
}

override fun botDefenderDidUpdateHeaders(headers: HashMap<String, String>, appId: String) {
println("Headers Were Updated")
}

// properties

const val appId = "PXj9y4Q8Em"

// HUMAN

fun start(application: Application) {
println("SDK version: ${HumanSecurity.sdkVersion()}")

start(application, appId)
setCustomParameters()
}

private fun start(application: Application, appId: String) {
// Create and configure the policy //
val policy = HSPolicy()
policy.storageMethod = HSStorageMethod.DATA_STORE
policy.automaticInterceptorPolicy.interceptorType = HSAutomaticInterceptorType.INTERCEPT_AND_RETRY_REQUEST
policy.doctorAppPolicy.enabled = true

// Start HUMAN SDK with your AppID //
HumanSecurity.start(application, appId, policy)
}

private fun setCustomParameters() {
val customParameters = HashMap<String, String>()
customParameters["custom_param1"] = "hello"
customParameters["custom_param2"] = "world"
HumanSecurity.BD.setCustomParameters(customParameters, null)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.perimeterx.android_sdk_demo
package com.humansecurity.human_sdk_demo

import com.perimeterx.mobile_sdk.PerimeterX
import com.perimeterx.mobile_sdk.main.PXInterceptor
import com.humansecurity.mobile_sdk.HumanSecurity
import com.humansecurity.mobile_sdk.main.HSBotDefenderErrorType
import com.humansecurity.mobile_sdk.main.HSInterceptor
import io.ktor.client.*
import io.ktor.client.call.body
import io.ktor.client.engine.okhttp.*
Expand All @@ -13,11 +14,11 @@ object KtorClientExample {

private val ktorHttpClient: HttpClient = HttpClient(OkHttp) {
install(HttpTimeout) {
requestTimeoutMillis = HttpTimeout.INFINITE_TIMEOUT_MS
requestTimeoutMillis = Long.MAX_VALUE
}
engine {
// addInterceptor(MyInterceptor()) // An example of manual integration. Should be added when PXPolicy.urlRequestInterceptionType is set to `PXPolicyUrlRequestInterceptionType/none`
addInterceptor(PXInterceptor()) // When PXPolicy.urlRequestInterceptionType is set to any value rather than `PXPolicyUrlRequestInterceptionType/none`. MUST BE THE LAST INTERCEPTOR IN THE CHAIN
addInterceptor(HSInterceptor()) // When PXPolicy.urlRequestInterceptionType is set to any value rather than `PXPolicyUrlRequestInterceptionType/none`. MUST BE THE LAST INTERCEPTOR IN THE CHAIN
}
}

Expand All @@ -28,14 +29,19 @@ object KtorClientExample {
val response: HttpResponse = ktorHttpClient.request(APIDataManager.loginUrl) {}
println("request was finished")
val responseBody = response.body<String>()
if (responseBody.contains(PerimeterX.blockedErrorBody())) {
println("request was blocked by PX")
}
if (responseBody.contains(PerimeterX.challengeSolvedErrorBody())) {
println("request was blocked by PX and user solved the challenge")
}
if (responseBody.contains(PerimeterX.challengeCancelledErrorBody())) {
println("request was blocked by PX and challenge was cancelled")
when (HumanSecurity.BD.errorType(responseBody)) {
HSBotDefenderErrorType.REQUEST_WAS_BLOCKED -> {
println("request was blocked by HUMAN")
}
HSBotDefenderErrorType.CHALLENGE_WAS_SOLVED -> {
println("request was blocked by HUMAN and user solved the challenge")
}
HSBotDefenderErrorType.CHALLENGE_WAS_CANCELLED -> {
println("request was blocked by HUMAN and challenge was cancelled")
}
else -> {
println("unknown error")
}
}
} catch (exception: Exception) {
println("request was failed. exception: $exception")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.perimeterx.android_sdk_demo
package com.humansecurity.human_sdk_demo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import com.perimeterx.mobile_sdk.PerimeterX
import com.humansecurity.android_sdk_demo.R
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.perimeterx.android_sdk_demo
package com.humansecurity.human_sdk_demo

import android.app.Application

Expand All @@ -7,6 +7,6 @@ class MainApplication: Application() {
override fun onCreate() {
super.onCreate()

PerimeterxManager.start(this)
HumanManager.start(this)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.humansecurity.human_sdk_demo

import com.humansecurity.mobile_sdk.HumanSecurity
import com.humansecurity.mobile_sdk.main.HSBotDefenderErrorType
import okhttp3.Interceptor
import okhttp3.Response
import okhttp3.ResponseBody.Companion.toResponseBody

/*
This is an example to how write a c custom interceptor that interacts with the SDK manually.
*/
class MyInterceptor: Interceptor {

override fun intercept(chain: Interceptor.Chain): Response {
val newRequest = chain.request().newBuilder()

// When HSPolicy.automaticInterceptorPolicy.interceptorType is set to `HSAutomaticInterceptorType/none` => get HTTP headers from HUMAN and add them to your request //
val headers = HumanSecurity.BD.headersForURLRequest(null)
for ((key, value) in headers) {
newRequest.addHeader(key, value)
}

val response = chain.proceed(newRequest.build())
if (!response.isSuccessful) {
// The code below is an example to how you can check that the request was blocked. This is not required
val responseBody = response.body?.string()
if (responseBody != null) {
// When HSPolicy.automaticInterceptorPolicy.interceptorType is set to any value rather than `HSAutomaticInterceptorType/none` => check that the error is "Request blocked by HUMAN" //
when (HumanSecurity.BD.errorType(responseBody)) {
HSBotDefenderErrorType.REQUEST_WAS_BLOCKED -> {
println("request was blocked by HUMAN")
}
else -> {
println("unknown error")
}
}

// When HSPolicy.automaticInterceptorPolicy.interceptorType is set to `HSAutomaticInterceptorType/none` => pass the data and response to HUMAN to handle it //
val isHandledByPX = HumanSecurity.BD.handleResponse(responseBody) { result ->
println("challenge result = $result")
}
if (isHandledByPX) {
println("block response was handled by HUMAN")
// Replace the original response with a specific blocked error
return response.newBuilder().body(HumanSecurity.BD.errorBody(HSBotDefenderErrorType.REQUEST_WAS_BLOCKED).toResponseBody()).build()
}

// Put back the response's body (can be read only once and we just did)
return response.newBuilder().body(responseBody.toResponseBody()).build()
}
}
return response
}
}
Loading