2020-01-13 18:46:58 -08:00
|
|
|
import { OptionsWithUri } from 'request';
|
|
|
|
|
2020-01-01 22:51:41 -08:00
|
|
|
import { IExecuteFunctions } from 'n8n-core';
|
|
|
|
import {
|
2020-01-07 16:29:11 -08:00
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
2020-01-01 22:51:41 -08:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
export class OAuth implements INodeType {
|
2020-01-07 16:29:11 -08:00
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'OAuth',
|
|
|
|
name: 'oauth',
|
2020-01-01 22:51:41 -08:00
|
|
|
icon: 'fa:code-branch',
|
2020-01-07 16:29:11 -08:00
|
|
|
group: ['input'],
|
|
|
|
version: 1,
|
|
|
|
description: 'Gets, sends data to Oauth API Endpoint and receives generic information.',
|
|
|
|
defaults: {
|
|
|
|
name: 'OAuth',
|
|
|
|
color: '#0033AA',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'oAuth2Api',
|
|
|
|
required: true,
|
|
|
|
}
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Operation',
|
|
|
|
name: 'operation',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Get',
|
|
|
|
value: 'get',
|
|
|
|
description: 'Returns the OAuth token data.',
|
|
|
|
},
|
2020-01-13 18:46:58 -08:00
|
|
|
{
|
|
|
|
name: 'Request',
|
|
|
|
value: 'request',
|
|
|
|
description: 'Make an OAuth signed requ.',
|
|
|
|
},
|
2020-01-07 16:29:11 -08:00
|
|
|
],
|
|
|
|
default: 'get',
|
|
|
|
description: 'The operation to perform.',
|
|
|
|
},
|
2020-01-13 18:46:58 -08:00
|
|
|
{
|
|
|
|
displayName: 'Request Method',
|
|
|
|
name: 'requestMethod',
|
|
|
|
type: 'options',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
operation: [
|
|
|
|
'request',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'DELETE',
|
|
|
|
value: 'DELETE'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'GET',
|
|
|
|
value: 'GET'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'HEAD',
|
|
|
|
value: 'HEAD'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'PATCH',
|
|
|
|
value: 'PATCH'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'POST',
|
|
|
|
value: 'POST'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'PUT',
|
|
|
|
value: 'PUT'
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'GET',
|
|
|
|
description: 'The request method to use.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'URL',
|
|
|
|
name: 'url',
|
|
|
|
type: 'string',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
operation: [
|
|
|
|
'request',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
default: '',
|
|
|
|
placeholder: 'http://example.com/index.html',
|
|
|
|
description: 'The URL to make the request to.',
|
|
|
|
required: true,
|
|
|
|
},
|
2020-01-07 16:29:11 -08:00
|
|
|
]
|
|
|
|
};
|
2020-01-01 22:51:41 -08:00
|
|
|
|
2020-01-07 16:29:11 -08:00
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const credentials = this.getCredentials('oAuth2Api');
|
|
|
|
if (credentials === undefined) {
|
|
|
|
throw new Error('No credentials got returned!');
|
|
|
|
}
|
2020-01-01 22:51:41 -08:00
|
|
|
|
2020-01-07 16:29:11 -08:00
|
|
|
if (credentials.oauthTokenData === undefined) {
|
|
|
|
throw new Error('OAuth credentials not connected');
|
|
|
|
}
|
2020-01-01 22:51:41 -08:00
|
|
|
|
2020-01-07 16:29:11 -08:00
|
|
|
const operation = this.getNodeParameter('operation', 0) as string;
|
|
|
|
if (operation === 'get') {
|
|
|
|
// credentials.oauthTokenData has the refreshToken and accessToken available
|
|
|
|
// it would be nice to have credentials.getOAuthToken() which returns the accessToken
|
|
|
|
// and also handles an error case where if the token is to be refreshed, it does so
|
|
|
|
// without knowledge of the node.
|
2020-01-01 22:51:41 -08:00
|
|
|
|
2020-01-07 16:29:11 -08:00
|
|
|
return [this.helpers.returnJsonArray(JSON.parse(credentials.oauthTokenData as string))];
|
2020-01-13 18:46:58 -08:00
|
|
|
} else if (operation === 'request') {
|
|
|
|
const url = this.getNodeParameter('url', 0) as string;
|
|
|
|
const requestMethod = this.getNodeParameter('requestMethod', 0) as string;
|
|
|
|
|
|
|
|
// Authorization Code Grant
|
|
|
|
const requestOptions: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'User-Agent': 'some-user',
|
|
|
|
},
|
|
|
|
method: requestMethod,
|
|
|
|
uri: url,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
const responseData = await this.helpers.requestOAuth.call(this, 'oAuth2Api', requestOptions);
|
|
|
|
return [this.helpers.returnJsonArray(responseData)];
|
2020-01-07 16:29:11 -08:00
|
|
|
} else {
|
|
|
|
throw new Error('Unknown operation');
|
|
|
|
}
|
|
|
|
}
|
2020-01-01 22:51:41 -08:00
|
|
|
}
|