Skip to content

Commit

Permalink
✨ Support basic auth for ServiceNow
Browse files Browse the repository at this point in the history
  • Loading branch information
pemontto committed Jan 20, 2022
1 parent 58c9831 commit 075b6cf
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 4 deletions.
24 changes: 24 additions & 0 deletions packages/nodes-base/credentials/ServiceNowBasicApi.credentials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {
ICredentialType,
INodeProperties,
} from 'n8n-workflow';

export class ServiceNowBasicApi implements ICredentialType {
name = 'serviceNowBasicApi';
extends = [
'httpBasicAuth',
];
displayName = 'ServiceNow Basic Auth API';
documentationUrl = 'serviceNow';
properties: INodeProperties[] = [
{
displayName: 'Subdomain',
name: 'subdomain',
type: 'string',
default: '',
placeholder: '<subdomain>',
description: 'The subdomain of your ServiceNow environment',
required: true,
},
];
}
25 changes: 21 additions & 4 deletions packages/nodes-base/nodes/ServiceNow/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,28 @@ import {
IDataObject,
INodePropertyOptions,
NodeApiError,
NodeOperationError,
} from 'n8n-workflow';

export async function serviceNowApiRequest(this: IExecuteFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any

const credentials = await this.getCredentials('serviceNowOAuth2Api');
const headers = {} as IDataObject;
const authenticationMethod = this.getNodeParameter('authentication', 0, 'oAuth2') as string;

let credentials;

if (authenticationMethod === 'basicAuth') {
credentials = await this.getCredentials('serviceNowBasicApi');
if (credentials === undefined) {
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
}
headers.Authorization = `Basic ${Buffer.from(`${credentials.user}:${credentials.password}`).toString('base64')}`;
} else {
credentials = await this.getCredentials('serviceNowOAuth2Api');
}

const options: OptionsWithUri = {
headers: {},
headers,
method,
qs,
body,
Expand All @@ -38,8 +52,11 @@ export async function serviceNowApiRequest(this: IExecuteFunctions | ILoadOption
}

try {

return await this.helpers.requestOAuth2!.call(this, 'serviceNowOAuth2Api', options);
if (authenticationMethod === 'basicAuth') {
return await this.helpers.request!(options);
} else {
return await this.helpers.requestOAuth2!.call(this, 'serviceNowOAuth2Api', options);
}

} catch (error) {
throw new NodeApiError(this.getNode(), error);
Expand Down
35 changes: 35 additions & 0 deletions packages/nodes-base/nodes/ServiceNow/ServiceNow.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,44 @@ export class ServiceNow implements INodeType {
{
name: 'serviceNowOAuth2Api',
required: true,
displayOptions: {
show: {
authentication: [
'oAuth2',
],
},
},
},
{
name: 'serviceNowBasicApi',
required: true,
displayOptions: {
show: {
authentication: [
'basicAuth',
],
},
},
},
],
properties: [
{
displayName: 'Authentication',
name: 'authentication',
type: 'options',
options: [
{
name: 'Basic Auth',
value: 'basicAuth',
},
{
name: 'OAuth2',
value: 'oAuth2',
},
],
default: 'oAuth2',
description: 'The authentication method to use',
},
{
displayName: 'Resource',
name: 'resource',
Expand Down
1 change: 1 addition & 0 deletions packages/nodes-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@
"dist/credentials/SentryIoOAuth2Api.credentials.js",
"dist/credentials/SentryIoServerApi.credentials.js",
"dist/credentials/ServiceNowOAuth2Api.credentials.js",
"dist/credentials/ServiceNowBasicApi.credentials.js",
"dist/credentials/Sftp.credentials.js",
"dist/credentials/ShopifyApi.credentials.js",
"dist/credentials/Signl4Api.credentials.js",
Expand Down

0 comments on commit 075b6cf

Please sign in to comment.