feat(hubspot): Implement verify credentials for OAuth

This commit is contained in:
Faruk AYDIN
2023-09-08 19:35:20 +02:00
parent b12f39916f
commit dd1e8240b8
8 changed files with 110 additions and 29 deletions

View File

@@ -64,28 +64,25 @@ export default defineAction({
],
async run($) {
const company = $.step.parameters.company as string || undefined;
const email = $.step.parameters.email as string || undefined;
const firstname = $.step.parameters.firstname as string || undefined;
const lastname = $.step.parameters.lastname as string || undefined;
const phone = $.step.parameters.phone as string || undefined;
const website = $.step.parameters.website as string || undefined;
const hubspot_owner_id = $.step.parameters.hubspot_owner_id as number || undefined;
const company = $.step.parameters.company as string;
const email = $.step.parameters.email as string;
const firstname = $.step.parameters.firstname as string;
const lastname = $.step.parameters.lastname as string;
const phone = $.step.parameters.phone as string;
const website = $.step.parameters.website as string;
const hubspot_owner_id = $.step.parameters.hubspot_owner_id as string;
const response = await $.http.post(
`crm/v3/objects/contacts`,
{
properties: {
company,
email,
firstname,
lastname,
phone,
website,
hubspot_owner_id,
}
}
);
const response = await $.http.post(`crm/v3/objects/contacts`, {
properties: {
company,
email,
firstname,
lastname,
phone,
website,
hubspot_owner_id,
},
});
$.setActionItem({ raw: response.data });
},

View File

@@ -1,7 +1,6 @@
import { IField, IGlobalVariable } from '@automatisch/types';
import { URLSearchParams } from 'url';
const scopes = ['crm.objects.contacts.read', 'crm.objects.contacts.write'];
import scopes from '../common/scopes';
export default async function generateAuthUrl($: IGlobalVariable) {
const oauthRedirectUrlField = $.app.auth.fields.find(

View File

@@ -1,6 +1,7 @@
import generateAuthUrl from './generate-auth-url';
import verifyCredentials from './verify-credentials';
import isStillVerified from './is-still-verified';
import refreshToken from './refresh-token';
export default {
fields: [
@@ -43,4 +44,5 @@ export default {
generateAuthUrl,
verifyCredentials,
isStillVerified,
refreshToken,
};

View File

@@ -1,8 +1,9 @@
import { IGlobalVariable } from '@automatisch/types';
import verifyCredentials from "./verify-credentials";
import getAccessTokenInfo from '../common/get-access-token-info';
const isStillVerified = async ($: IGlobalVariable) => {
await verifyCredentials($);
await getAccessTokenInfo($);
return true;
};

View File

@@ -0,0 +1,28 @@
import { IGlobalVariable, IField } from '@automatisch/types';
import { URLSearchParams } from 'url';
const refreshToken = async ($: IGlobalVariable) => {
const oauthRedirectUrlField = $.app.auth.fields.find(
(field: IField) => field.key == 'oAuthRedirectUrl'
);
const callbackUrl = oauthRedirectUrlField.value as string;
const params = new URLSearchParams({
grant_type: 'refresh_token',
client_id: $.auth.data.clientId as string,
client_secret: $.auth.data.clientSecret as string,
redirect_uri: callbackUrl,
refresh_token: $.auth.data.refreshToken as string,
});
const { data } = await $.http.post('/oauth/v1/token', params.toString());
await $.auth.set({
accessToken: data.access_token,
expiresIn: data.expires_in,
refreshToken: data.refresh_token,
});
};
export default refreshToken;

View File

@@ -1,11 +1,51 @@
import { IGlobalVariable } from '@automatisch/types';
import { IGlobalVariable, IField } from '@automatisch/types';
import { URLSearchParams } from 'url';
import getAccessTokenInfo from '../common/get-access-token-info';
const verifyCredentials = async ($: IGlobalVariable) => {
await $.http.get(
`/crm/v3/objects/contacts?limit=1`,
const oauthRedirectUrlField = $.app.auth.fields.find(
(field: IField) => field.key == 'oAuthRedirectUrl'
);
const callbackUrl = oauthRedirectUrlField.value as string;
const params = new URLSearchParams({
grant_type: 'authorization_code',
client_id: $.auth.data.clientId as string,
client_secret: $.auth.data.clientSecret as string,
redirect_uri: callbackUrl,
code: $.auth.data.code as string,
});
const { data: verifiedCredentials } = await $.http.post(
'/oauth/v1/token',
params.toString()
);
const {
access_token: accessToken,
refresh_token: refreshToken,
expires_in: expiresIn,
} = verifiedCredentials;
await $.auth.set({
screenName: $.auth.data?.displayName,
accessToken,
refreshToken,
expiresIn,
});
const accessTokenInfo = await getAccessTokenInfo($);
await $.auth.set({
screenName: accessTokenInfo.user,
hubDomain: accessTokenInfo.hub_domain,
scopes: accessTokenInfo.scopes,
scopeToScopeGroupPks: accessTokenInfo.scope_to_scope_group_pks,
trialScopes: accessTokenInfo.trial_scopes,
trialScopeToScoreGroupPks: accessTokenInfo.trial_scope_to_scope_group_pks,
hubId: accessTokenInfo.hub_id,
appId: accessTokenInfo.app_id,
userId: accessTokenInfo.user_id,
expiresIn: accessTokenInfo.expires_in,
tokenType: accessTokenInfo.token_type,
});
};

View File

@@ -0,0 +1,11 @@
import { IGlobalVariable, IJSONObject } from '@automatisch/types';
const getAccessTokenInfo = async ($: IGlobalVariable): Promise<IJSONObject> => {
const response = await $.http.get(
`/oauth/v1/access-tokens/${$.auth.data.accessToken}`
);
return response.data;
};
export default getAccessTokenInfo;

View File

@@ -0,0 +1,3 @@
const scopes = ['crm.objects.contacts.read', 'crm.objects.contacts.write'];
export default scopes;