From 92053ea25abd5f50da15c5b306bed060304fe1e8 Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Thu, 1 Sep 2022 16:07:20 +0300 Subject: [PATCH] refactor: Slack authentication by passing flow, connection and step --- .../backend/src/apps/slack/authentication.ts | 41 +++++-------------- .../client/endpoints/verify-access-token.ts | 35 ++++++++++++++++ .../backend/src/apps/slack/client/index.ts | 23 +++++++++++ packages/backend/src/apps/slack/index.ts | 25 ++++++----- .../src/apps/twitter/authentication.ts | 2 +- 5 files changed, 83 insertions(+), 43 deletions(-) create mode 100644 packages/backend/src/apps/slack/client/endpoints/verify-access-token.ts create mode 100644 packages/backend/src/apps/slack/client/index.ts diff --git a/packages/backend/src/apps/slack/authentication.ts b/packages/backend/src/apps/slack/authentication.ts index 8b5e2c42..b08394f9 100644 --- a/packages/backend/src/apps/slack/authentication.ts +++ b/packages/backend/src/apps/slack/authentication.ts @@ -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; diff --git a/packages/backend/src/apps/slack/client/endpoints/verify-access-token.ts b/packages/backend/src/apps/slack/client/endpoints/verify-access-token.ts new file mode 100644 index 00000000..d5b3bfb5 --- /dev/null +++ b/packages/backend/src/apps/slack/client/endpoints/verify-access-token.ts @@ -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; + } +} diff --git a/packages/backend/src/apps/slack/client/index.ts b/packages/backend/src/apps/slack/client/index.ts new file mode 100644 index 00000000..c5d741ca --- /dev/null +++ b/packages/backend/src/apps/slack/client/index.ts @@ -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); + } +} diff --git a/packages/backend/src/apps/slack/index.ts b/packages/backend/src/apps/slack/index.ts index ab42e253..19a2c034 100644 --- a/packages/backend/src/apps/slack/index.ts +++ b/packages/backend/src/apps/slack/index.ts @@ -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); } } diff --git a/packages/backend/src/apps/twitter/authentication.ts b/packages/backend/src/apps/twitter/authentication.ts index 54551e85..5ddb83d7 100644 --- a/packages/backend/src/apps/twitter/authentication.ts +++ b/packages/backend/src/apps/twitter/authentication.ts @@ -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';