UNPKG

@fusebit-int/microsoft-bot-framework-connector

Version:

Connector for the Microsoft Bot Framework

77 lines 3.51 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ServiceConnector = void 0; const superagent_1 = __importDefault(require("superagent")); const framework_1 = require("@fusebit-int/framework"); const schema_json_1 = __importDefault(require("./config/schema.json")); const uischema_json_1 = __importDefault(require("./config/uischema.json")); const Service_1 = require("./Service"); const TOKEN_URL = 'https://login.microsoftonline.com/common/oauth2/v2.0/token'; class ServiceConnector extends framework_1.Connector { constructor() { super(); this.router.use((ctx, next) => { var _a; const { config: cfg } = ctx.state.manager; cfg.configuration.constants = { urls: { production: { tokenUrl: TOKEN_URL, }, webhookUrl: `${ctx.state.params.baseUrl}/api/fusebit_webhook_event`, }, }; // Make sure there is a sensible default value for tokenUrl, but still allow it to be // overwritten if necessary. if ((_a = cfg.configuration.mode) === null || _a === void 0 ? void 0 : _a.useProduction) { cfg.configuration.tokenUrl = cfg.configuration.tokenUrl || cfg.configuration.constants.urls.production.tokenUrl; } return next(); }); this.router.get('/api/configure', this.middleware.authorizeUser('connector:put'), async (ctx, next) => { ctx.body = JSON.parse(JSON.stringify({ data: Object.assign({}, ctx.state.manager.config.configuration), schema: schema_json_1.default, uischema: uischema_json_1.default, })); return next(); }); this.router.get('/api/credentials', this.middleware.authorizeUser('connector:execute'), async (ctx) => { try { const botFrameworkAccessTokenResponse = await superagent_1.default .get(ctx.state.manager.config.configuration.tokenUrl) .type('form') .send({ grant_type: 'client_credentials', client_id: ctx.state.manager.config.configuration.clientId, client_secret: ctx.state.manager.config.configuration.clientSecret, scope: ctx.state.manager.config.configuration.scope, }); const botFrameworkAccessToken = botFrameworkAccessTokenResponse.body.access_token; ctx.body = { accessToken: botFrameworkAccessToken, botClientId: ctx.state.manager.config.configuration.clientId, }; } catch (error) { if (error instanceof Error) { ctx.throw(500, error.message); } // TODO log this error object (or whatever that is at this point) ctx.throw(500); } }); } createService() { return new ServiceConnector.Service(); } } exports.ServiceConnector = ServiceConnector; ServiceConnector.Service = Service_1.Service; const connector = new ServiceConnector(); exports.default = connector; //# sourceMappingURL=index.js.map