2018-04-21 02:59:30 -03:00
|
|
|
/*****************************************************************************\
|
2024-01-09 17:40:48 -03:00
|
|
|
| █████ █████ ██ █ █████ █████ ████ ██ ████ █████ █████ ███ ® |
|
|
|
|
| ██ █ ███ █ █ ██ ██ ██ ██ ██ ██ █ ██ ██ █ █ |
|
|
|
|
| ██ ███ ████ █ ██ █ ████ █████ ██████ ██ ████ █ █ █ ██ |
|
|
|
|
| ██ ██ █ █ ██ █ █ ██ ██ ██ ██ ██ ██ █ ██ ██ █ █ |
|
|
|
|
| █████ █████ █ ███ █████ ██ ██ ██ ██ █████ ████ █████ █ ███ |
|
2018-04-21 02:59:30 -03:00
|
|
|
| |
|
2024-04-20 17:24:00 -03:00
|
|
|
| General Bots Copyright (c) pragmatismo.cloud. All rights reserved. |
|
2018-04-21 02:59:30 -03:00
|
|
|
| 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, |
|
2023-02-26 15:03:24 -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. |
|
|
|
|
| |
|
2024-04-20 17:24:00 -03:00
|
|
|
| "General Bots" is a registered trademark of pragmatismo.cloud. |
|
2018-04-21 02:59:30 -03:00
|
|
|
| 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
|
|
|
|
2022-11-18 22:39:14 -03:00
|
|
|
import crypto from 'crypto';
|
|
|
|
import urlJoin from 'url-join';
|
2019-03-08 06:37:13 -03:00
|
|
|
import { WaterfallDialog } from 'botbuilder-dialogs';
|
2023-12-20 18:07:09 -03:00
|
|
|
import { GBMinInstance, IGBDialog } from 'botlib';
|
2022-11-18 22:39:14 -03:00
|
|
|
import { GBDeployer } from '../../core.gbapp/services/GBDeployer.js';
|
|
|
|
import { GBImporter } from '../../core.gbapp/services/GBImporterService.js';
|
|
|
|
import { Messages } from '../strings.js';
|
|
|
|
import { GBAdminService } from '../services/GBAdminService.js';
|
2020-05-14 12:46:57 -03:00
|
|
|
import { CollectionUtil } from 'pragmatismo-io-framework';
|
2023-02-26 15:03:24 -03:00
|
|
|
import { SecService } from '../../security.gbapp/services/SecService.js';
|
2023-12-20 18:07:09 -03:00
|
|
|
import { GBConfigService } from '../../core.gbapp/services/GBConfigService.js';
|
2020-05-14 12:46:57 -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 {
|
2022-12-22 11:31:37 -03:00
|
|
|
public static isIntentYes(locale, utterance) {
|
2020-05-19 12:36:17 -03:00
|
|
|
return utterance.toLowerCase().match(Messages[locale].affirmative_sentences);
|
|
|
|
}
|
|
|
|
|
2022-12-22 11:31:37 -03:00
|
|
|
public static isIntentNo(locale, utterance) {
|
2020-05-19 12:36:17 -03:00
|
|
|
return utterance.toLowerCase().match(Messages[locale].negative_sentences);
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2022-12-22 11:31:37 -03:00
|
|
|
public static setup(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
|
|
|
|
2019-01-31 11:32:33 -02:00
|
|
|
AdminDialog.setupSecurityDialogs(min);
|
|
|
|
|
2020-05-24 17:06:05 -03:00
|
|
|
min.dialogs.add(
|
|
|
|
new WaterfallDialog('/admin-auth', [
|
2021-05-10 10:53:53 -03:00
|
|
|
async step => {
|
|
|
|
if (step.context.activity.channelId !== 'msteams' && process.env.ENABLE_AUTH) {
|
|
|
|
return await step.beginDialog('/auth');
|
2022-11-19 23:34:58 -03:00
|
|
|
} else {
|
2021-05-10 10:53:53 -03:00
|
|
|
return await step.next(step.options);
|
|
|
|
}
|
|
|
|
},
|
2020-05-24 17:06:05 -03:00
|
|
|
async step => {
|
|
|
|
const locale = step.context.activity.locale;
|
|
|
|
const prompt = Messages[locale].authenticate;
|
|
|
|
|
|
|
|
return await min.conversationalService.prompt(min, step, prompt);
|
|
|
|
},
|
|
|
|
async step => {
|
|
|
|
const locale = step.context.activity.locale;
|
2022-06-01 20:55:58 -03:00
|
|
|
const sensitive = step.context.activity['originalText'];
|
2020-05-24 17:06:05 -03:00
|
|
|
|
2022-06-01 20:55:58 -03:00
|
|
|
if (sensitive === min.instance.adminPass) {
|
2020-05-24 17:06:05 -03:00
|
|
|
await min.conversationalService.sendText(min, step, Messages[locale].welcome);
|
|
|
|
|
|
|
|
return await step.endDialog(true);
|
|
|
|
} else {
|
|
|
|
await min.conversationalService.sendText(min, step, Messages[locale].wrong_password);
|
|
|
|
return await step.replaceDialog('/admin-auth');
|
|
|
|
}
|
2020-09-20 12:05:24 -03:00
|
|
|
}
|
|
|
|
])
|
|
|
|
);
|
2020-05-24 17:06:05 -03:00
|
|
|
|
2018-11-01 21:06:11 -03:00
|
|
|
min.dialogs.add(
|
2018-11-12 12:20:44 -02:00
|
|
|
new WaterfallDialog('/admin', [
|
2021-05-10 10:53:53 -03:00
|
|
|
async step => {
|
|
|
|
if (step.context.activity.channelId !== 'msteams' && process.env.ENABLE_AUTH) {
|
|
|
|
return await step.beginDialog('/auth');
|
2022-11-19 23:34:58 -03:00
|
|
|
} else {
|
2021-05-10 10:53:53 -03:00
|
|
|
return await step.next(step.options);
|
|
|
|
}
|
|
|
|
},
|
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:16:28 -02:00
|
|
|
|
2020-05-19 12:36:17 -03:00
|
|
|
return await min.conversationalService.prompt(min, step, prompt);
|
2020-03-08 09:24:28 -03:00
|
|
|
},
|
|
|
|
async step => {
|
|
|
|
const locale = step.context.activity.locale;
|
2022-08-26 11:53:47 -03:00
|
|
|
const sensitive = step.context.activity['originalText'];
|
2018-12-06 10:16:28 -02:00
|
|
|
|
2020-04-15 05:08:50 +00:00
|
|
|
if (sensitive === min.instance.adminPass) {
|
2020-05-17 21:30:21 +00:00
|
|
|
await min.conversationalService.sendText(min, step, Messages[locale].welcome);
|
2018-12-06 10:16:28 -02:00
|
|
|
|
2020-05-19 12:36:17 -03:00
|
|
|
return await min.conversationalService.prompt(min, step, Messages[locale].which_task);
|
2020-03-08 09:24:28 -03:00
|
|
|
} else {
|
2020-05-17 21:30:21 +00:00
|
|
|
await min.conversationalService.sendText(min, step, Messages[locale].wrong_password);
|
2018-12-06 10:16:28 -02:00
|
|
|
|
2020-03-08 09:24:28 -03:00
|
|
|
return await step.endDialog();
|
|
|
|
}
|
2018-11-01 21:06:11 -03:00
|
|
|
},
|
|
|
|
async step => {
|
2019-03-08 19:13:00 -03:00
|
|
|
const locale: string = step.context.activity.locale;
|
|
|
|
// tslint:disable-next-line:no-unsafe-any
|
2021-09-13 19:04:32 -03:00
|
|
|
const text: string = step.context.activity['originalText'];
|
2018-11-12 12:20:44 -02:00
|
|
|
const cmdName = text.split(' ')[0];
|
2018-09-24 15:27:26 -03:00
|
|
|
|
2020-05-17 21:30:21 +00:00
|
|
|
await min.conversationalService.sendText(min, step, Messages[locale].working(cmdName));
|
2018-11-01 21:06:11 -03:00
|
|
|
let unknownCommand = false;
|
2018-09-24 15:27:26 -03:00
|
|
|
|
2019-08-23 14:36:47 -03:00
|
|
|
try {
|
|
|
|
if (text === 'quit') {
|
2023-11-19 17:56:13 -03:00
|
|
|
return await step.replaceDialog('/');
|
2019-08-23 14:36:47 -03:00
|
|
|
} else if (cmdName === 'setupSecurity') {
|
|
|
|
return await step.beginDialog('/setupSecurity');
|
|
|
|
} else {
|
|
|
|
unknownCommand = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unknownCommand) {
|
2020-05-17 21:30:21 +00:00
|
|
|
await min.conversationalService.sendText(min, step, Messages[locale].unknown_command);
|
2019-08-23 14:36:47 -03:00
|
|
|
} else {
|
2020-05-17 21:30:21 +00:00
|
|
|
await min.conversationalService.sendText(min, step, Messages[locale].finished_working);
|
2019-08-23 14:36:47 -03:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2020-11-30 21:25:29 -03:00
|
|
|
await min.conversationalService.sendText(min, step, error.message ? error.message : error);
|
2018-11-01 21:06:11 -03:00
|
|
|
}
|
2019-08-23 14:36:47 -03:00
|
|
|
await step.replaceDialog('/ask', { isReturning: true });
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
2020-09-20 12:05:24 -03:00
|
|
|
])
|
|
|
|
);
|
|
|
|
|
|
|
|
min.dialogs.add(
|
|
|
|
new WaterfallDialog('/install', [
|
2021-05-10 10:53:53 -03:00
|
|
|
async step => {
|
|
|
|
if (step.context.activity.channelId !== 'msteams' && process.env.ENABLE_AUTH) {
|
|
|
|
return await step.beginDialog('/auth');
|
2022-11-19 23:34:58 -03:00
|
|
|
} else {
|
2021-05-10 10:53:53 -03:00
|
|
|
return await step.next(step.options);
|
|
|
|
}
|
|
|
|
},
|
2020-09-20 12:05:24 -03:00
|
|
|
async step => {
|
|
|
|
step.activeDialog.state.options.args = (step.options as any).args;
|
|
|
|
if (step.activeDialog.state.options.confirm) {
|
|
|
|
return await step.next('sim');
|
|
|
|
} else {
|
|
|
|
const locale = step.context.activity.locale;
|
|
|
|
return await min.conversationalService.prompt(min, step, Messages[locale].publish_type_yes);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async step => {
|
|
|
|
const locale = step.context.activity.locale;
|
2019-08-23 14:36:47 -03:00
|
|
|
|
2020-09-20 12:05:24 -03:00
|
|
|
// If the user says yes, starts publishing.
|
|
|
|
|
|
|
|
if (AdminDialog.isIntentYes(locale, step.result)) {
|
2022-12-22 11:31:37 -03:00
|
|
|
const list = min.core.getParam(min.instance, '.gbapp List', null);
|
|
|
|
const items = list ? list.split(';') : [];
|
|
|
|
|
2020-09-20 12:05:24 -03:00
|
|
|
step.activeDialog.state.options.args;
|
2020-10-17 23:03:27 -03:00
|
|
|
|
2022-12-22 11:31:37 -03:00
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
for (let j = 0; j < min.appPackages.length; j++) {
|
|
|
|
if (items[i] === min.appPackages[j]['name']) {
|
|
|
|
const element = min.appPackages[i];
|
|
|
|
await element.onExchangeData(min, 'install', null);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-09-20 12:05:24 -03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
await min.conversationalService.sendText(min, step, Messages[locale].publish_canceled);
|
|
|
|
}
|
|
|
|
}
|
2018-11-01 21:06:11 -03:00
|
|
|
])
|
|
|
|
);
|
2020-05-12 09:06:47 -03:00
|
|
|
|
2023-02-12 14:31:21 -03:00
|
|
|
|
|
|
|
min.dialogs.add(
|
|
|
|
new WaterfallDialog('/logs', [
|
|
|
|
async step => {
|
|
|
|
if (step.context.activity.channelId !== 'msteams' && process.env.ENABLE_AUTH) {
|
|
|
|
return await step.beginDialog('/auth');
|
|
|
|
} else {
|
|
|
|
return await step.next(step.options);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async step => {
|
2023-11-19 17:56:13 -03:00
|
|
|
const logs = await min.core['getLatestLogs']();
|
2023-02-12 14:31:21 -03:00
|
|
|
await min.conversationalService.sendText(min, step, logs);
|
|
|
|
return await step.replaceDialog('/ask', { isReturning: true });
|
|
|
|
}
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
2020-05-12 09:06:47 -03:00
|
|
|
min.dialogs.add(
|
|
|
|
new WaterfallDialog('/publish', [
|
2021-05-10 10:53:53 -03:00
|
|
|
async step => {
|
|
|
|
if (step.context.activity.channelId !== 'msteams' && process.env.ENABLE_AUTH) {
|
|
|
|
return await step.beginDialog('/auth');
|
2022-11-19 23:34:58 -03:00
|
|
|
} else {
|
2021-05-10 10:53:53 -03:00
|
|
|
return await step.next(step.options);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-05-12 09:06:47 -03:00
|
|
|
async step => {
|
2023-08-11 11:37:41 -03:00
|
|
|
step.activeDialog.state.options.confirm = true; // Feature removed.
|
2022-11-19 23:34:58 -03:00
|
|
|
if (step.activeDialog.state.options.confirm || process.env.ADMIN_OPEN_PUBLISH === 'true') {
|
2020-05-19 12:36:17 -03:00
|
|
|
return await step.next('sim');
|
2020-09-20 12:05:24 -03:00
|
|
|
} else {
|
2020-05-19 12:36:17 -03:00
|
|
|
const locale = step.context.activity.locale;
|
|
|
|
return await min.conversationalService.prompt(min, step, Messages[locale].publish_type_yes);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async step => {
|
|
|
|
const locale = step.context.activity.locale;
|
2020-05-12 09:06:47 -03:00
|
|
|
|
2020-05-19 12:36:17 -03:00
|
|
|
// If the user says yes, starts publishing.
|
|
|
|
|
|
|
|
if (AdminDialog.isIntentYes(locale, step.result)) {
|
|
|
|
let from = step.context.activity.from.id;
|
2020-05-23 11:10:06 -03:00
|
|
|
|
2020-10-27 12:34:46 -03:00
|
|
|
let canPublish: Boolean;
|
|
|
|
if (step.activeDialog.state.options.firstTime) {
|
|
|
|
canPublish = true;
|
|
|
|
} else {
|
2022-11-19 23:34:58 -03:00
|
|
|
canPublish = AdminDialog.canPublish(min, from) || process.env.ADMIN_OPEN_PUBLISH === 'true';
|
2020-10-27 12:34:46 -03:00
|
|
|
}
|
2020-05-19 12:36:17 -03:00
|
|
|
|
2020-05-24 17:06:05 -03:00
|
|
|
if (!canPublish) {
|
|
|
|
await step.beginDialog('/admin-auth');
|
2020-09-20 12:05:24 -03:00
|
|
|
} else {
|
2020-05-25 17:59:02 -03:00
|
|
|
await step.next(true);
|
2020-05-24 17:06:05 -03:00
|
|
|
}
|
2020-09-20 12:05:24 -03:00
|
|
|
} else {
|
2020-05-24 17:06:05 -03:00
|
|
|
await min.conversationalService.sendText(min, step, Messages[locale].publish_canceled);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async step => {
|
|
|
|
const locale = step.context.activity.locale;
|
|
|
|
if (!step.result) {
|
|
|
|
await min.conversationalService.sendText(min, step, Messages[locale].publish_must_be_admin);
|
|
|
|
|
|
|
|
return step.endDialog();
|
|
|
|
}
|
2020-05-19 12:36:17 -03:00
|
|
|
|
2020-05-24 17:06:05 -03:00
|
|
|
const botId = min.instance.botId;
|
2020-05-25 17:59:02 -03:00
|
|
|
|
2020-05-24 17:06:05 -03:00
|
|
|
await min.conversationalService.sendText(min, step, Messages[locale].working('Publishing'));
|
2020-05-19 12:36:17 -03:00
|
|
|
|
2020-05-24 17:06:05 -03:00
|
|
|
step.activeDialog.state.options.args = (step.options as any).args;
|
2020-09-20 12:05:24 -03:00
|
|
|
const filename = step.activeDialog.state.options.args
|
|
|
|
? step.activeDialog.state.options.args.split(' ')[0]
|
|
|
|
: null;
|
2020-05-23 19:58:38 -03:00
|
|
|
|
2020-05-24 17:06:05 -03:00
|
|
|
const packages = [];
|
2020-06-14 18:06:29 -03:00
|
|
|
let skipError = false;
|
2024-05-25 10:08:39 -03:00
|
|
|
if (!filename || filename === '') {
|
2020-06-14 18:06:29 -03:00
|
|
|
await min.conversationalService.sendText(min, step, `Starting publishing for ${botId} packages...`);
|
2023-11-29 19:06:41 -03:00
|
|
|
packages.push(`${botId}.gbot`);
|
2020-06-14 18:06:29 -03:00
|
|
|
packages.push(`${botId}.gbtheme`);
|
2023-11-29 19:06:41 -03:00
|
|
|
packages.push(`${botId}.gbkb`);
|
2020-06-14 18:06:29 -03:00
|
|
|
packages.push(`${botId}.gbdialog`);
|
|
|
|
skipError = true;
|
2020-05-24 17:06:05 -03:00
|
|
|
} else {
|
|
|
|
packages.push(filename);
|
|
|
|
}
|
2020-05-14 12:46:57 -03:00
|
|
|
|
2023-02-26 15:03:24 -03:00
|
|
|
|
2020-06-14 18:06:29 -03:00
|
|
|
await CollectionUtil.asyncForEach(packages, async packageName => {
|
2023-02-05 14:41:33 -03:00
|
|
|
let cmd1;
|
|
|
|
|
|
|
|
if (
|
2023-02-26 15:03:24 -03:00
|
|
|
packageName.toLowerCase() === 'gbdialog' ||
|
|
|
|
packageName.toLowerCase() === 'gbkb' ||
|
|
|
|
packageName.toLowerCase() === 'gbot' ||
|
|
|
|
packageName.toLowerCase() === 'gbtheme'
|
2023-02-05 14:41:33 -03:00
|
|
|
) {
|
|
|
|
packageName = `${min.botId}.${packageName}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (packageName.indexOf('.') !== -1) {
|
2024-08-19 23:03:58 -03:00
|
|
|
cmd1 = `deployPackage ${process.env.STORAGE_SITE} /${GBConfigService.get('STORAGE_LIBRARY')}/${botId}.gbai/${packageName}`;
|
2023-02-05 14:41:33 -03:00
|
|
|
} else {
|
|
|
|
cmd1 = `deployPackage ${packageName}`;
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
(await (deployer as any).getStoragePackageByName(min.instance.instanceId, packageName)) !== null &&
|
|
|
|
!process.env.DONT_DOWNLOAD
|
|
|
|
) {
|
|
|
|
const cmd2 = `undeployPackage ${packageName}`;
|
|
|
|
await GBAdminService.undeployPackageCommand(cmd2, min);
|
|
|
|
}
|
2023-02-26 15:03:24 -03:00
|
|
|
let sec = new SecService();
|
|
|
|
const member = step.context.activity.from;
|
|
|
|
const user = await sec.ensureUser(
|
2024-02-05 12:36:20 -03:00
|
|
|
min,
|
2023-02-26 15:03:24 -03:00
|
|
|
member.id,
|
|
|
|
member.name,
|
|
|
|
'',
|
|
|
|
'web',
|
|
|
|
member.name,
|
|
|
|
null
|
|
|
|
);
|
2023-11-19 17:56:13 -03:00
|
|
|
|
2023-02-26 15:03:24 -03:00
|
|
|
await GBAdminService.deployPackageCommand(min, user, cmd1, deployer);
|
|
|
|
|
2020-06-14 18:06:29 -03:00
|
|
|
});
|
2023-02-26 15:03:24 -03:00
|
|
|
await min.conversationalService.sendText(min, step, `Training is finished.`);
|
|
|
|
|
2020-05-24 17:06:05 -03:00
|
|
|
if (!step.activeDialog.state.options.confirm) {
|
|
|
|
return await step.replaceDialog('/ask', { isReturning: true });
|
2020-09-20 12:05:24 -03:00
|
|
|
} else {
|
2020-05-24 17:06:05 -03:00
|
|
|
return await step.endDialog();
|
|
|
|
}
|
2020-09-20 12:05:24 -03:00
|
|
|
}
|
|
|
|
])
|
|
|
|
);
|
2018-09-16 17:00:17 -03:00
|
|
|
}
|
|
|
|
|
2020-05-19 12:36:17 -03:00
|
|
|
/**
|
2020-09-20 12:05:24 -03:00
|
|
|
* Check if the specified phone can receive a message by running
|
|
|
|
* the /broadcast command with specific phone numbers.
|
|
|
|
* @param phone Phone number to check (eg.: +5521900002233)
|
|
|
|
*/
|
2022-12-22 11:31:37 -03:00
|
|
|
public static canPublish(min: GBMinInstance, phone: string): Boolean {
|
2020-10-27 12:34:46 -03:00
|
|
|
if (process.env.SECURITY_CAN_PUBLISH !== undefined) {
|
2021-08-19 08:02:56 -03:00
|
|
|
let list = process.env.SECURITY_CAN_PUBLISH.split(';');
|
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
const canPublish = min.core.getParam(min.instance, 'Can Publish', null);
|
2021-08-19 08:02:56 -03:00
|
|
|
if (canPublish) {
|
|
|
|
list = list.concat(canPublish.split(';'));
|
|
|
|
}
|
|
|
|
|
2020-10-27 12:34:46 -03:00
|
|
|
let result = list.includes(phone);
|
|
|
|
|
|
|
|
if (!result && min.instance.params) {
|
|
|
|
const params = JSON.parse(min.instance.params);
|
2023-02-05 14:41:33 -03:00
|
|
|
if (params) {
|
2022-12-27 11:59:14 -03:00
|
|
|
return list.includes(params['Can Publish']);
|
|
|
|
}
|
2020-10-27 12:34:46 -03:00
|
|
|
}
|
|
|
|
return result;
|
2020-05-23 17:59:01 -03:00
|
|
|
}
|
|
|
|
}
|
2020-05-23 19:58:38 -03:00
|
|
|
|
2022-12-22 11:31:37 -03:00
|
|
|
private static setupSecurityDialogs(min: GBMinInstance) {
|
2019-01-31 11:32:33 -02:00
|
|
|
min.dialogs.add(
|
|
|
|
new WaterfallDialog('/setupSecurity', [
|
|
|
|
async step => {
|
2023-11-19 17:56:13 -03:00
|
|
|
const tokenName = step.activeDialog.state.tokenName = step.options['args'];
|
|
|
|
if (tokenName) {
|
|
|
|
step.activeDialog.state.clientId = min.core.getParam<string>(min.instance, `${tokenName} Client ID`, null),
|
|
|
|
step.activeDialog.state.host = min.core.getParam<string>(min.instance, `${tokenName} Host`, null),
|
|
|
|
step.activeDialog.state.tenant = min.core.getParam<string>(min.instance, `${tokenName} Tenant`, null)
|
|
|
|
}
|
2021-05-10 10:53:53 -03:00
|
|
|
if (step.context.activity.channelId !== 'msteams' && process.env.ENABLE_AUTH) {
|
|
|
|
return await step.beginDialog('/auth');
|
2022-11-19 23:34:58 -03:00
|
|
|
} else {
|
2021-05-10 10:53:53 -03:00
|
|
|
return await step.next(step.options);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
async step => {
|
2023-11-17 14:27:12 -03:00
|
|
|
if (step.activeDialog.state.tokenName) {
|
|
|
|
return await step.next(step.options);
|
|
|
|
}
|
|
|
|
|
2019-01-31 11:32:33 -02:00
|
|
|
const locale = step.context.activity.locale;
|
|
|
|
const prompt = Messages[locale].enter_authenticator_tenant;
|
|
|
|
|
2020-05-19 12:36:17 -03:00
|
|
|
return await min.conversationalService.prompt(min, step, prompt);
|
2019-01-31 11:32:33 -02:00
|
|
|
},
|
|
|
|
async step => {
|
2023-11-17 14:27:12 -03:00
|
|
|
if (step.activeDialog.state.tokenName) {
|
|
|
|
return await step.next(step.options);
|
|
|
|
}
|
2024-04-13 17:08:10 -03:00
|
|
|
step.activeDialog.state.authenticatorTenant = step.context.activity['originalText'];
|
2019-01-31 11:32:33 -02:00
|
|
|
const locale = step.context.activity.locale;
|
|
|
|
const prompt = Messages[locale].enter_authenticator_authority_host_url;
|
|
|
|
|
2020-05-19 12:36:17 -03:00
|
|
|
return await min.conversationalService.prompt(min, step, prompt);
|
2019-01-31 11:32:33 -02:00
|
|
|
},
|
|
|
|
async step => {
|
2023-11-19 17:56:13 -03:00
|
|
|
|
2024-04-13 17:08:10 -03:00
|
|
|
step.activeDialog.state.authenticatorAuthorityHostUrl = step.context.activity['originalText'];
|
2023-11-19 17:56:13 -03:00
|
|
|
|
2023-11-17 14:27:12 -03:00
|
|
|
const tokenName = step.activeDialog.state.tokenName;
|
2019-01-31 11:32:33 -02:00
|
|
|
|
2023-11-19 17:56:13 -03:00
|
|
|
if (!tokenName) {
|
2023-11-17 14:27:12 -03:00
|
|
|
min.instance.authenticatorAuthorityHostUrl = step.activeDialog.state.authenticatorAuthorityHostUrl;
|
|
|
|
min.instance.authenticatorTenant = step.activeDialog.state.authenticatorTenant;
|
2023-11-20 11:29:36 -03:00
|
|
|
|
2020-11-30 21:25:29 -03:00
|
|
|
|
2019-01-31 11:32:33 -02:00
|
|
|
await min.adminService.updateSecurityInfo(
|
|
|
|
min.instance.instanceId,
|
2023-11-19 17:56:13 -03:00
|
|
|
tokenName ? step.activeDialog.state.tenant : step.activeDialog.state.authenticatorTenant,
|
|
|
|
tokenName ? step.activeDialog.state.host : step.activeDialog.state.authenticatorAuthorityHostUrl
|
2019-01-31 11:32:33 -02:00
|
|
|
);
|
2023-11-20 11:29:36 -03:00
|
|
|
}
|
2019-01-31 11:32:33 -02:00
|
|
|
const locale = step.context.activity.locale;
|
2019-08-21 21:04:55 +00:00
|
|
|
const buf = Buffer.alloc(16);
|
|
|
|
const state = `${min.instance.instanceId}${crypto.randomFillSync(buf).toString('hex')}`;
|
2020-05-12 09:06:47 -03:00
|
|
|
|
2023-11-17 14:27:12 -03:00
|
|
|
min.adminService.setValue(min.instance.instanceId, `${tokenName}AntiCSRFAttackState`, state);
|
2019-01-31 11:32:33 -02:00
|
|
|
|
2023-11-19 17:56:13 -03:00
|
|
|
const redirectUri = urlJoin(process.env.BOT_URL, min.instance.botId,
|
|
|
|
tokenName ? `/token?value=${tokenName}` : '/token');
|
|
|
|
const scope = tokenName ? '' : 'https://graph.microsoft.com/.default';
|
|
|
|
const host = tokenName ? step.activeDialog.state.host : 'https://login.microsoftonline.com'
|
|
|
|
const tenant = tokenName ? step.activeDialog.state.tenant : min.instance.authenticatorTenant;
|
2023-12-20 18:07:09 -03:00
|
|
|
const clientId = tokenName ? step.activeDialog.state.clientId : (min.instance.marketplaceId ?
|
|
|
|
min.instance.marketplaceId : GBConfigService.get('MARKETPLACE_ID'));
|
2023-11-19 17:56:13 -03:00
|
|
|
const oauth2 = tokenName ? 'oauth' : 'oauth2';
|
|
|
|
const url = `${host}/${tenant}/${oauth2}/authorize?client_id=${clientId}&response_type=code&redirect_uri=${redirectUri}&scope=${scope}&state=${state}&response_mode=query`;
|
2019-01-31 11:32:33 -02:00
|
|
|
|
2020-05-17 21:30:21 +00:00
|
|
|
await min.conversationalService.sendText(min, step, Messages[locale].consent(url));
|
2019-01-31 11:32:33 -02:00
|
|
|
|
2019-03-08 06:37:13 -03:00
|
|
|
return await step.replaceDialog('/ask', { isReturning: true });
|
2019-01-31 11:32:33 -02:00
|
|
|
}
|
|
|
|
])
|
|
|
|
);
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
|
|
|
}
|