From 5295696e601a9b07653002750ea7b631f06b7700 Mon Sep 17 00:00:00 2001 From: Rupenieks <32895755+Rupenieks@users.noreply.github.com> Date: Sat, 25 Jul 2020 10:09:52 +0200 Subject: [PATCH] :sparkles: OAuth1 support for HTTP Request Node (#781) * Add OAuth1 * :white_check_mark: Added OAuth1 support. Changes to NodeExecuteFunctions: - Accommodating to use of both a URL and URI property in request options. - qs if not set is undefined, so checking for its length returns an error --- packages/core/src/NodeExecuteFunctions.ts | 17 ++++++++++---- packages/nodes-base/nodes/HttpRequest.node.ts | 23 +++++++++++++++++-- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/packages/core/src/NodeExecuteFunctions.ts b/packages/core/src/NodeExecuteFunctions.ts index f9811099ec..79b34457a8 100644 --- a/packages/core/src/NodeExecuteFunctions.ts +++ b/packages/core/src/NodeExecuteFunctions.ts @@ -188,7 +188,7 @@ export function requestOAuth2(this: IAllExecuteFunctions, credentialsType: strin * @param {(OptionsWithUrl | requestPromise.RequestPromiseOptions)} requestOptionså * @returns */ -export function requestOAuth1(this: IAllExecuteFunctions, credentialsType: string, requestOptions: OptionsWithUrl | requestPromise.RequestPromiseOptions) { +export function requestOAuth1(this: IAllExecuteFunctions, credentialsType: string, requestOptions: OptionsWithUrl | OptionsWithUri | requestPromise.RequestPromiseOptions) { const credentials = this.getCredentials(credentialsType) as ICredentialDataDecryptedObject; if (credentials === undefined) { @@ -221,14 +221,22 @@ export function requestOAuth1(this: IAllExecuteFunctions, credentialsType: strin }; const newRequestOptions = { - //@ts-ignore - url: requestOptions.url, method: requestOptions.method, data: { ...requestOptions.qs, ...requestOptions.body }, json: requestOptions.json, }; - if (Object.keys(requestOptions.qs).length !== 0) { + // Some RequestOptions have a URI and some have a URL + //@ts-ignores + if (requestOptions.url !== undefined) { + //@ts-ignore + newRequestOptions.url = requestOptions.url; + } else { + //@ts-ignore + newRequestOptions.url = requestOptions.uri; + } + + if (requestOptions.qs !== undefined) { //@ts-ignore newRequestOptions.qs = oauth.authorize(newRequestOptions as RequestOptions, token); } else { @@ -698,6 +706,7 @@ export function getExecuteFunctions(workflow: Workflow, runExecutionData: IRunEx return requestOAuth2.call(this, credentialsType, requestOptions, node, additionalData, tokenType, property); }, requestOAuth1(this: IAllExecuteFunctions, credentialsType: string, requestOptions: OptionsWithUrl | requestPromise.RequestPromiseOptions): Promise { // tslint:disable-line:no-any + console.log(requestOptions); return requestOAuth1.call(this, credentialsType, requestOptions); }, returnJsonArray, diff --git a/packages/nodes-base/nodes/HttpRequest.node.ts b/packages/nodes-base/nodes/HttpRequest.node.ts index 08d3a0ac15..41dedd25a1 100644 --- a/packages/nodes-base/nodes/HttpRequest.node.ts +++ b/packages/nodes-base/nodes/HttpRequest.node.ts @@ -71,6 +71,17 @@ export class HttpRequest implements INodeType { }, }, }, + { + name: 'oAuth1Api', + required: true, + displayOptions: { + show: { + authentication: [ + 'oAuth1', + ], + }, + }, + }, { name: 'oAuth2Api', required: true, @@ -101,6 +112,10 @@ export class HttpRequest implements INodeType { name: 'Header Auth', value: 'headerAuth' }, + { + name: 'OAuth1', + value: 'oAuth1' + }, { name: 'OAuth2', value: 'oAuth2' @@ -578,6 +593,7 @@ export class HttpRequest implements INodeType { const httpBasicAuth = this.getCredentials('httpBasicAuth'); const httpDigestAuth = this.getCredentials('httpDigestAuth'); const httpHeaderAuth = this.getCredentials('httpHeaderAuth'); + const oAuth1Api = this.getCredentials('oAuth1Api'); const oAuth2Api = this.getCredentials('oAuth2Api'); let requestOptions: OptionsWithUri; @@ -799,8 +815,11 @@ export class HttpRequest implements INodeType { } try { // Now that the options are all set make the actual http request - - if (oAuth2Api !== undefined) { + if (oAuth1Api !== undefined) { + //@ts-ignore + response = await this.helpers.requestOAuth1.call(this, 'oAuth1Api', requestOptions); + } + else if (oAuth2Api !== undefined) { //@ts-ignore response = await this.helpers.requestOAuth2.call(this, 'oAuth2Api', requestOptions, 'Bearer'); } else {