1
0
Fork 0
mirror of https://github.com/n8n-io/n8n.git synced 2025-03-05 20:50:17 -08:00

Add API key to Twilio credentials (#1797)

*  Add API key to Twilio credentials

*  Minor simplification

Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
This commit is contained in:
Iván Ovejero 2021-05-19 02:05:49 +02:00 committed by GitHub
parent 6e8de269ec
commit 0fdae7fec3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 77 additions and 6 deletions

View file

@ -9,6 +9,22 @@ export class TwilioApi implements ICredentialType {
displayName = 'Twilio API';
documentationUrl = 'twilio';
properties = [
{
displayName: 'Auth Type',
name: 'authType',
type: 'options' as NodePropertyTypes,
default: 'authToken',
options: [
{
name: 'Auth Token',
value: 'authToken',
},
{
name: 'API Key',
value: 'apiKey',
},
],
},
{
displayName: 'Account SID',
name: 'accountSid',
@ -20,6 +36,42 @@ export class TwilioApi implements ICredentialType {
name: 'authToken',
type: 'string' as NodePropertyTypes,
default: '',
displayOptions: {
show: {
authType: [
'authToken',
],
},
},
},
{
displayName: 'API Key SID',
name: 'apiKeySid',
type: 'string' as NodePropertyTypes,
default: '',
displayOptions: {
show: {
authType: [
'apiKey',
],
},
},
},
{
displayName: 'API Key Secret',
name: 'apiKeySecret',
type: 'string' as NodePropertyTypes,
typeOptions: {
password: true,
},
default: '',
displayOptions: {
show: {
authType: [
'apiKey',
],
},
},
},
];
}

View file

@ -7,6 +7,10 @@ import {
IDataObject, NodeApiError, NodeOperationError,
} from 'n8n-workflow';
import {
OptionsWithUri,
} from 'request';
/**
* Make an API request to Twilio
*
@ -17,7 +21,14 @@ import {
* @returns {Promise<any>}
*/
export async function twilioApiRequest(this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, body: IDataObject, query?: IDataObject): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('twilioApi');
const credentials = this.getCredentials('twilioApi') as {
accountSid: string;
authType: 'authToken' | 'apiKey';
authToken: string;
apiKeySid: string;
apiKeySecret: string;
};
if (credentials === undefined) {
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
}
@ -26,18 +37,26 @@ export async function twilioApiRequest(this: IHookFunctions | IExecuteFunctions,
query = {};
}
const options = {
const options: OptionsWithUri = {
method,
form: body,
qs: query,
uri: `https://api.twilio.com/2010-04-01/Accounts/${credentials.accountSid}${endpoint}`,
auth: {
user: credentials.accountSid as string,
pass: credentials.authToken as string,
},
json: true,
};
if (credentials.authType === 'apiKey') {
options.auth = {
user: credentials.apiKeySid,
password: credentials.apiKeySecret,
};
} else if (credentials.authType === 'authToken') {
options.auth = {
user: credentials.accountSid,
pass: credentials.authToken,
};
}
try {
return await this.helpers.request(options);
} catch (error) {