UNPKG

files.com

Version:
200 lines (199 loc) 9.96 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof = require("@babel/runtime/helpers/typeof"); exports.__esModule = true; exports.default = void 0; var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _Api = _interopRequireDefault(require("../Api")); var errors = _interopRequireWildcard(require("../Errors")); var _utils = require("../utils"); var _Session; /* eslint-disable no-unused-vars */ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } /* eslint-enable no-unused-vars */ /** * Class Session */ var Session = /*#__PURE__*/(0, _createClass2.default)(function Session() { var _this = this; var attributes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; (0, _classCallCheck2.default)(this, Session); (0, _defineProperty2.default)(this, "attributes", {}); (0, _defineProperty2.default)(this, "options", {}); (0, _defineProperty2.default)(this, "isLoaded", function () { return !!_this.attributes.id; }); // string # Session ID (0, _defineProperty2.default)(this, "getId", function () { return _this.attributes.id; }); (0, _defineProperty2.default)(this, "setId", function (value) { _this.attributes.id = value; }); // string # Session language (0, _defineProperty2.default)(this, "getLanguage", function () { return _this.attributes.language; }); (0, _defineProperty2.default)(this, "setLanguage", function (value) { _this.attributes.language = value; }); // boolean # Is this session read only? (0, _defineProperty2.default)(this, "getReadOnly", function () { return _this.attributes.read_only; }); (0, _defineProperty2.default)(this, "setReadOnly", function (value) { _this.attributes.read_only = value; }); // boolean # Are insecure SFTP ciphers allowed for this user? (If this is set to true, the site administrator has signaled that it is ok to use less secure SSH ciphers for this user.) (0, _defineProperty2.default)(this, "getSftpInsecureCiphers", function () { return _this.attributes.sftp_insecure_ciphers; }); (0, _defineProperty2.default)(this, "setSftpInsecureCiphers", function (value) { _this.attributes.sftp_insecure_ciphers = value; }); // string # Username to sign in as (0, _defineProperty2.default)(this, "getUsername", function () { return _this.attributes.username; }); (0, _defineProperty2.default)(this, "setUsername", function (value) { _this.attributes.username = value; }); // string # Password for sign in (0, _defineProperty2.default)(this, "getPassword", function () { return _this.attributes.password; }); (0, _defineProperty2.default)(this, "setPassword", function (value) { _this.attributes.password = value; }); // string # If this user has a 2FA device, provide its OTP or code here. (0, _defineProperty2.default)(this, "getOtp", function () { return _this.attributes.otp; }); (0, _defineProperty2.default)(this, "setOtp", function (value) { _this.attributes.otp = value; }); // string # Identifier for a partially-completed login (0, _defineProperty2.default)(this, "getPartialSessionId", function () { return _this.attributes.partial_session_id; }); (0, _defineProperty2.default)(this, "setPartialSessionId", function (value) { _this.attributes.partial_session_id = value; }); (0, _defineProperty2.default)(this, "save", /*#__PURE__*/(0, _asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function _callee() { var newObject; return _regenerator.default.wrap(function (_context) { while (1) switch (_context.prev = _context.next) { case 0: if (!_this.attributes.id) { _context.next = 1; break; } throw new errors.NotImplementedError('The Session object doesn\'t support updates.'); case 1: _context.next = 2; return Session.create(_this.attributes, _this.options); case 2: newObject = _context.sent; _this.attributes = _objectSpread({}, newObject.attributes); return _context.abrupt("return", true); case 3: case "end": return _context.stop(); } }, _callee); }))); Object.entries(attributes).forEach(function (_ref2) { var _ref3 = (0, _slicedToArray2.default)(_ref2, 2), key = _ref3[0], value = _ref3[1]; var normalizedKey = key.replace('?', ''); _this.attributes[normalizedKey] = value; Object.defineProperty(_this, normalizedKey, { value: value, writable: false }); }); this.options = _objectSpread({}, options); }); _Session = Session; // Parameters: // username - string - Username to sign in as // password - string - Password for sign in // otp - string - If this user has a 2FA device, provide its OTP or code here. // partial_session_id - string - Identifier for a partially-completed login (0, _defineProperty2.default)(Session, "create", /*#__PURE__*/(0, _asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function _callee2() { var params, options, response, _args2 = arguments; return _regenerator.default.wrap(function (_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: params = _args2.length > 0 && _args2[0] !== undefined ? _args2[0] : {}; options = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : {}; if (!(params.username && !(0, _utils.isString)(params.username))) { _context2.next = 1; break; } throw new errors.InvalidParameterError("Bad parameter: username must be of type String, received ".concat((0, _utils.getType)(params.username))); case 1: if (!(params.password && !(0, _utils.isString)(params.password))) { _context2.next = 2; break; } throw new errors.InvalidParameterError("Bad parameter: password must be of type String, received ".concat((0, _utils.getType)(params.password))); case 2: if (!(params.otp && !(0, _utils.isString)(params.otp))) { _context2.next = 3; break; } throw new errors.InvalidParameterError("Bad parameter: otp must be of type String, received ".concat((0, _utils.getType)(params.otp))); case 3: if (!(params.partial_session_id && !(0, _utils.isString)(params.partial_session_id))) { _context2.next = 4; break; } throw new errors.InvalidParameterError("Bad parameter: partial_session_id must be of type String, received ".concat((0, _utils.getType)(params.partial_session_id))); case 4: _context2.next = 5; return _Api.default.sendRequest('/sessions', 'POST', params, options); case 5: response = _context2.sent; return _context2.abrupt("return", new _Session(response === null || response === void 0 ? void 0 : response.data, options)); case 6: case "end": return _context2.stop(); } }, _callee2); }))); (0, _defineProperty2.default)(Session, "delete", /*#__PURE__*/(0, _asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function _callee3() { var options, _args3 = arguments; return _regenerator.default.wrap(function (_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: options = _args3.length > 0 && _args3[0] !== undefined ? _args3[0] : {}; _context3.next = 1; return _Api.default.sendRequest('/sessions', 'DELETE', {}, options); case 1: case "end": return _context3.stop(); } }, _callee3); }))); (0, _defineProperty2.default)(Session, "destroy", function () { var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; return _Session.delete(params, options); }); var _default = exports.default = Session; module.exports = Session; module.exports.default = Session;