@@ -4,93 +4,85 @@ import com.aheaditec.talsec_security.security.api.Talsec
44import com.aheaditec.talsec_security.security.api.TalsecConfig
55import com.aheaditec.talsec_security.security.api.ThreatListener
66import com.facebook.react.bridge.Arguments
7+ import com.facebook.react.bridge.Promise
78import com.facebook.react.bridge.ReactApplicationContext
89import com.facebook.react.bridge.ReactContextBaseJavaModule
910import com.facebook.react.bridge.ReactMethod
10- import com.facebook.react.bridge.WritableMap
1111import com.facebook.react.bridge.ReadableMap
12+ import com.facebook.react.bridge.WritableArray
1213import com.facebook.react.modules.core.DeviceEventManagerModule
1314
1415class FreeraspReactNativeModule (val reactContext : ReactApplicationContext ) :
15- ReactContextBaseJavaModule (reactContext), ThreatListener .ThreatDetected ,
16- FreeraspDeviceStateListener .DeviceStateListener {
16+ ReactContextBaseJavaModule (reactContext) {
1717
18- private val listener = ThreatListener (this , FreeraspDeviceStateListener )
18+ private val listener = ThreatListener (FreeraspThreatHandler , FreeraspThreatHandler )
1919
2020 override fun getName (): String {
2121 return NAME
2222 }
2323
24+ init {
25+ appReactContext = reactContext
26+ }
27+
2428 @ReactMethod
2529 fun talsecStart (
26- options : ReadableMap
30+ options : ReadableMap ,
31+ promise : Promise
2732 ) {
2833
2934 try {
3035 val config = parseTalsecConfig(options)
31- FreeraspDeviceStateListener .listener = this
36+ FreeraspThreatHandler .listener = ThreatListener
3237 listener.registerListener(reactContext)
3338 Talsec .start(reactContext, config)
34- sendOngoingPluginResult( " started" , null )
39+ promise.resolve( " freeRASP started" )
3540
3641 } catch (e: Exception ) {
3742 val params = Arguments .createMap().apply {
3843 putString(" message" , e.message)
3944 }
40- sendOngoingPluginResult (" initializationError" , params)
45+ promise.reject (" initializationError" , params)
4146 }
4247 }
4348
49+ /* *
50+ * Method to get the random identifiers of callbacks
51+ */
4452 @ReactMethod
45- fun addListener (@Suppress( " UNUSED_PARAMETER " ) eventName : String ) {
46- // Set up any upstream listeners or background tasks as necessary
53+ fun getThreatIdentifiers ( promise : Promise ) {
54+ promise.resolve( Threat .getThreatValues())
4755 }
4856
57+ /* *
58+ * Method to setup the message passing between native and React Native
59+ * @return list of [THREAT_CHANNEL_NAME, THREAT_CHANNEL_KEY]
60+ */
4961 @ReactMethod
50- fun removeListeners (@Suppress(" UNUSED_PARAMETER" ) count : Int ) {
51- // Remove upstream listeners, stop unnecessary background tasks
52- }
53-
54- override fun onRootDetected () {
55- sendOngoingPluginResult(" privilegedAccess" , null )
56- }
57-
58- override fun onDebuggerDetected () {
59- sendOngoingPluginResult(" debug" , null )
60- }
61-
62- override fun onEmulatorDetected () {
63- sendOngoingPluginResult(" simulator" , null )
64- }
65-
66- override fun onTamperDetected () {
67- sendOngoingPluginResult(" appIntegrity" , null )
68- }
69-
70- override fun onUntrustedInstallationSourceDetected () {
71- sendOngoingPluginResult(" unofficialStore" , null )
62+ fun getThreatChannelData (promise : Promise ) {
63+ val channelData: WritableArray = Arguments .createArray()
64+ channelData.pushString(THREAT_CHANNEL_NAME )
65+ channelData.pushString(THREAT_CHANNEL_KEY )
66+ promise.resolve(channelData)
7267 }
7368
74- override fun onHookDetected () {
75- sendOngoingPluginResult(" hooks" , null )
76- }
77-
78- override fun onDeviceBindingDetected () {
79- sendOngoingPluginResult(" deviceBinding" , null )
80- }
81-
82- override fun onObfuscationIssuesDetected () {
83- sendOngoingPluginResult(" obfuscationIssues" , null )
69+ /* *
70+ * We never send an invalid callback over our channel.
71+ * Therefore, if this happens, we want to kill the app.
72+ */
73+ @ReactMethod
74+ fun onInvalidCallback () {
75+ android.os.Process .killProcess(android.os.Process .myPid())
8476 }
8577
86- override fun deviceStateChangeDetected (threatType : String ) {
87- sendOngoingPluginResult(threatType, null )
78+ @ReactMethod
79+ fun addListener (@Suppress(" UNUSED_PARAMETER" ) eventName : String ) {
80+ // Set up any upstream listeners or background tasks as necessary
8881 }
8982
90- private fun sendOngoingPluginResult (eventName : String , params : WritableMap ? ) {
91- reactContext
92- .getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
93- .emit(eventName, params)
83+ @ReactMethod
84+ fun removeListeners (@Suppress(" UNUSED_PARAMETER" ) count : Int ) {
85+ // Remove upstream listeners, stop unnecessary background tasks
9486 }
9587
9688 private fun parseTalsecConfig (config : ReadableMap ): TalsecConfig {
@@ -125,5 +117,23 @@ class FreeraspReactNativeModule(val reactContext: ReactApplicationContext) :
125117
126118 companion object {
127119 const val NAME = " FreeraspReactNative"
120+ val THREAT_CHANNEL_NAME = (10000 .. 999999999 ).random()
121+ .toString() // name of the channel over which threat callbacks are sent
122+ val THREAT_CHANNEL_KEY = (10000 .. 999999999 ).random()
123+ .toString() // key of the argument map under which threats are expected
124+ private lateinit var appReactContext: ReactApplicationContext
125+ private fun notifyListeners (threat : Threat ) {
126+ val params = Arguments .createMap()
127+ params.putInt(THREAT_CHANNEL_KEY , threat.value)
128+ appReactContext
129+ .getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
130+ .emit(THREAT_CHANNEL_NAME , params)
131+ }
132+ }
133+
134+ internal object ThreatListener : FreeraspThreatHandler.TalsecReactNative {
135+ override fun threatDetected (threatType : Threat ) {
136+ notifyListeners(threatType)
137+ }
128138 }
129139}
0 commit comments