UNPKG

3.35 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var core_1 = require("@aerogear/core");
4var metrics_1 = require("./metrics");
5/**
6 * Service module for handling performing and reporting possible security
7 * issues in a mobile application.
8 *
9 * This requires the @aerogear/cordova-plugin-aerogear-security plugin to be
10 * included in an application.
11 */
12var SecurityService = /** @class */ (function () {
13 function SecurityService() {
14 }
15 /**
16 * Execute the provided security check and return the result.
17 *
18 * @returns The result of the provided check.
19 */
20 SecurityService.prototype.check = function (check) {
21 return check.check();
22 };
23 /**
24 * Execute the provided security check and publish the result as a metric.
25 *
26 * @return The sent metric for the check result.
27 */
28 SecurityService.prototype.checkAndPublishMetric = function (check) {
29 var _this = this;
30 return this.check(check)
31 .then(function (checkResult) { return _this.publishCheckResultMetrics(checkResult); })
32 .then(function (checkMetrics) { return checkMetrics[0]; });
33 };
34 /**
35 * Execute the provided security checks and return the results in an array.
36 *
37 * @returns An array of results for the provided checks.
38 */
39 SecurityService.prototype.checkMany = function () {
40 var checks = [];
41 for (var _i = 0; _i < arguments.length; _i++) {
42 checks[_i] = arguments[_i];
43 }
44 return Promise.all(checks.map(function (check) { return check.check(); }));
45 };
46 /**
47 * Execute the provided security checks and publish the results as metrics.
48 *
49 * @return An array of the sent metrics.
50 */
51 SecurityService.prototype.checkManyAndPublishMetric = function () {
52 var _this = this;
53 var checks = [];
54 for (var _i = 0; _i < arguments.length; _i++) {
55 checks[_i] = arguments[_i];
56 }
57 return this.checkMany.apply(this, checks).then(function (checkResults) { return _this.publishCheckResultMetrics.apply(_this, checkResults); });
58 };
59 /**
60 * Publish metrics results from self defence checks to a metrics service.
61 * Application configuration must be provided to the security service on
62 * creation, otherwise metrics sending will always fail.
63 *
64 * @return Promise with the result of the underlying metrics publisher.
65 */
66 SecurityService.prototype.publishCheckResultMetrics = function () {
67 var results = [];
68 for (var _i = 0; _i < arguments.length; _i++) {
69 results[_i] = arguments[_i];
70 }
71 if (!results || results.length === 0) {
72 return Promise.resolve([]);
73 }
74 var checkResultMetrics = new metrics_1.CheckResultMetrics(results);
75 if (!core_1.INSTANCE || !core_1.INSTANCE.metrics) {
76 return Promise.reject(new Error("Metrics configuration is not available."));
77 }
78 return core_1.INSTANCE.metrics.publish(SecurityService.METRICS_KEY, [checkResultMetrics])
79 .then(function () { return checkResultMetrics.collect(); });
80 };
81 SecurityService.METRICS_KEY = "security";
82 return SecurityService;
83}());
84exports.SecurityService = SecurityService;
85//# sourceMappingURL=SecurityService.js.map
\No newline at end of file