Merge pull request #679 from n8n-io/NextCloud-OAuth2-support

NextCloud OAuth2 support
This commit is contained in:
Ricardo Espinoza 2020-07-22 18:00:52 -04:00 committed by GitHub
commit b4508aae48
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 174 additions and 21 deletions

View file

@ -12,7 +12,7 @@ export class NextCloudApi implements ICredentialType {
displayName: 'Web DAV URL', displayName: 'Web DAV URL',
name: 'webDavUrl', name: 'webDavUrl',
type: 'string' as NodePropertyTypes, type: 'string' as NodePropertyTypes,
placeholder: 'https://nextcloud.example.com/remote.php/webdav/', placeholder: 'https://nextcloud.example.com/remote.php/webdav',
default: '', default: '',
}, },
{ {

View file

@ -0,0 +1,54 @@
import {
ICredentialType,
NodePropertyTypes,
} from 'n8n-workflow';
export class NextCloudOAuth2Api implements ICredentialType {
name = 'nextCloudOAuth2Api';
extends = [
'oAuth2Api',
];
displayName = 'NextCloud OAuth2 API';
properties = [
{
displayName: 'Web DAV URL',
name: 'webDavUrl',
type: 'string' as NodePropertyTypes,
placeholder: 'https://nextcloud.example.com/remote.php/webdav',
default: '',
},
{
displayName: 'Authorization URL',
name: 'authUrl',
type: 'string' as NodePropertyTypes,
default: 'https://nextcloud.example.com/apps/oauth2/authorize',
required: true,
},
{
displayName: 'Access Token URL',
name: 'accessTokenUrl',
type: 'string' as NodePropertyTypes,
default: 'https://nextcloud.example.com/apps/oauth2/api/v1/token',
required: true,
},
{
displayName: 'Scope',
name: 'scope',
type: 'hidden' as NodePropertyTypes,
default: '',
},
{
displayName: 'Auth URI Query Parameters',
name: 'authQueryParameters',
type: 'hidden' as NodePropertyTypes,
default: '',
},
{
displayName: 'Authentication',
name: 'authentication',
type: 'hidden' as NodePropertyTypes,
default: 'body',
},
];
}

View file

@ -0,0 +1,63 @@
import {
IExecuteFunctions,
IHookFunctions,
} from 'n8n-core';
import {
OptionsWithUri,
} from 'request';
/**
* Make an API request to NextCloud
*
* @param {IHookFunctions} this
* @param {string} method
* @param {string} url
* @param {object} body
* @returns {Promise<any>}
*/
export async function nextCloudApiRequest(this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, body: object | string | Buffer, headers?: object, encoding?: null | undefined, query?: object): Promise<any> { // tslint:disable-line:no-any
const options : OptionsWithUri = {
headers,
method,
body,
qs: {},
uri: '',
json: false,
};
if (encoding === null) {
options.encoding = null;
}
const authenticationMethod = this.getNodeParameter('authentication', 0);
try {
if (authenticationMethod === 'accessToken') {
const credentials = this.getCredentials('nextCloudApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
options.auth = {
user: credentials.user as string,
pass: credentials.password as string,
};
options.uri = `${credentials.webDavUrl}/${encodeURI(endpoint)}`;
return await this.helpers.request(options);
} else {
const credentials = this.getCredentials('nextCloudOAuth2Api');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
options.uri = `${credentials.webDavUrl}/${encodeURI(endpoint)}`;
return await this.helpers.requestOAuth2!.call(this, 'nextCloudOAuth2Api', options);
}
} catch (error) {
throw new Error(`NextCloud Error. Status Code: ${error.statusCode}. Message: ${error.message}`);
}
}

View file

@ -2,6 +2,7 @@ import {
BINARY_ENCODING, BINARY_ENCODING,
IExecuteFunctions, IExecuteFunctions,
} from 'n8n-core'; } from 'n8n-core';
import { import {
IDataObject, IDataObject,
INodeTypeDescription, INodeTypeDescription,
@ -9,9 +10,13 @@ import {
INodeType, INodeType,
} from 'n8n-workflow'; } from 'n8n-workflow';
import { parseString } from 'xml2js'; import {
import { OptionsWithUri } from 'request'; parseString,
} from 'xml2js';
import {
nextCloudApiRequest,
} from './GenericFunctions';
export class NextCloud implements INodeType { export class NextCloud implements INodeType {
description: INodeTypeDescription = { description: INodeTypeDescription = {
@ -24,7 +29,7 @@ export class NextCloud implements INodeType {
description: 'Access data on NextCloud', description: 'Access data on NextCloud',
defaults: { defaults: {
name: 'NextCloud', name: 'NextCloud',
color: '#22BB44', color: '#1cafff',
}, },
inputs: ['main'], inputs: ['main'],
outputs: ['main'], outputs: ['main'],
@ -32,9 +37,44 @@ export class NextCloud implements INodeType {
{ {
name: 'nextCloudApi', name: 'nextCloudApi',
required: true, required: true,
} displayOptions: {
show: {
authentication: [
'accessToken',
],
},
},
},
{
name: 'nextCloudOAuth2Api',
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',
@ -446,7 +486,14 @@ export class NextCloud implements INodeType {
const items = this.getInputData().slice(); const items = this.getInputData().slice();
const returnData: IDataObject[] = []; const returnData: IDataObject[] = [];
const credentials = this.getCredentials('nextCloudApi'); const authenticationMethod = this.getNodeParameter('authentication', 0);
let credentials;
if (authenticationMethod === 'accessToken') {
credentials = this.getCredentials('nextCloudApi');
} else {
credentials = this.getCredentials('nextCloudOAuth2Api');
}
if (credentials === undefined) { if (credentials === undefined) {
throw new Error('No credentials got returned!'); throw new Error('No credentials got returned!');
@ -562,26 +609,14 @@ export class NextCloud implements INodeType {
webDavUrl = webDavUrl.slice(0, -1); webDavUrl = webDavUrl.slice(0, -1);
} }
const options: OptionsWithUri = { let encoding = undefined;
auth: {
user: credentials.user as string,
pass: credentials.password as string,
},
headers,
method: requestMethod,
body,
qs: {},
uri: `${credentials.webDavUrl}/${encodeURI(endpoint)}`,
json: false,
};
if (resource === 'file' && operation === 'download') { if (resource === 'file' && operation === 'download') {
// Return the data as a buffer // Return the data as a buffer
options.encoding = null; encoding = null;
} }
try { try {
responseData = await this.helpers.request(options); responseData = await nextCloudApiRequest.call(this, requestMethod, endpoint, body, headers, encoding);
} catch (error) { } catch (error) {
if (this.continueOnFail() === true) { if (this.continueOnFail() === true) {
returnData.push({ error }); returnData.push({ error });

View file

@ -104,6 +104,7 @@
"dist/credentials/Msg91Api.credentials.js", "dist/credentials/Msg91Api.credentials.js",
"dist/credentials/MySql.credentials.js", "dist/credentials/MySql.credentials.js",
"dist/credentials/NextCloudApi.credentials.js", "dist/credentials/NextCloudApi.credentials.js",
"dist/credentials/NextCloudOAuth2Api.credentials.js",
"dist/credentials/OAuth1Api.credentials.js", "dist/credentials/OAuth1Api.credentials.js",
"dist/credentials/OAuth2Api.credentials.js", "dist/credentials/OAuth2Api.credentials.js",
"dist/credentials/OpenWeatherMapApi.credentials.js", "dist/credentials/OpenWeatherMapApi.credentials.js",