From da56cbdef0e10d4e82d55229e509c0b101264909 Mon Sep 17 00:00:00 2001 From: Gaurav Oberoi Date: Wed, 19 Jun 2024 12:08:17 +0530 Subject: [PATCH] feat: added zoho oauth endpoints --- .../src/connections/zoho/init.ts | 44 +++++++++++++++++++ .../src/connections/zoho/refresh.ts | 42 ++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 integrationos-platform-oauth/src/connections/zoho/init.ts create mode 100644 integrationos-platform-oauth/src/connections/zoho/refresh.ts diff --git a/integrationos-platform-oauth/src/connections/zoho/init.ts b/integrationos-platform-oauth/src/connections/zoho/init.ts new file mode 100644 index 00000000..0db49f21 --- /dev/null +++ b/integrationos-platform-oauth/src/connections/zoho/init.ts @@ -0,0 +1,44 @@ +import axios from "axios"; +import { DataObject, OAuthResponse } from "../../lib/types"; + +export const init = async ({ body }: DataObject): Promise => { + try { + const { + clientId, + clientSecret, + metadata: { + additionalData, + code, + formData: { ZOHO_ACCOUNTS_DOMAIN }, + redirectUri, + }, + } = body; + let url = `${ZOHO_ACCOUNTS_DOMAIN}/oauth/v2/token?grant_type=authorization_code`; + url += `&client_id=${clientId}&client_secret=${clientSecret}&code=${code}&redirect_uri=${redirectUri}`; + + const response = await axios.post(url); + + const { + data: { + access_token: accessToken, + refresh_token: refreshToken, + api_domain: apiDomain, + token_type: tokenType, + expires_in: expiresIn, + }, + } = response; + + return { + accessToken, + refreshToken, + expiresIn, + tokenType, + meta: { + apiDomain, + ...additionalData, + }, + }; + } catch (error) { + throw new Error(`Error fetching access token for Zoho: ${error}`); + } +}; diff --git a/integrationos-platform-oauth/src/connections/zoho/refresh.ts b/integrationos-platform-oauth/src/connections/zoho/refresh.ts new file mode 100644 index 00000000..cb19c885 --- /dev/null +++ b/integrationos-platform-oauth/src/connections/zoho/refresh.ts @@ -0,0 +1,42 @@ +import axios from "axios"; +import { DataObject, OAuthResponse } from "../../lib/types"; + +export const refresh = async ({ body }: DataObject): Promise => { + try { + const { + OAUTH_CLIENT_ID: client_id, + OAUTH_CLIENT_SECRET: client_secret, + OAUTH_REFRESH_TOKEN: refresh_token, + OAUTH_REQUEST_PAYLOAD: { + formData: { ZOHO_ACCOUNTS_DOMAIN }, + }, + OAUTH_METADATA, + } = body; + + let url = `${ZOHO_ACCOUNTS_DOMAIN}/oauth/v2/token?grant_type=refresh_token`; + url += `&client_id=${client_id}&client_secret=${client_secret}&refresh_token=${refresh_token}`; + + const response = await axios.post(url); + + const { + data: { + access_token: accessToken, + refresh_token: refreshToken, + token_type: tokenType, + expires_in: expiresIn, + }, + } = response; + + return { + accessToken, + refreshToken, + expiresIn, + tokenType, + meta: { + ...OAUTH_METADATA?.meta, + }, + }; + } catch (error) { + throw new Error(`Error fetching refresh token for Zoho: ${error}`); + } +};