This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
/
SecurityService.ts
77 lines (68 loc) · 2.57 KB
/
SecurityService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { DeviceCheck, DeviceCheckResult } from "./deviceTrust";
import { CheckResultMetrics, SecurityCheckResultMetric } from "./metrics";
import { MetricsService } from "@aerogear/core";
/**
* Service module for handling performing and reporting possible security
* issues in a mobile application.
*
* This requires the @aerogear/cordova-plugin-aerogear-security plugin to be
* included in an application.
*/
export class SecurityService {
private static readonly METRICS_KEY = "security";
constructor(private metrics?: MetricsService) {
}
/**
* Execute the provided security check and return the result.
*
* @returns The result of the provided check.
*/
public check(check: DeviceCheck): Promise<DeviceCheckResult> {
return check.check();
}
/**
* Execute the provided security check and publish the result as a metric.
*
* @return The sent metric for the check result.
*/
public checkAndPublishMetric(check: DeviceCheck): Promise<SecurityCheckResultMetric> {
return this.check(check)
.then(checkResult => this.publishCheckResultMetrics(checkResult))
.then(checkMetrics => checkMetrics[0]);
}
/**
* Execute the provided security checks and return the results in an array.
*
* @returns An array of results for the provided checks.
*/
public checkMany(...checks: DeviceCheck[]): Promise<DeviceCheckResult[]> {
return Promise.all(checks.map(check => check.check()));
}
/**
* Execute the provided security checks and publish the results as metrics.
*
* @return An array of the sent metrics.
*/
public checkManyAndPublishMetric(...checks: DeviceCheck[]): Promise<SecurityCheckResultMetric[]> {
return this.checkMany(...checks)
.then(checkResults => this.publishCheckResultMetrics(...checkResults));
}
/**
* Publish metrics results from self defence checks to a metrics service.
* Application configuration must be provided to the security service on
* creation, otherwise metrics sending will always fail.
*
* @return Promise with the result of the underlying metrics publisher.
*/
private publishCheckResultMetrics(...results: DeviceCheckResult[]): Promise<SecurityCheckResultMetric[]> {
if (!results || results.length === 0) {
return Promise.resolve([]);
}
const checkResultMetrics = new CheckResultMetrics(results);
if (!this.metrics) {
return Promise.reject(new Error("Metrics configuration is not available."));
}
return this.metrics.publish(SecurityService.METRICS_KEY, [checkResultMetrics])
.then(() => checkResultMetrics.collect());
}
}