2018-04-21 02:59:30 -03:00
|
|
|
/*****************************************************************************\
|
|
|
|
| ( )_ _ |
|
|
|
|
| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ |
|
|
|
|
| ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| | | |/',__)/' _ `\ /'_`\ |
|
|
|
|
| | (_) )| | ( (_| |( (_) || ( ) ( ) |( (_| || |_ | |\__, \| ( ) |( (_) ) |
|
|
|
|
| | ,__/'(_) `\__,_)`\__ |(_) (_) (_)`\__,_)`\__)(_)(____/(_) (_)`\___/' |
|
|
|
|
| | | ( )_) | |
|
|
|
|
| (_) \___/' |
|
|
|
|
| |
|
|
|
|
| General Bots Copyright (c) Pragmatismo.io. All rights reserved. |
|
|
|
|
| Licensed under the AGPL-3.0. |
|
2018-11-11 19:09:18 -02:00
|
|
|
| |
|
2018-04-21 02:59:30 -03:00
|
|
|
| According to our dual licensing model, this program can be used either |
|
|
|
|
| under the terms of the GNU Affero General Public License, version 3, |
|
|
|
|
| or under a proprietary license. |
|
|
|
|
| |
|
|
|
|
| The texts of the GNU Affero General Public License with an additional |
|
|
|
|
| permission and of our proprietary license can be found at and |
|
|
|
|
| in the LICENSE file you have received along with this program. |
|
|
|
|
| |
|
|
|
|
| This program is distributed in the hope that it will be useful, |
|
2018-09-09 18:11:41 -03:00
|
|
|
| but WITHOUT ANY WARRANTY without even the implied warranty of |
|
2018-04-21 02:59:30 -03:00
|
|
|
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
|
|
| GNU Affero General Public License for more details. |
|
|
|
|
| |
|
|
|
|
| "General Bots" is a registered trademark of Pragmatismo.io. |
|
|
|
|
| The licensing of the program under the AGPLv3 does not imply a |
|
|
|
|
| trademark license. Therefore any rights, title and interest in |
|
|
|
|
| our trademarks remain entirely with us. |
|
|
|
|
| |
|
|
|
|
\*****************************************************************************/
|
|
|
|
|
2018-11-11 19:09:18 -02:00
|
|
|
/**
|
|
|
|
* @fileoverview General Bots server core.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
2018-09-16 17:00:17 -03:00
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
const UrlJoin = require('url-join');
|
|
|
|
import { BotAdapter } from 'botbuilder';
|
|
|
|
import { WaterfallDialog } from 'botbuilder-dialogs';
|
|
|
|
import { GBMinInstance } from 'botlib';
|
|
|
|
import { IGBDialog } from 'botlib';
|
|
|
|
import { GBConfigService } from '../../core.gbapp/services/GBConfigService';
|
|
|
|
import { GBDeployer } from '../../core.gbapp/services/GBDeployer';
|
2018-12-06 10:45:16 -02:00
|
|
|
import { GBImporter } from '../../core.gbapp/services/GBImporterService';
|
2018-11-12 12:20:44 -02:00
|
|
|
import { GBAdminService } from '../services/GBAdminService';
|
|
|
|
import { Messages } from '../strings';
|
2018-10-14 19:58:54 -03:00
|
|
|
|
2018-09-09 14:39:37 -03:00
|
|
|
/**
|
|
|
|
* Dialogs for administration tasks.
|
|
|
|
*/
|
2018-04-21 02:59:30 -03:00
|
|
|
export class AdminDialog extends IGBDialog {
|
2018-11-12 12:20:44 -02:00
|
|
|
public static async createFarmCommand(text: any, min: GBMinInstance) {}
|
2018-10-14 19:58:54 -03:00
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
public static async undeployPackageCommand(text: any, min: GBMinInstance) {
|
|
|
|
const packageName = text.split(' ')[1];
|
|
|
|
const importer = new GBImporter(min.core);
|
|
|
|
const deployer = new GBDeployer(min.core, importer);
|
2018-12-06 10:45:16 -02:00
|
|
|
await deployer.undeployPackageFromLocalPath(min.instance, UrlJoin('packages', packageName));
|
2018-09-09 14:39:37 -03:00
|
|
|
}
|
|
|
|
|
2018-12-06 10:45:16 -02:00
|
|
|
public static async deployPackageCommand(min: GBMinInstance, text: string, deployer: GBDeployer) {
|
2018-11-12 12:20:44 -02:00
|
|
|
const packageName = text.split(' ')[1];
|
|
|
|
const additionalPath = GBConfigService.get('ADDITIONAL_DEPLOY_PATH');
|
2018-12-06 10:45:16 -02:00
|
|
|
await deployer.deployPackageFromLocalPath(min, UrlJoin(additionalPath, packageName));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async rebuildIndexPackageCommand(min: GBMinInstance, text: string, deployer: GBDeployer) {
|
|
|
|
await deployer.rebuildIndex(min.instance);
|
2018-09-09 14:39:37 -03:00
|
|
|
}
|
2018-12-06 10:45:16 -02:00
|
|
|
|
|
|
|
public static async addConnectionCommand(min: GBMinInstance, text: any) {
|
|
|
|
const packageName = text.split(' ')[1];
|
|
|
|
const importer = new GBImporter(min.core);
|
|
|
|
const admin = new GBAdminService(min.core);
|
|
|
|
// TODO: await admin.addConnection
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-09 14:39:37 -03:00
|
|
|
/**
|
|
|
|
* Setup dialogs flows and define services call.
|
2018-09-16 17:00:17 -03:00
|
|
|
*
|
2018-09-09 14:39:37 -03:00
|
|
|
* @param bot The bot adapter.
|
|
|
|
* @param min The minimal bot instance data.
|
|
|
|
*/
|
2018-11-12 12:20:44 -02:00
|
|
|
public static setup(bot: BotAdapter, min: GBMinInstance) {
|
2018-09-09 14:39:37 -03:00
|
|
|
// Setup services.
|
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
const importer = new GBImporter(min.core);
|
|
|
|
const deployer = new GBDeployer(min.core, importer);
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2018-11-01 21:06:11 -03:00
|
|
|
min.dialogs.add(
|
2018-11-12 12:20:44 -02:00
|
|
|
new WaterfallDialog('/admin', [
|
2018-11-01 21:06:11 -03:00
|
|
|
async step => {
|
|
|
|
const locale = step.context.activity.locale;
|
|
|
|
const prompt = Messages[locale].authenticate;
|
2018-12-06 10:45:16 -02:00
|
|
|
|
|
|
|
return await step.prompt('textPrompt', prompt);
|
2018-11-01 21:06:11 -03:00
|
|
|
},
|
|
|
|
async step => {
|
|
|
|
const locale = step.context.activity.locale;
|
2018-11-12 12:20:44 -02:00
|
|
|
const password = step.result;
|
2018-12-06 10:45:16 -02:00
|
|
|
|
|
|
|
if (password === GBConfigService.get('ADMIN_PASS')) {
|
2018-11-01 21:06:11 -03:00
|
|
|
await step.context.sendActivity(Messages[locale].welcome);
|
2018-12-06 10:45:16 -02:00
|
|
|
|
|
|
|
return await step.prompt('textPrompt', Messages[locale].which_task);
|
2018-11-01 21:06:11 -03:00
|
|
|
} else {
|
2018-12-06 10:45:16 -02:00
|
|
|
await step.context.sendActivity(Messages[locale].wrong_password);
|
|
|
|
|
|
|
|
return await step.endDialog();
|
2018-11-01 21:06:11 -03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
async step => {
|
|
|
|
const locale = step.context.activity.locale;
|
2018-11-12 12:20:44 -02:00
|
|
|
const text = step.result;
|
|
|
|
const cmdName = text.split(' ')[0];
|
2018-09-24 15:27:26 -03:00
|
|
|
|
2018-11-01 21:06:11 -03:00
|
|
|
step.context.sendActivity(Messages[locale].working(cmdName));
|
|
|
|
let unknownCommand = false;
|
2018-09-24 15:27:26 -03:00
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
if (text === 'quit') {
|
2018-12-06 10:45:16 -02:00
|
|
|
return await step.replaceDialog('/');
|
2018-11-12 12:20:44 -02:00
|
|
|
} else if (cmdName === 'createFarm') {
|
2018-11-01 21:06:11 -03:00
|
|
|
await AdminDialog.createFarmCommand(text, deployer);
|
2018-12-06 10:45:16 -02:00
|
|
|
|
|
|
|
return await step.replaceDialog('/admin', { firstRun: false });
|
2018-11-12 12:20:44 -02:00
|
|
|
} else if (cmdName === 'deployPackage') {
|
2018-12-06 10:45:16 -02:00
|
|
|
await AdminDialog.deployPackageCommand(min, text, deployer);
|
|
|
|
|
|
|
|
return await step.replaceDialog('/admin', { firstRun: false });
|
2018-11-12 12:20:44 -02:00
|
|
|
} else if (cmdName === 'redeployPackage') {
|
2018-11-01 21:06:11 -03:00
|
|
|
await AdminDialog.undeployPackageCommand(text, min);
|
2018-12-06 10:45:16 -02:00
|
|
|
await AdminDialog.deployPackageCommand(min, text, deployer);
|
|
|
|
|
|
|
|
return await step.replaceDialog('/admin', { firstRun: false });
|
|
|
|
} else if (cmdName === 'rebuildIndex') {
|
|
|
|
await AdminDialog.rebuildIndexPackageCommand(min, text, deployer);
|
|
|
|
|
|
|
|
return await step.replaceDialog('/admin', { firstRun: false });
|
|
|
|
} else if (cmdName === 'addConnection') {
|
|
|
|
await AdminDialog.addConnectionCommand(min, text);
|
|
|
|
|
|
|
|
return await step.replaceDialog('/admin', { firstRun: false });
|
2018-11-12 12:20:44 -02:00
|
|
|
} else if (cmdName === 'undeployPackage') {
|
2018-11-01 21:06:11 -03:00
|
|
|
await AdminDialog.undeployPackageCommand(text, min);
|
2018-12-06 10:45:16 -02:00
|
|
|
|
|
|
|
return await step.replaceDialog('/admin', { firstRun: false });
|
2018-11-12 12:20:44 -02:00
|
|
|
} else if (cmdName === 'setupSecurity') {
|
2018-11-01 21:06:11 -03:00
|
|
|
await AdminDialog.setupSecurity(min, step);
|
|
|
|
} else {
|
|
|
|
unknownCommand = true;
|
|
|
|
}
|
2018-09-24 15:27:26 -03:00
|
|
|
|
2018-11-01 21:06:11 -03:00
|
|
|
if (unknownCommand) {
|
|
|
|
await step.context.sendActivity(Messages[locale].unknown_command);
|
|
|
|
} else {
|
2018-12-06 10:45:16 -02:00
|
|
|
await step.context.sendActivity(Messages[locale].finshed_working(cmdName));
|
2018-11-01 21:06:11 -03:00
|
|
|
}
|
|
|
|
await step.endDialog();
|
2018-12-06 10:45:16 -02:00
|
|
|
|
|
|
|
return await step.replaceDialog('/answer', { query: text });
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
2018-11-01 21:06:11 -03:00
|
|
|
])
|
|
|
|
);
|
2018-09-16 17:00:17 -03:00
|
|
|
}
|
|
|
|
|
2018-11-01 21:06:11 -03:00
|
|
|
private static async setupSecurity(min: any, step: any) {
|
|
|
|
const locale = step.activity.locale;
|
2018-12-06 10:45:16 -02:00
|
|
|
const state = `${min.instance.instanceId}${Math.floor(Math.random() * 1000000000)}`;
|
|
|
|
await min.adminService.setValue(min.instance.instanceId, 'AntiCSRFAttackState', state);
|
|
|
|
const url = `https://login.microsoftonline.com/${min.instance.authenticatorTenant}/oauth2/authorize?client_id=${
|
2018-09-24 11:04:36 -03:00
|
|
|
min.instance.authenticatorClientId
|
2018-10-15 19:05:43 -03:00
|
|
|
}&response_type=code&redirect_uri=${min.instance.botEndpoint}/${
|
2018-09-24 11:04:36 -03:00
|
|
|
min.instance.botId
|
|
|
|
}/token&state=${state}&response_mode=query`;
|
|
|
|
|
2018-11-01 21:06:11 -03:00
|
|
|
await step.sendActivity(Messages[locale].consent(url));
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
|
|
|
}
|