refactor: Pass app data within the constructor of an app

This commit is contained in:
Faruk AYDIN
2022-02-27 18:22:17 +03:00
committed by Ali BARIN
parent 003213d223
commit c864a1062d
24 changed files with 216 additions and 174 deletions

View File

@@ -1,24 +1,25 @@
import { URLSearchParams } from 'url';
import axios, { AxiosInstance } from 'axios';
import App from '../../models/app';
import Field from '../../types/field';
export default class Authentication {
client?: any
connectionData: any
appData: any
scope: string[] = ['identify', 'email']
client?: any;
connectionData: any;
appData: any;
scope: string[] = ['identify', 'email'];
httpClient: AxiosInstance = axios.create({
baseURL: 'https://discord.com/api/'
})
baseURL: 'https://discord.com/api/',
});
constructor(connectionData: any) {
constructor(appData: any, connectionData: any) {
this.appData = appData;
this.connectionData = connectionData;
this.appData = App.findOneByKey('discord');
}
get oauthRedirectUrl() {
return this.appData.fields.find((field: Field) => field.key == 'oAuthRedirectUrl').value;
return this.appData.fields.find(
(field: Field) => field.key == 'oAuthRedirectUrl'
).value;
}
async createAuthData() {
@@ -44,7 +45,10 @@ export default class Authentication {
code: this.connectionData.oauthVerifier,
grant_type: 'authorization_code',
});
const { data: verifiedCredentials }: any = await this.httpClient.post('/oauth2/token', params.toString());
const { data: verifiedCredentials }: any = await this.httpClient.post(
'/oauth2/token',
params.toString()
);
const {
access_token: accessToken,
@@ -84,7 +88,7 @@ export default class Authentication {
return true;
} catch {
return false
return false;
}
}
}

View File

@@ -1,9 +1,9 @@
import Authentication from './authentication';
export default class Discord {
authenticationClient: any
authenticationClient: any;
constructor(connectionData: any) {
this.authenticationClient = new Authentication(connectionData);
constructor(appData: any, connectionData: any) {
this.authenticationClient = new Authentication(appData, connectionData);
}
}

View File

@@ -1,47 +1,50 @@
import { google as GoogleApi } from 'googleapis';
import App from '../../models/app';
import Field from '../../types/field';
export default class Authentication {
oauthClient: any
connectionData: any
appData: any
oauthClient: any;
connectionData: any;
appData: any;
scopes: string[] = [
'https://www.googleapis.com/auth/datastore',
'https://www.googleapis.com/auth/firebase',
'https://www.googleapis.com/auth/user.emails.read',
'profile',
]
];
constructor(connectionData: any) {
this.appData = App.findOneByKey('firebase');
constructor(appData: any, connectionData: any) {
this.appData = appData;
this.connectionData = connectionData;
this.oauthClient = new GoogleApi.auth.OAuth2(
connectionData.consumerKey,
connectionData.consumerSecret,
this.oauthRedirectUrl,
this.oauthRedirectUrl
);
GoogleApi.options({ auth: this.oauthClient });
}
get oauthRedirectUrl() {
return this.appData.fields.find((field: Field) => field.key == 'oAuthRedirectUrl').value;
return this.appData.fields.find(
(field: Field) => field.key == 'oAuthRedirectUrl'
).value;
}
async createAuthData() {
const url = this.oauthClient.generateAuthUrl({
access_type: 'offline',
scope: this.scopes
scope: this.scopes,
});
return { url };
}
async verifyCredentials() {
const { tokens } = await this.oauthClient.getToken(this.connectionData.oauthVerifier);
const { tokens } = await this.oauthClient.getToken(
this.connectionData.oauthVerifier
);
this.oauthClient.setCredentials(tokens);
const people = GoogleApi.people('v1');
@@ -52,7 +55,9 @@ export default class Authentication {
});
const { emailAddresses, resourceName: userId } = data;
const primaryEmailAddress = emailAddresses.find(emailAddress => emailAddress.metadata.primary);
const primaryEmailAddress = emailAddresses.find(
(emailAddress) => emailAddress.metadata.primary
);
return {
consumerKey: this.connectionData.consumerKey,
@@ -64,7 +69,7 @@ export default class Authentication {
scope: tokens.scope,
screenName: primaryEmailAddress.value,
userId,
}
};
}
async isStillVerified() {
@@ -72,7 +77,7 @@ export default class Authentication {
await this.oauthClient.getTokenInfo(this.connectionData.accessToken);
return true;
} catch {
return false
return false;
}
}
}

View File

@@ -1,9 +1,9 @@
import Authentication from './authentication'
import Authentication from './authentication';
export default class Firebase {
authenticationClient: any
authenticationClient: any;
constructor(connectionData: any) {
this.authenticationClient = new Authentication(connectionData);
constructor(appData: any, connectionData: any) {
this.authenticationClient = new Authentication(appData, connectionData);
}
}

View File

@@ -1,15 +1,17 @@
import FlickrApi from 'flickr-sdk';
import App from '../../models/app';
import Field from '../../types/field';
export default class Authentication {
oauthClient: any
client: any
connectionData: any
appData: any
oauthClient: any;
client: any;
connectionData: any;
appData: any;
constructor(connectionData: any) {
this.oauthClient = new FlickrApi.OAuth(connectionData.consumerKey, connectionData.consumerSecret);
constructor(appData: any, connectionData: any) {
this.oauthClient = new FlickrApi.OAuth(
connectionData.consumerKey,
connectionData.consumerSecret
);
if (connectionData.accessToken && connectionData.accessSecret) {
this.client = new FlickrApi(
@@ -17,21 +19,26 @@ export default class Authentication {
connectionData.consumerKey,
connectionData.consumerSecret,
connectionData.accessToken,
connectionData.accessSecret,
connectionData.accessSecret
)
);
}
this.connectionData = connectionData;
this.appData = App.findOneByKey('flickr');
this.appData = appData;
}
async createAuthData() {
const appFields = this.appData.fields.find((field: Field) => field.key == 'oAuthRedirectUrl')
const appFields = this.appData.fields.find(
(field: Field) => field.key == 'oAuthRedirectUrl'
);
const callbackUrl = appFields.value;
const oauthData = (await this.oauthClient.request(callbackUrl)).body;
const url = await this.oauthClient.authorizeUrl(oauthData.oauth_token, 'delete');
const url = await this.oauthClient.authorizeUrl(
oauthData.oauth_token,
'delete'
);
return {
accessToken: oauthData.oauth_token,
@@ -41,11 +48,13 @@ export default class Authentication {
}
async verifyCredentials() {
const verifiedCredentials = (await this.oauthClient.verify(
this.connectionData.accessToken,
this.connectionData.oauthVerifier,
this.connectionData.accessSecret
)).body;
const verifiedCredentials = (
await this.oauthClient.verify(
this.connectionData.accessToken,
this.connectionData.oauthVerifier,
this.connectionData.accessSecret
)
).body;
return {
consumerKey: this.connectionData.consumerKey,
@@ -53,8 +62,8 @@ export default class Authentication {
accessToken: verifiedCredentials.oauth_token,
accessSecret: verifiedCredentials.oauth_token_secret,
userId: verifiedCredentials.user_nsid,
screenName: verifiedCredentials.fullname
}
screenName: verifiedCredentials.fullname,
};
}
async isStillVerified() {

View File

@@ -1,9 +1,9 @@
import Authentication from './authentication'
import Authentication from './authentication';
export default class Flickr {
authenticationClient: any
authenticationClient: any;
constructor(connectionData: any) {
this.authenticationClient = new Authentication(connectionData);
constructor(appData: any, connectionData: any) {
this.authenticationClient = new Authentication(appData, connectionData);
}
}

View File

@@ -3,26 +3,27 @@ import {
exchangeWebFlowCode,
checkToken,
} from '@octokit/oauth-methods';
import App from '../../models/app';
import Field from '../../types/field';
export default class Authentication {
connectionData: any
appData: any
scopes: string[] = ['repo']
connectionData: any;
appData: any;
scopes: string[] = ['repo'];
constructor(connectionData: any) {
constructor(appData: any, connectionData: any) {
this.connectionData = connectionData;
this.appData = App.findOneByKey('github');
this.appData = appData;
}
get oauthRedirectUrl(): string {
return this.appData.fields.find((field: Field) => field.key == 'oAuthRedirectUrl').value;
return this.appData.fields.find(
(field: Field) => field.key == 'oAuthRedirectUrl'
).value;
}
async createAuthData(): { url: string } {
const { url } = await getWebFlowAuthorizationUrl({
clientType: "oauth-app",
clientType: 'oauth-app',
clientId: this.connectionData.consumerKey,
redirectUrl: this.oauthRedirectUrl,
scopes: this.scopes,
@@ -35,7 +36,7 @@ export default class Authentication {
async verifyCredentials(): any {
const { data } = await exchangeWebFlowCode({
clientType: "oauth-app",
clientType: 'oauth-app',
clientId: this.connectionData.consumerKey,
clientSecret: this.connectionData.consumerSecret,
code: this.connectionData.oauthVerifier,
@@ -53,12 +54,12 @@ export default class Authentication {
tokenType: data.token_type,
userId: tokenInfo.data.user.id,
screenName: tokenInfo.data.user.login,
}
};
}
async getTokenInfo() {
return checkToken({
clientType: "oauth-app",
clientType: 'oauth-app',
clientId: this.connectionData.consumerKey,
clientSecret: this.connectionData.consumerSecret,
token: this.connectionData.accessToken,

View File

@@ -1,9 +1,9 @@
import Authentication from './authentication';
export default class Github {
authenticationClient: any
authenticationClient: any;
constructor(connectionData: any) {
this.authenticationClient = new Authentication(connectionData);
constructor(appData: any, connectionData: any) {
this.authenticationClient = new Authentication(appData, connectionData);
}
}

View File

@@ -1,12 +1,11 @@
import { Client } from 'pg';
import App from '../../models/app';
export default class Authentication {
client: any
connectionData: any
appData: any
client: any;
connectionData: any;
appData: any;
constructor(connectionData: any) {
constructor(appData: any, connectionData: any) {
this.client = new Client({
host: connectionData.host,
port: connectionData.port,
@@ -14,26 +13,26 @@ export default class Authentication {
user: connectionData.username,
password: connectionData.password,
ssl: connectionData.ssl,
})
});
this.connectionData = connectionData;
this.appData = App.findOneByKey('postgresql');
this.appData = appData;
}
async verifyCredentials() {
await this.client.connect()
await this.client.connect();
return {
screenName: this.connectionData.database
}
screenName: this.connectionData.database,
};
}
async isStillVerified() {
try {
await this.client.connect()
await this.client.connect();
return true;
} catch(error) {
return false
} catch (error) {
return false;
}
}
}

View File

@@ -1,9 +1,9 @@
import Authentication from './authentication'
import Authentication from './authentication';
export default class PostgreSQL {
authenticationClient: any;
constructor(connectionData: any) {
this.authenticationClient = new Authentication(connectionData);
constructor(appData: any, connectionData: any) {
this.authenticationClient = new Authentication(appData, connectionData);
}
}

View File

@@ -1,12 +1,11 @@
import nodemailer from 'nodemailer';
import App from '../../models/app';
export default class Authentication {
client: any
connectionData: any
appData: any
client: any;
connectionData: any;
appData: any;
constructor(connectionData: any) {
constructor(appData: any, connectionData: any) {
this.client = nodemailer.createTransport({
host: connectionData.host,
port: connectionData.port,
@@ -18,23 +17,23 @@ export default class Authentication {
});
this.connectionData = connectionData;
this.appData = App.findOneByKey('smtp');
this.appData = appData;
}
async verifyCredentials() {
await this.client.verify()
await this.client.verify();
return {
screenName: this.connectionData.username
}
screenName: this.connectionData.username,
};
}
async isStillVerified() {
try {
await this.client.verify()
await this.client.verify();
return true;
} catch(error) {
return false
} catch (error) {
return false;
}
}
}

View File

@@ -1,9 +1,9 @@
import Authentication from './authentication';
export default class SMTP {
authenticationClient: any
authenticationClient: any;
constructor(connectionData: any) {
this.authenticationClient = new Authentication(connectionData);
constructor(appData: any, connectionData: any) {
this.authenticationClient = new Authentication(appData, connectionData);
}
}

View File

@@ -1,31 +1,33 @@
import TwilioApi from 'twilio';
import App from '../../models/app';
export default class Authentication {
client: any
connectionData: any
appData: any
client: any;
connectionData: any;
appData: any;
constructor(connectionData: any) {
this.client = TwilioApi(connectionData.accountSid, connectionData.authToken);
constructor(appData: any, connectionData: any) {
this.client = TwilioApi(
connectionData.accountSid,
connectionData.authToken
);
this.connectionData = connectionData;
this.appData = App.findOneByKey('twilio');
this.appData = appData;
}
async verifyCredentials() {
await this.verify();
return {
screenName: this.connectionData.accountSid
}
screenName: this.connectionData.accountSid,
};
}
async verify() {
try {
await this.client.keys.list({ limit: 1 })
await this.client.keys.list({ limit: 1 });
return true;
} catch(error) {
} catch (error) {
// Test credentials throw HTTP 403 and thus, we need to have an exception.
return error?.status === 403;
}

View File

@@ -3,7 +3,7 @@ import Authentication from './authentication';
export default class Twilio {
authenticationClient: any;
constructor(connectionData: any) {
this.authenticationClient = new Authentication(connectionData);
constructor(appData: any, connectionData: any) {
this.authenticationClient = new Authentication(appData, connectionData);
}
}

View File

@@ -1,6 +1,5 @@
import TwitchApi from 'twitch-js';
import fetchUtil from 'twitch-js/lib/utils/fetch';
import App from '../../models/app';
import Field from '../../types/field';
type TwitchTokenResponse = {
@@ -11,13 +10,13 @@ type TwitchTokenResponse = {
};
export default class Authentication {
client: any
connectionData: any
appData: any
client: any;
connectionData: any;
appData: any;
constructor(connectionData: any) {
constructor(appData: any, connectionData: any) {
this.connectionData = connectionData;
this.appData = App.findOneByKey('twitch');
this.appData = appData;
if (this.clientOptions.token) {
this.client = new TwitchApi(this.clientOptions);
@@ -28,12 +27,14 @@ export default class Authentication {
return {
token: this.connectionData.accessToken,
clientId: this.connectionData.consumerKey,
log: { enabled: true }
log: { enabled: true },
};
}
get oauthRedirectUrl() {
return this.appData.fields.find((field: Field) => field.key == 'oAuthRedirectUrl').value;
return this.appData.fields.find(
(field: Field) => field.key == 'oAuthRedirectUrl'
).value;
}
async createAuthData() {
@@ -43,23 +44,26 @@ export default class Authentication {
redirect_uri: this.oauthRedirectUrl,
response_type: 'code',
scope: 'user:read:email',
}
},
});
return { url };
}
async verifyCredentials() {
const verifiedCredentials = await fetchUtil('https://id.twitch.tv/oauth2/token', {
method: 'post',
search: {
client_id: this.connectionData.consumerKey,
client_secret: this.connectionData.consumerSecret,
code: this.connectionData.oauthVerifier,
grant_type: 'authorization_code',
redirect_uri: this.oauthRedirectUrl
const verifiedCredentials = (await fetchUtil(
'https://id.twitch.tv/oauth2/token',
{
method: 'post',
search: {
client_id: this.connectionData.consumerKey,
client_secret: this.connectionData.consumerSecret,
code: this.connectionData.oauthVerifier,
grant_type: 'authorization_code',
redirect_uri: this.oauthRedirectUrl,
},
}
}) as TwitchTokenResponse;
)) as TwitchTokenResponse;
this.connectionData.accessToken = verifiedCredentials.accessToken;
@@ -77,7 +81,7 @@ export default class Authentication {
tokenType: verifiedCredentials.tokenType,
userId: user.id,
screenName: user.displayName,
}
};
}
async isStillVerified() {
@@ -90,7 +94,7 @@ export default class Authentication {
return true;
} catch (err) {
return false
return false;
}
}
}

View File

@@ -1,9 +1,9 @@
import Authentication from './authentication';
export default class Twitch {
authenticationClient: any
authenticationClient: any;
constructor(connectionData: any) {
this.authenticationClient = new Authentication(connectionData);
constructor(appData: any, connectionData: any) {
this.authenticationClient = new Authentication(appData, connectionData);
}
}

View File

@@ -1,26 +1,27 @@
import TwitterApi from 'twitter-api-v2';
import App from '../../models/app';
import Field from '../../types/field';
export default class Authentication {
client: any
connectionData: any
appData: any
client: any;
connectionData: any;
appData: any;
constructor(appData: any, connectionData: any) {
this.appData = appData;
this.connectionData = connectionData;
constructor(connectionData: any) {
this.client = new TwitterApi({
appKey: connectionData.consumerKey,
appSecret: connectionData.consumerSecret,
accessToken: connectionData.accessToken,
accessSecret: connectionData.accessSecret
accessSecret: connectionData.accessSecret,
});
this.connectionData = connectionData;
this.appData = App.findOneByKey('twitter');
}
async createAuthData() {
const appFields = this.appData.fields.find((field: Field) => field.key == 'oAuthRedirectUrl')
const appFields = this.appData.fields.find(
(field: Field) => field.key == 'oAuthRedirectUrl'
);
const callbackUrl = appFields.value;
const authLink = await this.client.generateAuthLink(callbackUrl);
@@ -29,11 +30,13 @@ export default class Authentication {
url: authLink.url,
accessToken: authLink.oauth_token,
accessSecret: authLink.oauth_token_secret,
}
};
}
async verifyCredentials() {
const verifiedCredentials = await this.client.login(this.connectionData.oauthVerifier)
const verifiedCredentials = await this.client.login(
this.connectionData.oauthVerifier
);
return {
consumerKey: this.connectionData.consumerKey,
@@ -41,8 +44,8 @@ export default class Authentication {
accessToken: verifiedCredentials.accessToken,
accessSecret: verifiedCredentials.accessSecret,
userId: verifiedCredentials.userId,
screenName: verifiedCredentials.screenName
}
screenName: verifiedCredentials.screenName,
};
}
async isStillVerified() {
@@ -50,7 +53,7 @@ export default class Authentication {
await this.client.currentUser();
return true;
} catch {
return false
return false;
}
}
}

View File

@@ -7,8 +7,8 @@ export default class Twitter {
triggers: any;
actions: any;
constructor(connectionData: any, parameters: any) {
this.authenticationClient = new Authentication(connectionData);
constructor(appData: any, connectionData: any, parameters: any) {
this.authenticationClient = new Authentication(appData, connectionData);
this.triggers = new Triggers(connectionData);
this.actions = new Actions(connectionData, parameters);
}

View File

@@ -1,13 +1,11 @@
import { URLSearchParams } from 'url';
import axios, { AxiosInstance } from
'axios';
import App from '../../models/app';
import axios, { AxiosInstance } from 'axios';
import Field from '../../types/field';
export default class Authentication {
client?: any
connectionData: any
appData: any
client?: any;
connectionData: any;
appData: any;
scope: string[] = [
'forms:read',
'forms:write',
@@ -16,18 +14,20 @@ export default class Authentication {
'responses:read',
'accounts:read',
'workspaces:read',
]
];
httpClient: AxiosInstance = axios.create({
baseURL: 'https://api.typeform.com'
})
baseURL: 'https://api.typeform.com',
});
constructor(connectionData: any) {
constructor(appData: any, connectionData: any) {
this.connectionData = connectionData;
this.appData = App.findOneByKey('typeform');
this.appData = appData;
}
get oauthRedirectUrl() {
return this.appData.fields.find((field: Field) => field.key == 'oAuthRedirectUrl').value;
return this.appData.fields.find(
(field: Field) => field.key == 'oAuthRedirectUrl'
).value;
}
async createAuthData() {
@@ -51,7 +51,10 @@ export default class Authentication {
redirect_uri: this.oauthRedirectUrl,
});
const { data: verifiedCredentials }: any = await this.httpClient.post('/oauth/token', params.toString());
const { data: verifiedCredentials }: any = await this.httpClient.post(
'/oauth/token',
params.toString()
);
const {
access_token: accessToken,
@@ -87,7 +90,7 @@ export default class Authentication {
return true;
} catch {
return false
return false;
}
}
}

View File

@@ -3,7 +3,7 @@ import Authentication from './authentication';
export default class Typeform {
authenticationClient: any;
constructor(connectionData: any) {
this.authenticationClient = new Authentication(connectionData);
constructor(appData: any, connectionData: any) {
this.authenticationClient = new Authentication(appData, connectionData);
}
}

View File

@@ -1,6 +1,7 @@
import { GraphQLNonNull, GraphQLString } from 'graphql';
import authLinkType from '../types/auth-link';
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
import App from '../../models/app';
type Params = {
id: string;
@@ -18,8 +19,9 @@ const createAuthDataResolver = async (
.throwIfNotFound();
const appClass = (await import(`../../apps/${connection.key}`)).default;
const appData = App.findOneByKey(connection.key);
const appInstance = new appClass({
const appInstance = new appClass(appData, {
consumerKey: connection.data.consumerKey,
consumerSecret: connection.data.consumerSecret,
});

View File

@@ -1,6 +1,7 @@
import { GraphQLString, GraphQLNonNull } from 'graphql';
import connectionType from '../types/connection';
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
import App from '../../models/app';
type Params = {
id: string;
@@ -18,8 +19,9 @@ const verifyConnectionResolver = async (
.throwIfNotFound();
const appClass = (await import(`../../apps/${connection.key}`)).default;
const appData = App.findOneByKey(connection.key);
const appInstance = new appClass(connection.data);
const appInstance = new appClass(appData, connection.data);
const verifiedCredentials =
await appInstance.authenticationClient.verifyCredentials();

View File

@@ -1,6 +1,7 @@
import { GraphQLString, GraphQLNonNull } from 'graphql';
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
import connectionType from '../types/connection';
import App from '../../models/app';
type Params = {
id: string;
@@ -18,8 +19,9 @@ const testConnectionResolver = async (
.throwIfNotFound();
const appClass = (await import(`../../apps/${connection.key}`)).default;
const appData = App.findOneByKey(connection.key);
const appInstance = new appClass(connection.data);
const appInstance = new appClass(appData, connection.data);
const isStillVerified =
await appInstance.authenticationClient.isStillVerified();

View File

@@ -1,3 +1,4 @@
import App from '../models/app';
import Flow from '../models/flow';
import Step from '../models/step';
import Execution from '../models/execution';
@@ -27,9 +28,11 @@ class Processor {
let fetchedData;
for await (const step of steps) {
const appData = App.findOneByKey(step.appKey);
if (step.type.toString() === 'trigger') {
const appClass = (await import(`../apps/${step.appKey}`)).default;
const appInstance = new appClass(step.connection.data);
const appInstance = new appClass(appData, step.connection.data);
fetchedData = await appInstance.triggers[step.key].run();
previousExecutionStep = await execution
@@ -41,7 +44,11 @@ class Processor {
});
} else {
const appClass = (await import(`../apps/${step.appKey}`)).default;
const appInstance = new appClass(step.connection.data, step.parameters);
const appInstance = new appClass(
appData,
step.connection.data,
step.parameters
);
fetchedData = await appInstance.actions[step.key].run();
previousExecutionStep = await execution