diff --git a/packages/cli/src/Server.ts b/packages/cli/src/Server.ts index d3333d361..374a64460 100644 --- a/packages/cli/src/Server.ts +++ b/packages/cli/src/Server.ts @@ -1255,6 +1255,15 @@ class App { let options = {}; + const oAuth2Parameters = { + clientId: _.get(oauthCredentials, 'clientId') as string, + clientSecret: _.get(oauthCredentials, 'clientSecret', '') as string, + accessTokenUri: _.get(oauthCredentials, 'accessTokenUrl', '') as string, + authorizationUri: _.get(oauthCredentials, 'authUrl', '') as string, + redirectUri: `${WebhookHelpers.getWebhookBaseUrl()}${this.restEndpoint}/oauth2-credential/callback`, + scopes: _.split(_.get(oauthCredentials, 'scope', 'openid,') as string, ',') + }; + if (_.get(oauthCredentials, 'authentication', 'header') as string === 'body') { options = { body: { @@ -1262,17 +1271,11 @@ class App { client_secret: _.get(oauthCredentials, 'clientSecret', '') as string, }, }; + delete oAuth2Parameters.clientSecret; } const redirectUri = `${WebhookHelpers.getWebhookBaseUrl()}${this.restEndpoint}/oauth2-credential/callback`; - const oAuthObj = new clientOAuth2({ - clientId: _.get(oauthCredentials, 'clientId') as string, - clientSecret: _.get(oauthCredentials, 'clientSecret', '') as string, - accessTokenUri: _.get(oauthCredentials, 'accessTokenUrl', '') as string, - authorizationUri: _.get(oauthCredentials, 'authUrl', '') as string, - redirectUri, - scopes: _.split(_.get(oauthCredentials, 'scope', 'openid,') as string, ',') - }); + const oAuthObj = new clientOAuth2(oAuth2Parameters); const queryParameters = req.originalUrl.split('?').splice(1, 1).join(''); diff --git a/packages/nodes-base/credentials/BitlyOAuth2Api.credentials.ts b/packages/nodes-base/credentials/BitlyOAuth2Api.credentials.ts new file mode 100644 index 000000000..0919e3063 --- /dev/null +++ b/packages/nodes-base/credentials/BitlyOAuth2Api.credentials.ts @@ -0,0 +1,68 @@ +import { + ICredentialType, + NodePropertyTypes, +} from 'n8n-workflow'; + + +export class BitlyOAuth2Api implements ICredentialType { + name = 'bitlyOAuth2Api'; + displayName = 'Bitly OAuth2 API'; + + extends = [ + 'oAuth2Api', + ]; + properties = [ + { + displayName: 'Authorization URL', + name: 'authUrl', + type: 'hidden' as NodePropertyTypes, + default: 'https://bitly.com/oauth/authorize', + required: true, + }, + { + displayName: 'Access Token URL', + name: 'accessTokenUrl', + type: 'hidden' as NodePropertyTypes, + default: 'https://api-ssl.bitly.com/oauth/access_token', + required: true, + }, + { + displayName: 'Client ID', + name: 'clientId', + type: 'string' as NodePropertyTypes, + default: '', + required: true, + }, + { + displayName: 'Client Secret', + name: 'clientSecret', + type: 'string' as NodePropertyTypes, + typeOptions: { + password: true, + }, + default: '', + required: true, + }, + { + displayName: 'Scope', + name: 'scope', + type: 'hidden' as NodePropertyTypes, + default: '', + }, + { + displayName: 'Auth URI Query Parameters', + name: 'authQueryParameters', + type: 'hidden' as NodePropertyTypes, + default: '', + description: 'For some services additional query parameters have to be set which can be defined here.', + placeholder: '', + }, + { + displayName: 'Authentication', + name: 'authentication', + type: 'hidden' as NodePropertyTypes, + default: 'body', + description: 'Resource to consume.', + }, + ]; +} diff --git a/packages/nodes-base/nodes/Bitly/Bitly.node.ts b/packages/nodes-base/nodes/Bitly/Bitly.node.ts index 725185300..d712238ef 100644 --- a/packages/nodes-base/nodes/Bitly/Bitly.node.ts +++ b/packages/nodes-base/nodes/Bitly/Bitly.node.ts @@ -1,6 +1,7 @@ import { IExecuteFunctions, } from 'n8n-core'; + import { IDataObject, INodeTypeDescription, @@ -9,10 +10,12 @@ import { ILoadOptionsFunctions, INodePropertyOptions, } from 'n8n-workflow'; + import { linkFields, linkOperations } from './LinkDescription'; + import { bitlyApiRequest, bitlyApiRequestAllItems, @@ -37,9 +40,44 @@ export class Bitly implements INodeType { { name: 'bitlyApi', required: true, - } + displayOptions: { + show: { + authentication: [ + 'accessToken', + ], + }, + }, + }, + { + name: 'bitlyOAuth2Api', + required: true, + displayOptions: { + show: { + authentication: [ + 'oAuth2', + ], + }, + }, + }, ], properties: [ + { + displayName: 'Authentication', + name: 'authentication', + type: 'options', + options: [ + { + name: 'Access Token', + value: 'accessToken', + }, + { + name: 'OAuth2', + value: 'oAuth2', + }, + ], + default: 'accessToken', + description: 'The resource to operate on.', + }, { displayName: 'Resource', name: 'resource', diff --git a/packages/nodes-base/nodes/Bitly/GenericFunctions.ts b/packages/nodes-base/nodes/Bitly/GenericFunctions.ts index 1564c4949..b44a894ad 100644 --- a/packages/nodes-base/nodes/Bitly/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Bitly/GenericFunctions.ts @@ -1,19 +1,22 @@ -import { OptionsWithUri } from 'request'; +import { + OptionsWithUri, + } from 'request'; + import { IExecuteFunctions, IExecuteSingleFunctions, IHookFunctions, ILoadOptionsFunctions, } from 'n8n-core'; -import { IDataObject } from 'n8n-workflow'; + +import { + IDataObject, + } from 'n8n-workflow'; export async function bitlyApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise { // tslint:disable-line:no-any - const credentials = this.getCredentials('bitlyApi'); - if (credentials === undefined) { - throw new Error('No credentials got returned!'); - } + const authenticationMethod = this.getNodeParameter('authentication', 0) as string; let options: OptionsWithUri = { - headers: { Authorization: `Bearer ${credentials.accessToken}`}, + headers: {}, method, qs, body, @@ -24,10 +27,30 @@ export async function bitlyApiRequest(this: IHookFunctions | IExecuteFunctions | if (Object.keys(options.body).length === 0) { delete options.body; } - try { - return await this.helpers.request!(options); - } catch (err) { - throw new Error(err); + + try{ + if (authenticationMethod === 'accessToken') { + const credentials = this.getCredentials('bitlyApi'); + if (credentials === undefined) { + throw new Error('No credentials got returned!'); + } + options.headers = { Authorization: `Bearer ${credentials.accessToken}`}; + + return await this.helpers.request!(options); + } else { + + return await this.helpers.requestOAuth2!.call(this, 'bitlyOAuth2Api', options, { tokenType: 'Bearer' }); + } + } catch(error) { + + if (error.response && error.response.body && error.response.body.message) { + // Try to return the error prettier + const errorBody = error.response.body; + throw new Error(`Bitly error response [${error.statusCode}]: ${errorBody.message}`); + } + + // Expected error data did not get returned so throw the actual error + throw error; } } diff --git a/packages/nodes-base/package.json b/packages/nodes-base/package.json index 60f8d387c..ddceee619 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -38,6 +38,7 @@ "dist/credentials/BannerbearApi.credentials.js", "dist/credentials/BitbucketApi.credentials.js", "dist/credentials/BitlyApi.credentials.js", + "dist/credentials/BitlyOAuth2Api.credentials.js", "dist/credentials/BoxOAuth2Api.credentials.js", "dist/credentials/ChargebeeApi.credentials.js", "dist/credentials/CircleCiApi.credentials.js",