UNPKG

@grouparoo/core

Version:
130 lines (129 loc) 5.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OAuthClientView = exports.OAuthClientEdit = exports.OAuthClientStart = exports.OAuthListProviders = void 0; require("isomorphic-fetch"); const actionhero_1 = require("actionhero"); const clsAction_1 = require("../classes/actions/clsAction"); const plugin_1 = require("../modules/plugin"); const OAuthRequest_1 = require("../models/OAuthRequest"); class OAuthListProviders extends actionhero_1.Action { constructor() { super(...arguments); this.name = "oAuth:listProviders"; this.description = "list the available oAuth Providers"; this.inputs = { type: { required: false }, }; this.outputExample = {}; } async run({ params }) { if (!actionhero_1.config.oAuth.host) return { providers: [] }; let fullUrl = `${actionhero_1.config.oAuth.host}/api/v1/oauth/providers`; if (params.type) fullUrl += `?type=${params.type}`; const response = await fetch(fullUrl, { method: "GET", headers: { "Content-Type": "application/json" }, }).then((r) => r.json()); throwTelemetryError(response); return response; } } exports.OAuthListProviders = OAuthListProviders; class OAuthClientStart extends clsAction_1.CLSAction { constructor() { super(...arguments); this.name = "oAuth:client:start"; this.description = "start the oauth flow and redirect the user to the oauth provider"; this.outputExample = {}; this.inputs = { id: { required: false }, provider: { required: true }, type: { required: true }, appId: { required: false }, appOption: { required: false }, }; } async runWithinTransaction({ params, }) { const oauthRequest = await OAuthRequest_1.OAuthRequest.create({ id: params.id, type: params.type, provider: params.provider, appId: params.appId, appOption: params.appOption, }); const callbackUrl = `${process.env.WEB_URL}/oauth/callback`; const { value: customerId } = await plugin_1.plugin.readSetting("telemetry", "customer-id"); const fullUrl = `${actionhero_1.config.oAuth.host}/api/v1/oauth/${params.provider}/client/start`; const response = await fetch(fullUrl, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ type: params.type, requestId: oauthRequest.id, customerId, callbackUrl, }), }).then((r) => r.json()); throwTelemetryError(response); return { location: response.location, error: response.error, }; } } exports.OAuthClientStart = OAuthClientStart; class OAuthClientEdit extends clsAction_1.CLSAction { constructor() { super(...arguments); this.name = "oAuth:client:edit"; this.description = "edit an oAuth request given the requestId and token pair"; this.inputs = { requestId: { required: true }, }; } async runWithinTransaction({ params, }) { const oAuthRequest = await OAuthRequest_1.OAuthRequest.findById(params.requestId); const customerId = (await plugin_1.plugin.readSetting("telemetry", "customer-id")) .value; const fullUrl = `${actionhero_1.config.oAuth.host}/api/v1/oauth/client/request/${params.requestId}/view?requestId=${oAuthRequest.id}&customerId=${customerId}`; const response = await fetch(fullUrl, { method: "GET", headers: { "Content-Type": "application/json" }, }).then((r) => r.json()); throwTelemetryError(response); // on a page reload, we don't want to erase any data we already have if (response.oAuthRequest.token) { await oAuthRequest.update({ token: response.oAuthRequest.token }); } if (response.oAuthRequest.identities.length > 0) { await oAuthRequest.update({ identities: response.oAuthRequest.identities, }); } return { oAuthRequest: await oAuthRequest.apiData() }; } } exports.OAuthClientEdit = OAuthClientEdit; class OAuthClientView extends clsAction_1.CLSAction { constructor() { super(...arguments); this.name = "oAuth:client:view"; this.description = "view and oAuth request"; this.inputs = { requestId: { required: true }, }; } async runWithinTransaction({ params, }) { const oAuthRequest = await OAuthRequest_1.OAuthRequest.findById(params.requestId); return { oAuthRequest: await oAuthRequest.apiData() }; } } exports.OAuthClientView = OAuthClientView; function throwTelemetryError(response) { var _a; if ((_a = response.error) === null || _a === void 0 ? void 0 : _a.message) { throw new Error(`[ telemetry ] ${response.error.message}`); } }