Add sandbox option to Salesforce OAuth2 credentials (#1979)

*  Add environment dropdown to OAuth2 creds

*  Add sandbox URL to OAuth2 call

*  Revert options change

*  Set OAuth2 URLs with expressions

*  Extract instance URL from credentials
This commit is contained in:
Iván Ovejero 2021-07-14 18:51:51 +02:00 committed by GitHub
parent e40f0e00a8
commit c5a1bc007f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 6 deletions

View file

@ -11,19 +11,35 @@ export class SalesforceOAuth2Api implements ICredentialType {
displayName = 'Salesforce OAuth2 API'; displayName = 'Salesforce OAuth2 API';
documentationUrl = 'salesforce'; documentationUrl = 'salesforce';
properties: INodeProperties[] = [ properties: INodeProperties[] = [
{
displayName: 'Environment Type',
name: 'environment',
type: 'options',
options: [
{
name: 'Production',
value: 'production',
},
{
name: 'Sandbox',
value: 'sandbox',
},
],
default: 'production',
},
{ {
displayName: 'Authorization URL', displayName: 'Authorization URL',
name: 'authUrl', name: 'authUrl',
type: 'hidden', type: 'hidden',
default: 'https://login.salesforce.com/services/oauth2/authorize',
required: true, required: true,
default: '={{ $self["environment"] === "sandbox" ? "https://test.salesforce.com/services/oauth2/authorize" : "https://login.salesforce.com/services/oauth2/authorize" }}',
}, },
{ {
displayName: 'Access Token URL', displayName: 'Access Token URL',
name: 'accessTokenUrl', name: 'accessTokenUrl',
type: 'string', type: 'hidden',
default: 'https://yourcompany.salesforce.com/services/oauth2/token',
required: true, required: true,
default: '={{ $self["environment"] === "sandbox" ? "https://test.salesforce.com/services/oauth2/token" : "https://login.salesforce.com/services/oauth2/token" }}',
}, },
{ {
displayName: 'Scope', displayName: 'Scope',

View file

@ -40,9 +40,8 @@ export async function salesforceApiRequest(this: IExecuteFunctions | IExecuteSin
} else { } else {
// https://help.salesforce.com/articleView?id=remoteaccess_oauth_web_server_flow.htm&type=5 // https://help.salesforce.com/articleView?id=remoteaccess_oauth_web_server_flow.htm&type=5
const credentialsType = 'salesforceOAuth2Api'; const credentialsType = 'salesforceOAuth2Api';
const credentials = this.getCredentials(credentialsType); const credentials = this.getCredentials(credentialsType) as { oauthTokenData: { instance_url: string } };
const subdomain = ((credentials!.accessTokenUrl as string).match(/https:\/\/(.+).salesforce\.com/) || [])[1]; const options = getOptions.call(this, method, (uri || endpoint), body, qs, credentials.oauthTokenData.instance_url);
const options = getOptions.call(this, method, (uri || endpoint), body, qs, `https://${subdomain}.salesforce.com`);
Logger.debug(`Authentication for "Salesforce" node is using "OAuth2". Invoking URI ${options.uri}`); Logger.debug(`Authentication for "Salesforce" node is using "OAuth2". Invoking URI ${options.uri}`);
//@ts-ignore //@ts-ignore
return await this.helpers.requestOAuth2.call(this, credentialsType, options); return await this.helpers.requestOAuth2.call(this, credentialsType, options);