@looker/sdk-node
Version:
Looker SDK Runtime for Node Library
164 lines (163 loc) • 6.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.NodeSession = void 0;
var _sdkRtl = require("@looker/sdk-rtl");
var _nodeTransport = require("./nodeTransport");
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var strPost = 'POST';
var strDelete = 'DELETE';
class NodeSession extends _sdkRtl.AuthSession {
constructor(settings, transport) {
super(settings, transport || new _nodeTransport.NodeTransport(settings));
this.settings = settings;
_defineProperty(this, "apiPath", '/api/4.0');
_defineProperty(this, "_authToken", new _sdkRtl.AuthToken());
_defineProperty(this, "_sudoToken", new _sdkRtl.AuthToken());
}
get activeToken() {
if (this._sudoToken.access_token) {
return this._sudoToken;
}
return this._authToken;
}
isAuthenticated() {
var token = this.activeToken;
if (!(token && token.access_token)) return false;
return token.isActive();
}
authenticate(props) {
var _this = this;
return _asyncToGenerator(function* () {
var token = yield _this.getToken();
if (token && token.access_token) {
props.headers.Authorization = "Bearer ".concat(token.access_token);
}
return props;
})();
}
isSudo() {
return !!this.sudoId && this._sudoToken.isActive();
}
getToken() {
var _this2 = this;
return _asyncToGenerator(function* () {
if (!_this2.isAuthenticated()) {
yield _this2.login();
}
return _this2.activeToken;
})();
}
reset() {
this.sudoId = '';
this._authToken.reset();
this._sudoToken.reset();
}
login(sudoId) {
var _this3 = this;
return _asyncToGenerator(function* () {
if (sudoId || sudoId !== _this3.sudoId || !_this3.isAuthenticated()) {
if (sudoId) {
yield _this3._login(sudoId.toString());
} else {
yield _this3._login();
}
}
return _this3.activeToken;
})();
}
logout() {
var _this4 = this;
return _asyncToGenerator(function* () {
var result = false;
if (_this4.isAuthenticated()) {
result = yield _this4._logout();
}
return result;
})();
}
sudoLogout() {
var _this5 = this;
return _asyncToGenerator(function* () {
var result = false;
if (_this5.isSudo()) {
result = yield _this5.logout();
_this5._sudoToken.reset();
}
return result;
})();
}
_login(newId) {
var _this6 = this;
return _asyncToGenerator(function* () {
yield _this6.sudoLogout();
if (newId !== _this6.sudoId) {
_this6.sudoId = newId || '';
}
if (!_this6._authToken.isActive()) {
_this6.reset();
var section = _this6.settings.readConfig();
var clientId = section.client_id;
var clientSecret = section.client_secret;
if (!clientId || !clientSecret) {
throw (0, _sdkRtl.sdkError)({
message: 'API credentials client_id and/or client_secret are not set'
});
}
var body = (0, _sdkRtl.encodeParams)({
client_id: clientId,
client_secret: clientSecret
});
var headers = {
'content-type': 'application/x-www-form-urlencoded'
};
var token = yield _this6.ok(_this6.transport.request(strPost, "".concat(_this6.apiPath, "/login"), undefined, body, undefined, {
headers
}));
_this6._authToken.setToken(token);
}
if (_this6.sudoId) {
var _token = _this6.activeToken;
var promise = _this6.transport.request(strPost, encodeURI("".concat(_this6.apiPath, "/login/").concat(newId)), null, null, init => {
if (_token.access_token) {
init.headers.Authorization = "Bearer ".concat(_token.access_token);
}
return init;
}, _this6.settings);
var accessToken = yield _this6.ok(promise);
_this6._sudoToken.setToken(accessToken);
}
return _this6.activeToken;
})();
}
_logout() {
var _this7 = this;
return _asyncToGenerator(function* () {
var token = _this7.activeToken;
var promise = _this7.transport.request(strDelete, "".concat(_this7.apiPath, "/logout"), null, null, init => {
if (token.access_token) {
init.headers.Authorization = "Bearer ".concat(token.access_token);
}
return init;
}, _this7.settings);
yield _this7.ok(promise);
if (_this7.sudoId) {
_this7.sudoId = '';
_this7._sudoToken.reset();
if (!_this7._authToken.isActive()) {
yield _this7.login();
}
} else {
_this7.reset();
}
return true;
})();
}
}
exports.NodeSession = NodeSession;
//# sourceMappingURL=nodeSession.js.map