refactor: Generate authentication steps from auth properties

This commit is contained in:
Faruk AYDIN
2022-11-10 18:49:49 +01:00
parent 6d3606abf0
commit 10e2794ff9
15 changed files with 115 additions and 599 deletions

View File

@@ -27,42 +27,6 @@ export default {
clickToCopy: false, clickToCopy: false,
}, },
], ],
authenticationSteps: [
{
type: 'mutation' as const,
name: 'createConnection',
arguments: [
{
name: 'key',
value: '{key}',
},
{
name: 'formattedData',
value: null,
properties: [
{
name: 'screenName',
value: '{fields.screenName}',
},
{
name: 'authenticationKey',
value: '{fields.authenticationKey}',
},
],
},
],
},
{
type: 'mutation' as const,
name: 'verifyConnection',
arguments: [
{
name: 'id',
value: '{createConnection.id}',
},
],
},
],
verifyCredentials, verifyCredentials,
isStillVerified, isStillVerified,

View File

@@ -3,6 +3,7 @@ import verifyCredentials from './verify-credentials';
import isStillVerified from './is-still-verified'; import isStillVerified from './is-still-verified';
export default { export default {
generateAuthUrl: true,
fields: [ fields: [
{ {
key: 'oAuthRedirectUrl', key: 'oAuthRedirectUrl',
@@ -54,86 +55,6 @@ export default {
clickToCopy: false, clickToCopy: false,
}, },
], ],
authenticationSteps: [
{
type: 'mutation' as const,
name: 'createConnection',
arguments: [
{
name: 'key',
value: '{key}',
},
{
name: 'formattedData',
value: null,
properties: [
{
name: 'consumerKey',
value: '{fields.consumerKey}',
},
{
name: 'consumerSecret',
value: '{fields.consumerSecret}',
},
{
name: 'botToken',
value: '{fields.botToken}',
},
],
},
],
},
{
type: 'mutation' as const,
name: 'createAuthData',
arguments: [
{
name: 'id',
value: '{createConnection.id}',
},
],
},
{
type: 'openWithPopup' as const,
name: 'openAuthPopup',
arguments: [
{
name: 'url',
value: '{createAuthData.url}',
},
],
},
{
type: 'mutation' as const,
name: 'updateConnection',
arguments: [
{
name: 'id',
value: '{createConnection.id}',
},
{
name: 'formattedData',
value: null,
properties: [
{
name: 'oauthVerifier',
value: '{openAuthPopup.code}',
},
],
},
],
},
{
type: 'mutation' as const,
name: 'verifyConnection',
arguments: [
{
name: 'id',
value: '{createConnection.id}',
},
],
},
],
createAuthData, createAuthData,
verifyCredentials, verifyCredentials,

View File

@@ -14,7 +14,7 @@ const verifyCredentials = async ($: IGlobalVariable) => {
response_type: 'code', response_type: 'code',
scope: scopes.join(' '), scope: scopes.join(' '),
client_secret: $.auth.data.consumerSecret as string, client_secret: $.auth.data.consumerSecret as string,
code: $.auth.data.oauthVerifier as string, code: $.auth.data.code as string,
grant_type: 'authorization_code', grant_type: 'authorization_code',
}); });
const { data: verifiedCredentials } = await $.http.post( const { data: verifiedCredentials } = await $.http.post(

View File

@@ -3,6 +3,7 @@ import verifyCredentials from './verify-credentials';
import isStillVerified from './is-still-verified'; import isStillVerified from './is-still-verified';
export default { export default {
generateAuthUrl: true,
fields: [ fields: [
{ {
key: 'oAuthRedirectUrl', key: 'oAuthRedirectUrl',
@@ -42,82 +43,6 @@ export default {
clickToCopy: false, clickToCopy: false,
}, },
], ],
authenticationSteps: [
{
type: 'mutation' as const,
name: 'createConnection',
arguments: [
{
name: 'key',
value: '{key}',
},
{
name: 'formattedData',
value: null,
properties: [
{
name: 'consumerKey',
value: '{fields.consumerKey}',
},
{
name: 'consumerSecret',
value: '{fields.consumerSecret}',
},
],
},
],
},
{
type: 'mutation' as const,
name: 'createAuthData',
arguments: [
{
name: 'id',
value: '{createConnection.id}',
},
],
},
{
type: 'openWithPopup' as const,
name: 'openAuthPopup',
arguments: [
{
name: 'url',
value: '{createAuthData.url}',
},
],
},
{
type: 'mutation' as const,
name: 'updateConnection',
arguments: [
{
name: 'id',
value: '{createConnection.id}',
},
{
name: 'formattedData',
value: null,
properties: [
{
name: 'oauthVerifier',
value: '{openAuthPopup.oauth_verifier}',
},
],
},
],
},
{
type: 'mutation' as const,
name: 'verifyConnection',
arguments: [
{
name: 'id',
value: '{createConnection.id}',
},
],
},
],
createAuthData, createAuthData,
verifyCredentials, verifyCredentials,

View File

@@ -3,7 +3,7 @@ import { URLSearchParams } from 'url';
const verifyCredentials = async ($: IGlobalVariable) => { const verifyCredentials = async ($: IGlobalVariable) => {
const response = await $.http.post( const response = await $.http.post(
`/oauth/access_token?oauth_verifier=${$.auth.data.oauthVerifier}&oauth_token=${$.auth.data.accessToken}`, `/oauth/access_token?oauth_verifier=${$.auth.data.oauth_verifier}&oauth_token=${$.auth.data.accessToken}`,
null null
); );

View File

@@ -3,6 +3,7 @@ import verifyCredentials from './verify-credentials';
import isStillVerified from './is-still-verified'; import isStillVerified from './is-still-verified';
export default { export default {
generateAuthUrl: true,
fields: [ fields: [
{ {
key: 'oAuthRedirectUrl', key: 'oAuthRedirectUrl',
@@ -42,82 +43,6 @@ export default {
clickToCopy: false, clickToCopy: false,
}, },
], ],
authenticationSteps: [
{
type: 'mutation' as const,
name: 'createConnection',
arguments: [
{
name: 'key',
value: '{key}',
},
{
name: 'formattedData',
value: null,
properties: [
{
name: 'consumerKey',
value: '{fields.consumerKey}',
},
{
name: 'consumerSecret',
value: '{fields.consumerSecret}',
},
],
},
],
},
{
type: 'mutation' as const,
name: 'createAuthData',
arguments: [
{
name: 'id',
value: '{createConnection.id}',
},
],
},
{
type: 'openWithPopup' as const,
name: 'openAuthPopup',
arguments: [
{
name: 'url',
value: '{createAuthData.url}',
},
],
},
{
type: 'mutation' as const,
name: 'updateConnection',
arguments: [
{
name: 'id',
value: '{createConnection.id}',
},
{
name: 'formattedData',
value: null,
properties: [
{
name: 'oauthVerifier',
value: '{openAuthPopup.code}',
},
],
},
],
},
{
type: 'mutation' as const,
name: 'verifyConnection',
arguments: [
{
name: 'id',
value: '{createConnection.id}',
},
],
},
],
createAuthData, createAuthData,
verifyCredentials, verifyCredentials,

View File

@@ -7,7 +7,7 @@ const verifyCredentials = async ($: IGlobalVariable) => {
{ {
client_id: $.auth.data.consumerKey, client_id: $.auth.data.consumerKey,
client_secret: $.auth.data.consumerSecret, client_secret: $.auth.data.consumerSecret,
code: $.auth.data.oauthVerifier, code: $.auth.data.code,
}, },
{ {
headers: { headers: {

View File

@@ -3,6 +3,7 @@ import verifyCredentials from './verify-credentials';
import isStillVerified from './is-still-verified'; import isStillVerified from './is-still-verified';
export default { export default {
generateAuthUrl: true,
fields: [ fields: [
{ {
key: 'oAuthRedirectUrl', key: 'oAuthRedirectUrl',
@@ -60,91 +61,6 @@ export default {
clickToCopy: false, clickToCopy: false,
}, },
], ],
authenticationSteps: [
{
step: 1,
type: 'mutation' as const,
name: 'createConnection',
arguments: [
{
name: 'key',
value: '{key}',
},
{
name: 'formattedData',
value: null,
properties: [
{
name: 'oauth2Url',
value: '{fields.oauth2Url}',
},
{
name: 'consumerKey',
value: '{fields.consumerKey}',
},
{
name: 'consumerSecret',
value: '{fields.consumerSecret}',
},
],
},
],
},
{
step: 2,
type: 'mutation' as const,
name: 'createAuthData',
arguments: [
{
name: 'id',
value: '{createConnection.id}',
},
],
},
{
step: 3,
type: 'openWithPopup' as const,
name: 'openAuthPopup',
arguments: [
{
name: 'url',
value: '{createAuthData.url}',
},
],
},
{
step: 4,
type: 'mutation' as const,
name: 'updateConnection',
arguments: [
{
name: 'id',
value: '{createConnection.id}',
},
{
name: 'formattedData',
value: null,
properties: [
{
name: 'code',
value: '{openAuthPopup.code}',
},
],
},
],
},
{
step: 5,
type: 'mutation' as const,
name: 'verifyConnection',
arguments: [
{
name: 'id',
value: '{createConnection.id}',
},
],
},
],
createAuthData, createAuthData,
verifyCredentials, verifyCredentials,

View File

@@ -3,6 +3,7 @@ import verifyCredentials from './verify-credentials';
import isStillVerified from './is-still-verified'; import isStillVerified from './is-still-verified';
export default { export default {
generateAuthUrl: true,
fields: [ fields: [
{ {
key: 'oAuthRedirectUrl', key: 'oAuthRedirectUrl',
@@ -39,86 +40,6 @@ export default {
clickToCopy: false, clickToCopy: false,
}, },
], ],
authenticationSteps: [
{
type: 'mutation' as const,
name: 'createConnection',
arguments: [
{
name: 'key',
value: '{key}',
},
{
name: 'formattedData',
value: null,
properties: [
{
name: 'consumerKey',
value: '{fields.consumerKey}',
},
{
name: 'consumerSecret',
value: '{fields.consumerSecret}',
},
],
},
],
},
{
type: 'mutation' as const,
name: 'createAuthData',
arguments: [
{
name: 'id',
value: '{createConnection.id}',
},
],
},
{
type: 'openWithPopup' as const,
name: 'openAuthPopup',
arguments: [
{
name: 'url',
value: '{createAuthData.url}',
},
],
},
{
type: 'mutation' as const,
name: 'updateConnection',
arguments: [
{
name: 'id',
value: '{createConnection.id}',
},
{
name: 'formattedData',
value: null,
properties: [
{
name: 'code',
value: '{openAuthPopup.code}',
},
{
name: 'state',
value: '{openAuthPopup.state}',
},
],
},
],
},
{
type: 'mutation' as const,
name: 'verifyConnection',
arguments: [
{
name: 'id',
value: '{createConnection.id}',
},
],
},
],
createAuthData, createAuthData,
verifyCredentials, verifyCredentials,

View File

@@ -86,58 +86,6 @@ export default {
clickToCopy: false, clickToCopy: false,
}, },
], ],
authenticationSteps: [
{
type: 'mutation' as const,
name: 'createConnection',
arguments: [
{
name: 'key',
value: '{key}',
},
{
name: 'formattedData',
value: null,
properties: [
{
name: 'host',
value: '{fields.host}',
},
{
name: 'username',
value: '{fields.username}',
},
{
name: 'password',
value: '{fields.password}',
},
{
name: 'useTLS',
value: '{fields.useTls}',
},
{
name: 'port',
value: '{fields.port}',
},
{
name: 'fromEmail',
value: '{fields.fromEmail}',
},
],
},
],
},
{
type: 'mutation' as const,
name: 'verifyConnection',
arguments: [
{
name: 'id',
value: '{createConnection.id}',
},
],
},
],
verifyCredentials, verifyCredentials,
isStillVerified, isStillVerified,
}; };

View File

@@ -27,42 +27,6 @@ export default {
clickToCopy: false, clickToCopy: false,
}, },
], ],
authenticationSteps: [
{
type: 'mutation' as const,
name: 'createConnection',
arguments: [
{
name: 'key',
value: '{key}',
},
{
name: 'formattedData',
value: null,
properties: [
{
name: 'accountSid',
value: '{fields.accountSid}',
},
{
name: 'authToken',
value: '{fields.authToken}',
},
],
},
],
},
{
type: 'mutation' as const,
name: 'verifyConnection',
arguments: [
{
name: 'id',
value: '{createConnection.id}',
},
],
},
],
verifyCredentials, verifyCredentials,
isStillVerified, isStillVerified,

View File

@@ -3,6 +3,7 @@ import verifyCredentials from './verify-credentials';
import isStillVerified from './is-still-verified'; import isStillVerified from './is-still-verified';
export default { export default {
generateAuthUrl: true,
fields: [ fields: [
{ {
key: 'oAuthRedirectUrl', key: 'oAuthRedirectUrl',
@@ -39,76 +40,6 @@ export default {
clickToCopy: false, clickToCopy: false,
}, },
], ],
authenticationSteps: [
{
type: 'mutation' as const,
name: 'createConnection',
arguments: [
{
name: 'key',
value: '{key}',
},
{
name: 'formattedData',
value: null,
properties: [
{
name: 'consumerKey',
value: '{fields.consumerKey}',
},
{
name: 'consumerSecret',
value: '{fields.consumerSecret}',
},
],
},
],
},
{
type: 'mutation' as const,
name: 'createAuthData',
arguments: [
{
name: 'id',
value: '{createConnection.id}',
},
],
},
{
type: 'openWithPopup' as const,
name: 'openAuthPopup',
arguments: [
{
name: 'url',
value: '{createAuthData.url}',
},
],
},
{
type: 'mutation' as const,
name: 'updateConnection',
arguments: [
{
name: 'id',
value: '{createConnection.id}',
},
{
name: 'formattedData',
value: '{openAuthPopup.all}',
},
],
},
{
type: 'mutation' as const,
name: 'verifyConnection',
arguments: [
{
name: 'id',
value: '{createConnection.id}',
},
],
},
],
createAuthData, createAuthData,
verifyCredentials, verifyCredentials,

View File

@@ -0,0 +1,101 @@
import { IApp } from '@automatisch/types';
function addAuthenticationSteps(app: IApp): IApp {
if (app.auth.generateAuthUrl) {
app.auth.authenticationSteps = authenticationStepsWithAuthUrl;
} else {
app.auth.authenticationSteps = authenticationStepsWithoutAuthUrl;
}
return app;
}
const authenticationStepsWithoutAuthUrl = [
{
type: 'mutation' as const,
name: 'createConnection',
arguments: [
{
name: 'key',
value: '{key}',
},
{
name: 'formattedData',
value: '{fields.all}',
},
],
},
{
type: 'mutation' as const,
name: 'verifyConnection',
arguments: [
{
name: 'id',
value: '{createConnection.id}',
},
],
},
];
const authenticationStepsWithAuthUrl = [
{
type: 'mutation' as const,
name: 'createConnection',
arguments: [
{
name: 'key',
value: '{key}',
},
{
name: 'formattedData',
value: '{fields.all}',
},
],
},
{
type: 'mutation' as const,
name: 'createAuthData',
arguments: [
{
name: 'id',
value: '{createConnection.id}',
},
],
},
{
type: 'openWithPopup' as const,
name: 'openAuthPopup',
arguments: [
{
name: 'url',
value: '{createAuthData.url}',
},
],
},
{
type: 'mutation' as const,
name: 'updateConnection',
arguments: [
{
name: 'id',
value: '{createConnection.id}',
},
{
name: 'formattedData',
value: '{openAuthPopup.all}',
},
],
},
{
type: 'mutation' as const,
name: 'verifyConnection',
arguments: [
{
name: 'id',
value: '{createConnection.id}',
},
],
},
];
export default addAuthenticationSteps;

View File

@@ -8,14 +8,12 @@ import {
ITrigger, ITrigger,
} from '@automatisch/types'; } from '@automatisch/types';
import { omit, cloneDeep } from 'lodash'; import { omit, cloneDeep } from 'lodash';
import addAuthenticationSteps from './add-authentication-steps';
import addReconnectionSteps from './add-reconnection-steps'; import addReconnectionSteps from './add-reconnection-steps';
type TApps = Record<string, Promise<{ default: IApp }>>; type TApps = Record<string, Promise<{ default: IApp }>>;
const apps = fs const apps = fs
.readdirSync( .readdirSync(path.resolve(__dirname, `../apps/`), { withFileTypes: true })
path.resolve(__dirname, `../apps/`),
{ withFileTypes: true }
)
.reduce((apps, dirent) => { .reduce((apps, dirent) => {
if (!dirent.isDirectory()) return apps; if (!dirent.isDirectory()) return apps;
@@ -36,6 +34,7 @@ const getApp = async (appKey: string, stripFuncs = true) => {
let appData: IApp = cloneDeep(await getDefaultExport(appKey)); let appData: IApp = cloneDeep(await getDefaultExport(appKey));
if (appData.auth) { if (appData.auth) {
appData = addAuthenticationSteps(appData);
appData = addReconnectionSteps(appData); appData = addReconnectionSteps(appData);
} }

View File

@@ -181,8 +181,9 @@ export interface IAuth {
createAuthData?($: IGlobalVariable): Promise<void>; createAuthData?($: IGlobalVariable): Promise<void>;
verifyCredentials($: IGlobalVariable): Promise<any>; verifyCredentials($: IGlobalVariable): Promise<any>;
isStillVerified($: IGlobalVariable): Promise<boolean>; isStillVerified($: IGlobalVariable): Promise<boolean>;
generateAuthUrl?: boolean;
fields: IField[]; fields: IField[];
authenticationSteps: IAuthenticationStep[]; authenticationSteps?: IAuthenticationStep[];
reconnectionSteps?: IAuthenticationStep[]; reconnectionSteps?: IAuthenticationStep[];
} }