forked from integration-os/integrationos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added salesforce crm oauth endpoints (integration-os#87)
- Loading branch information
1 parent
f34ddd6
commit ed18980
Showing
11 changed files
with
201 additions
and
76 deletions.
There are no files selected for viewing
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
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
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
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
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
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
108 changes: 108 additions & 0 deletions
108
integrationos-platform-oauth/src/connections/salesforce/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,108 @@ | ||
import axios from "axios"; | ||
import qs from "qs"; | ||
import { DataObject, OAuthResponse } from "../../lib/types"; | ||
import { differenceInSeconds } from "../../lib/helpers"; | ||
|
||
const getListAllId = async (accessToken: string, url: string) => { | ||
const response = await axios.get(url, { | ||
headers: { | ||
Authorization: `Bearer ${accessToken}`, | ||
}, | ||
}); | ||
const { | ||
data: { listviews }, | ||
} = response; | ||
|
||
if (listviews?.length) { | ||
const filteredListviews = listviews.filter((lv: any) => | ||
lv.label.startsWith("All") | ||
); | ||
if (filteredListviews.length) { | ||
return filteredListviews[0].id; | ||
} | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
export const init = async ({ body }: DataObject): Promise<OAuthResponse> => { | ||
try { | ||
const { | ||
clientId, | ||
clientSecret, | ||
metadata: { | ||
code, | ||
formData: { SALESFORCE_DOMAIN }, // Example: https://flow-fun-2719.my.salesforce.com | ||
redirectUri, | ||
}, | ||
} = body; | ||
const baseUrl = `${SALESFORCE_DOMAIN}/services/oauth2`; | ||
|
||
const requestBody = { | ||
grant_type: "authorization_code", | ||
code, | ||
redirect_uri: redirectUri, | ||
client_id: body.clientId, | ||
client_secret: body.clientSecret, | ||
}; | ||
const response = await axios({ | ||
url: `${baseUrl}/token`, | ||
method: "POST", | ||
headers: { "Content-Type": "application/x-www-form-urlencoded" }, | ||
data: qs.stringify(requestBody), | ||
}); | ||
|
||
const { | ||
data: { | ||
access_token: accessToken, | ||
refresh_token: refreshToken, | ||
token_type: tokenType, | ||
}, | ||
} = response; | ||
|
||
// Get expiry time through introspection | ||
const introspection = await axios({ | ||
url: `${baseUrl}/introspect`, | ||
method: "POST", | ||
headers: { "Content-Type": "application/x-www-form-urlencoded" }, | ||
data: qs.stringify({ | ||
client_id: clientId, | ||
client_secret: clientSecret, | ||
token: accessToken, | ||
token_type_hint: "access_token", | ||
}), | ||
}); | ||
const { | ||
data: { exp: expiresAt }, | ||
} = introspection; | ||
// Converting expiresAt to date object and getting difference in seconds | ||
const expiresIn = differenceInSeconds(new Date(expiresAt * 1000)); | ||
|
||
// Get all listview ids and save in meta for getMany | ||
const listViewBaseUrl = `${SALESFORCE_DOMAIN}/services/data/v61.0/sobjects`; | ||
const contactListView = `${listViewBaseUrl}/contact/listviews`; | ||
const opportunityListView = `${listViewBaseUrl}/opportunity/listviews`; | ||
const leadListView = `${listViewBaseUrl}/lead/listviews`; | ||
|
||
const contactListId = await getListAllId(accessToken, contactListView); | ||
const opportunityListId = await getListAllId( | ||
accessToken, | ||
opportunityListView | ||
); | ||
const leadListId = await getListAllId(accessToken, leadListView); | ||
|
||
return { | ||
accessToken, | ||
refreshToken, | ||
expiresIn, | ||
tokenType, | ||
meta: { | ||
contactListId, | ||
opportunityListId, | ||
leadListId, | ||
}, | ||
}; | ||
} catch (error) { | ||
throw new Error(`Error fetching access token for Salesforce: ${error}`); | ||
} | ||
}; |
64 changes: 64 additions & 0 deletions
64
integrationos-platform-oauth/src/connections/salesforce/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,64 @@ | ||
import axios from "axios"; | ||
import qs from "qs"; | ||
import { DataObject, OAuthResponse } from "../../lib/types"; | ||
import { differenceInSeconds, generateBasicHeaders } from "../../lib/helpers"; | ||
|
||
export const refresh = async ({ body }: DataObject): Promise<OAuthResponse> => { | ||
try { | ||
const { | ||
OAUTH_CLIENT_ID: clientId, | ||
OAUTH_CLIENT_SECRET: clientSecret, | ||
OAUTH_REFRESH_TOKEN: refresh_token, | ||
OAUTH_REQUEST_PAYLOAD: { | ||
formData: { SALESFORCE_DOMAIN }, | ||
}, | ||
OAUTH_METADATA, | ||
} = body; | ||
const baseUrl = `${SALESFORCE_DOMAIN}/services/oauth2`; | ||
|
||
const requestBody = { | ||
grant_type: "refresh_token", | ||
refresh_token, | ||
}; | ||
const response = await axios({ | ||
url: `${baseUrl}/token`, | ||
method: "POST", | ||
headers: generateBasicHeaders(clientId, clientSecret), | ||
data: qs.stringify(requestBody), | ||
}); | ||
|
||
const { | ||
data: { access_token: accessToken, token_type: tokenType }, | ||
} = response; | ||
|
||
// Get expiry time through introspection | ||
const introspection = await axios({ | ||
url: `${baseUrl}/introspect`, | ||
method: "POST", | ||
headers: { "Content-Type": "application/x-www-form-urlencoded" }, | ||
data: qs.stringify({ | ||
client_id: clientId, | ||
client_secret: clientSecret, | ||
token: accessToken, | ||
token_type_hint: "access_token", | ||
}), | ||
}); | ||
const { | ||
data: { exp: expiresAt }, | ||
} = introspection; | ||
// Converting expiresAt to date object and getting difference in seconds | ||
const expiresIn = differenceInSeconds(new Date(expiresAt * 1000)); | ||
|
||
return { | ||
accessToken, | ||
refreshToken: refresh_token, | ||
expiresIn, | ||
tokenType, | ||
meta: { | ||
...OAUTH_METADATA?.meta, | ||
}, | ||
}; | ||
} catch (error) { | ||
throw new Error(`Error fetching refresh token for Salesforce: ${error}`); | ||
} | ||
}; |
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
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
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