Skip to content

Commit

Permalink
feat: added zendesk oauth endpoints (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
oberoi-gaurav authored Sep 30, 2024
1 parent d64af29 commit 82b4340
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
58 changes: 58 additions & 0 deletions integrationos-oauth/src/connections/zendesk/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import axios from 'axios';
import { DataObject, OAuthResponse } from '../../lib/types';

export const init = async ({ body }: DataObject): Promise<OAuthResponse> => {
try {
const {
clientId: client_id,
clientSecret: client_secret,
metadata: {
code,
formData: { ZENDESK_SUBDOMAIN },
redirectUri: redirect_uri,
},
} = body;

const requestBody = {
grant_type: 'authorization_code',
code,
client_id,
client_secret,
redirect_uri,
scope: 'read write',
code_verifier:
'eNmtK0mRHQ.-93QxgrniB7rb.-TZ_Tjbonygt2aqk1.ltiwXQHmNFeFJPh19MZwXzFDfmFMpUZbAFVtSAtChNU08R4txdDBi7EY6ZHiBp6I8F1drUHZR',
};

const response = await axios.post(
`https://${ZENDESK_SUBDOMAIN}.zendesk.com/oauth/tokens`,
requestBody,
);

const {
data: { token_type: tokenType, access_token: accessToken },
} = response;

const profileResponse = await axios.get(
`https://${ZENDESK_SUBDOMAIN}.zendesk.com/api/v2/users/me.json`,
{ headers: { Authorization: `Bearer ${accessToken}` } },
);
const {
data: {
user: { email: ZENDESK_EMAIL_ADDRESS },
},
} = profileResponse;

return {
accessToken,
refreshToken: accessToken,
expiresIn: 2147483647,
tokenType,
meta: {
ZENDESK_EMAIL_ADDRESS,
},
};
} catch (error) {
throw new Error(`Error fetching access token for Zendesk: ${error}`);
}
};
18 changes: 18 additions & 0 deletions integrationos-oauth/src/connections/zendesk/refresh.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { DataObject, OAuthResponse } from '../../lib/types';

export const refresh = async ({ body }: DataObject): Promise<OAuthResponse> => {
try {
const {
OAUTH_METADATA: { accessToken, refreshToken, tokenType, meta },
} = body;
return {
accessToken,
refreshToken,
expiresIn: 2147483647,
tokenType,
meta,
};
} catch (error) {
throw new Error(`Error fetching refresh token for Zendesk: ${error}`);
}
};

0 comments on commit 82b4340

Please sign in to comment.