Skip to content

Commit 344c3fb

Browse files
committed
refactor: rename MalwareScanScope to ScanScope and extract config helpers
1 parent 91370b1 commit 344c3fb

4 files changed

Lines changed: 49 additions & 47 deletions

File tree

android/src/main/java/com/freeraspreactnative/utils/Extensions.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ internal fun PackageInfo.toRNPackageInfo(context: ReactContext): RNPackageInfo {
9696
)
9797
}
9898

99-
internal fun ReadableMap.toMalwareScanScope(): MalwareScanScope {
99+
internal fun ReadableMap.toScanScope(): MalwareScanScope {
100100
val scanScope = ScopeType.valueOf(getStringThrowing("scanScope"))
101101
val trustedInstallSources = getArraySafe("trustedInstallSources").toList().ifEmpty { null }
102102
return MalwareScanScope(scanScope, trustedInstallSources)
@@ -107,9 +107,9 @@ internal fun ReadableMap.toSuspiciousAppDetectionConfig(): SuspiciousAppDetectio
107107
val hashes = getArraySafe("hashes").toSet().ifEmpty { null }
108108
val requestedPermissions = getNestedArraySafe("requestedPermissions").map { it.toSet() }.toSet().ifEmpty { null }
109109
val grantedPermissions = getNestedArraySafe("grantedPermissions").map { it.toSet() }.toSet().ifEmpty { null }
110-
val malwareScanScope = getMapThrowing("malwareScanScope").toMalwareScanScope()
110+
val scanScope = getMapThrowing("scanScope").toScanScope()
111111
val reasonMode = ReasonMode.valueOf(getStringThrowing("reasonMode"))
112-
return SuspiciousAppDetectionConfig(packageNames, hashes, requestedPermissions, grantedPermissions, malwareScanScope, reasonMode)
112+
return SuspiciousAppDetectionConfig(packageNames, hashes, requestedPermissions, grantedPermissions, scanScope, reasonMode)
113113
}
114114

115115
/**

src/api/methods/native.ts

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,7 @@
11
import { Platform } from 'react-native';
22
import { FreeraspReactNative } from '../nativeModules';
3-
import type {
4-
MalwareScanScope,
5-
ReasonMode,
6-
SuspiciousAppDetectionConfig,
7-
TalsecAndroidConfig,
8-
TalsecConfig,
9-
} from '../../types/types';
10-
11-
const DEFAULT_MALWARE_SCAN_SCOPE: MalwareScanScope = {
12-
scanScope: 'SIDELOADED_ONLY',
13-
};
14-
15-
const DEFAULT_REASON_MODE: ReasonMode = 'HIGHEST_CONFIDENCE';
16-
17-
const withSuspiciousAppDetectionDefaults = (
18-
config: SuspiciousAppDetectionConfig
19-
): SuspiciousAppDetectionConfig => ({
20-
...config,
21-
malwareScanScope: config.malwareScanScope ?? DEFAULT_MALWARE_SCAN_SCOPE,
22-
reasonMode: config.reasonMode ?? DEFAULT_REASON_MODE,
23-
});
24-
25-
const normalizeAndroidConfig = (
26-
androidConfig: TalsecAndroidConfig
27-
): TalsecAndroidConfig => {
28-
if (!androidConfig.suspiciousAppDetectionConfig) {
29-
return androidConfig;
30-
}
31-
return {
32-
...androidConfig,
33-
suspiciousAppDetectionConfig: withSuspiciousAppDetectionDefaults(
34-
androidConfig.suspiciousAppDetectionConfig
35-
),
36-
};
37-
};
38-
39-
const normalizeConfig = (options: TalsecConfig): TalsecConfig => ({
40-
...options,
41-
androidConfig: options.androidConfig
42-
? normalizeAndroidConfig(options.androidConfig)
43-
: undefined,
44-
});
3+
import type { TalsecConfig } from '../../types/types';
4+
import { normalizeConfig } from '../../utils/config';
455

466
export const talsecStart = async (options: TalsecConfig): Promise<string> => {
477
return FreeraspReactNative.talsecStart(normalizeConfig(options));

src/types/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export type ScopeType =
1515

1616
export type ReasonMode = 'ALL' | 'HIGHEST_CONFIDENCE';
1717

18-
export type MalwareScanScope = {
18+
export type ScanScope = {
1919
scanScope: ScopeType;
2020
trustedInstallSources?: string[];
2121
};
@@ -25,7 +25,7 @@ export type SuspiciousAppDetectionConfig = {
2525
hashes?: string[];
2626
requestedPermissions?: string[][];
2727
grantedPermissions?: string[][];
28-
malwareScanScope: MalwareScanScope;
28+
scanScope: ScanScope;
2929
reasonMode: ReasonMode;
3030
};
3131

src/utils/config.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import type {
2+
ReasonMode,
3+
ScanScope,
4+
SuspiciousAppDetectionConfig,
5+
TalsecAndroidConfig,
6+
TalsecConfig,
7+
} from '../types/types';
8+
9+
const DEFAULT_SCAN_SCOPE: ScanScope = {
10+
scanScope: 'SIDELOADED_ONLY',
11+
};
12+
13+
const DEFAULT_REASON_MODE: ReasonMode = 'HIGHEST_CONFIDENCE';
14+
15+
export const withDefaults = (
16+
config: SuspiciousAppDetectionConfig
17+
): SuspiciousAppDetectionConfig => ({
18+
...config,
19+
scanScope: config.scanScope ?? DEFAULT_SCAN_SCOPE,
20+
reasonMode: config.reasonMode ?? DEFAULT_REASON_MODE,
21+
});
22+
23+
export const normalizeAndroidConfig = (
24+
androidConfig: TalsecAndroidConfig
25+
): TalsecAndroidConfig => {
26+
if (!androidConfig.suspiciousAppDetectionConfig) {
27+
return androidConfig;
28+
}
29+
return {
30+
...androidConfig,
31+
suspiciousAppDetectionConfig: withDefaults(
32+
androidConfig.suspiciousAppDetectionConfig
33+
),
34+
};
35+
};
36+
37+
export const normalizeConfig = (options: TalsecConfig): TalsecConfig => ({
38+
...options,
39+
androidConfig: options.androidConfig
40+
? normalizeAndroidConfig(options.androidConfig)
41+
: undefined,
42+
});

0 commit comments

Comments
 (0)