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 type { IAuthentication, IJSONObject } from '@automatisch/types';
|
||||||
import HttpClient from '../../helpers/http-client';
|
import SlackClient from './client';
|
||||||
import qs from 'qs';
|
|
||||||
|
|
||||||
export default class Authentication implements IAuthentication {
|
export default class Authentication implements IAuthentication {
|
||||||
appData: IApp;
|
client: SlackClient;
|
||||||
connectionData: IJSONObject;
|
|
||||||
client: HttpClient;
|
|
||||||
static requestOptions: IJSONObject = {
|
static requestOptions: IJSONObject = {
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/x-www-form-urlencoded',
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
constructor(appData: IApp, connectionData: IJSONObject) {
|
constructor(client: SlackClient) {
|
||||||
this.client = new HttpClient({ baseURL: 'https://slack.com/api' });
|
this.client = client;
|
||||||
|
|
||||||
this.connectionData = connectionData;
|
|
||||||
this.appData = appData;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async verifyCredentials() {
|
async verifyCredentials() {
|
||||||
const response = await this.client.post(
|
const { bot_id: botId, user: screenName } =
|
||||||
'/auth.test',
|
await this.client.verifyAccessToken.run();
|
||||||
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;
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
botId,
|
botId,
|
||||||
screenName,
|
screenName,
|
||||||
token: this.connectionData.accessToken,
|
token: this.client.connection.formattedData.accessToken,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async isStillVerified() {
|
async isStillVerified() {
|
||||||
try {
|
try {
|
||||||
await this.client.post(
|
await this.client.verifyAccessToken.run();
|
||||||
'/auth.test',
|
|
||||||
qs.stringify({ token: this.connectionData.accessToken }),
|
|
||||||
Authentication.requestOptions
|
|
||||||
);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return false;
|
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 {
|
import {
|
||||||
IService,
|
IService,
|
||||||
IAuthentication,
|
IAuthentication,
|
||||||
IApp,
|
IConnection,
|
||||||
IJSONObject,
|
IFlow,
|
||||||
|
IStep,
|
||||||
} from '@automatisch/types';
|
} from '@automatisch/types';
|
||||||
import Authentication from './authentication';
|
import Authentication from './authentication';
|
||||||
import Triggers from './triggers';
|
import Triggers from './triggers';
|
||||||
import Actions from './actions';
|
import Actions from './actions';
|
||||||
import Data from './data';
|
import Data from './data';
|
||||||
|
import SlackClient from './client';
|
||||||
|
|
||||||
export default class Slack implements IService {
|
export default class Slack implements IService {
|
||||||
|
client: SlackClient;
|
||||||
|
|
||||||
authenticationClient: IAuthentication;
|
authenticationClient: IAuthentication;
|
||||||
triggers: Triggers;
|
triggers: Triggers;
|
||||||
actions: Actions;
|
actions: Actions;
|
||||||
data: Data;
|
data: Data;
|
||||||
|
|
||||||
constructor(
|
constructor(connection: IConnection, flow?: IFlow, step?: IStep) {
|
||||||
appData: IApp,
|
this.client = new SlackClient(connection, flow, step);
|
||||||
connectionData: IJSONObject,
|
|
||||||
parameters: IJSONObject
|
this.authenticationClient = new Authentication(this.client);
|
||||||
) {
|
|
||||||
this.authenticationClient = new Authentication(appData, connectionData);
|
// this.triggers = new Triggers(this.client);
|
||||||
this.data = new Data(connectionData);
|
// this.actions = new Actions(this.client);
|
||||||
this.triggers = new Triggers(connectionData, parameters);
|
// this.data = new Data(this.client);
|
||||||
this.actions = new Actions(connectionData, parameters);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import type { IAuthentication, IField, IApp } from '@automatisch/types';
|
import type { IAuthentication, IField } from '@automatisch/types';
|
||||||
import { URLSearchParams } from 'url';
|
import { URLSearchParams } from 'url';
|
||||||
import TwitterClient from './client';
|
import TwitterClient from './client';
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user