UNPKG

ryuu

Version:

Domo App Dev Studio CLI, The main tool used to create, edit, and publish app designs to Domo

138 lines 5.85 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Oauth = void 0; const fs = require("fs-extra"); // import path = require('path'); // import Domo = require('ryuu-client'); const manifest_1 = require("./manifest"); const axios_1 = __importDefault(require("axios")); const qs = require("qs"); const open = require("open"); const Configstore = require('configstore'); const jwt_decode = require('jwt-decode'); // const home = Domo.getHomeDir(); class Oauth { static isOAuthEnabled(manifestName) { if (manifest_1.ManifestUtils.hasManifest(manifestName)) { const manifest = manifest_1.ManifestUtils.getManifest(manifestName); return !!manifest['oAuthEnabled']; } return false; } static hasAppProxyId(manifestName) { const manifest = manifest_1.ManifestUtils.getManifest(manifestName); return (!!Object.keys(manifest).includes('cardId') || !!Object.keys(manifest).includes('proxyId') || !!Object.keys(manifest).includes('appContextId')); } static getScopes(manifestName) { const manifest = manifest_1.ManifestUtils.getManifest(manifestName); if (Object.keys(manifest).includes('scopes')) { return ['domoapps', ...manifest.scopes]; } return ['domoapps']; } static hasTokens(registration) { const scopes = this.getScopes(registration.manifestName); const configstore = new Configstore.default('/ryuu/' + registration.instance); const token = configstore.get(`${registration.proxyId}-${scopes.join('-')}-accessToken`); if (token) { jwt_decode.jwtDecode(token); return true; } return false; } static register(registration) { const options = { url: `https://${registration.instance}/api/oauth2/device_authorization`, method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, data: qs.stringify({ client_id: `domo:ryuu:${registration.proxyId}`, scope: this.getScopes(registration.manifestName).join(' '), }), }; return (0, axios_1.default)(options) .then(response => { return Promise.resolve(response); }) .catch(error => { return Promise.reject(error); }); } static verifyRegistration(registration) { return new Promise((resolve, reject) => { open.default(`${registration.verification_uri}?user_code=${registration.user_code}`); // scopes are space separated const scopes = this.getScopes(registration.manifestName).join(' '); const options = { url: `https://${registration.instance}/domoapps/admin/device-registration/${registration.proxyId}?scope=${scopes}&device_code=${registration.device_code}`, method: 'GET', }; const checkRegistration = setInterval(() => { new Promise((resolve, reject) => { (0, axios_1.default)(options) .then(response => { resolve(response); }) .catch(error => { reject(error); }); }) .then(result => { clearInterval(checkRegistration); resolve(result); }) .catch(() => { console.log('Waiting for browser oauth registration'); }); }, 1000); setTimeout(() => { clearInterval(checkRegistration); reject('Timed out for oauth registration verification.'); }, 20000); }); } static getLoginData(loginFile) { return fs.existsSync(loginFile) ? fs.readJsonSync(loginFile) : {}; } static persistTokens(registration) { const configstore = new Configstore.default('/ryuu/' + registration.instance); const scopes = this.getScopes(registration.manifestName); configstore.set(`${registration.proxyId}-${scopes.join('-')}-accessToken`, registration.accessToken); configstore.set(`${registration.proxyId}-${scopes.join('-')}-refreshToken`, registration.refreshToken); return { accessToken: registration.accessToken, refreshToken: registration.refreshToken, }; } static deleteTokens(registration) { const configstore = new Configstore.default('/ryuu/' + registration.instance); const scopes = this.getScopes(registration.manifestName); return Promise.all([ configstore.delete(`${registration.proxyId}-${scopes.join('-')}-accessToken`), configstore.delete(`${registration.proxyId}-${scopes.join('-')}-refreshToken`), ]); } static getTokens(registration) { const configstore = new Configstore.default('/ryuu/' + registration.instance); const scopes = this.getScopes(registration.manifestName); return new Promise((resolve, reject) => { Promise.all([ configstore.get(`${registration.proxyId}-${scopes.join('-')}-accessToken`), configstore.get(`${registration.proxyId}-${scopes.join('-')}-refreshToken`), ]) .then(([accessToken, refreshToken]) => { resolve({ accessToken, refreshToken }); }) .catch(reject); }); } } exports.Oauth = Oauth; //# sourceMappingURL=oauth.js.map