mirror of
https://github.com/ToolJet/ToolJet
synced 2026-04-28 17:07:22 +00:00
* Feature: Prometheus plugin (#13161) * prometheus plugin * added existing props * Host and database can be dynamically configured in query builder for PostgreSQL and MySQL data sources (#13163) * Fix: Postgresql datasource tries to connect via ssl even when ssl toggle is off (#13167) * The ability to provide a partition key for deleting items in CosmosDB datasource has been enabled (#13166) * Feature: Ability to configure the database name in Redis datasource (#13165) * Fix: Avoid setting Content-Type header for requests without body and configure different host for all environments in OpenAPI [PRE-RELEASE] (#13230) * Send content-type only with body in request * Persist OpenAPI parameters per operation only * Configure different host * Add disable styles to the select input * Feat: New fields 'client id' and 'client secret' have been introduced in the Slack datasource configuration page in pre-release (#13162) * Update slack frontend * Update slack backend to handle custom creds * Add backfill migrations * Dynamically change dropdown according to versions * Change migration file name * Correctly access scope in chat:write logic --------- Co-authored-by: Akshay Sasidharan <akshaysasidharan93@gmail.com> Co-authored-by: Parth <108089718+parthy007@users.noreply.github.com> Co-authored-by: Akshay <akshaysasidrn@gmail.com>
166 lines
5.4 KiB
TypeScript
166 lines
5.4 KiB
TypeScript
import { QueryError, QueryResult, QueryService } from '@tooljet-plugins/common';
|
|
import got, { Headers } from 'got';
|
|
import { SourceOptions, QueryOptions } from './types';
|
|
|
|
export default class SlackQueryService implements QueryService {
|
|
authUrl(options): string {
|
|
const source_options = Object.entries(options).map(([key, value]: [string, any]) => {
|
|
return { key, ...value };
|
|
});
|
|
const { useCredsFromEnv, useCredsFromDatasourceConfiguration, clientIdValue } =
|
|
this.shouldUseCredentialsFromEnv(source_options);
|
|
|
|
let clientId = null;
|
|
if (useCredsFromEnv) clientId = process.env.SLACK_CLIENT_ID;
|
|
if (useCredsFromDatasourceConfiguration) clientId = clientIdValue;
|
|
const host = process.env.TOOLJET_HOST;
|
|
const subpath = process.env.SUB_PATH;
|
|
const fullUrl = `${host}${subpath ? subpath : '/'}`;
|
|
return `https://slack.com/oauth/v2/authorize?response_type=code&client_id=${clientId}&redirect_uri=${fullUrl}oauth2/authorize`;
|
|
}
|
|
|
|
shouldUseCredentialsFromEnv(sourceOptions = []): {
|
|
useCredsFromEnv: boolean;
|
|
useCredsFromDatasourceConfiguration: boolean;
|
|
clientSecretValue: string | null;
|
|
clientIdValue: string | null;
|
|
} {
|
|
let useCredsFromEnv = false;
|
|
let useCredsFromDatasourceConfiguration = false;
|
|
let clientIdValue = null;
|
|
let clientSecretValue = null;
|
|
|
|
sourceOptions.forEach((sourceOption) => {
|
|
if (sourceOption.key === 'credential_source') {
|
|
if (sourceOption.value === 'from_env') useCredsFromEnv = true;
|
|
if (sourceOption.value === 'from_datasource_configuration') useCredsFromDatasourceConfiguration = true;
|
|
}
|
|
if (sourceOption.key === 'client_id') clientIdValue = sourceOption.value;
|
|
if (sourceOption.key === 'client_secret') clientSecretValue = sourceOption.value;
|
|
});
|
|
return { useCredsFromEnv, useCredsFromDatasourceConfiguration, clientIdValue, clientSecretValue };
|
|
}
|
|
|
|
async accessDetailsFrom(authCode: string, options: any, resetSecureData = false): Promise<object> {
|
|
if (resetSecureData) {
|
|
return [
|
|
['access_token', ''],
|
|
['refresh_token', ''],
|
|
];
|
|
}
|
|
|
|
const accessTokenUrl = 'https://slack.com/api/oauth.v2.access';
|
|
const { useCredsFromEnv, useCredsFromDatasourceConfiguration, clientIdValue, clientSecretValue } =
|
|
this.shouldUseCredentialsFromEnv(options);
|
|
let clientId = null;
|
|
let clientSecret = null;
|
|
if (useCredsFromEnv) {
|
|
clientId = process.env.SLACK_CLIENT_ID;
|
|
clientSecret = process.env.SLACK_CLIENT_SECRET;
|
|
}
|
|
if (useCredsFromDatasourceConfiguration) {
|
|
clientId = clientIdValue;
|
|
clientSecret = clientSecretValue;
|
|
}
|
|
|
|
const host = process.env.TOOLJET_HOST;
|
|
const subpath = process.env.SUB_PATH;
|
|
const fullUrl = `${host}${subpath ? subpath : '/'}`;
|
|
const redirectUri = `${fullUrl}oauth2/authorize`;
|
|
|
|
const body = `code=${authCode}&client_id=${clientId}&client_secret=${clientSecret}&redirect_uri=${redirectUri}`;
|
|
|
|
const response = await got(accessTokenUrl, {
|
|
method: 'post',
|
|
body,
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
});
|
|
|
|
const result = JSON.parse(response.body);
|
|
|
|
if (response.statusCode !== 200) {
|
|
throw Error('could not connect to Slack');
|
|
}
|
|
|
|
const authDetails = [];
|
|
|
|
if (result['access_token']) {
|
|
authDetails.push(['access_token', result['access_token']]);
|
|
}
|
|
|
|
if (result['refresh_token']) {
|
|
authDetails.push(['refresh_token', result['refresh_token']]);
|
|
}
|
|
|
|
return authDetails;
|
|
}
|
|
|
|
authHeader(token: string): Headers {
|
|
return { Authorization: `Bearer ${token}` };
|
|
}
|
|
|
|
async run(sourceOptions: SourceOptions, queryOptions: QueryOptions, dataSourceId: string): Promise<QueryResult> {
|
|
let result = {};
|
|
let response = null;
|
|
const operation = queryOptions.operation;
|
|
const accessToken = sourceOptions.access_token;
|
|
|
|
try {
|
|
switch (operation) {
|
|
case 'list_users':
|
|
response = await got('https://slack.com/api/users.list', {
|
|
method: 'get',
|
|
headers: this.authHeader(accessToken),
|
|
});
|
|
|
|
result = JSON.parse(response.body);
|
|
break;
|
|
|
|
case 'send_message': {
|
|
if (sourceOptions.access_type === 'chat:write') {
|
|
const body = {
|
|
channel: queryOptions.channel,
|
|
text: queryOptions.message,
|
|
as_user: queryOptions.sendAsUser,
|
|
};
|
|
|
|
response = await got('https://slack.com/api/chat.postMessage', {
|
|
method: 'post',
|
|
json: body,
|
|
headers: this.authHeader(accessToken),
|
|
});
|
|
|
|
result = JSON.parse(response.body);
|
|
} else {
|
|
result = {
|
|
ok: false,
|
|
error: 'You do not have the required permissions to perform this operation',
|
|
};
|
|
}
|
|
break;
|
|
}
|
|
|
|
case 'list_messages':
|
|
response = await got('https://slack.com/api/conversations.history', {
|
|
method: 'post',
|
|
form: {
|
|
channel: queryOptions.channel,
|
|
limit: queryOptions.limit || 100,
|
|
cursor: queryOptions.cursor || '',
|
|
},
|
|
headers: this.authHeader(accessToken),
|
|
});
|
|
|
|
result = JSON.parse(response.body);
|
|
break;
|
|
}
|
|
} catch (error) {
|
|
throw new QueryError('Query could not be completed', error.message, {});
|
|
}
|
|
|
|
return {
|
|
status: 'ok',
|
|
data: result,
|
|
};
|
|
}
|
|
}
|