Skip to content

Commit

Permalink
feat: added zoho oauth endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
oberoi-gaurav committed Jun 19, 2024
1 parent c0b63d0 commit da56cbd
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
44 changes: 44 additions & 0 deletions integrationos-platform-oauth/src/connections/zoho/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import axios from "axios";
import { DataObject, OAuthResponse } from "../../lib/types";

export const init = async ({ body }: DataObject): Promise<OAuthResponse> => {
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}`);
}
};
42 changes: 42 additions & 0 deletions integrationos-platform-oauth/src/connections/zoho/refresh.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import axios from "axios";
import { DataObject, OAuthResponse } from "../../lib/types";

export const refresh = async ({ body }: DataObject): Promise<OAuthResponse> => {
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}`);
}
};

0 comments on commit da56cbd

Please sign in to comment.