refactor: Pass app data within the constructor of an app
This commit is contained in:
@@ -1,24 +1,25 @@
|
|||||||
import { URLSearchParams } from 'url';
|
import { URLSearchParams } from 'url';
|
||||||
import axios, { AxiosInstance } from 'axios';
|
import axios, { AxiosInstance } from 'axios';
|
||||||
import App from '../../models/app';
|
|
||||||
import Field from '../../types/field';
|
import Field from '../../types/field';
|
||||||
|
|
||||||
export default class Authentication {
|
export default class Authentication {
|
||||||
client?: any
|
client?: any;
|
||||||
connectionData: any
|
connectionData: any;
|
||||||
appData: any
|
appData: any;
|
||||||
scope: string[] = ['identify', 'email']
|
scope: string[] = ['identify', 'email'];
|
||||||
httpClient: AxiosInstance = axios.create({
|
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.connectionData = connectionData;
|
||||||
this.appData = App.findOneByKey('discord');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get oauthRedirectUrl() {
|
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() {
|
async createAuthData() {
|
||||||
@@ -44,7 +45,10 @@ export default class Authentication {
|
|||||||
code: this.connectionData.oauthVerifier,
|
code: this.connectionData.oauthVerifier,
|
||||||
grant_type: 'authorization_code',
|
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 {
|
const {
|
||||||
access_token: accessToken,
|
access_token: accessToken,
|
||||||
@@ -84,7 +88,7 @@ export default class Authentication {
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch {
|
} catch {
|
||||||
return false
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,9 +1,9 @@
|
|||||||
import Authentication from './authentication';
|
import Authentication from './authentication';
|
||||||
|
|
||||||
export default class Discord {
|
export default class Discord {
|
||||||
authenticationClient: any
|
authenticationClient: any;
|
||||||
|
|
||||||
constructor(connectionData: any) {
|
constructor(appData: any, connectionData: any) {
|
||||||
this.authenticationClient = new Authentication(connectionData);
|
this.authenticationClient = new Authentication(appData, connectionData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,47 +1,50 @@
|
|||||||
import { google as GoogleApi } from 'googleapis';
|
import { google as GoogleApi } from 'googleapis';
|
||||||
import App from '../../models/app';
|
|
||||||
import Field from '../../types/field';
|
import Field from '../../types/field';
|
||||||
|
|
||||||
export default class Authentication {
|
export default class Authentication {
|
||||||
oauthClient: any
|
oauthClient: any;
|
||||||
connectionData: any
|
connectionData: any;
|
||||||
appData: any
|
appData: any;
|
||||||
|
|
||||||
scopes: string[] = [
|
scopes: string[] = [
|
||||||
'https://www.googleapis.com/auth/datastore',
|
'https://www.googleapis.com/auth/datastore',
|
||||||
'https://www.googleapis.com/auth/firebase',
|
'https://www.googleapis.com/auth/firebase',
|
||||||
'https://www.googleapis.com/auth/user.emails.read',
|
'https://www.googleapis.com/auth/user.emails.read',
|
||||||
'profile',
|
'profile',
|
||||||
]
|
];
|
||||||
|
|
||||||
constructor(connectionData: any) {
|
constructor(appData: any, connectionData: any) {
|
||||||
this.appData = App.findOneByKey('firebase');
|
this.appData = appData;
|
||||||
this.connectionData = connectionData;
|
this.connectionData = connectionData;
|
||||||
|
|
||||||
this.oauthClient = new GoogleApi.auth.OAuth2(
|
this.oauthClient = new GoogleApi.auth.OAuth2(
|
||||||
connectionData.consumerKey,
|
connectionData.consumerKey,
|
||||||
connectionData.consumerSecret,
|
connectionData.consumerSecret,
|
||||||
this.oauthRedirectUrl,
|
this.oauthRedirectUrl
|
||||||
);
|
);
|
||||||
|
|
||||||
GoogleApi.options({ auth: this.oauthClient });
|
GoogleApi.options({ auth: this.oauthClient });
|
||||||
}
|
}
|
||||||
|
|
||||||
get oauthRedirectUrl() {
|
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() {
|
async createAuthData() {
|
||||||
const url = this.oauthClient.generateAuthUrl({
|
const url = this.oauthClient.generateAuthUrl({
|
||||||
access_type: 'offline',
|
access_type: 'offline',
|
||||||
scope: this.scopes
|
scope: this.scopes,
|
||||||
});
|
});
|
||||||
|
|
||||||
return { url };
|
return { url };
|
||||||
}
|
}
|
||||||
|
|
||||||
async verifyCredentials() {
|
async verifyCredentials() {
|
||||||
const { tokens } = await this.oauthClient.getToken(this.connectionData.oauthVerifier);
|
const { tokens } = await this.oauthClient.getToken(
|
||||||
|
this.connectionData.oauthVerifier
|
||||||
|
);
|
||||||
this.oauthClient.setCredentials(tokens);
|
this.oauthClient.setCredentials(tokens);
|
||||||
|
|
||||||
const people = GoogleApi.people('v1');
|
const people = GoogleApi.people('v1');
|
||||||
@@ -52,7 +55,9 @@ export default class Authentication {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const { emailAddresses, resourceName: userId } = data;
|
const { emailAddresses, resourceName: userId } = data;
|
||||||
const primaryEmailAddress = emailAddresses.find(emailAddress => emailAddress.metadata.primary);
|
const primaryEmailAddress = emailAddresses.find(
|
||||||
|
(emailAddress) => emailAddress.metadata.primary
|
||||||
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
consumerKey: this.connectionData.consumerKey,
|
consumerKey: this.connectionData.consumerKey,
|
||||||
@@ -64,7 +69,7 @@ export default class Authentication {
|
|||||||
scope: tokens.scope,
|
scope: tokens.scope,
|
||||||
screenName: primaryEmailAddress.value,
|
screenName: primaryEmailAddress.value,
|
||||||
userId,
|
userId,
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async isStillVerified() {
|
async isStillVerified() {
|
||||||
@@ -72,7 +77,7 @@ export default class Authentication {
|
|||||||
await this.oauthClient.getTokenInfo(this.connectionData.accessToken);
|
await this.oauthClient.getTokenInfo(this.connectionData.accessToken);
|
||||||
return true;
|
return true;
|
||||||
} catch {
|
} catch {
|
||||||
return false
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,9 +1,9 @@
|
|||||||
import Authentication from './authentication'
|
import Authentication from './authentication';
|
||||||
|
|
||||||
export default class Firebase {
|
export default class Firebase {
|
||||||
authenticationClient: any
|
authenticationClient: any;
|
||||||
|
|
||||||
constructor(connectionData: any) {
|
constructor(appData: any, connectionData: any) {
|
||||||
this.authenticationClient = new Authentication(connectionData);
|
this.authenticationClient = new Authentication(appData, connectionData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,15 +1,17 @@
|
|||||||
import FlickrApi from 'flickr-sdk';
|
import FlickrApi from 'flickr-sdk';
|
||||||
import App from '../../models/app';
|
|
||||||
import Field from '../../types/field';
|
import Field from '../../types/field';
|
||||||
|
|
||||||
export default class Authentication {
|
export default class Authentication {
|
||||||
oauthClient: any
|
oauthClient: any;
|
||||||
client: any
|
client: any;
|
||||||
connectionData: any
|
connectionData: any;
|
||||||
appData: any
|
appData: any;
|
||||||
|
|
||||||
constructor(connectionData: any) {
|
constructor(appData: any, connectionData: any) {
|
||||||
this.oauthClient = new FlickrApi.OAuth(connectionData.consumerKey, connectionData.consumerSecret);
|
this.oauthClient = new FlickrApi.OAuth(
|
||||||
|
connectionData.consumerKey,
|
||||||
|
connectionData.consumerSecret
|
||||||
|
);
|
||||||
|
|
||||||
if (connectionData.accessToken && connectionData.accessSecret) {
|
if (connectionData.accessToken && connectionData.accessSecret) {
|
||||||
this.client = new FlickrApi(
|
this.client = new FlickrApi(
|
||||||
@@ -17,21 +19,26 @@ export default class Authentication {
|
|||||||
connectionData.consumerKey,
|
connectionData.consumerKey,
|
||||||
connectionData.consumerSecret,
|
connectionData.consumerSecret,
|
||||||
connectionData.accessToken,
|
connectionData.accessToken,
|
||||||
connectionData.accessSecret,
|
connectionData.accessSecret
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.connectionData = connectionData;
|
this.connectionData = connectionData;
|
||||||
this.appData = App.findOneByKey('flickr');
|
this.appData = appData;
|
||||||
}
|
}
|
||||||
|
|
||||||
async createAuthData() {
|
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 callbackUrl = appFields.value;
|
||||||
|
|
||||||
const oauthData = (await this.oauthClient.request(callbackUrl)).body;
|
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 {
|
return {
|
||||||
accessToken: oauthData.oauth_token,
|
accessToken: oauthData.oauth_token,
|
||||||
@@ -41,11 +48,13 @@ export default class Authentication {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async verifyCredentials() {
|
async verifyCredentials() {
|
||||||
const verifiedCredentials = (await this.oauthClient.verify(
|
const verifiedCredentials = (
|
||||||
|
await this.oauthClient.verify(
|
||||||
this.connectionData.accessToken,
|
this.connectionData.accessToken,
|
||||||
this.connectionData.oauthVerifier,
|
this.connectionData.oauthVerifier,
|
||||||
this.connectionData.accessSecret
|
this.connectionData.accessSecret
|
||||||
)).body;
|
)
|
||||||
|
).body;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
consumerKey: this.connectionData.consumerKey,
|
consumerKey: this.connectionData.consumerKey,
|
||||||
@@ -53,8 +62,8 @@ export default class Authentication {
|
|||||||
accessToken: verifiedCredentials.oauth_token,
|
accessToken: verifiedCredentials.oauth_token,
|
||||||
accessSecret: verifiedCredentials.oauth_token_secret,
|
accessSecret: verifiedCredentials.oauth_token_secret,
|
||||||
userId: verifiedCredentials.user_nsid,
|
userId: verifiedCredentials.user_nsid,
|
||||||
screenName: verifiedCredentials.fullname
|
screenName: verifiedCredentials.fullname,
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async isStillVerified() {
|
async isStillVerified() {
|
||||||
|
@@ -1,9 +1,9 @@
|
|||||||
import Authentication from './authentication'
|
import Authentication from './authentication';
|
||||||
|
|
||||||
export default class Flickr {
|
export default class Flickr {
|
||||||
authenticationClient: any
|
authenticationClient: any;
|
||||||
|
|
||||||
constructor(connectionData: any) {
|
constructor(appData: any, connectionData: any) {
|
||||||
this.authenticationClient = new Authentication(connectionData);
|
this.authenticationClient = new Authentication(appData, connectionData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -3,26 +3,27 @@ import {
|
|||||||
exchangeWebFlowCode,
|
exchangeWebFlowCode,
|
||||||
checkToken,
|
checkToken,
|
||||||
} from '@octokit/oauth-methods';
|
} from '@octokit/oauth-methods';
|
||||||
import App from '../../models/app';
|
|
||||||
import Field from '../../types/field';
|
import Field from '../../types/field';
|
||||||
|
|
||||||
export default class Authentication {
|
export default class Authentication {
|
||||||
connectionData: any
|
connectionData: any;
|
||||||
appData: any
|
appData: any;
|
||||||
scopes: string[] = ['repo']
|
scopes: string[] = ['repo'];
|
||||||
|
|
||||||
constructor(connectionData: any) {
|
constructor(appData: any, connectionData: any) {
|
||||||
this.connectionData = connectionData;
|
this.connectionData = connectionData;
|
||||||
this.appData = App.findOneByKey('github');
|
this.appData = appData;
|
||||||
}
|
}
|
||||||
|
|
||||||
get oauthRedirectUrl(): string {
|
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 } {
|
async createAuthData(): { url: string } {
|
||||||
const { url } = await getWebFlowAuthorizationUrl({
|
const { url } = await getWebFlowAuthorizationUrl({
|
||||||
clientType: "oauth-app",
|
clientType: 'oauth-app',
|
||||||
clientId: this.connectionData.consumerKey,
|
clientId: this.connectionData.consumerKey,
|
||||||
redirectUrl: this.oauthRedirectUrl,
|
redirectUrl: this.oauthRedirectUrl,
|
||||||
scopes: this.scopes,
|
scopes: this.scopes,
|
||||||
@@ -35,7 +36,7 @@ export default class Authentication {
|
|||||||
|
|
||||||
async verifyCredentials(): any {
|
async verifyCredentials(): any {
|
||||||
const { data } = await exchangeWebFlowCode({
|
const { data } = await exchangeWebFlowCode({
|
||||||
clientType: "oauth-app",
|
clientType: 'oauth-app',
|
||||||
clientId: this.connectionData.consumerKey,
|
clientId: this.connectionData.consumerKey,
|
||||||
clientSecret: this.connectionData.consumerSecret,
|
clientSecret: this.connectionData.consumerSecret,
|
||||||
code: this.connectionData.oauthVerifier,
|
code: this.connectionData.oauthVerifier,
|
||||||
@@ -53,12 +54,12 @@ export default class Authentication {
|
|||||||
tokenType: data.token_type,
|
tokenType: data.token_type,
|
||||||
userId: tokenInfo.data.user.id,
|
userId: tokenInfo.data.user.id,
|
||||||
screenName: tokenInfo.data.user.login,
|
screenName: tokenInfo.data.user.login,
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async getTokenInfo() {
|
async getTokenInfo() {
|
||||||
return checkToken({
|
return checkToken({
|
||||||
clientType: "oauth-app",
|
clientType: 'oauth-app',
|
||||||
clientId: this.connectionData.consumerKey,
|
clientId: this.connectionData.consumerKey,
|
||||||
clientSecret: this.connectionData.consumerSecret,
|
clientSecret: this.connectionData.consumerSecret,
|
||||||
token: this.connectionData.accessToken,
|
token: this.connectionData.accessToken,
|
||||||
|
@@ -1,9 +1,9 @@
|
|||||||
import Authentication from './authentication';
|
import Authentication from './authentication';
|
||||||
|
|
||||||
export default class Github {
|
export default class Github {
|
||||||
authenticationClient: any
|
authenticationClient: any;
|
||||||
|
|
||||||
constructor(connectionData: any) {
|
constructor(appData: any, connectionData: any) {
|
||||||
this.authenticationClient = new Authentication(connectionData);
|
this.authenticationClient = new Authentication(appData, connectionData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,12 +1,11 @@
|
|||||||
import { Client } from 'pg';
|
import { Client } from 'pg';
|
||||||
import App from '../../models/app';
|
|
||||||
|
|
||||||
export default class Authentication {
|
export default class Authentication {
|
||||||
client: any
|
client: any;
|
||||||
connectionData: any
|
connectionData: any;
|
||||||
appData: any
|
appData: any;
|
||||||
|
|
||||||
constructor(connectionData: any) {
|
constructor(appData: any, connectionData: any) {
|
||||||
this.client = new Client({
|
this.client = new Client({
|
||||||
host: connectionData.host,
|
host: connectionData.host,
|
||||||
port: connectionData.port,
|
port: connectionData.port,
|
||||||
@@ -14,26 +13,26 @@ export default class Authentication {
|
|||||||
user: connectionData.username,
|
user: connectionData.username,
|
||||||
password: connectionData.password,
|
password: connectionData.password,
|
||||||
ssl: connectionData.ssl,
|
ssl: connectionData.ssl,
|
||||||
})
|
});
|
||||||
|
|
||||||
this.connectionData = connectionData;
|
this.connectionData = connectionData;
|
||||||
this.appData = App.findOneByKey('postgresql');
|
this.appData = appData;
|
||||||
}
|
}
|
||||||
|
|
||||||
async verifyCredentials() {
|
async verifyCredentials() {
|
||||||
await this.client.connect()
|
await this.client.connect();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
screenName: this.connectionData.database
|
screenName: this.connectionData.database,
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async isStillVerified() {
|
async isStillVerified() {
|
||||||
try {
|
try {
|
||||||
await this.client.connect()
|
await this.client.connect();
|
||||||
return true;
|
return true;
|
||||||
} catch(error) {
|
} catch (error) {
|
||||||
return false
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,9 +1,9 @@
|
|||||||
import Authentication from './authentication'
|
import Authentication from './authentication';
|
||||||
|
|
||||||
export default class PostgreSQL {
|
export default class PostgreSQL {
|
||||||
authenticationClient: any;
|
authenticationClient: any;
|
||||||
|
|
||||||
constructor(connectionData: any) {
|
constructor(appData: any, connectionData: any) {
|
||||||
this.authenticationClient = new Authentication(connectionData);
|
this.authenticationClient = new Authentication(appData, connectionData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,12 +1,11 @@
|
|||||||
import nodemailer from 'nodemailer';
|
import nodemailer from 'nodemailer';
|
||||||
import App from '../../models/app';
|
|
||||||
|
|
||||||
export default class Authentication {
|
export default class Authentication {
|
||||||
client: any
|
client: any;
|
||||||
connectionData: any
|
connectionData: any;
|
||||||
appData: any
|
appData: any;
|
||||||
|
|
||||||
constructor(connectionData: any) {
|
constructor(appData: any, connectionData: any) {
|
||||||
this.client = nodemailer.createTransport({
|
this.client = nodemailer.createTransport({
|
||||||
host: connectionData.host,
|
host: connectionData.host,
|
||||||
port: connectionData.port,
|
port: connectionData.port,
|
||||||
@@ -18,23 +17,23 @@ export default class Authentication {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.connectionData = connectionData;
|
this.connectionData = connectionData;
|
||||||
this.appData = App.findOneByKey('smtp');
|
this.appData = appData;
|
||||||
}
|
}
|
||||||
|
|
||||||
async verifyCredentials() {
|
async verifyCredentials() {
|
||||||
await this.client.verify()
|
await this.client.verify();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
screenName: this.connectionData.username
|
screenName: this.connectionData.username,
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async isStillVerified() {
|
async isStillVerified() {
|
||||||
try {
|
try {
|
||||||
await this.client.verify()
|
await this.client.verify();
|
||||||
return true;
|
return true;
|
||||||
} catch(error) {
|
} catch (error) {
|
||||||
return false
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,9 +1,9 @@
|
|||||||
import Authentication from './authentication';
|
import Authentication from './authentication';
|
||||||
|
|
||||||
export default class SMTP {
|
export default class SMTP {
|
||||||
authenticationClient: any
|
authenticationClient: any;
|
||||||
|
|
||||||
constructor(connectionData: any) {
|
constructor(appData: any, connectionData: any) {
|
||||||
this.authenticationClient = new Authentication(connectionData);
|
this.authenticationClient = new Authentication(appData, connectionData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,31 +1,33 @@
|
|||||||
import TwilioApi from 'twilio';
|
import TwilioApi from 'twilio';
|
||||||
import App from '../../models/app';
|
|
||||||
|
|
||||||
export default class Authentication {
|
export default class Authentication {
|
||||||
client: any
|
client: any;
|
||||||
connectionData: any
|
connectionData: any;
|
||||||
appData: any
|
appData: any;
|
||||||
|
|
||||||
constructor(connectionData: any) {
|
constructor(appData: any, connectionData: any) {
|
||||||
this.client = TwilioApi(connectionData.accountSid, connectionData.authToken);
|
this.client = TwilioApi(
|
||||||
|
connectionData.accountSid,
|
||||||
|
connectionData.authToken
|
||||||
|
);
|
||||||
|
|
||||||
this.connectionData = connectionData;
|
this.connectionData = connectionData;
|
||||||
this.appData = App.findOneByKey('twilio');
|
this.appData = appData;
|
||||||
}
|
}
|
||||||
|
|
||||||
async verifyCredentials() {
|
async verifyCredentials() {
|
||||||
await this.verify();
|
await this.verify();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
screenName: this.connectionData.accountSid
|
screenName: this.connectionData.accountSid,
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async verify() {
|
async verify() {
|
||||||
try {
|
try {
|
||||||
await this.client.keys.list({ limit: 1 })
|
await this.client.keys.list({ limit: 1 });
|
||||||
return true;
|
return true;
|
||||||
} catch(error) {
|
} catch (error) {
|
||||||
// Test credentials throw HTTP 403 and thus, we need to have an exception.
|
// Test credentials throw HTTP 403 and thus, we need to have an exception.
|
||||||
return error?.status === 403;
|
return error?.status === 403;
|
||||||
}
|
}
|
||||||
|
@@ -3,7 +3,7 @@ import Authentication from './authentication';
|
|||||||
export default class Twilio {
|
export default class Twilio {
|
||||||
authenticationClient: any;
|
authenticationClient: any;
|
||||||
|
|
||||||
constructor(connectionData: any) {
|
constructor(appData: any, connectionData: any) {
|
||||||
this.authenticationClient = new Authentication(connectionData);
|
this.authenticationClient = new Authentication(appData, connectionData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,5 @@
|
|||||||
import TwitchApi from 'twitch-js';
|
import TwitchApi from 'twitch-js';
|
||||||
import fetchUtil from 'twitch-js/lib/utils/fetch';
|
import fetchUtil from 'twitch-js/lib/utils/fetch';
|
||||||
import App from '../../models/app';
|
|
||||||
import Field from '../../types/field';
|
import Field from '../../types/field';
|
||||||
|
|
||||||
type TwitchTokenResponse = {
|
type TwitchTokenResponse = {
|
||||||
@@ -11,13 +10,13 @@ type TwitchTokenResponse = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export default class Authentication {
|
export default class Authentication {
|
||||||
client: any
|
client: any;
|
||||||
connectionData: any
|
connectionData: any;
|
||||||
appData: any
|
appData: any;
|
||||||
|
|
||||||
constructor(connectionData: any) {
|
constructor(appData: any, connectionData: any) {
|
||||||
this.connectionData = connectionData;
|
this.connectionData = connectionData;
|
||||||
this.appData = App.findOneByKey('twitch');
|
this.appData = appData;
|
||||||
|
|
||||||
if (this.clientOptions.token) {
|
if (this.clientOptions.token) {
|
||||||
this.client = new TwitchApi(this.clientOptions);
|
this.client = new TwitchApi(this.clientOptions);
|
||||||
@@ -28,12 +27,14 @@ export default class Authentication {
|
|||||||
return {
|
return {
|
||||||
token: this.connectionData.accessToken,
|
token: this.connectionData.accessToken,
|
||||||
clientId: this.connectionData.consumerKey,
|
clientId: this.connectionData.consumerKey,
|
||||||
log: { enabled: true }
|
log: { enabled: true },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
get oauthRedirectUrl() {
|
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() {
|
async createAuthData() {
|
||||||
@@ -43,23 +44,26 @@ export default class Authentication {
|
|||||||
redirect_uri: this.oauthRedirectUrl,
|
redirect_uri: this.oauthRedirectUrl,
|
||||||
response_type: 'code',
|
response_type: 'code',
|
||||||
scope: 'user:read:email',
|
scope: 'user:read:email',
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return { url };
|
return { url };
|
||||||
}
|
}
|
||||||
|
|
||||||
async verifyCredentials() {
|
async verifyCredentials() {
|
||||||
const verifiedCredentials = await fetchUtil('https://id.twitch.tv/oauth2/token', {
|
const verifiedCredentials = (await fetchUtil(
|
||||||
|
'https://id.twitch.tv/oauth2/token',
|
||||||
|
{
|
||||||
method: 'post',
|
method: 'post',
|
||||||
search: {
|
search: {
|
||||||
client_id: this.connectionData.consumerKey,
|
client_id: this.connectionData.consumerKey,
|
||||||
client_secret: this.connectionData.consumerSecret,
|
client_secret: this.connectionData.consumerSecret,
|
||||||
code: this.connectionData.oauthVerifier,
|
code: this.connectionData.oauthVerifier,
|
||||||
grant_type: 'authorization_code',
|
grant_type: 'authorization_code',
|
||||||
redirect_uri: this.oauthRedirectUrl
|
redirect_uri: this.oauthRedirectUrl,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}) as TwitchTokenResponse;
|
)) as TwitchTokenResponse;
|
||||||
|
|
||||||
this.connectionData.accessToken = verifiedCredentials.accessToken;
|
this.connectionData.accessToken = verifiedCredentials.accessToken;
|
||||||
|
|
||||||
@@ -77,7 +81,7 @@ export default class Authentication {
|
|||||||
tokenType: verifiedCredentials.tokenType,
|
tokenType: verifiedCredentials.tokenType,
|
||||||
userId: user.id,
|
userId: user.id,
|
||||||
screenName: user.displayName,
|
screenName: user.displayName,
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async isStillVerified() {
|
async isStillVerified() {
|
||||||
@@ -90,7 +94,7 @@ export default class Authentication {
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return false
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,9 +1,9 @@
|
|||||||
import Authentication from './authentication';
|
import Authentication from './authentication';
|
||||||
|
|
||||||
export default class Twitch {
|
export default class Twitch {
|
||||||
authenticationClient: any
|
authenticationClient: any;
|
||||||
|
|
||||||
constructor(connectionData: any) {
|
constructor(appData: any, connectionData: any) {
|
||||||
this.authenticationClient = new Authentication(connectionData);
|
this.authenticationClient = new Authentication(appData, connectionData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,26 +1,27 @@
|
|||||||
import TwitterApi from 'twitter-api-v2';
|
import TwitterApi from 'twitter-api-v2';
|
||||||
import App from '../../models/app';
|
|
||||||
import Field from '../../types/field';
|
import Field from '../../types/field';
|
||||||
|
|
||||||
export default class Authentication {
|
export default class Authentication {
|
||||||
client: any
|
client: any;
|
||||||
connectionData: any
|
connectionData: any;
|
||||||
appData: any
|
appData: any;
|
||||||
|
|
||||||
|
constructor(appData: any, connectionData: any) {
|
||||||
|
this.appData = appData;
|
||||||
|
this.connectionData = connectionData;
|
||||||
|
|
||||||
constructor(connectionData: any) {
|
|
||||||
this.client = new TwitterApi({
|
this.client = new TwitterApi({
|
||||||
appKey: connectionData.consumerKey,
|
appKey: connectionData.consumerKey,
|
||||||
appSecret: connectionData.consumerSecret,
|
appSecret: connectionData.consumerSecret,
|
||||||
accessToken: connectionData.accessToken,
|
accessToken: connectionData.accessToken,
|
||||||
accessSecret: connectionData.accessSecret
|
accessSecret: connectionData.accessSecret,
|
||||||
});
|
});
|
||||||
|
|
||||||
this.connectionData = connectionData;
|
|
||||||
this.appData = App.findOneByKey('twitter');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async createAuthData() {
|
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 callbackUrl = appFields.value;
|
||||||
|
|
||||||
const authLink = await this.client.generateAuthLink(callbackUrl);
|
const authLink = await this.client.generateAuthLink(callbackUrl);
|
||||||
@@ -29,11 +30,13 @@ export default class Authentication {
|
|||||||
url: authLink.url,
|
url: authLink.url,
|
||||||
accessToken: authLink.oauth_token,
|
accessToken: authLink.oauth_token,
|
||||||
accessSecret: authLink.oauth_token_secret,
|
accessSecret: authLink.oauth_token_secret,
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async verifyCredentials() {
|
async verifyCredentials() {
|
||||||
const verifiedCredentials = await this.client.login(this.connectionData.oauthVerifier)
|
const verifiedCredentials = await this.client.login(
|
||||||
|
this.connectionData.oauthVerifier
|
||||||
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
consumerKey: this.connectionData.consumerKey,
|
consumerKey: this.connectionData.consumerKey,
|
||||||
@@ -41,8 +44,8 @@ export default class Authentication {
|
|||||||
accessToken: verifiedCredentials.accessToken,
|
accessToken: verifiedCredentials.accessToken,
|
||||||
accessSecret: verifiedCredentials.accessSecret,
|
accessSecret: verifiedCredentials.accessSecret,
|
||||||
userId: verifiedCredentials.userId,
|
userId: verifiedCredentials.userId,
|
||||||
screenName: verifiedCredentials.screenName
|
screenName: verifiedCredentials.screenName,
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async isStillVerified() {
|
async isStillVerified() {
|
||||||
@@ -50,7 +53,7 @@ export default class Authentication {
|
|||||||
await this.client.currentUser();
|
await this.client.currentUser();
|
||||||
return true;
|
return true;
|
||||||
} catch {
|
} catch {
|
||||||
return false
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -7,8 +7,8 @@ export default class Twitter {
|
|||||||
triggers: any;
|
triggers: any;
|
||||||
actions: any;
|
actions: any;
|
||||||
|
|
||||||
constructor(connectionData: any, parameters: any) {
|
constructor(appData: any, connectionData: any, parameters: any) {
|
||||||
this.authenticationClient = new Authentication(connectionData);
|
this.authenticationClient = new Authentication(appData, connectionData);
|
||||||
this.triggers = new Triggers(connectionData);
|
this.triggers = new Triggers(connectionData);
|
||||||
this.actions = new Actions(connectionData, parameters);
|
this.actions = new Actions(connectionData, parameters);
|
||||||
}
|
}
|
||||||
|
@@ -1,13 +1,11 @@
|
|||||||
import { URLSearchParams } from 'url';
|
import { URLSearchParams } from 'url';
|
||||||
import axios, { AxiosInstance } from
|
import axios, { AxiosInstance } from 'axios';
|
||||||
'axios';
|
|
||||||
import App from '../../models/app';
|
|
||||||
import Field from '../../types/field';
|
import Field from '../../types/field';
|
||||||
|
|
||||||
export default class Authentication {
|
export default class Authentication {
|
||||||
client?: any
|
client?: any;
|
||||||
connectionData: any
|
connectionData: any;
|
||||||
appData: any
|
appData: any;
|
||||||
scope: string[] = [
|
scope: string[] = [
|
||||||
'forms:read',
|
'forms:read',
|
||||||
'forms:write',
|
'forms:write',
|
||||||
@@ -16,18 +14,20 @@ export default class Authentication {
|
|||||||
'responses:read',
|
'responses:read',
|
||||||
'accounts:read',
|
'accounts:read',
|
||||||
'workspaces:read',
|
'workspaces:read',
|
||||||
]
|
];
|
||||||
httpClient: AxiosInstance = axios.create({
|
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.connectionData = connectionData;
|
||||||
this.appData = App.findOneByKey('typeform');
|
this.appData = appData;
|
||||||
}
|
}
|
||||||
|
|
||||||
get oauthRedirectUrl() {
|
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() {
|
async createAuthData() {
|
||||||
@@ -51,7 +51,10 @@ export default class Authentication {
|
|||||||
redirect_uri: this.oauthRedirectUrl,
|
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 {
|
const {
|
||||||
access_token: accessToken,
|
access_token: accessToken,
|
||||||
@@ -87,7 +90,7 @@ export default class Authentication {
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch {
|
} catch {
|
||||||
return false
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -3,7 +3,7 @@ import Authentication from './authentication';
|
|||||||
export default class Typeform {
|
export default class Typeform {
|
||||||
authenticationClient: any;
|
authenticationClient: any;
|
||||||
|
|
||||||
constructor(connectionData: any) {
|
constructor(appData: any, connectionData: any) {
|
||||||
this.authenticationClient = new Authentication(connectionData);
|
this.authenticationClient = new Authentication(appData, connectionData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
import { GraphQLNonNull, GraphQLString } from 'graphql';
|
import { GraphQLNonNull, GraphQLString } from 'graphql';
|
||||||
import authLinkType from '../types/auth-link';
|
import authLinkType from '../types/auth-link';
|
||||||
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
|
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
|
||||||
|
import App from '../../models/app';
|
||||||
|
|
||||||
type Params = {
|
type Params = {
|
||||||
id: string;
|
id: string;
|
||||||
@@ -18,8 +19,9 @@ const createAuthDataResolver = async (
|
|||||||
.throwIfNotFound();
|
.throwIfNotFound();
|
||||||
|
|
||||||
const appClass = (await import(`../../apps/${connection.key}`)).default;
|
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,
|
consumerKey: connection.data.consumerKey,
|
||||||
consumerSecret: connection.data.consumerSecret,
|
consumerSecret: connection.data.consumerSecret,
|
||||||
});
|
});
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
import { GraphQLString, GraphQLNonNull } from 'graphql';
|
import { GraphQLString, GraphQLNonNull } from 'graphql';
|
||||||
import connectionType from '../types/connection';
|
import connectionType from '../types/connection';
|
||||||
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
|
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
|
||||||
|
import App from '../../models/app';
|
||||||
|
|
||||||
type Params = {
|
type Params = {
|
||||||
id: string;
|
id: string;
|
||||||
@@ -18,8 +19,9 @@ const verifyConnectionResolver = async (
|
|||||||
.throwIfNotFound();
|
.throwIfNotFound();
|
||||||
|
|
||||||
const appClass = (await import(`../../apps/${connection.key}`)).default;
|
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 =
|
const verifiedCredentials =
|
||||||
await appInstance.authenticationClient.verifyCredentials();
|
await appInstance.authenticationClient.verifyCredentials();
|
||||||
|
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
import { GraphQLString, GraphQLNonNull } from 'graphql';
|
import { GraphQLString, GraphQLNonNull } from 'graphql';
|
||||||
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
|
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
|
||||||
import connectionType from '../types/connection';
|
import connectionType from '../types/connection';
|
||||||
|
import App from '../../models/app';
|
||||||
|
|
||||||
type Params = {
|
type Params = {
|
||||||
id: string;
|
id: string;
|
||||||
@@ -18,8 +19,9 @@ const testConnectionResolver = async (
|
|||||||
.throwIfNotFound();
|
.throwIfNotFound();
|
||||||
|
|
||||||
const appClass = (await import(`../../apps/${connection.key}`)).default;
|
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 =
|
const isStillVerified =
|
||||||
await appInstance.authenticationClient.isStillVerified();
|
await appInstance.authenticationClient.isStillVerified();
|
||||||
|
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
import App from '../models/app';
|
||||||
import Flow from '../models/flow';
|
import Flow from '../models/flow';
|
||||||
import Step from '../models/step';
|
import Step from '../models/step';
|
||||||
import Execution from '../models/execution';
|
import Execution from '../models/execution';
|
||||||
@@ -27,9 +28,11 @@ class Processor {
|
|||||||
let fetchedData;
|
let fetchedData;
|
||||||
|
|
||||||
for await (const step of steps) {
|
for await (const step of steps) {
|
||||||
|
const appData = App.findOneByKey(step.appKey);
|
||||||
|
|
||||||
if (step.type.toString() === 'trigger') {
|
if (step.type.toString() === 'trigger') {
|
||||||
const appClass = (await import(`../apps/${step.appKey}`)).default;
|
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();
|
fetchedData = await appInstance.triggers[step.key].run();
|
||||||
|
|
||||||
previousExecutionStep = await execution
|
previousExecutionStep = await execution
|
||||||
@@ -41,7 +44,11 @@ class Processor {
|
|||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
const appClass = (await import(`../apps/${step.appKey}`)).default;
|
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();
|
fetchedData = await appInstance.actions[step.key].run();
|
||||||
|
|
||||||
previousExecutionStep = await execution
|
previousExecutionStep = await execution
|
||||||
|
Reference in New Issue
Block a user