-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c0b63d0
commit ea83a17
Showing
2 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
integrationos-platform-oauth/src/connections/front/init.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
54
integrationos-platform-oauth/src/connections/front/refresh.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
} | ||
}; |