From a135d398f6827396533dbd3cbb54957172c680f3 Mon Sep 17 00:00:00 2001 From: ricardo Date: Mon, 6 Jun 2022 16:45:00 -0400 Subject: [PATCH 1/7] :zap: Add OAuth2 client credentials grant type --- packages/core/src/NodeExecuteFunctions.ts | 28 +++++++++++++- .../CredentialEdit/CredentialEdit.vue | 2 +- .../AcuitySchedulingOAuth2Api.credentials.ts | 6 +++ .../credentials/AsanaOAuth2Api.credentials.ts | 6 +++ .../credentials/BitlyOAuth2Api.credentials.ts | 6 +++ .../credentials/BoxOAuth2Api.credentials.ts | 6 +++ .../CiscoWebexOAuth2Api.credentials.ts | 6 +++ .../ClickUpOAuth2Api.credentials.ts | 6 +++ .../credentials/DriftOAuth2Api.credentials.ts | 6 +++ .../DropboxOAuth2Api.credentials.ts | 6 +++ .../EventbriteOAuth2Api.credentials.ts | 6 +++ .../FormstackOAuth2Api.credentials.ts | 6 +++ .../GetResponseOAuth2Api.credentials.ts | 6 +++ .../GithubOAuth2Api.credentials.ts | 6 +++ .../GitlabOAuth2Api.credentials.ts | 6 +++ .../GoToWebinarOAuth2Api.credentials.ts | 6 +++ .../GoogleOAuth2Api.credentials.ts | 6 +++ .../HarvestOAuth2Api.credentials.ts | 6 +++ .../HelpScoutOAuth2Api.credentials.ts | 6 +++ .../HubspotDeveloperApi.credentials.ts | 6 +++ .../HubspotOAuth2Api.credentials.ts | 6 +++ .../credentials/KeapOAuth2Api.credentials.ts | 6 +++ .../LineNotifyOAuth2Api.credentials.ts | 6 +++ .../LinkedInOAuth2Api.credentials.ts | 6 +++ .../MailchimpOAuth2Api.credentials.ts | 6 +++ .../MauticOAuth2Api.credentials.ts | 6 +++ .../MediumOAuth2Api.credentials.ts | 6 +++ .../MicrosoftOAuth2Api.credentials.ts | 6 +++ .../MondayComOAuth2Api.credentials.ts | 6 +++ .../NextCloudOAuth2Api.credentials.ts | 6 +++ .../NotionOAuth2Api.credentials.ts | 6 +++ .../credentials/OAuth2Api.credentials.ts | 37 +++++++++++++++++++ .../PagerDutyOAuth2Api.credentials.ts | 6 +++ .../PhilipsHueOAuth2Api.credentials.ts | 6 +++ .../PipedriveOAuth2Api.credentials.ts | 6 +++ .../PushbulletOAuth2Api.credentials.ts | 6 +++ .../QuickBooksOAuth2Api.credentials.ts | 6 +++ .../RaindropOAuth2Api.credentials.ts | 6 +++ .../RedditOAuth2Api.credentials.ts | 6 +++ .../SalesforceOAuth2Api.credentials.ts | 6 +++ .../SentryIoOAuth2Api.credentials.ts | 6 +++ .../ServiceNowOAuth2Api.credentials.ts | 6 +++ .../credentials/SlackOAuth2Api.credentials.ts | 6 +++ .../SpotifyOAuth2Api.credentials.ts | 6 +++ .../StravaOAuth2Api.credentials.ts | 6 +++ .../SurveyMonkeyOAuth2Api.credentials.ts | 6 +++ .../TodoistOAuth2Api.credentials.ts | 6 +++ .../credentials/TwistOAuth2Api.credentials.ts | 6 +++ .../TypeformOAuth2Api.credentials.ts | 6 +++ .../WebflowOAuth2Api.credentials.ts | 6 +++ .../credentials/XeroOAuth2Api.credentials.ts | 6 +++ .../ZendeskOAuth2Api.credentials.ts | 6 +++ .../credentials/ZohoOAuth2Api.credentials.ts | 6 +++ .../credentials/ZoomOAuth2Api.credentials.ts | 6 +++ packages/workflow/src/Interfaces.ts | 5 +++ 55 files changed, 376 insertions(+), 2 deletions(-) diff --git a/packages/core/src/NodeExecuteFunctions.ts b/packages/core/src/NodeExecuteFunctions.ts index 0d0dbfe4a4429..9125faa3b3ee1 100644 --- a/packages/core/src/NodeExecuteFunctions.ts +++ b/packages/core/src/NodeExecuteFunctions.ts @@ -57,6 +57,7 @@ import { WorkflowExecuteMode, LoggerProxy as Logger, IExecuteData, + OAuth2GranType, } from 'n8n-workflow'; import { Agent } from 'https'; @@ -893,7 +894,32 @@ export async function requestOAuth2( accessTokenUri: credentials.accessTokenUrl as string, }); - const oauthTokenData = credentials.oauthTokenData as clientOAuth2.Data; + let oauthTokenData = credentials.oauthTokenData as clientOAuth2.Data; + + // if it's the first time using the credentials, get the access token and save it into the DB. + if (credentials.grantType === OAuth2GranType.clientCredentials && oauthTokenData === undefined) { + const { data } = await oAuthClient.credentials.getToken(); + + credentials.oauthTokenData = data; + + // Find the credentials + if (!node.credentials || !node.credentials[credentialsType]) { + throw new Error( + `The node "${node.name}" does not have credentials of type "${credentialsType}"!`, + ); + } + + const nodeCredentials = node.credentials[credentialsType]; + + // Save the refreshed token + await additionalData.credentialsHelper.updateCredentials( + nodeCredentials, + credentialsType, + credentials, + ); + + oauthTokenData = data; + } const token = oAuthClient.createToken( get(oauthTokenData, oAuth2Options?.property as string) || oauthTokenData.accessToken, diff --git a/packages/editor-ui/src/components/CredentialEdit/CredentialEdit.vue b/packages/editor-ui/src/components/CredentialEdit/CredentialEdit.vue index 5b42ce9036c39..47cd63ad53495 100644 --- a/packages/editor-ui/src/components/CredentialEdit/CredentialEdit.vue +++ b/packages/editor-ui/src/components/CredentialEdit/CredentialEdit.vue @@ -302,7 +302,7 @@ export default mixins(showMessage, nodeHelpers).extend({ ['oAuth1Api', 'oAuth2Api'].includes(this.credentialTypeName) || this.parentTypes.includes('oAuth1Api') || this.parentTypes.includes('oAuth2Api') - ); + ) && (this.credentialData.grantType === 'authorizationCode'); }, isOAuthConnected(): boolean { return this.isOAuthType && !!this.credentialData.oauthTokenData; diff --git a/packages/nodes-base/credentials/AcuitySchedulingOAuth2Api.credentials.ts b/packages/nodes-base/credentials/AcuitySchedulingOAuth2Api.credentials.ts index b9526517c9050..e5b4832b55d63 100644 --- a/packages/nodes-base/credentials/AcuitySchedulingOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/AcuitySchedulingOAuth2Api.credentials.ts @@ -12,6 +12,12 @@ export class AcuitySchedulingOAuth2Api implements ICredentialType { displayName = 'AcuityScheduling OAuth2 API'; documentationUrl = 'acuityScheduling'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/AsanaOAuth2Api.credentials.ts b/packages/nodes-base/credentials/AsanaOAuth2Api.credentials.ts index 918425eb64665..f1a5d54e3a9c4 100644 --- a/packages/nodes-base/credentials/AsanaOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/AsanaOAuth2Api.credentials.ts @@ -11,6 +11,12 @@ export class AsanaOAuth2Api implements ICredentialType { displayName = 'Asana OAuth2 API'; documentationUrl = 'asana'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/BitlyOAuth2Api.credentials.ts b/packages/nodes-base/credentials/BitlyOAuth2Api.credentials.ts index 2db80630c84b2..d60dd421fc212 100644 --- a/packages/nodes-base/credentials/BitlyOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/BitlyOAuth2Api.credentials.ts @@ -12,6 +12,12 @@ export class BitlyOAuth2Api implements ICredentialType { 'oAuth2Api', ]; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/BoxOAuth2Api.credentials.ts b/packages/nodes-base/credentials/BoxOAuth2Api.credentials.ts index f900954e55a03..527da9dc9d8c0 100644 --- a/packages/nodes-base/credentials/BoxOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/BoxOAuth2Api.credentials.ts @@ -11,6 +11,12 @@ export class BoxOAuth2Api implements ICredentialType { displayName = 'Box OAuth2 API'; documentationUrl = 'box'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/CiscoWebexOAuth2Api.credentials.ts b/packages/nodes-base/credentials/CiscoWebexOAuth2Api.credentials.ts index c728e626f539b..2c96fe93fd5fc 100644 --- a/packages/nodes-base/credentials/CiscoWebexOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/CiscoWebexOAuth2Api.credentials.ts @@ -10,6 +10,12 @@ export class CiscoWebexOAuth2Api implements ICredentialType { ]; displayName = 'Cisco Webex OAuth2 API'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/ClickUpOAuth2Api.credentials.ts b/packages/nodes-base/credentials/ClickUpOAuth2Api.credentials.ts index b9ed95ea2892a..82dcdbf407fe8 100644 --- a/packages/nodes-base/credentials/ClickUpOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/ClickUpOAuth2Api.credentials.ts @@ -11,6 +11,12 @@ export class ClickUpOAuth2Api implements ICredentialType { displayName = 'ClickUp OAuth2 API'; documentationUrl = 'clickUp'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/DriftOAuth2Api.credentials.ts b/packages/nodes-base/credentials/DriftOAuth2Api.credentials.ts index 3fa072d66d77c..1d1fc9386bbec 100644 --- a/packages/nodes-base/credentials/DriftOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/DriftOAuth2Api.credentials.ts @@ -12,6 +12,12 @@ export class DriftOAuth2Api implements ICredentialType { displayName = 'Drift OAuth2 API'; documentationUrl = 'drift'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/DropboxOAuth2Api.credentials.ts b/packages/nodes-base/credentials/DropboxOAuth2Api.credentials.ts index 6ac0b512d3950..84596444533db 100644 --- a/packages/nodes-base/credentials/DropboxOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/DropboxOAuth2Api.credentials.ts @@ -18,6 +18,12 @@ export class DropboxOAuth2Api implements ICredentialType { displayName = 'Dropbox OAuth2 API'; documentationUrl = 'dropbox'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/EventbriteOAuth2Api.credentials.ts b/packages/nodes-base/credentials/EventbriteOAuth2Api.credentials.ts index ea339573ae0c5..cbfb2b4d0b95a 100644 --- a/packages/nodes-base/credentials/EventbriteOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/EventbriteOAuth2Api.credentials.ts @@ -12,6 +12,12 @@ export class EventbriteOAuth2Api implements ICredentialType { displayName = 'Eventbrite OAuth2 API'; documentationUrl = 'eventbrite'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/FormstackOAuth2Api.credentials.ts b/packages/nodes-base/credentials/FormstackOAuth2Api.credentials.ts index fc83e1187e32b..6d90f0441e5ad 100644 --- a/packages/nodes-base/credentials/FormstackOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/FormstackOAuth2Api.credentials.ts @@ -13,6 +13,12 @@ export class FormstackOAuth2Api implements ICredentialType { displayName = 'Formstack OAuth2 API'; documentationUrl = 'formstackTrigger'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/GetResponseOAuth2Api.credentials.ts b/packages/nodes-base/credentials/GetResponseOAuth2Api.credentials.ts index 9bd8e6a61ef24..98696ed33a5f5 100644 --- a/packages/nodes-base/credentials/GetResponseOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/GetResponseOAuth2Api.credentials.ts @@ -10,6 +10,12 @@ export class GetResponseOAuth2Api implements ICredentialType { ]; displayName = 'GetResponse OAuth2 API'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/GithubOAuth2Api.credentials.ts b/packages/nodes-base/credentials/GithubOAuth2Api.credentials.ts index 401b44825f758..40f2def13e7d6 100644 --- a/packages/nodes-base/credentials/GithubOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/GithubOAuth2Api.credentials.ts @@ -12,6 +12,12 @@ export class GithubOAuth2Api implements ICredentialType { displayName = 'GitHub OAuth2 API'; documentationUrl = 'github'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Github Server', name: 'server', diff --git a/packages/nodes-base/credentials/GitlabOAuth2Api.credentials.ts b/packages/nodes-base/credentials/GitlabOAuth2Api.credentials.ts index 2f49e4caedcd6..6ed5a06868ca6 100644 --- a/packages/nodes-base/credentials/GitlabOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/GitlabOAuth2Api.credentials.ts @@ -12,6 +12,12 @@ export class GitlabOAuth2Api implements ICredentialType { displayName = 'GitLab OAuth2 API'; documentationUrl = 'gitlab'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Gitlab Server', name: 'server', diff --git a/packages/nodes-base/credentials/GoToWebinarOAuth2Api.credentials.ts b/packages/nodes-base/credentials/GoToWebinarOAuth2Api.credentials.ts index e21166610ac85..afc0264814160 100644 --- a/packages/nodes-base/credentials/GoToWebinarOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/GoToWebinarOAuth2Api.credentials.ts @@ -11,6 +11,12 @@ export class GoToWebinarOAuth2Api implements ICredentialType { displayName = 'GoToWebinar OAuth2 API'; documentationUrl = 'goToWebinar'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/GoogleOAuth2Api.credentials.ts b/packages/nodes-base/credentials/GoogleOAuth2Api.credentials.ts index 241c6e051d28c..82027c928288d 100644 --- a/packages/nodes-base/credentials/GoogleOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/GoogleOAuth2Api.credentials.ts @@ -12,6 +12,12 @@ export class GoogleOAuth2Api implements ICredentialType { documentationUrl = 'google'; icon = 'file:Google.svg'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/HarvestOAuth2Api.credentials.ts b/packages/nodes-base/credentials/HarvestOAuth2Api.credentials.ts index 9a9a454f8d490..6d71477ba2544 100644 --- a/packages/nodes-base/credentials/HarvestOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/HarvestOAuth2Api.credentials.ts @@ -11,6 +11,12 @@ export class HarvestOAuth2Api implements ICredentialType { ]; displayName = 'Harvest OAuth2 API'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/HelpScoutOAuth2Api.credentials.ts b/packages/nodes-base/credentials/HelpScoutOAuth2Api.credentials.ts index 1b6252bc503a3..85aa345c194a5 100644 --- a/packages/nodes-base/credentials/HelpScoutOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/HelpScoutOAuth2Api.credentials.ts @@ -11,6 +11,12 @@ export class HelpScoutOAuth2Api implements ICredentialType { displayName = 'HelpScout OAuth2 API'; documentationUrl = 'helpScout'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/HubspotDeveloperApi.credentials.ts b/packages/nodes-base/credentials/HubspotDeveloperApi.credentials.ts index e03782052caa9..a3d66aa8a159d 100644 --- a/packages/nodes-base/credentials/HubspotDeveloperApi.credentials.ts +++ b/packages/nodes-base/credentials/HubspotDeveloperApi.credentials.ts @@ -20,6 +20,12 @@ export class HubspotDeveloperApi implements ICredentialType { 'oAuth2Api', ]; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/HubspotOAuth2Api.credentials.ts b/packages/nodes-base/credentials/HubspotOAuth2Api.credentials.ts index fd31a368d9a7a..0e3e21a06f4fc 100644 --- a/packages/nodes-base/credentials/HubspotOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/HubspotOAuth2Api.credentials.ts @@ -26,6 +26,12 @@ export class HubspotOAuth2Api implements ICredentialType { displayName = 'HubSpot OAuth2 API'; documentationUrl = 'hubspot'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/KeapOAuth2Api.credentials.ts b/packages/nodes-base/credentials/KeapOAuth2Api.credentials.ts index b123e5e29f766..3f62fc0f88e11 100644 --- a/packages/nodes-base/credentials/KeapOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/KeapOAuth2Api.credentials.ts @@ -15,6 +15,12 @@ export class KeapOAuth2Api implements ICredentialType { displayName = 'Keap OAuth2 API'; documentationUrl = 'keap'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/LineNotifyOAuth2Api.credentials.ts b/packages/nodes-base/credentials/LineNotifyOAuth2Api.credentials.ts index 618b0e1d2078b..3d63f20f90548 100644 --- a/packages/nodes-base/credentials/LineNotifyOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/LineNotifyOAuth2Api.credentials.ts @@ -11,6 +11,12 @@ export class LineNotifyOAuth2Api implements ICredentialType { displayName = 'Line Notify OAuth2 API'; documentationUrl = 'line'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/LinkedInOAuth2Api.credentials.ts b/packages/nodes-base/credentials/LinkedInOAuth2Api.credentials.ts index 100e9ec779aa9..012b5795ba869 100644 --- a/packages/nodes-base/credentials/LinkedInOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/LinkedInOAuth2Api.credentials.ts @@ -12,6 +12,12 @@ export class LinkedInOAuth2Api implements ICredentialType { displayName = 'LinkedIn OAuth2 API'; documentationUrl = 'linkedIn'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Organization Support', name: 'organizationSupport', diff --git a/packages/nodes-base/credentials/MailchimpOAuth2Api.credentials.ts b/packages/nodes-base/credentials/MailchimpOAuth2Api.credentials.ts index 3cd433efb24e7..392fadbecc5c3 100644 --- a/packages/nodes-base/credentials/MailchimpOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/MailchimpOAuth2Api.credentials.ts @@ -12,6 +12,12 @@ export class MailchimpOAuth2Api implements ICredentialType { displayName = 'Mailchimp OAuth2 API'; documentationUrl = 'mailchimp'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/MauticOAuth2Api.credentials.ts b/packages/nodes-base/credentials/MauticOAuth2Api.credentials.ts index ea5e131ded5fe..58e4898c36c7e 100644 --- a/packages/nodes-base/credentials/MauticOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/MauticOAuth2Api.credentials.ts @@ -11,6 +11,12 @@ export class MauticOAuth2Api implements ICredentialType { displayName = 'Mautic OAuth2 API'; documentationUrl = 'mautic'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'URL', name: 'url', diff --git a/packages/nodes-base/credentials/MediumOAuth2Api.credentials.ts b/packages/nodes-base/credentials/MediumOAuth2Api.credentials.ts index 5f01fcfda892e..1f9ae1199fdfd 100644 --- a/packages/nodes-base/credentials/MediumOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/MediumOAuth2Api.credentials.ts @@ -11,6 +11,12 @@ export class MediumOAuth2Api implements ICredentialType { displayName = 'Medium OAuth2 API'; documentationUrl = 'medium'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/MicrosoftOAuth2Api.credentials.ts b/packages/nodes-base/credentials/MicrosoftOAuth2Api.credentials.ts index 193c056fb5db0..d491e4b5fed4c 100644 --- a/packages/nodes-base/credentials/MicrosoftOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/MicrosoftOAuth2Api.credentials.ts @@ -12,6 +12,12 @@ export class MicrosoftOAuth2Api implements ICredentialType { displayName = 'Microsoft OAuth2 API'; documentationUrl = 'microsoft'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, //info about the tenantID //https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-v2-protocols#endpoints { diff --git a/packages/nodes-base/credentials/MondayComOAuth2Api.credentials.ts b/packages/nodes-base/credentials/MondayComOAuth2Api.credentials.ts index dbd3dd512f4f8..b4ab8efde134a 100644 --- a/packages/nodes-base/credentials/MondayComOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/MondayComOAuth2Api.credentials.ts @@ -16,6 +16,12 @@ export class MondayComOAuth2Api implements ICredentialType { displayName = 'Monday.com OAuth2 API'; documentationUrl = 'monday'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/NextCloudOAuth2Api.credentials.ts b/packages/nodes-base/credentials/NextCloudOAuth2Api.credentials.ts index 67af8dad117fc..e3acd84de0677 100644 --- a/packages/nodes-base/credentials/NextCloudOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/NextCloudOAuth2Api.credentials.ts @@ -12,6 +12,12 @@ export class NextCloudOAuth2Api implements ICredentialType { displayName = 'NextCloud OAuth2 API'; documentationUrl = 'nextCloud'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Web DAV URL', name: 'webDavUrl', diff --git a/packages/nodes-base/credentials/NotionOAuth2Api.credentials.ts b/packages/nodes-base/credentials/NotionOAuth2Api.credentials.ts index e9fe60abfec94..845f7bc4e3a54 100644 --- a/packages/nodes-base/credentials/NotionOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/NotionOAuth2Api.credentials.ts @@ -11,6 +11,12 @@ export class NotionOAuth2Api implements ICredentialType { displayName = 'Notion OAuth2 API'; documentationUrl = 'notion'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/OAuth2Api.credentials.ts b/packages/nodes-base/credentials/OAuth2Api.credentials.ts index f68b5fa25c9b9..2bb90f4359b2e 100644 --- a/packages/nodes-base/credentials/OAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/OAuth2Api.credentials.ts @@ -10,10 +10,33 @@ export class OAuth2Api implements ICredentialType { documentationUrl = 'httpRequest'; genericAuth = true; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'options', + options: [ + { + name: 'Authorization Code', + value: 'authorizationCode', + }, + { + name: 'Client Credentials', + value: 'clientCredentials', + }, + ], + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', type: 'string', + displayOptions: { + show: { + grantType: [ + 'authorizationCode', + ], + }, + }, default: '', required: true, }, @@ -51,6 +74,13 @@ export class OAuth2Api implements ICredentialType { displayName: 'Auth URI Query Parameters', name: 'authQueryParameters', type: 'string', + displayOptions: { + show: { + grantType: [ + 'authorizationCode', + ], + }, + }, default: '', description: 'For some services additional query parameters have to be set which can be defined here', placeholder: 'access_type=offline', @@ -59,6 +89,13 @@ export class OAuth2Api implements ICredentialType { displayName: 'Authentication', name: 'authentication', type: 'options', + displayOptions: { + show: { + grantType: [ + 'authorizationCode', + ], + }, + }, options: [ { name: 'Body', diff --git a/packages/nodes-base/credentials/PagerDutyOAuth2Api.credentials.ts b/packages/nodes-base/credentials/PagerDutyOAuth2Api.credentials.ts index a14961d303e27..c1af87cd64061 100644 --- a/packages/nodes-base/credentials/PagerDutyOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/PagerDutyOAuth2Api.credentials.ts @@ -11,6 +11,12 @@ export class PagerDutyOAuth2Api implements ICredentialType { displayName = 'PagerDuty OAuth2 API'; documentationUrl = 'pagerDuty'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/PhilipsHueOAuth2Api.credentials.ts b/packages/nodes-base/credentials/PhilipsHueOAuth2Api.credentials.ts index c5ed89b6f64ac..1222627d6168a 100644 --- a/packages/nodes-base/credentials/PhilipsHueOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/PhilipsHueOAuth2Api.credentials.ts @@ -11,6 +11,12 @@ export class PhilipsHueOAuth2Api implements ICredentialType { displayName = 'PhilipHue OAuth2 API'; documentationUrl = 'philipsHue'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'APP ID', name: 'appId', diff --git a/packages/nodes-base/credentials/PipedriveOAuth2Api.credentials.ts b/packages/nodes-base/credentials/PipedriveOAuth2Api.credentials.ts index 19ab08b823269..f82fb5af32dce 100644 --- a/packages/nodes-base/credentials/PipedriveOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/PipedriveOAuth2Api.credentials.ts @@ -11,6 +11,12 @@ export class PipedriveOAuth2Api implements ICredentialType { displayName = 'Pipedrive OAuth2 API'; documentationUrl = 'pipedrive'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/PushbulletOAuth2Api.credentials.ts b/packages/nodes-base/credentials/PushbulletOAuth2Api.credentials.ts index 7be6b14fbc5c6..891e04b123555 100644 --- a/packages/nodes-base/credentials/PushbulletOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/PushbulletOAuth2Api.credentials.ts @@ -11,6 +11,12 @@ export class PushbulletOAuth2Api implements ICredentialType { displayName = 'Pushbullet OAuth2 API'; documentationUrl = 'pushbullet'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/QuickBooksOAuth2Api.credentials.ts b/packages/nodes-base/credentials/QuickBooksOAuth2Api.credentials.ts index d1683dea490ae..3a7ed6b17cb34 100644 --- a/packages/nodes-base/credentials/QuickBooksOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/QuickBooksOAuth2Api.credentials.ts @@ -13,6 +13,12 @@ export class QuickBooksOAuth2Api implements ICredentialType { displayName = 'QuickBooks Online OAuth2 API'; documentationUrl = 'quickbooks'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/RaindropOAuth2Api.credentials.ts b/packages/nodes-base/credentials/RaindropOAuth2Api.credentials.ts index abe26491dd320..5940bff4a758d 100644 --- a/packages/nodes-base/credentials/RaindropOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/RaindropOAuth2Api.credentials.ts @@ -13,6 +13,12 @@ export class RaindropOAuth2Api implements ICredentialType { displayName = 'Raindrop OAuth2 API'; documentationUrl = 'raindrop'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/RedditOAuth2Api.credentials.ts b/packages/nodes-base/credentials/RedditOAuth2Api.credentials.ts index b800a03b7f9e4..03cab714fba40 100644 --- a/packages/nodes-base/credentials/RedditOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/RedditOAuth2Api.credentials.ts @@ -23,6 +23,12 @@ export class RedditOAuth2Api implements ICredentialType { displayName = 'Reddit OAuth2 API'; documentationUrl = 'reddit'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Auth URI Query Parameters', name: 'authQueryParameters', diff --git a/packages/nodes-base/credentials/SalesforceOAuth2Api.credentials.ts b/packages/nodes-base/credentials/SalesforceOAuth2Api.credentials.ts index 517fde6f5ba60..b46e58295a5e9 100644 --- a/packages/nodes-base/credentials/SalesforceOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/SalesforceOAuth2Api.credentials.ts @@ -27,6 +27,12 @@ export class SalesforceOAuth2Api implements ICredentialType { ], default: 'production', }, + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/SentryIoOAuth2Api.credentials.ts b/packages/nodes-base/credentials/SentryIoOAuth2Api.credentials.ts index d1d8893017360..596ec6936b9c5 100644 --- a/packages/nodes-base/credentials/SentryIoOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/SentryIoOAuth2Api.credentials.ts @@ -11,6 +11,12 @@ export class SentryIoOAuth2Api implements ICredentialType { displayName = 'Sentry.io OAuth2 API'; documentationUrl = 'sentryIo'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/ServiceNowOAuth2Api.credentials.ts b/packages/nodes-base/credentials/ServiceNowOAuth2Api.credentials.ts index 4bd0bc2e19269..4877bd5c53d4a 100644 --- a/packages/nodes-base/credentials/ServiceNowOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/ServiceNowOAuth2Api.credentials.ts @@ -19,6 +19,12 @@ export class ServiceNowOAuth2Api implements ICredentialType { hint: 'The subdomain can be extracted from the URL. If the URL is: https://dev99890.service-now.com the subdomain is dev99890', required: true, }, + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/SlackOAuth2Api.credentials.ts b/packages/nodes-base/credentials/SlackOAuth2Api.credentials.ts index fa8efae44c96f..f8e7cacebd94d 100644 --- a/packages/nodes-base/credentials/SlackOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/SlackOAuth2Api.credentials.ts @@ -30,6 +30,12 @@ export class SlackOAuth2Api implements ICredentialType { displayName = 'Slack OAuth2 API'; documentationUrl = 'slack'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/SpotifyOAuth2Api.credentials.ts b/packages/nodes-base/credentials/SpotifyOAuth2Api.credentials.ts index 7dcce7ae75667..7da55605c7dce 100644 --- a/packages/nodes-base/credentials/SpotifyOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/SpotifyOAuth2Api.credentials.ts @@ -18,6 +18,12 @@ export class SpotifyOAuth2Api implements ICredentialType { type: 'hidden', default: 'https://api.spotify.com/', }, + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/StravaOAuth2Api.credentials.ts b/packages/nodes-base/credentials/StravaOAuth2Api.credentials.ts index 9769aeebe08f4..6c2bb32617544 100644 --- a/packages/nodes-base/credentials/StravaOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/StravaOAuth2Api.credentials.ts @@ -11,6 +11,12 @@ export class StravaOAuth2Api implements ICredentialType { displayName = 'Strava OAuth2 API'; documentationUrl = 'strava'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/SurveyMonkeyOAuth2Api.credentials.ts b/packages/nodes-base/credentials/SurveyMonkeyOAuth2Api.credentials.ts index 6ba797098667a..6626c8d48ff6c 100644 --- a/packages/nodes-base/credentials/SurveyMonkeyOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/SurveyMonkeyOAuth2Api.credentials.ts @@ -20,6 +20,12 @@ export class SurveyMonkeyOAuth2Api implements ICredentialType { displayName = 'SurveyMonkey OAuth2 API'; documentationUrl = 'surveyMonkey'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/TodoistOAuth2Api.credentials.ts b/packages/nodes-base/credentials/TodoistOAuth2Api.credentials.ts index 6547c773dd680..ab0c5371c99d6 100644 --- a/packages/nodes-base/credentials/TodoistOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/TodoistOAuth2Api.credentials.ts @@ -11,6 +11,12 @@ export class TodoistOAuth2Api implements ICredentialType { displayName = 'Todoist OAuth2 API'; documentationUrl = 'todoist'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/TwistOAuth2Api.credentials.ts b/packages/nodes-base/credentials/TwistOAuth2Api.credentials.ts index 3f0011ac491ec..971edfc48fb01 100644 --- a/packages/nodes-base/credentials/TwistOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/TwistOAuth2Api.credentials.ts @@ -20,6 +20,12 @@ export class TwistOAuth2Api implements ICredentialType { displayName = 'Twist OAuth2 API'; documentationUrl = 'twist'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/TypeformOAuth2Api.credentials.ts b/packages/nodes-base/credentials/TypeformOAuth2Api.credentials.ts index 79c629632f349..ab7efa81c1e74 100644 --- a/packages/nodes-base/credentials/TypeformOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/TypeformOAuth2Api.credentials.ts @@ -17,6 +17,12 @@ export class TypeformOAuth2Api implements ICredentialType { displayName = 'Typeform OAuth2 API'; documentationUrl = 'typeform'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/WebflowOAuth2Api.credentials.ts b/packages/nodes-base/credentials/WebflowOAuth2Api.credentials.ts index 653b114c2eaf2..56b3bb0badb73 100644 --- a/packages/nodes-base/credentials/WebflowOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/WebflowOAuth2Api.credentials.ts @@ -11,6 +11,12 @@ export class WebflowOAuth2Api implements ICredentialType { displayName = 'Webflow OAuth2 API'; documentationUrl = 'webflow'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/XeroOAuth2Api.credentials.ts b/packages/nodes-base/credentials/XeroOAuth2Api.credentials.ts index 04d7f88c593c1..79fb3a9ad6f76 100644 --- a/packages/nodes-base/credentials/XeroOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/XeroOAuth2Api.credentials.ts @@ -18,6 +18,12 @@ export class XeroOAuth2Api implements ICredentialType { displayName = 'Xero OAuth2 API'; documentationUrl = 'xero'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/ZendeskOAuth2Api.credentials.ts b/packages/nodes-base/credentials/ZendeskOAuth2Api.credentials.ts index e37a1f125a650..feba9ca66a9a5 100644 --- a/packages/nodes-base/credentials/ZendeskOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/ZendeskOAuth2Api.credentials.ts @@ -25,6 +25,12 @@ export class ZendeskOAuth2Api implements ICredentialType { description: 'The subdomain of your Zendesk work environment', required: true, }, + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/ZohoOAuth2Api.credentials.ts b/packages/nodes-base/credentials/ZohoOAuth2Api.credentials.ts index 811efaeee3c78..86a966f4aa847 100644 --- a/packages/nodes-base/credentials/ZohoOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/ZohoOAuth2Api.credentials.ts @@ -11,6 +11,12 @@ export class ZohoOAuth2Api implements ICredentialType { displayName = 'Zoho OAuth2 API'; documentationUrl = 'zoho'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/nodes-base/credentials/ZoomOAuth2Api.credentials.ts b/packages/nodes-base/credentials/ZoomOAuth2Api.credentials.ts index db38dd541c3f8..7268aaba141a6 100644 --- a/packages/nodes-base/credentials/ZoomOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/ZoomOAuth2Api.credentials.ts @@ -9,6 +9,12 @@ export class ZoomOAuth2Api implements ICredentialType { displayName = 'Zoom OAuth2 API'; documentationUrl = 'zoom'; properties: INodeProperties[] = [ + { + displayName: 'Grant Type', + name: 'grantType', + type: 'hidden', + default: 'authorizationCode', + }, { displayName: 'Authorization URL', name: 'authUrl', diff --git a/packages/workflow/src/Interfaces.ts b/packages/workflow/src/Interfaces.ts index 0e9d8888ea1d1..91d0903d0b966 100644 --- a/packages/workflow/src/Interfaces.ts +++ b/packages/workflow/src/Interfaces.ts @@ -1535,3 +1535,8 @@ export interface IConnectedNode { indicies: number[]; depth: number; } + +export enum OAuth2GranType { + authorizationCode = 'authorizationCode', + clientCredentials = 'clientCredentials', +} From 8a9df7e78e81ebebe702ecdef6bf55c4f9334f0e Mon Sep 17 00:00:00 2001 From: ricardo Date: Mon, 6 Jun 2022 17:48:01 -0400 Subject: [PATCH 2/7] :zap: Improvements --- packages/core/src/NodeExecuteFunctions.ts | 18 ++++++++++++------ packages/workflow/src/Interfaces.ts | 10 ++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/packages/core/src/NodeExecuteFunctions.ts b/packages/core/src/NodeExecuteFunctions.ts index 9125faa3b3ee1..a4ef78c41b52e 100644 --- a/packages/core/src/NodeExecuteFunctions.ts +++ b/packages/core/src/NodeExecuteFunctions.ts @@ -58,6 +58,7 @@ import { LoggerProxy as Logger, IExecuteData, OAuth2GranType, + IOAuth2Credentials, } from 'n8n-workflow'; import { Agent } from 'https'; @@ -882,16 +883,21 @@ export async function requestOAuth2( oAuth2Options?: IOAuth2Options, isN8nRequest = false, ) { - const credentials = await this.getCredentials(credentialsType); + const credentials = (await this.getCredentials(credentialsType)) as unknown as IOAuth2Credentials; - if (credentials.oauthTokenData === undefined) { + // Only the authorization code grant type needs connection + if ( + credentials.grantType === OAuth2GranType.authorizationCode && + credentials.oauthTokenData === undefined + ) { throw new Error('OAuth credentials not connected!'); } const oAuthClient = new clientOAuth2({ - clientId: credentials.clientId as string, - clientSecret: credentials.clientSecret as string, - accessTokenUri: credentials.accessTokenUrl as string, + clientId: credentials.clientId, + clientSecret: credentials.clientSecret, + accessTokenUri: credentials.accessTokenUrl, + scopes: credentials.scope.split(' '), }); let oauthTokenData = credentials.oauthTokenData as clientOAuth2.Data; @@ -915,7 +921,7 @@ export async function requestOAuth2( await additionalData.credentialsHelper.updateCredentials( nodeCredentials, credentialsType, - credentials, + credentials as unknown as ICredentialDataDecryptedObject, ); oauthTokenData = data; diff --git a/packages/workflow/src/Interfaces.ts b/packages/workflow/src/Interfaces.ts index 91d0903d0b966..e7241e77506b8 100644 --- a/packages/workflow/src/Interfaces.ts +++ b/packages/workflow/src/Interfaces.ts @@ -1540,3 +1540,13 @@ export enum OAuth2GranType { authorizationCode = 'authorizationCode', clientCredentials = 'clientCredentials', } +export interface IOAuth2Credentials { + grantType: 'OAuth2GranType'; + clientId: string; + clientSecret: string; + accessTokenUrl: string; + authUrl: string; + authQueryParameters: string; + authentication: 'body' | 'header'; + scope: string; +} From 0ac93425851ae4662447ca9a530c25c226705541 Mon Sep 17 00:00:00 2001 From: ricardo Date: Thu, 9 Jun 2022 10:38:58 -0400 Subject: [PATCH 3/7] :bug: Fix linting issue --- packages/core/src/NodeExecuteFunctions.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/core/src/NodeExecuteFunctions.ts b/packages/core/src/NodeExecuteFunctions.ts index c533cf4546162..48cae5320dd3e 100644 --- a/packages/core/src/NodeExecuteFunctions.ts +++ b/packages/core/src/NodeExecuteFunctions.ts @@ -955,8 +955,8 @@ export async function requestOAuth2( if (oAuth2Options?.includeCredentialsOnRefreshOnBody) { const body: IDataObject = { - client_id: credentials.clientId as string, - client_secret: credentials.clientSecret as string, + client_id: credentials.clientId, + client_secret: credentials.clientSecret, }; tokenRefreshOptions.body = body; // Override authorization property so the credentails are not included in it @@ -989,7 +989,7 @@ export async function requestOAuth2( await additionalData.credentialsHelper.updateCredentials( nodeCredentials, credentialsType, - credentials, + credentials as unknown as ICredentialDataDecryptedObject, ); Logger.debug( From ea7e58276283b3d9c1089d4d3e431e2119d89d76 Mon Sep 17 00:00:00 2001 From: ricardo Date: Thu, 9 Jun 2022 14:07:34 -0400 Subject: [PATCH 4/7] :bug: Fix typo --- packages/core/src/NodeExecuteFunctions.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/core/src/NodeExecuteFunctions.ts b/packages/core/src/NodeExecuteFunctions.ts index 48cae5320dd3e..be3a90a8f3ba3 100644 --- a/packages/core/src/NodeExecuteFunctions.ts +++ b/packages/core/src/NodeExecuteFunctions.ts @@ -57,7 +57,7 @@ import { WorkflowExecuteMode, LoggerProxy as Logger, IExecuteData, - OAuth2GranType, + OAuth2GrantType, IOAuth2Credentials, } from 'n8n-workflow'; @@ -887,7 +887,7 @@ export async function requestOAuth2( // Only the OAuth2 with authorization code grant needs connection if ( - credentials.grantType === OAuth2GranType.authorizationCode && + credentials.grantType === OAuth2GrantType.authorizationCode && credentials.oauthTokenData === undefined ) { throw new Error('OAuth credentials not connected!'); @@ -902,7 +902,7 @@ export async function requestOAuth2( let oauthTokenData = credentials.oauthTokenData as clientOAuth2.Data; // if it's the first time using the credentials, get the access token and save it into the DB. - if (credentials.grantType === OAuth2GranType.clientCredentials && oauthTokenData === undefined) { + if (credentials.grantType === OAuth2GrantType.clientCredentials && oauthTokenData === undefined) { const { data } = await oAuthClient.credentials.getToken(); // Find the credentials From 9e1323bc3cea8517a3ddc70f9c1810f502569b12 Mon Sep 17 00:00:00 2001 From: ricardo Date: Thu, 9 Jun 2022 18:03:46 -0400 Subject: [PATCH 5/7] :bug: Fix small issue with type --- packages/workflow/src/Interfaces.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/workflow/src/Interfaces.ts b/packages/workflow/src/Interfaces.ts index d3aea36c2c702..bca49df2743e4 100644 --- a/packages/workflow/src/Interfaces.ts +++ b/packages/workflow/src/Interfaces.ts @@ -1536,7 +1536,7 @@ export interface IConnectedNode { depth: number; } -export enum OAuth2GranType { +export enum OAuth2GrantType { authorizationCode = 'authorizationCode', clientCredentials = 'clientCredentials', } From 518a6dc1045c71fdc2e667cbdc6204004bfdd2d1 Mon Sep 17 00:00:00 2001 From: ricardo Date: Fri, 10 Jun 2022 10:47:50 -0400 Subject: [PATCH 6/7] :bug: When token expire get a new one instead of refreshing it --- packages/core/src/NodeExecuteFunctions.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/core/src/NodeExecuteFunctions.ts b/packages/core/src/NodeExecuteFunctions.ts index be3a90a8f3ba3..1d93eb615be60 100644 --- a/packages/core/src/NodeExecuteFunctions.ts +++ b/packages/core/src/NodeExecuteFunctions.ts @@ -969,7 +969,15 @@ export async function requestOAuth2( `OAuth2 token for "${credentialsType}" used by node "${node.name}" expired. Should revalidate.`, ); - const newToken = await token.refresh(tokenRefreshOptions); + let newToken; + + // if it's OAuth2 with client credentials grant type, get a new token + // instead of refreshing it. + if (OAuth2GrantType.clientCredentials === credentials.grantType) { + newToken = await token.client.credentials.getToken(); + } else { + newToken = await token.refresh(tokenRefreshOptions); + } Logger.debug( `OAuth2 token for "${credentialsType}" used by node "${node.name}" has been renewed.`, From b2c01053dc853e817f4836c87267f5918706166c Mon Sep 17 00:00:00 2001 From: Jan Oberhauser Date: Mon, 13 Jun 2022 22:24:30 -0700 Subject: [PATCH 7/7] :zap: Fix issue that it did not display it correctly for OAuth1 --- .../components/CredentialEdit/CredentialEdit.vue | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/editor-ui/src/components/CredentialEdit/CredentialEdit.vue b/packages/editor-ui/src/components/CredentialEdit/CredentialEdit.vue index 47cd63ad53495..6b79502bcbcf5 100644 --- a/packages/editor-ui/src/components/CredentialEdit/CredentialEdit.vue +++ b/packages/editor-ui/src/components/CredentialEdit/CredentialEdit.vue @@ -299,10 +299,18 @@ export default mixins(showMessage, nodeHelpers).extend({ }, isOAuthType(): boolean { return !!this.credentialTypeName && ( - ['oAuth1Api', 'oAuth2Api'].includes(this.credentialTypeName) || - this.parentTypes.includes('oAuth1Api') || - this.parentTypes.includes('oAuth2Api') - ) && (this.credentialData.grantType === 'authorizationCode'); + ( + ( + this.credentialTypeName === 'oAuth2Api' || + this.parentTypes.includes('oAuth2Api') + ) && this.credentialData.grantType === 'authorizationCode' + ) + || + ( + this.credentialTypeName === 'oAuth1Api' || + this.parentTypes.includes('oAuth1Api') + ) + ); }, isOAuthConnected(): boolean { return this.isOAuthType && !!this.credentialData.oauthTokenData;