ryuu
Version:
Domo App Dev Studio CLI, The main tool used to create, edit, and publish app designs to Domo
151 lines • 6.68 kB
JavaScript
;
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Oauth = void 0;
var fs = require("fs-extra");
var Domo = require("ryuu-client");
var manifest_1 = require("./manifest");
var axios_1 = require("axios");
var qs = require("qs");
var open = require("open");
var chalk = require("chalk");
var Configstore = require("configstore");
var jwt_decode_1 = require("jwt-decode");
var home = Domo.getHomeDir();
var Oauth = /** @class */ (function () {
function Oauth() {
}
Oauth.isOAuthEnabled = function (manifestName) {
if (manifest_1.ManifestUtils.hasManifest(manifestName)) {
var manifest = manifest_1.ManifestUtils.getManifest(manifestName);
return !!manifest['oAuthEnabled'];
}
return false;
};
Oauth.hasAppProxyId = function (manifestName) {
var manifest = manifest_1.ManifestUtils.getManifest(manifestName);
return (!!Object.keys(manifest).includes('cardId') ||
!!Object.keys(manifest).includes('proxyId') ||
!!Object.keys(manifest).includes('appContextId'));
};
Oauth.getScopes = function (manifestName) {
var manifest = manifest_1.ManifestUtils.getManifest(manifestName);
if (!!Object.keys(manifest).includes('scopes')) {
return __spreadArray(['domoapps'], manifest.scopes, true);
}
return ['domoapps'];
};
Oauth.hasTokens = function (registration) {
var scopes = this.getScopes(registration.manifestName);
var configstore = new Configstore('/ryuu/' + registration.instance);
var token = configstore.get("".concat(registration.proxyId, "-").concat(scopes.join('-'), "-accessToken"));
if (token) {
var decoded = (0, jwt_decode_1.default)(token);
console.log(decoded);
return true;
}
return false;
};
Oauth.register = function (registration) {
var options = {
url: "https://".concat(registration.instance, "/api/oauth2/device_authorization"),
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
data: qs.stringify({
client_id: "domo:ryuu:".concat(registration.proxyId),
scope: this.getScopes(registration.manifestName).join(' '),
}),
};
return (0, axios_1.default)(options)
.then(function (response) {
return Promise.resolve(response);
})
.catch(function (error) {
return Promise.reject(error);
});
};
Oauth.verifyRegistration = function (registration) {
var _this = this;
return new Promise(function (resolve, reject) {
open("".concat(registration.verification_uri, "?user_code=").concat(registration.user_code));
// scopes are space separated
var scopes = _this.getScopes(registration.manifestName).join(' ');
var options = {
url: "https://".concat(registration.instance, "/domoapps/admin/device-registration/").concat(registration.proxyId, "?scope=").concat(scopes, "&device_code=").concat(registration.device_code),
method: 'GET',
};
var checkRegistration = setInterval(function () {
new Promise(function (resolve, reject) {
(0, axios_1.default)(options)
.then(function (response) {
resolve(response);
})
.catch(function (error) {
reject(error);
});
})
.then(function (result) {
clearInterval(checkRegistration);
//@ts-ignore
resolve(result);
})
.catch(function (error) {
console.log(chalk.grey('Waiting for browser oauth registration'));
});
}, 1000);
setTimeout(function () {
clearInterval(checkRegistration);
reject('Timed out for oauth registration verification.');
}, 20000);
});
};
Oauth.getLoginData = function (loginFile) {
return fs.existsSync(loginFile) ? fs.readJsonSync(loginFile) : {};
};
Oauth.persistTokens = function (registration) {
var configstore = new Configstore('/ryuu/' + registration.instance);
var scopes = this.getScopes(registration.manifestName);
configstore.set("".concat(registration.proxyId, "-").concat(scopes.join('-'), "-accessToken"), registration.accessToken);
configstore.set("".concat(registration.proxyId, "-").concat(scopes.join('-'), "-refreshToken"), registration.refreshToken);
return {
accessToken: registration.accessToken,
refreshToken: registration.refreshToken,
};
};
Oauth.deleteTokens = function (registration) {
var configstore = new Configstore('/ryuu/' + registration.instance);
var scopes = this.getScopes(registration.manifestName);
return Promise.all([
configstore.delete("".concat(registration.proxyId, "-").concat(scopes.join('-'), "-accessToken")),
configstore.delete("".concat(registration.proxyId, "-").concat(scopes.join('-'), "-refreshToken")),
]);
};
Oauth.getTokens = function (registration) {
var configstore = new Configstore('/ryuu/' + registration.instance);
var scopes = this.getScopes(registration.manifestName);
return new Promise(function (resolve, reject) {
Promise.all([
configstore.get("".concat(registration.proxyId, "-").concat(scopes.join('-'), "-accessToken")),
configstore.get("".concat(registration.proxyId, "-").concat(scopes.join('-'), "-refreshToken")),
])
.then(function (_a) {
var accessToken = _a[0], refreshToken = _a[1];
resolve({ accessToken: accessToken, refreshToken: refreshToken });
})
.catch(reject);
});
};
return Oauth;
}());
exports.Oauth = Oauth;
//# sourceMappingURL=oauth.js.map