refactor: Slack authentication by passing flow, connection and step
This commit is contained in:
@@ -1,54 +1,33 @@
|
||||
import type { IAuthentication, IApp, IJSONObject } from '@automatisch/types';
|
||||
import HttpClient from '../../helpers/http-client';
|
||||
import qs from 'qs';
|
||||
import type { IAuthentication, IJSONObject } from '@automatisch/types';
|
||||
import SlackClient from './client';
|
||||
|
||||
export default class Authentication implements IAuthentication {
|
||||
appData: IApp;
|
||||
connectionData: IJSONObject;
|
||||
client: HttpClient;
|
||||
client: SlackClient;
|
||||
|
||||
static requestOptions: IJSONObject = {
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
};
|
||||
|
||||
constructor(appData: IApp, connectionData: IJSONObject) {
|
||||
this.client = new HttpClient({ baseURL: 'https://slack.com/api' });
|
||||
|
||||
this.connectionData = connectionData;
|
||||
this.appData = appData;
|
||||
constructor(client: SlackClient) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
async verifyCredentials() {
|
||||
const response = await this.client.post(
|
||||
'/auth.test',
|
||||
qs.stringify({ token: this.connectionData.accessToken }),
|
||||
Authentication.requestOptions
|
||||
);
|
||||
|
||||
if (response.data.ok === false) {
|
||||
throw new Error(
|
||||
`Error occured while verifying credentials: ${response.data.error}.(More info: https://api.slack.com/methods/auth.test#errors)`
|
||||
);
|
||||
}
|
||||
|
||||
const { bot_id: botId, user: screenName } = response.data;
|
||||
const { bot_id: botId, user: screenName } =
|
||||
await this.client.verifyAccessToken.run();
|
||||
|
||||
return {
|
||||
botId,
|
||||
screenName,
|
||||
token: this.connectionData.accessToken,
|
||||
token: this.client.connection.formattedData.accessToken,
|
||||
};
|
||||
}
|
||||
|
||||
async isStillVerified() {
|
||||
try {
|
||||
await this.client.post(
|
||||
'/auth.test',
|
||||
qs.stringify({ token: this.connectionData.accessToken }),
|
||||
Authentication.requestOptions
|
||||
);
|
||||
|
||||
await this.client.verifyAccessToken.run();
|
||||
return true;
|
||||
} catch (error) {
|
||||
return false;
|
||||
|
@@ -0,0 +1,35 @@
|
||||
import { IJSONObject } from '@automatisch/types';
|
||||
import qs from 'qs';
|
||||
import SlackClient from '../index';
|
||||
|
||||
export default class VerifyAccessToken {
|
||||
client: SlackClient;
|
||||
|
||||
static requestOptions: IJSONObject = {
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
};
|
||||
|
||||
constructor(client: SlackClient) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
async run() {
|
||||
const response = await this.client.httpClient.post(
|
||||
'/auth.test',
|
||||
qs.stringify({
|
||||
token: this.client.connection.formattedData.accessToken,
|
||||
}),
|
||||
VerifyAccessToken.requestOptions
|
||||
);
|
||||
|
||||
if (response.data.ok === false) {
|
||||
throw new Error(
|
||||
`Error occured while verifying credentials: ${response.data.error}.(More info: https://api.slack.com/methods/auth.test#errors)`
|
||||
);
|
||||
}
|
||||
|
||||
return response.data;
|
||||
}
|
||||
}
|
23
packages/backend/src/apps/slack/client/index.ts
Normal file
23
packages/backend/src/apps/slack/client/index.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { IFlow, IStep, IConnection } from '@automatisch/types';
|
||||
import HttpClient from '../../../helpers/http-client';
|
||||
import VerifyAccessToken from './endpoints/verify-access-token';
|
||||
|
||||
export default class SlackClient {
|
||||
flow: IFlow;
|
||||
step: IStep;
|
||||
connection: IConnection;
|
||||
httpClient: HttpClient;
|
||||
|
||||
verifyAccessToken: VerifyAccessToken;
|
||||
|
||||
static baseUrl = 'https://slack.com/api';
|
||||
|
||||
constructor(connection: IConnection, flow?: IFlow, step?: IStep) {
|
||||
this.connection = connection;
|
||||
this.flow = flow;
|
||||
this.step = step;
|
||||
|
||||
this.httpClient = new HttpClient({ baseURL: SlackClient.baseUrl });
|
||||
this.verifyAccessToken = new VerifyAccessToken(this);
|
||||
}
|
||||
}
|
@@ -1,28 +1,31 @@
|
||||
import {
|
||||
IService,
|
||||
IAuthentication,
|
||||
IApp,
|
||||
IJSONObject,
|
||||
IConnection,
|
||||
IFlow,
|
||||
IStep,
|
||||
} from '@automatisch/types';
|
||||
import Authentication from './authentication';
|
||||
import Triggers from './triggers';
|
||||
import Actions from './actions';
|
||||
import Data from './data';
|
||||
import SlackClient from './client';
|
||||
|
||||
export default class Slack implements IService {
|
||||
client: SlackClient;
|
||||
|
||||
authenticationClient: IAuthentication;
|
||||
triggers: Triggers;
|
||||
actions: Actions;
|
||||
data: Data;
|
||||
|
||||
constructor(
|
||||
appData: IApp,
|
||||
connectionData: IJSONObject,
|
||||
parameters: IJSONObject
|
||||
) {
|
||||
this.authenticationClient = new Authentication(appData, connectionData);
|
||||
this.data = new Data(connectionData);
|
||||
this.triggers = new Triggers(connectionData, parameters);
|
||||
this.actions = new Actions(connectionData, parameters);
|
||||
constructor(connection: IConnection, flow?: IFlow, step?: IStep) {
|
||||
this.client = new SlackClient(connection, flow, step);
|
||||
|
||||
this.authenticationClient = new Authentication(this.client);
|
||||
|
||||
// this.triggers = new Triggers(this.client);
|
||||
// this.actions = new Actions(this.client);
|
||||
// this.data = new Data(this.client);
|
||||
}
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import type { IAuthentication, IField, IApp } from '@automatisch/types';
|
||||
import type { IAuthentication, IField } from '@automatisch/types';
|
||||
import { URLSearchParams } from 'url';
|
||||
import TwitterClient from './client';
|
||||
|
||||
|
Reference in New Issue
Block a user