diff --git a/packages/backend/src/apps/pipedrive/assets/favicon.svg b/packages/backend/src/apps/pipedrive/assets/favicon.svg new file mode 100644 index 00000000..5efad428 --- /dev/null +++ b/packages/backend/src/apps/pipedrive/assets/favicon.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/packages/backend/src/apps/pipedrive/auth/generate-auth-url.ts b/packages/backend/src/apps/pipedrive/auth/generate-auth-url.ts new file mode 100644 index 00000000..1e35bada --- /dev/null +++ b/packages/backend/src/apps/pipedrive/auth/generate-auth-url.ts @@ -0,0 +1,19 @@ +import { IField, IGlobalVariable } from '@automatisch/types'; +import { URLSearchParams } from 'url'; + +export default async function generateAuthUrl($: IGlobalVariable) { + const oauthRedirectUrlField = $.app.auth.fields.find( + (field: IField) => field.key == 'oAuthRedirectUrl' + ); + const redirectUri = oauthRedirectUrlField.value as string; + const searchParams = new URLSearchParams({ + client_id: $.auth.data.clientId as string, + redirect_uri: redirectUri, + }); + + const url = `https://oauth.pipedrive.com/oauth/authorize?${searchParams.toString()}`; + + await $.auth.set({ + url, + }); +} diff --git a/packages/backend/src/apps/pipedrive/auth/index.ts b/packages/backend/src/apps/pipedrive/auth/index.ts new file mode 100644 index 00000000..efd31fd5 --- /dev/null +++ b/packages/backend/src/apps/pipedrive/auth/index.ts @@ -0,0 +1,48 @@ +import generateAuthUrl from './generate-auth-url'; +import verifyCredentials from './verify-credentials'; +import refreshToken from './refresh-token'; +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/pipedrive/connections/add', + placeholder: null, + description: + 'When asked to input a redirect URL in Google Cloud, enter the URL above.', + clickToCopy: true, + }, + { + key: 'clientId', + label: 'Client ID', + type: 'string' as const, + required: true, + readOnly: false, + value: null, + placeholder: null, + description: null, + clickToCopy: false, + }, + { + key: 'clientSecret', + label: 'Client Secret', + type: 'string' as const, + required: true, + readOnly: false, + value: null, + placeholder: null, + description: null, + clickToCopy: false, + }, + ], + + generateAuthUrl, + verifyCredentials, + isStillVerified, + refreshToken, +}; diff --git a/packages/backend/src/apps/pipedrive/auth/is-still-verified.ts b/packages/backend/src/apps/pipedrive/auth/is-still-verified.ts new file mode 100644 index 00000000..93a01099 --- /dev/null +++ b/packages/backend/src/apps/pipedrive/auth/is-still-verified.ts @@ -0,0 +1,9 @@ +import { IGlobalVariable } from '@automatisch/types'; +import getCurrentUser from '../common/get-current-user'; + +const isStillVerified = async ($: IGlobalVariable) => { + const currentUser = await getCurrentUser($); + return !!currentUser; +}; + +export default isStillVerified; diff --git a/packages/backend/src/apps/pipedrive/auth/refresh-token.ts b/packages/backend/src/apps/pipedrive/auth/refresh-token.ts new file mode 100644 index 00000000..89a3b71f --- /dev/null +++ b/packages/backend/src/apps/pipedrive/auth/refresh-token.ts @@ -0,0 +1,32 @@ +import { URLSearchParams } from 'node:url'; +import { IGlobalVariable } from '@automatisch/types'; + +const refreshToken = async ($: IGlobalVariable) => { + const params = new URLSearchParams({ + grant_type: 'refresh_token', + refresh_token: $.auth.data.refreshToken as string, + }); + + const headers = { + Authorization: `Basic ${Buffer.from( + $.auth.data.clientId + ':' + $.auth.data.clientSecret + ).toString('base64')}`, + 'Content-Type': 'application/x-www-form-urlencoded', + }; + + const response = await $.http.post( + 'https://oauth.pipedrive.com/oauth/token', + params.toString(), + { + headers, + } + ); + + await $.auth.set({ + accessToken: response.data.access_token, + tokenType: response.data.token_type, + expiresIn: response.data.expires_in, + }); +}; + +export default refreshToken; diff --git a/packages/backend/src/apps/pipedrive/auth/verify-credentials.ts b/packages/backend/src/apps/pipedrive/auth/verify-credentials.ts new file mode 100644 index 00000000..70f87630 --- /dev/null +++ b/packages/backend/src/apps/pipedrive/auth/verify-credentials.ts @@ -0,0 +1,55 @@ +import { IField, IGlobalVariable } from '@automatisch/types'; +import { URLSearchParams } from 'url'; +import getCurrentUser from '../common/get-current-user'; + +const verifyCredentials = async ($: IGlobalVariable) => { + const oauthRedirectUrlField = $.app.auth.fields.find( + (field: IField) => field.key == 'oAuthRedirectUrl' + ); + const redirectUri = oauthRedirectUrlField.value as string; + const params = new URLSearchParams({ + grant_type: 'authorization_code', + code: $.auth.data.code as string, + redirect_uri: redirectUri, + }); + + const headers = { + Authorization: `Basic ${Buffer.from( + $.auth.data.clientId + ':' + $.auth.data.clientSecret + ).toString('base64')}`, + 'Content-Type': 'application/x-www-form-urlencoded', + }; + + const response = await $.http.post( + `https://oauth.pipedrive.com/oauth/token`, + params.toString(), + { headers } + ); + + const { + access_token: accessToken, + api_domain: apiDomain, + expires_in: expiresIn, + refresh_token: refreshToken, + scope: scope, + token_type: tokenType, + } = response.data; + + await $.auth.set({ + accessToken, + apiDomain, + expiresIn, + refreshToken, + scope, + tokenType, + }); + + const user = await getCurrentUser($); + + await $.auth.set({ + userId: user.id, + screenName: user.name, + }); +}; + +export default verifyCredentials; diff --git a/packages/backend/src/apps/pipedrive/common/add-auth-header.ts b/packages/backend/src/apps/pipedrive/common/add-auth-header.ts new file mode 100644 index 00000000..8e7798b8 --- /dev/null +++ b/packages/backend/src/apps/pipedrive/common/add-auth-header.ts @@ -0,0 +1,11 @@ +import { TBeforeRequest } from '@automatisch/types'; + +const addAuthHeader: TBeforeRequest = ($, requestConfig) => { + if ($.auth.data?.accessToken) { + requestConfig.headers.Authorization = `${$.auth.data.tokenType} ${$.auth.data.accessToken}`; + } + + return requestConfig; +}; + +export default addAuthHeader; diff --git a/packages/backend/src/apps/pipedrive/common/get-current-user.ts b/packages/backend/src/apps/pipedrive/common/get-current-user.ts new file mode 100644 index 00000000..024ede73 --- /dev/null +++ b/packages/backend/src/apps/pipedrive/common/get-current-user.ts @@ -0,0 +1,10 @@ +import { IGlobalVariable, IJSONObject } from '@automatisch/types'; + +const getCurrentUser = async ($: IGlobalVariable): Promise => { + const response = await $.http.get(`${$.auth.data.apiDomain}/api/v1/users/me`); + const currentUser = response.data.data; + + return currentUser; +}; + +export default getCurrentUser; diff --git a/packages/backend/src/apps/pipedrive/index.d.ts b/packages/backend/src/apps/pipedrive/index.d.ts new file mode 100644 index 00000000..e69de29b diff --git a/packages/backend/src/apps/pipedrive/index.ts b/packages/backend/src/apps/pipedrive/index.ts new file mode 100644 index 00000000..9999729e --- /dev/null +++ b/packages/backend/src/apps/pipedrive/index.ts @@ -0,0 +1,16 @@ +import defineApp from '../../helpers/define-app'; +import addAuthHeader from './common/add-auth-header'; +import auth from './auth'; + +export default defineApp({ + name: 'Pipedrive', + key: 'pipedrive', + baseUrl: '', + apiBaseUrl: '', + iconUrl: '{BASE_URL}/apps/pipedrive/assets/favicon.svg', + authDocUrl: 'https://automatisch.io/docs/apps/pipedrive/connection', + primaryColor: 'FFFFFF', + supportsConnections: true, + beforeRequest: [addAuthHeader], + auth, +});