feat(salesforce): add authentication

This commit is contained in:
Ali BARIN
2022-11-03 23:08:28 +01:00
parent 7d82ca5d3c
commit ede55a70aa
9 changed files with 289 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
import { IField, IGlobalVariable } from '@automatisch/types';
import qs from 'qs';
export default async function createAuthData($: IGlobalVariable) {
try {
const oauthRedirectUrlField = $.app.auth.fields.find(
(field: IField) => field.key == 'oAuthRedirectUrl'
);
const redirectUri = oauthRedirectUrlField.value;
const searchParams = qs.stringify({
client_id: $.auth.data.consumerKey as string,
redirect_uri: redirectUri,
response_type: 'code'
})
await $.auth.set({
url: `${$.auth.data.oauth2Url}/authorize?${searchParams}`,
});
} catch (error) {
throw new Error(
`Error occured while verifying credentials: ${error}`
);
}
}

View File

@@ -0,0 +1,152 @@
import createAuthData from './create-auth-data';
import verifyCredentials from './verify-credentials';
import isStillVerified from './is-still-verified';
export default {
fields: [
{
key: 'oAuthRedirectUrl',
label: 'OAuth Redirect URL',
type: 'string' as const,
required: true,
readOnly: true,
value: '{WEB_APP_URL}/app/salesforce/connections/add',
placeholder: null,
description:
'When asked to input an OAuth callback or redirect URL in Salesforce OAuth, enter the URL above.',
clickToCopy: true,
},
{
key: 'oauth2Url',
label: 'Salesforce Environment',
type: 'dropdown' as const,
required: true,
readOnly: false,
value: 'https://login.salesforce.com/services/oauth2',
placeholder: null,
description: 'Most people should choose the default, "production".',
clickToCopy: false,
options: [
{
label: 'production',
value: 'https://login.salesforce.com/services/oauth2',
},
{
label: 'sandbox',
value: 'https://test.salesforce.com/services/oauth2',
}
]
},
{
key: 'consumerKey',
label: 'Consumer Key',
type: 'string' as const,
required: true,
readOnly: false,
value: null,
placeholder: null,
description: null,
clickToCopy: false,
},
{
key: 'consumerSecret',
label: 'Consumer Secret',
type: 'string' as const,
required: true,
readOnly: false,
value: null,
placeholder: null,
description: null,
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,
verifyCredentials,
isStillVerified,
};

View File

@@ -0,0 +1,13 @@
import { IGlobalVariable } from '@automatisch/types';
import getCurrentUser from '../common/get-current-user';
const isStillVerified = async ($: IGlobalVariable) => {
try {
const user = await getCurrentUser($);
return !!user;
} catch (error) {
return false;
}
};
export default isStillVerified;

View File

@@ -0,0 +1,41 @@
import { IGlobalVariable } from '@automatisch/types';
import getCurrentUser from '../common/get-current-user';
import qs from 'qs';
const verifyCredentials = async ($: IGlobalVariable) => {
try {
const oauthRedirectUrlField = $.app.auth.fields.find(
(field) => field.key == 'oAuthRedirectUrl'
);
const redirectUri = oauthRedirectUrlField.value;
const searchParams = qs.stringify({
code: $.auth.data.code,
grant_type: 'authorization_code',
client_id: $.auth.data.consumerKey as string,
client_secret: $.auth.data.consumerSecret as string,
redirect_uri: redirectUri
});
const { data } = await $.http.post(
`${$.auth.data.oauth2Url}/token?${searchParams}`,
);
await $.auth.set({
accessToken: data.access_token,
tokenType: data.token_type,
instanceUrl: data.instance_url,
signature: data.signature,
userId: data.id,
screenName: data.instance_url,
});
const currentUser = await getCurrentUser($);
await $.auth.set({
screenName: `${currentUser.displayName} - ${data.instance_url}`,
});
} catch (error) {
throw new Error(error.response.data);
}
};
export default verifyCredentials;