Skip to content

Commit

Permalink
feat: Zoho OAuth endpoints without formData (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
oberoi-gaurav authored Sep 10, 2024
1 parent 44b7150 commit 9146ef4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
19 changes: 11 additions & 8 deletions integrationos-oauth/src/connections/zoho/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ export const init = async ({ body }: DataObject): Promise<OAuthResponse> => {
const {
clientId,
clientSecret,
metadata: {
additionalData,
code,
formData: { ZOHO_ACCOUNTS_DOMAIN },
redirectUri,
},
metadata: { code, redirectUri, additionalData },
} = body;

// Decode the accounts-server for authorization URL
const ZOHO_ACCOUNTS_DOMAIN = decodeURIComponent(
additionalData['accounts-server'],
);

let url = `${ZOHO_ACCOUNTS_DOMAIN}/oauth/v2/token?grant_type=authorization_code`;
url += `&client_id=${clientId}&client_secret=${clientSecret}&code=${code}&redirect_uri=${redirectUri}`;
url += `&client_id=${clientId}&client_secret=${clientSecret}`;
url += `&code=${code}&redirect_uri=${redirectUri}`;

const response = await axios.post(url);

Expand All @@ -34,8 +36,9 @@ export const init = async ({ body }: DataObject): Promise<OAuthResponse> => {
expiresIn,
tokenType,
meta: {
apiDomain,
...additionalData,
ZOHO_ACCOUNTS_DOMAIN,
apiDomain,
},
};
} catch (error) {
Expand Down
20 changes: 11 additions & 9 deletions integrationos-oauth/src/connections/zoho/refresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ export const refresh = async ({ body }: DataObject): Promise<OAuthResponse> => {
OAUTH_CLIENT_ID: client_id,
OAUTH_CLIENT_SECRET: client_secret,
OAUTH_REFRESH_TOKEN: refresh_token,
OAUTH_REQUEST_PAYLOAD: {
formData: { ZOHO_ACCOUNTS_DOMAIN },
},
OAUTH_METADATA,
OAUTH_METADATA: { meta },
} = body;

let refreshToken = refresh_token;
const ZOHO_ACCOUNTS_DOMAIN = meta.ZOHO_ACCOUNTS_DOMAIN;

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}`;

Expand All @@ -26,15 +26,17 @@ export const refresh = async ({ body }: DataObject): Promise<OAuthResponse> => {
},
} = response;

// Update refresh token if a new token is allocated
if (response.data.refresh_token) {
refreshToken = response.data.refresh_token;
}

return {
accessToken,
// Refresh token does not expire and stays the same for every request
refreshToken: refresh_token,
refreshToken,
expiresIn,
tokenType,
meta: {
...OAUTH_METADATA?.meta,
},
meta,
};
} catch (error) {
throw new Error(`Error fetching refresh token for Zoho: ${error}`);
Expand Down

0 comments on commit 9146ef4

Please sign in to comment.