NextCloud OAuth2 support

This commit is contained in:
Rupenieks
2020-06-17 19:38:30 +02:00
parent 346ae8efc9
commit cb1849db9d
4 changed files with 167 additions and 17 deletions

View File

@@ -11,6 +11,7 @@ import {
import { parseString } from 'xml2js';
import { OptionsWithUri } from 'request';
import { nextCloudApiRequest } from './GenericFunctions';
export class NextCloud implements INodeType {
@@ -32,9 +33,44 @@ export class NextCloud implements INodeType {
{
name: 'nextCloudApi',
required: true,
}
displayOptions: {
show: {
authentication: [
'accessToken',
],
},
},
},
{
name: 'nextCloudOAuth2Api',
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',
@@ -446,7 +482,14 @@ export class NextCloud implements INodeType {
const items = this.getInputData().slice();
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) {
throw new Error('No credentials got returned!');
@@ -562,26 +605,14 @@ export class NextCloud implements INodeType {
webDavUrl = webDavUrl.slice(0, -1);
}
const options: OptionsWithUri = {
auth: {
user: credentials.user as string,
pass: credentials.password as string,
},
headers,
method: requestMethod,
body,
qs: {},
uri: `${credentials.webDavUrl}/${encodeURI(endpoint)}`,
json: false,
};
let encoding = undefined;
if (resource === 'file' && operation === 'download') {
// Return the data as a buffer
options.encoding = null;
encoding = null;
}
try {
responseData = await this.helpers.request(options);
responseData = await nextCloudApiRequest.call(this, requestMethod, endpoint, body, headers, encoding);
} catch (error) {
if (this.continueOnFail() === true) {
returnData.push({ error });