UNPKG

kindergarten

Version:

Kindergarten is a JavaScript library which helps programmers to achieve modular security using sandbox pattern

83 lines (58 loc) 3.89 kB
'use strict'; exports.__esModule = true; exports.default = undefined; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _find = require('lodash/find'); var _find2 = _interopRequireDefault(_find); var _HeadGoverness2 = require('./HeadGoverness'); var _HeadGoverness3 = _interopRequireDefault(_HeadGoverness2); var _utils = require('../utils'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /** * German governess loves rules. She automatically guards all exposed methods. * This means that she calls `guard()` and passes the name of the exposed * method as a first argument and arguments passed to exposed method as well. */ var GermanGoverness = function (_HeadGoverness) { _inherits(GermanGoverness, _HeadGoverness); function GermanGoverness() { _classCallCheck(this, GermanGoverness); return _possibleConstructorReturn(this, (GermanGoverness.__proto__ || Object.getPrototypeOf(GermanGoverness)).apply(this, arguments)); } _createClass(GermanGoverness, [{ key: 'governed', /** * The overriden governed method of the HeadGoverness, that calls guard * method before any exposed method is executed. The first parameter passed * to guard method will be the name of the exposed method. So make sure the * name of exposed method and rule is the same when defining a perimeter. */ value: function governed(callback) { var args = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; var callingContext = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; var guardArgs = args; guardArgs.unshift(this._detectNameOfExposedMethod(callingContext, callback)); // Call the guard method on each exposed method this.guard.apply(this, guardArgs); return _HeadGoverness3.default.prototype.governed.call(this, callback, args, callingContext); } /** * Find the method in the perimeter. */ }, { key: '_detectNameOfExposedMethod', value: function _detectNameOfExposedMethod(perimeter, method) { if ((0, _utils.isPerimeter)(perimeter)) { return (0, _find2.default)(perimeter.expose || [], function (m) { return perimeter[m] === method; }); } throw new Error('German governess can only be used within sandbox.'); } }]); return GermanGoverness; }(_HeadGoverness3.default); exports.default = GermanGoverness;