2018-09-16 17:00:17 -03:00
|
|
|
/*****************************************************************************\
|
|
|
|
| ( )_ _ |
|
|
|
|
| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ |
|
2020-07-01 15:00:40 -03:00
|
|
|
| ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| | | |/',__)/' v `\ /'_`\ |
|
2019-03-09 16:59:31 -03:00
|
|
|
| | (_) )| | ( (_| |( (_) || ( ) ( ) |( (_| || |_ | |\__, \| (˅) |( (_) ) |
|
2018-09-16 17:00:17 -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-09-16 17:00:17 -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, |
|
|
|
|
| but WITHOUT ANY WARRANTY, without even the implied warranty of |
|
|
|
|
| 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
|
|
|
import { AuthenticationContext, TokenResponse } from 'adal-node';
|
2022-12-14 08:23:39 -03:00
|
|
|
import { GBMinInstance, IGBAdminService, IGBCoreService, IGBDeployer, IGBInstance } from 'botlib';
|
2022-01-03 13:11:21 -03:00
|
|
|
import { FindOptions } from 'sequelize/types';
|
2022-11-19 23:34:58 -03:00
|
|
|
import urlJoin from 'url-join';
|
2022-11-18 22:39:14 -03:00
|
|
|
import { AzureDeployerService } from '../../azuredeployer.gbapp/services/AzureDeployerService.js';
|
|
|
|
import { GuaribasInstance } from '../../core.gbapp/models/GBModel.js';
|
|
|
|
import { GBConfigService } from '../../core.gbapp/services/GBConfigService.js';
|
|
|
|
import { GBDeployer } from '../../core.gbapp/services/GBDeployer.js';
|
|
|
|
import { GBImporter } from '../../core.gbapp/services/GBImporterService.js';
|
|
|
|
import { GBSharePointService } from '../../sharepoint.gblib/services/SharePointService.js';
|
|
|
|
import { GuaribasAdmin } from '../models/AdminModel.js';
|
|
|
|
import msRestAzure from 'ms-rest-azure';
|
|
|
|
import Path from 'path';
|
|
|
|
import PasswordGenerator from 'strict-password-generator';
|
2022-11-19 23:34:58 -03:00
|
|
|
import crypto from 'crypto';
|
2022-12-22 20:55:49 -03:00
|
|
|
import Fs from 'fs';
|
2018-10-15 19:05:43 -03:00
|
|
|
|
2019-01-31 11:32:33 -02:00
|
|
|
/**
|
|
|
|
* Services for server administration.
|
|
|
|
*/
|
2019-03-08 06:37:13 -03:00
|
|
|
export class GBAdminService implements IGBAdminService {
|
2018-11-12 12:20:44 -02:00
|
|
|
public static GB_PROMPT: string = 'GeneralBots: ';
|
|
|
|
public static masterBotInstanceId = 0;
|
2018-10-15 19:05:43 -03:00
|
|
|
|
2019-01-31 11:32:33 -02:00
|
|
|
public static StrongRegex = new RegExp('^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*+_-])(?=.{8,})');
|
2018-09-16 17:00:17 -03:00
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
public core: IGBCoreService;
|
2018-09-24 11:04:36 -03:00
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
constructor (core: IGBCoreService) {
|
2018-09-24 11:04:36 -03:00
|
|
|
this.core = core;
|
|
|
|
}
|
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
public static generateUuid (): string {
|
2022-11-18 22:39:14 -03:00
|
|
|
return crypto.randomUUID();
|
2018-11-12 12:20:44 -02:00
|
|
|
}
|
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
public static getNodeVersion () {
|
2019-05-26 20:25:08 -03:00
|
|
|
const packageJson = urlJoin(process.cwd(), 'package.json');
|
2022-12-22 20:55:49 -03:00
|
|
|
const pkg = JSON.parse(Fs.readFileSync(packageJson, 'utf8'));
|
|
|
|
return pkg.engines.node.replace('=', '');
|
2019-05-26 20:25:08 -03:00
|
|
|
}
|
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
public static async getADALTokenFromUsername (username: string, password: string) {
|
2019-01-31 11:32:33 -02:00
|
|
|
const credentials = await GBAdminService.getADALCredentialsFromUsername(username, password);
|
|
|
|
|
2022-11-18 22:39:14 -03:00
|
|
|
return (credentials as any).tokenCache._entries[0].accessToken;
|
2018-11-12 12:20:44 -02:00
|
|
|
}
|
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
public static async getADALCredentialsFromUsername (username: string, password: string) {
|
|
|
|
return await msRestAzure.loginWithUsernamePassword(username, password);
|
2018-11-12 12:20:44 -02:00
|
|
|
}
|
2020-10-24 18:56:55 -03:00
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
public static getMobileCode () {
|
2020-07-26 16:46:37 -03:00
|
|
|
const passwordGenerator = new PasswordGenerator();
|
|
|
|
const options = {
|
|
|
|
upperCaseAlpha: false,
|
|
|
|
lowerCaseAlpha: false,
|
|
|
|
number: true,
|
|
|
|
specialCharacter: false,
|
|
|
|
minimumLength: 6,
|
|
|
|
maximumLength: 6
|
|
|
|
};
|
|
|
|
|
|
|
|
return passwordGenerator.generatePassword(options);
|
|
|
|
}
|
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
public static getRndPassword (): string {
|
2018-11-12 12:20:44 -02:00
|
|
|
const passwordGenerator = new PasswordGenerator();
|
|
|
|
const options = {
|
|
|
|
upperCaseAlpha: true,
|
|
|
|
lowerCaseAlpha: true,
|
|
|
|
number: true,
|
|
|
|
specialCharacter: true,
|
2020-12-04 13:44:33 -03:00
|
|
|
minimumLength: 14,
|
2018-11-12 12:20:44 -02:00
|
|
|
maximumLength: 14
|
|
|
|
};
|
|
|
|
let password = passwordGenerator.generatePassword(options);
|
2021-11-29 18:39:42 -03:00
|
|
|
password = password.replace(/[\@\[\=\:\;\?\"\'\#]/gi, '*');
|
2019-03-08 06:49:22 -03:00
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
return password;
|
|
|
|
}
|
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
public static getRndReadableIdentifier () {
|
2018-11-12 12:20:44 -02:00
|
|
|
const passwordGenerator = new PasswordGenerator();
|
|
|
|
const options = {
|
|
|
|
upperCaseAlpha: false,
|
|
|
|
lowerCaseAlpha: true,
|
|
|
|
number: false,
|
|
|
|
specialCharacter: false,
|
2020-12-04 13:44:33 -03:00
|
|
|
minimumLength: 14,
|
|
|
|
maximumLength: 14
|
|
|
|
};
|
|
|
|
|
|
|
|
return passwordGenerator.generatePassword(options);
|
|
|
|
}
|
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
public static getNumberIdentifier () {
|
2020-12-04 13:44:33 -03:00
|
|
|
const passwordGenerator = new PasswordGenerator();
|
|
|
|
const options = {
|
|
|
|
upperCaseAlpha: false,
|
|
|
|
lowerCaseAlpha: false,
|
|
|
|
number: true,
|
|
|
|
specialCharacter: false,
|
|
|
|
minimumLength: 14,
|
2018-11-12 12:20:44 -02:00
|
|
|
maximumLength: 14
|
|
|
|
};
|
2019-03-08 06:49:22 -03:00
|
|
|
|
2019-03-09 16:59:31 -03:00
|
|
|
return passwordGenerator.generatePassword(options);
|
2018-11-12 12:20:44 -02:00
|
|
|
}
|
|
|
|
|
2021-01-20 18:23:42 -03:00
|
|
|
/**
|
|
|
|
* @see https://stackoverflow.com/a/52171480
|
|
|
|
*/
|
2022-11-19 23:34:58 -03:00
|
|
|
public static getHash (str, seed = 0) {
|
|
|
|
let h1 = 0xdeadbeef ^ seed,
|
|
|
|
h2 = 0x41c6ce57 ^ seed;
|
2021-01-20 18:23:42 -03:00
|
|
|
for (let i = 0, ch; i < str.length; i++) {
|
|
|
|
ch = str.charCodeAt(i);
|
|
|
|
h1 = Math.imul(h1 ^ ch, 2654435761);
|
|
|
|
h2 = Math.imul(h2 ^ ch, 1597334677);
|
|
|
|
}
|
|
|
|
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
|
|
|
|
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
|
|
|
|
return 4294967296 * (2097151 & h2) + (h1 >>> 0);
|
|
|
|
}
|
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
public static async undeployPackageCommand (text: any, min: GBMinInstance) {
|
2020-12-31 15:36:19 -03:00
|
|
|
const packageName = text.split(' ')[1];
|
|
|
|
const importer = new GBImporter(min.core);
|
|
|
|
const deployer = new GBDeployer(min.core, importer);
|
|
|
|
const localFolder = Path.join('work', `${min.instance.botId}.gbai`, Path.basename(packageName));
|
|
|
|
await deployer.undeployPackageFromLocalPath(min.instance, localFolder);
|
|
|
|
}
|
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
public static isSharePointPath (path: string) {
|
2021-04-22 12:12:05 -03:00
|
|
|
return path.indexOf('sharepoint.com') !== -1;
|
2020-12-31 15:36:19 -03:00
|
|
|
}
|
2022-11-19 23:34:58 -03:00
|
|
|
public static async deployPackageCommand (min: GBMinInstance, text: string, deployer: IGBDeployer) {
|
2020-12-31 15:36:19 -03:00
|
|
|
const packageName = text.split(' ')[1];
|
|
|
|
|
|
|
|
if (!this.isSharePointPath(packageName)) {
|
|
|
|
const additionalPath = GBConfigService.get('ADDITIONAL_DEPLOY_PATH');
|
|
|
|
if (additionalPath === undefined) {
|
|
|
|
throw new Error('ADDITIONAL_DEPLOY_PATH is not set and deployPackage was called.');
|
|
|
|
}
|
|
|
|
await deployer.deployPackage(min, urlJoin(additionalPath, packageName));
|
|
|
|
} else {
|
|
|
|
const siteName = text.split(' ')[1];
|
|
|
|
const folderName = text.split(' ')[2];
|
|
|
|
|
|
|
|
const s = new GBSharePointService();
|
|
|
|
|
|
|
|
const localFolder = Path.join('work', `${min.instance.botId}.gbai`, Path.basename(folderName));
|
2021-01-07 19:42:03 -03:00
|
|
|
|
|
|
|
// .gbot packages are handled using storage API, so no download
|
|
|
|
// of local resources is required.
|
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
await deployer['downloadFolder'](min, Path.join('work', `${min.instance.botId}.gbai`), Path.basename(folderName));
|
2020-12-31 15:36:19 -03:00
|
|
|
await deployer.deployPackage(min, localFolder);
|
|
|
|
}
|
|
|
|
}
|
2022-12-15 10:56:27 -03:00
|
|
|
public static async rebuildIndexPackageCommand (min: GBMinInstance, deployer: GBDeployer) {
|
|
|
|
const service = await AzureDeployerService.createInstance(deployer);
|
2020-12-31 15:36:19 -03:00
|
|
|
await deployer.rebuildIndex(
|
|
|
|
min.instance,
|
2022-12-15 10:56:27 -03:00
|
|
|
service.getKBSearchSchema(min.instance.searchIndex)
|
2020-12-31 15:36:19 -03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
public static async syncBotServerCommand (min: GBMinInstance, deployer: GBDeployer) {
|
2020-12-31 15:36:19 -03:00
|
|
|
const serverName = `${min.instance.botId}-server`;
|
2022-12-15 10:56:27 -03:00
|
|
|
const service = await AzureDeployerService.createInstance(deployer );
|
2020-12-31 15:36:19 -03:00
|
|
|
service.syncBotServerRepository(min.instance.botId, serverName);
|
|
|
|
}
|
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
public async setValue (instanceId: number, key: string, value: string) {
|
2022-01-03 13:11:21 -03:00
|
|
|
const options = <FindOptions>{ where: {} };
|
2018-09-24 11:04:36 -03:00
|
|
|
options.where = { key: key };
|
2018-09-16 17:00:17 -03:00
|
|
|
let admin = await GuaribasAdmin.findOne(options);
|
2019-01-31 11:32:33 -02:00
|
|
|
if (admin === null) {
|
2018-09-16 17:00:17 -03:00
|
|
|
admin = new GuaribasAdmin();
|
|
|
|
admin.key = key;
|
|
|
|
}
|
|
|
|
admin.value = value;
|
2018-09-24 11:04:36 -03:00
|
|
|
admin.instanceId = instanceId;
|
2019-03-08 06:37:13 -03:00
|
|
|
await admin.save();
|
2018-09-16 17:00:17 -03:00
|
|
|
}
|
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
public async updateSecurityInfo (
|
2019-01-31 11:32:33 -02:00
|
|
|
instanceId: number,
|
|
|
|
authenticatorTenant: string,
|
2020-05-14 17:16:27 -03:00
|
|
|
authenticatorAuthorityHostUrl: string
|
2019-03-09 16:59:31 -03:00
|
|
|
): Promise<IGBInstance> {
|
2022-01-03 13:11:21 -03:00
|
|
|
const options = <FindOptions>{ where: {} };
|
2019-01-31 11:32:33 -02:00
|
|
|
options.where = { instanceId: instanceId };
|
|
|
|
const item = await GuaribasInstance.findOne(options);
|
|
|
|
item.authenticatorTenant = authenticatorTenant;
|
|
|
|
item.authenticatorAuthorityHostUrl = authenticatorAuthorityHostUrl;
|
|
|
|
|
|
|
|
return item.save();
|
|
|
|
}
|
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
public async getValue (instanceId: number, key: string): Promise<string> {
|
2022-01-03 13:11:21 -03:00
|
|
|
const options = <FindOptions>{ where: {} };
|
2018-09-24 11:04:36 -03:00
|
|
|
options.where = { key: key, instanceId: instanceId };
|
2018-11-12 12:20:44 -02:00
|
|
|
const obj = await GuaribasAdmin.findOne(options);
|
2019-01-31 11:32:33 -02:00
|
|
|
|
2020-08-19 13:00:21 -03:00
|
|
|
return obj.value;
|
2018-09-16 17:00:17 -03:00
|
|
|
}
|
2018-09-24 11:04:36 -03:00
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
public async acquireElevatedToken (instanceId: number): Promise<string> {
|
2020-05-12 09:06:47 -03:00
|
|
|
// TODO: Use boot bot as base for authentication.
|
|
|
|
|
2020-12-31 15:36:19 -03:00
|
|
|
const botId = GBConfigService.get('BOT_ID');
|
2020-05-12 09:06:47 -03:00
|
|
|
instanceId = (await this.core.loadInstanceByBotId(botId)).instanceId;
|
|
|
|
|
2018-09-24 11:04:36 -03:00
|
|
|
return new Promise<string>(async (resolve, reject) => {
|
2018-11-12 12:20:44 -02:00
|
|
|
const instance = await this.core.loadInstanceById(instanceId);
|
2018-09-24 11:04:36 -03:00
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
const expiresOn = new Date(await this.getValue(instanceId, 'expiresOn'));
|
2018-09-24 11:04:36 -03:00
|
|
|
if (expiresOn.getTime() > new Date().getTime()) {
|
2018-11-12 12:20:44 -02:00
|
|
|
const accessToken = await this.getValue(instanceId, 'accessToken');
|
2018-09-24 11:04:36 -03:00
|
|
|
resolve(accessToken);
|
|
|
|
} else {
|
2019-03-09 16:59:31 -03:00
|
|
|
const authorizationUrl = urlJoin(
|
2018-09-24 11:04:36 -03:00
|
|
|
instance.authenticatorAuthorityHostUrl,
|
|
|
|
instance.authenticatorTenant,
|
2018-11-12 12:20:44 -02:00
|
|
|
'/oauth2/authorize'
|
2018-09-24 11:04:36 -03:00
|
|
|
);
|
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
const refreshToken = await this.getValue(instanceId, 'refreshToken');
|
|
|
|
const resource = 'https://graph.microsoft.com';
|
|
|
|
const authenticationContext = new AuthenticationContext(authorizationUrl);
|
2021-01-20 18:23:42 -03:00
|
|
|
|
2018-09-24 11:04:36 -03:00
|
|
|
authenticationContext.acquireTokenWithRefreshToken(
|
|
|
|
refreshToken,
|
2020-05-14 17:16:27 -03:00
|
|
|
instance.marketplaceId,
|
|
|
|
instance.marketplacePassword,
|
2018-09-24 11:04:36 -03:00
|
|
|
resource,
|
|
|
|
async (err, res) => {
|
2020-03-08 09:24:28 -03:00
|
|
|
if (err !== null) {
|
2018-09-24 11:04:36 -03:00
|
|
|
reject(err);
|
|
|
|
} else {
|
2018-11-12 12:20:44 -02:00
|
|
|
const token = res as TokenResponse;
|
2020-10-17 23:03:27 -03:00
|
|
|
try {
|
|
|
|
await this.setValue(instanceId, 'accessToken', token.accessToken);
|
|
|
|
await this.setValue(instanceId, 'refreshToken', token.refreshToken);
|
|
|
|
await this.setValue(instanceId, 'expiresOn', token.expiresOn.toString());
|
|
|
|
resolve(token.accessToken);
|
|
|
|
} catch (error) {
|
|
|
|
reject(err);
|
|
|
|
}
|
2018-09-24 11:04:36 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-04-01 15:42:57 -03:00
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
public async publish (min: GBMinInstance, packageName: string, republish: boolean): Promise<void> {}
|
2018-09-16 17:00:17 -03:00
|
|
|
}
|