Skip to content

Commit 7817025

Browse files
committed
feat: refactor ios
1 parent c7f8843 commit 7817025

File tree

5 files changed

+110
-105
lines changed

5 files changed

+110
-105
lines changed

ios/FreeraspReactNative.swift

Lines changed: 11 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ class FreeraspReactNative: RCTEventEmitter {
66

77
public static var shared:FreeraspReactNative?
88

9-
let threatChannelKey = String(Int.random(in: 100_000..<999_999_999)) // key of the argument map under which threats are expected
10-
let threatChannelName = String(Int.random(in: 100_000..<999_999_999)) // name of the channel over which threat callbacks are sent
11-
12-
let raspExecutionStateChannelKey = String(Int.random(in: 100_000..<999_999_999)) // key of the argument map under which threats are expected
13-
let raspExecutionStateChannelName = String(Int.random(in: 100_000..<999_999_999)) // name of the channel over which threat callbacks are sent
14-
159
override init() {
1610
super.init()
1711
FreeraspReactNative.shared = self
@@ -77,48 +71,32 @@ class FreeraspReactNative: RCTEventEmitter {
7771
UserDefaults.standard.set(externalId, forKey: "app.talsec.externalid")
7872
resolve("OK - Store external ID")
7973
}
80-
81-
private func getProtectedWindow(completion: @escaping (UIWindow?) -> Void) {
82-
DispatchQueue.main.async {
83-
if #available(iOS 13.0, *) {
84-
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
85-
if let window = windowScene.windows.first {
86-
completion(window)
87-
} else {
88-
completion(nil)
89-
}
90-
} else {
91-
completion(nil)
92-
}
93-
}
94-
}
95-
}
9674

9775
/**
9876
* Method to setup the message passing between native and React Native
9977
*/
10078
@objc(getThreatChannelData:withRejecter:)
10179
private func getThreatChannelData(resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) -> Void {
102-
resolve([threatChannelName, threatChannelKey])
80+
resolve([EventIdentifiers.threatChannelName, EventIdentifiers.threatChannelKey])
10381
}
10482

10583
/**
10684
* Method to setup the message passing between native and React Native
10785
*/
10886
@objc(getRaspExecutionStateChannelData:withRejecter:)
10987
private func getRaspExecutionStateChannelData(resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) -> Void {
110-
resolve([raspExecutionStateChannelName, raspExecutionStateChannelKey])
88+
resolve([EventIdentifiers.raspExecutionStateChannelName, EventIdentifiers.raspExecutionStateChannelKey])
11189
}
11290

11391
func dispatchEvent(securityThreat: SecurityThreat) -> Void {
114-
FreeraspReactNative.shared!.sendEvent(withName: threatChannelName, body: [
115-
threatChannelKey: securityThreat.callbackIdentifier,
92+
FreeraspReactNative.shared!.sendEvent(withName: EventIdentifiers.threatChannelName, body: [
93+
EventIdentifiers.threatChannelKey: securityThreat.callbackIdentifier,
11694
])
11795
}
11896

11997
func dispatchRaspExecutionStateEvent(event: RaspExecutionStates) -> Void {
120-
FreeraspReactNative.shared!.sendEvent(withName: raspExecutionStateChannelName, body: [
121-
raspExecutionStateChannelKey: event.callbackIdentifier,
98+
FreeraspReactNative.shared!.sendEvent(withName: EventIdentifiers.raspExecutionStateChannelName, body: [
99+
EventIdentifiers.raspExecutionStateChannelKey: event.callbackIdentifier,
122100
])
123101
}
124102

@@ -127,15 +105,15 @@ class FreeraspReactNative: RCTEventEmitter {
127105
*/
128106
@objc(getThreatIdentifiers:withRejecter:)
129107
private func getThreatIdentifiers(resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) -> Void {
130-
resolve(getThreatIdentifiers())
108+
resolve(freerasp_react_native.getThreatIdentifiers())
131109
}
132110

133111
/**
134112
* Method to get the random identifiers of callbacks
135113
*/
136114
@objc(getRaspExecutionStateIdentifiers:withRejecter:)
137115
private func getRaspExecutionStateIdentifiers(resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) -> Void {
138-
resolve(getRaspExecutionStateIdentifiers())
116+
resolve(freerasp_react_native.getRaspExecutionStateIdentifiers())
139117
}
140118

141119
/**
@@ -147,71 +125,13 @@ class FreeraspReactNative: RCTEventEmitter {
147125
abort()
148126
}
149127

150-
private func getThreatIdentifiers() -> [Int] {
151-
return SecurityThreat.allCases
152-
.filter {
153-
threat in threat.rawValue != "passcodeChange"
154-
}
155-
.map {
156-
threat in threat.callbackIdentifier
157-
}
158-
}
159-
160-
private func getRaspExecutionStateIdentifiers() -> [Int] {
161-
return RaspExecutionStates.allCases
162-
.map {
163-
event in event.callbackIdentifier
164-
}
165-
}
166-
167128
override func supportedEvents() -> [String]! {
168-
return [threatChannelName, raspExecutionStateChannelName]
129+
return [EventIdentifiers.threatChannelName, EventIdentifiers.raspExecutionStateChannelName]
169130
}
170131
}
171132

172-
struct ThreatIdentifiers {
173-
static let threatIdentifierList: [Int] = (1...14).map { _ in Int.random(in: 100_000..<999_999_999) }
174-
static let raspExecutionStateIdentifierList: [Int] = (1...1).map { _ in Int.random(in: 100_000..<999_999_999) }
175-
}
176133

177-
/// An extension to unify callback names with RN ones.
178-
extension SecurityThreat {
179-
180-
var callbackIdentifier: Int {
181-
switch self {
182-
case .signature:
183-
return ThreatIdentifiers.threatIdentifierList[0]
184-
case .jailbreak:
185-
return ThreatIdentifiers.threatIdentifierList[1]
186-
case .debugger:
187-
return ThreatIdentifiers.threatIdentifierList[2]
188-
case .runtimeManipulation:
189-
return ThreatIdentifiers.threatIdentifierList[3]
190-
case .passcode:
191-
return ThreatIdentifiers.threatIdentifierList[4]
192-
case .passcodeChange:
193-
return ThreatIdentifiers.threatIdentifierList[5]
194-
case .simulator:
195-
return ThreatIdentifiers.threatIdentifierList[6]
196-
case .missingSecureEnclave:
197-
return ThreatIdentifiers.threatIdentifierList[7]
198-
case .systemVPN:
199-
return ThreatIdentifiers.threatIdentifierList[8]
200-
case .deviceChange:
201-
return ThreatIdentifiers.threatIdentifierList[9]
202-
case .deviceID:
203-
return ThreatIdentifiers.threatIdentifierList[10]
204-
case .unofficialStore:
205-
return ThreatIdentifiers.threatIdentifierList[11]
206-
case .screenshot:
207-
return ThreatIdentifiers.threatIdentifierList[12]
208-
case .screenRecording:
209-
return ThreatIdentifiers.threatIdentifierList[13]
210-
@unknown default:
211-
abort()
212-
}
213-
}
214-
}
134+
215135

216136
extension SecurityThreatCenter: @retroactive SecurityThreatHandler, @retroactive RaspExecutionState {
217137

@@ -228,18 +148,4 @@ extension SecurityThreatCenter: @retroactive SecurityThreatHandler, @retroactive
228148
}
229149
}
230150

231-
enum RaspExecutionStates : String, Codable, CaseIterable, Equatable {
232-
233-
case allChecksFinished
234-
235-
public static var allCases: [RaspExecutionStates] {
236-
return [.allChecksFinished]
237-
}
238-
239-
var callbackIdentifier: Int {
240-
switch self {
241-
case .allChecksFinished:
242-
return ThreatIdentifiers.raspExecutionStateIdentifierList[0]
243-
}
244-
}
245-
}
151+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
enum RaspExecutionStates : String, Codable, CaseIterable, Equatable {
2+
3+
case allChecksFinished
4+
5+
public static var allCases: [RaspExecutionStates] {
6+
return [.allChecksFinished]
7+
}
8+
9+
var callbackIdentifier: Int {
10+
switch self {
11+
case .allChecksFinished:
12+
return EventIdentifiers.raspExecutionStateIdentifierList[0]
13+
}
14+
}
15+
}

ios/models/SecurityThreat.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import TalsecRuntime
2+
3+
/// An extension to unify callback names with RN ones.
4+
extension SecurityThreat {
5+
6+
var callbackIdentifier: Int {
7+
switch self {
8+
case .signature:
9+
return EventIdentifiers.threatIdentifierList[0]
10+
case .jailbreak:
11+
return EventIdentifiers.threatIdentifierList[1]
12+
case .debugger:
13+
return EventIdentifiers.threatIdentifierList[2]
14+
case .runtimeManipulation:
15+
return EventIdentifiers.threatIdentifierList[3]
16+
case .passcode:
17+
return EventIdentifiers.threatIdentifierList[4]
18+
case .passcodeChange:
19+
return EventIdentifiers.threatIdentifierList[5]
20+
case .simulator:
21+
return EventIdentifiers.threatIdentifierList[6]
22+
case .missingSecureEnclave:
23+
return EventIdentifiers.threatIdentifierList[7]
24+
case .systemVPN:
25+
return EventIdentifiers.threatIdentifierList[8]
26+
case .deviceChange:
27+
return EventIdentifiers.threatIdentifierList[9]
28+
case .deviceID:
29+
return EventIdentifiers.threatIdentifierList[10]
30+
case .unofficialStore:
31+
return EventIdentifiers.threatIdentifierList[11]
32+
case .screenshot:
33+
return EventIdentifiers.threatIdentifierList[12]
34+
case .screenRecording:
35+
return EventIdentifiers.threatIdentifierList[13]
36+
@unknown default:
37+
abort()
38+
}
39+
}
40+
}

ios/utils/EventIdentifiers.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
struct EventIdentifiers {
2+
static let threatIdentifierList: [Int] = (1...14).map { _ in Int.random(in: 100_000..<999_999_999) }
3+
static let raspExecutionStateIdentifierList: [Int] = (1...1).map { _ in Int.random(in: 100_000..<999_999_999) }
4+
5+
// Channel identifiers for RN event emitter
6+
static let threatChannelKey: String = String(Int.random(in: 100_000..<999_999_999))
7+
static let threatChannelName: String = String(Int.random(in: 100_000..<999_999_999))
8+
static let raspExecutionStateChannelKey: String = String(Int.random(in: 100_000..<999_999_999))
9+
static let raspExecutionStateChannelName: String = String(Int.random(in: 100_000..<999_999_999))
10+
}

ios/utils/Utils.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import TalsecRuntime
2+
3+
internal func getProtectedWindow(completion: @escaping (UIWindow?) -> Void) {
4+
DispatchQueue.main.async {
5+
if #available(iOS 13.0, *) {
6+
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
7+
if let window = windowScene.windows.first {
8+
completion(window)
9+
} else {
10+
completion(nil)
11+
}
12+
} else {
13+
completion(nil)
14+
}
15+
}
16+
}
17+
}
18+
19+
internal func getThreatIdentifiers() -> [Int] {
20+
return SecurityThreat.allCases
21+
.filter {
22+
threat in threat.rawValue != "passcodeChange"
23+
}
24+
.map {
25+
threat in threat.callbackIdentifier
26+
}
27+
}
28+
29+
internal func getRaspExecutionStateIdentifiers() -> [Int] {
30+
return RaspExecutionStates.allCases
31+
.map {
32+
event in event.callbackIdentifier
33+
}
34+
}

0 commit comments

Comments
 (0)