mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-13 05:47:31 -08:00
Merge pull request #829 from n8n-io/Bitly-OAuth2-support
Bitly Oauth2 support
This commit is contained in:
commit
8baea56ac5
|
@ -1255,6 +1255,15 @@ class App {
|
||||||
|
|
||||||
let options = {};
|
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') {
|
if (_.get(oauthCredentials, 'authentication', 'header') as string === 'body') {
|
||||||
options = {
|
options = {
|
||||||
body: {
|
body: {
|
||||||
|
@ -1262,17 +1271,11 @@ class App {
|
||||||
client_secret: _.get(oauthCredentials, 'clientSecret', '') as string,
|
client_secret: _.get(oauthCredentials, 'clientSecret', '') as string,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
delete oAuth2Parameters.clientSecret;
|
||||||
}
|
}
|
||||||
const redirectUri = `${WebhookHelpers.getWebhookBaseUrl()}${this.restEndpoint}/oauth2-credential/callback`;
|
const redirectUri = `${WebhookHelpers.getWebhookBaseUrl()}${this.restEndpoint}/oauth2-credential/callback`;
|
||||||
|
|
||||||
const oAuthObj = new clientOAuth2({
|
const oAuthObj = new clientOAuth2(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,
|
|
||||||
scopes: _.split(_.get(oauthCredentials, 'scope', 'openid,') as string, ',')
|
|
||||||
});
|
|
||||||
|
|
||||||
const queryParameters = req.originalUrl.split('?').splice(1, 1).join('');
|
const queryParameters = req.originalUrl.split('?').splice(1, 1).join('');
|
||||||
|
|
||||||
|
|
|
@ -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.',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
import {
|
import {
|
||||||
IExecuteFunctions,
|
IExecuteFunctions,
|
||||||
} from 'n8n-core';
|
} from 'n8n-core';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
IDataObject,
|
IDataObject,
|
||||||
INodeTypeDescription,
|
INodeTypeDescription,
|
||||||
|
@ -9,10 +10,12 @@ import {
|
||||||
ILoadOptionsFunctions,
|
ILoadOptionsFunctions,
|
||||||
INodePropertyOptions,
|
INodePropertyOptions,
|
||||||
} from 'n8n-workflow';
|
} from 'n8n-workflow';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
linkFields,
|
linkFields,
|
||||||
linkOperations
|
linkOperations
|
||||||
} from './LinkDescription';
|
} from './LinkDescription';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
bitlyApiRequest,
|
bitlyApiRequest,
|
||||||
bitlyApiRequestAllItems,
|
bitlyApiRequestAllItems,
|
||||||
|
@ -37,9 +40,44 @@ export class Bitly implements INodeType {
|
||||||
{
|
{
|
||||||
name: 'bitlyApi',
|
name: 'bitlyApi',
|
||||||
required: true,
|
required: true,
|
||||||
}
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
authentication: [
|
||||||
|
'accessToken',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'bitlyOAuth2Api',
|
||||||
|
required: true,
|
||||||
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
authentication: [
|
||||||
|
'oAuth2',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
],
|
],
|
||||||
properties: [
|
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',
|
displayName: 'Resource',
|
||||||
name: 'resource',
|
name: 'resource',
|
||||||
|
|
|
@ -1,19 +1,22 @@
|
||||||
import { OptionsWithUri } from 'request';
|
import {
|
||||||
|
OptionsWithUri,
|
||||||
|
} from 'request';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
IExecuteFunctions,
|
IExecuteFunctions,
|
||||||
IExecuteSingleFunctions,
|
IExecuteSingleFunctions,
|
||||||
IHookFunctions,
|
IHookFunctions,
|
||||||
ILoadOptionsFunctions,
|
ILoadOptionsFunctions,
|
||||||
} from 'n8n-core';
|
} 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<any> { // tslint:disable-line:no-any
|
export async function bitlyApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
||||||
const credentials = this.getCredentials('bitlyApi');
|
const authenticationMethod = this.getNodeParameter('authentication', 0) as string;
|
||||||
if (credentials === undefined) {
|
|
||||||
throw new Error('No credentials got returned!');
|
|
||||||
}
|
|
||||||
let options: OptionsWithUri = {
|
let options: OptionsWithUri = {
|
||||||
headers: { Authorization: `Bearer ${credentials.accessToken}`},
|
headers: {},
|
||||||
method,
|
method,
|
||||||
qs,
|
qs,
|
||||||
body,
|
body,
|
||||||
|
@ -24,10 +27,30 @@ export async function bitlyApiRequest(this: IHookFunctions | IExecuteFunctions |
|
||||||
if (Object.keys(options.body).length === 0) {
|
if (Object.keys(options.body).length === 0) {
|
||||||
delete options.body;
|
delete options.body;
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
return await this.helpers.request!(options);
|
try{
|
||||||
} catch (err) {
|
if (authenticationMethod === 'accessToken') {
|
||||||
throw new Error(err);
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -38,6 +38,7 @@
|
||||||
"dist/credentials/BannerbearApi.credentials.js",
|
"dist/credentials/BannerbearApi.credentials.js",
|
||||||
"dist/credentials/BitbucketApi.credentials.js",
|
"dist/credentials/BitbucketApi.credentials.js",
|
||||||
"dist/credentials/BitlyApi.credentials.js",
|
"dist/credentials/BitlyApi.credentials.js",
|
||||||
|
"dist/credentials/BitlyOAuth2Api.credentials.js",
|
||||||
"dist/credentials/BoxOAuth2Api.credentials.js",
|
"dist/credentials/BoxOAuth2Api.credentials.js",
|
||||||
"dist/credentials/ChargebeeApi.credentials.js",
|
"dist/credentials/ChargebeeApi.credentials.js",
|
||||||
"dist/credentials/CircleCiApi.credentials.js",
|
"dist/credentials/CircleCiApi.credentials.js",
|
||||||
|
|
Loading…
Reference in a new issue