Skip to content

Commit

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

const generateHeaders = (clientId: string, clientSecret: string) => {
const credentials = clientId + ":" + clientSecret;
const encodedCredentials = Buffer.from(credentials).toString("base64");

return {
authorization: "Basic " + encodedCredentials,
"Content-Type": "application/x-www-form-urlencoded",
};
};

export const init = async ({ body }: DataObject): Promise<OAuthResponse> => {
try {
const requestBody = {
grant_type: "authorization_code",
code: body.metadata?.code,
redirect_uri: body.metadata?.redirectUri,
};

const response = await axios.post(
"https://app.frontapp.com/oauth/token",
requestBody,
{
headers: generateHeaders(body.clientId, body.clientSecret),
}
);

const {
access_token: accessToken,
refresh_token: refreshToken,
expires_at: expiresIn,
token_type: tokenType,
} = response.data;

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

const generateHeaders = (clientId: string, clientSecret: string) => {
const credentials = clientId + ":" + clientSecret;
const encodedCredentials = Buffer.from(credentials).toString("base64");

return {
authorization: "Basic " + encodedCredentials,
"Content-Type": "application/x-www-form-urlencoded",
};
};

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,
} = body;

const requestBody = {
grant_type: "refresh_token",
refresh_token,
};

const response = await axios.post(
"https://app.frontapp.com/oauth/token",
requestBody,
{
headers: generateHeaders(client_id, client_secret),
}
);

const {
access_token: accessToken,
refresh_token: refreshToken,
expires_at: expiresIn,
token_type: tokenType,
} = response.data;

return {
accessToken,
refreshToken,
expiresIn,
tokenType,
meta: {
...body?.OAUTH_METADATA?.meta,
},
};
} catch (error) {
throw new Error(`Error fetching refresh token for xero: ${error}`);
}
};

0 comments on commit ea83a17

Please sign in to comment.