diff --git a/integrationos-platform-oauth/src/connections/front/init.ts b/integrationos-platform-oauth/src/connections/front/init.ts new file mode 100644 index 00000000..642ad9bd --- /dev/null +++ b/integrationos-platform-oauth/src/connections/front/init.ts @@ -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 => { + 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}`); + } +}; diff --git a/integrationos-platform-oauth/src/connections/front/refresh.ts b/integrationos-platform-oauth/src/connections/front/refresh.ts new file mode 100644 index 00000000..cb3314dc --- /dev/null +++ b/integrationos-platform-oauth/src/connections/front/refresh.ts @@ -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 => { + 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}`); + } +};