Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERPNext Integration #754

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions packages/cli/src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1203,23 +1203,26 @@ 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: {
client_id: _.get(oauthCredentials, 'clientId') as string,
client_secret: _.get(oauthCredentials, 'clientSecret', '') as string,
},
};
delete oAuth2Parameters.clientSecret;
}

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: `${WebhookHelpers.getWebhookBaseUrl()}${this.restEndpoint}/oauth2-credential/callback`,
scopes: _.split(_.get(oauthCredentials, 'scope', 'openid,') as string, ',')
});
const oAuthObj = new clientOAuth2(oAuth2Parameters);

const oauthToken = await oAuthObj.code.getToken(req.originalUrl, options);

Expand Down
68 changes: 68 additions & 0 deletions packages/nodes-base/credentials/BitlyOAuth2Api.credentials.ts
Original file line number Diff line number Diff line change
@@ -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.',
},
];
}
32 changes: 32 additions & 0 deletions packages/nodes-base/credentials/ERPNextApi.credentials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
ICredentialType,
NodePropertyTypes,
} from 'n8n-workflow';


export class ERPNextApi implements ICredentialType {
name = 'erpNextApi';
displayName = 'ERPNext API';
properties = [
{
displayName: 'API Key',
name: 'apiKey',
type: 'string' as NodePropertyTypes,
default: '',
},
{
displayName: 'API Secret',
name: 'apiSecret',
type: 'string' as NodePropertyTypes,
default: '',
},
{
displayName: 'Subdomain',
name: 'subdomain',
type: 'string' as NodePropertyTypes,
default: '',
placeholder: 'n8n',
description: 'ERPNext subdomain. For instance, entering n8n will make the url look like: https://n8n.erpnext.com/.'
},
];
}
68 changes: 68 additions & 0 deletions packages/nodes-base/credentials/ERPNextOAuth2Api.credentials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import {
ICredentialType,
NodePropertyTypes,
} from 'n8n-workflow';


export class ERPNextOAuth2Api implements ICredentialType {
name = 'erpNextOAuth2Api';
extends = [
'oAuth2Api',
];
displayName = 'ERPNext OAuth2 API';
properties = [
{
displayName: 'Subdomain',
name: 'subdomain',
type: 'string' as NodePropertyTypes,
default: '',
placeholder: 'n8n',
description: 'ERPNext subdomain. For instance, entering n8n will make the url look like: https://n8n.erpnext.com/.'
},
{
displayName: 'Authorization URL',
name: 'authUrl',
type: 'string' as NodePropertyTypes,
default: 'https://{SUBDOMAIN_HERE}.erpnext.com/api/method/frappe.integrations.oauth2.authorize',
required: true,
},
{
displayName: 'Access Token URL',
name: 'accessTokenUrl',
type: 'string' as NodePropertyTypes,
default: 'https://{SUBDOMAIN_HERE}.erpnext.com/api/method/frappe.integrations.oauth2.get_token',
required: true,
},
{
displayName: 'Auth URI Query Parameters',
name: 'authQueryParameters',
type: 'hidden' as NodePropertyTypes,
default: '',
},
{
displayName: 'Scope',
name: 'scope',
type: 'string' as NodePropertyTypes,
default: '',
},
{
displayName: 'Authentication',
name: 'authentication',
type: 'options' as NodePropertyTypes,
options: [
{
name: 'Body',
value: 'body',
description: 'Send credentials in body',
},
{
name: 'Header',
value: 'header',
description: 'Send credentials as Basic Auth header',
},
],
default: 'header',
description: 'Resource to consume.',
},
];
}
37 changes: 36 additions & 1 deletion packages/nodes-base/nodes/Bitly/Bitly.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,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',
Expand Down
28 changes: 19 additions & 9 deletions packages/nodes-base/nodes/Bitly/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ import {
ILoadOptionsFunctions,
} from 'n8n-core';
import { IDataObject } from 'n8n-workflow';
import { attachmentFields } from '../Salesforce/AttachmentDescription';

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');
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,
Expand All @@ -24,10 +22,22 @@ 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 {
//@ts-ignore
return await this.helpers.requestOAuth2!.call(this, 'bitlyOAuth2Api', options, 'Bearer');
}
} catch(error) {
throw new Error(error);
}
}

Expand Down
Loading