UNPKG

@netology-group/account

Version:
83 lines (68 loc) 3.67 kB
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } export var IdP = /*#__PURE__*/ function () { function IdP(config) { var _this = this; _classCallCheck(this, IdP); _defineProperty(this, "endpoint", void 0); _defineProperty(this, "accountEndpoint", void 0); _defineProperty(this, "authnEndpoint", void 0); if (!config) throw new TypeError('Missing provider configuration'); var _accountEndpoint = function _accountEndpoint(conf) { if (!_this.endpoint && !conf.accountEndpoint) throw new TypeError('Could not resolve account endpoint'); return conf.accountEndpoint && typeof conf.accountEndpoint === 'function' ? conf.accountEndpoint() : conf.accountEndpoint || "".concat(_this.endpoint, "/accounts"); }; // eslint-disable-line semi var _authnEndpoint = function _authnEndpoint(conf) { if (!_this.endpoint && !conf.authnEndpoint) throw new TypeError('Could not resolve authentication endpoint'); return conf.authnEndpoint && typeof conf.authnEndpoint === 'function' ? conf.authnEndpoint() : conf.authnEndpoint || "".concat(_this.endpoint, "/authn"); }; this.endpoint = config.endpoint; this.accountEndpoint = _accountEndpoint(config); this.authnEndpoint = _authnEndpoint(config); } _createClass(IdP, [{ key: "refreshAccessToken", value: function refreshAccessToken(label, refreshToken) { if (!label) throw new TypeError('Incorrect parameter `label`'); if (!refreshToken) throw new TypeError("Incorrect parameter 'refreshToken': ".concat(refreshToken)); var uri = "".concat(this.accountEndpoint, "/").concat(label, "/refresh"); return new Request(uri, { method: 'POST', headers: { 'Authorization': "Bearer ".concat(refreshToken) } }); } }, { key: "revokeRefreshToken", value: function revokeRefreshToken(label, refreshToken) { if (!label) throw new TypeError('Incorrect parameter `label`'); if (!refreshToken) throw new TypeError("Incorrect parameter 'refreshToken': ".concat(refreshToken)); var uri = "".concat(this.accountEndpoint, "/").concat(label, "/revoke"); return new Request(uri, { method: 'POST', headers: { 'Authorization': "Bearer ".concat(refreshToken) } }); } }, { key: "account", value: function account(label, accessToken) { if (!label) throw new TypeError('Incorrect parameter `label`'); if (!accessToken) throw new TypeError("Incorrect parameter 'accessToken': ".concat(accessToken)); var uri = "".concat(this.authnEndpoint, "/").concat(label); return new Request(uri, { method: 'GET', headers: { 'Authorization': "Bearer ".concat(accessToken) } }); } }]); return IdP; }();