feat(core) Simplify authentication type (#3578)

*  Add generic auth type

*  Remove queryAuth

*  Remove bearer

*  Remove headerAuth

*  Remove basicAuth

*  Adjust tests

*  Small improvements

* 👕 Fix lint issue
This commit is contained in:
Jan Oberhauser 2022-06-26 15:55:51 -07:00 committed by GitHub
parent 1e4fd9e4df
commit 86721fc496
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 207 additions and 281 deletions

View file

@ -98,73 +98,41 @@ export class CredentialsHelper extends ICredentialsHelper {
if (typeof credentialType.authenticate === 'object') {
// Predefined authentication method
let keyResolved: string;
let valueResolved: string;
const { authenticate } = credentialType;
if (requestOptions.headers === undefined) {
requestOptions.headers = {};
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (authenticate.type === 'bearer') {
const tokenPropertyName: string =
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
authenticate.properties.tokenPropertyName ?? 'accessToken';
requestOptions.headers.Authorization = `Bearer ${
credentials[tokenPropertyName] as string
}`;
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
} else if (authenticate.type === 'basicAuth') {
const userPropertyName: string =
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
authenticate.properties.userPropertyName ?? 'user';
const passwordPropertyName: string =
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
authenticate.properties.passwordPropertyName ?? 'password';
if (authenticate.type === 'generic') {
Object.entries(authenticate.properties).forEach(([outerKey, outerValue]) => {
Object.entries(outerValue).forEach(([key, value]) => {
keyResolved = this.resolveValue(
key,
{ $credentials: credentials },
workflow,
node,
defaultTimezone,
);
requestOptions.auth = {
username: credentials[userPropertyName] as string,
password: credentials[passwordPropertyName] as string,
};
} else if (authenticate.type === 'headerAuth') {
const key = this.resolveValue(
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
authenticate.properties.name,
{ $credentials: credentials },
workflow,
node,
defaultTimezone,
);
valueResolved = this.resolveValue(
value as string,
{ $credentials: credentials },
workflow,
node,
defaultTimezone,
);
const value = this.resolveValue(
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
authenticate.properties.value,
{ $credentials: credentials },
workflow,
node,
defaultTimezone,
);
requestOptions.headers[key] = value;
} else if (authenticate.type === 'queryAuth') {
const key = this.resolveValue(
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
authenticate.properties.key,
{ $credentials: credentials },
workflow,
node,
defaultTimezone,
);
const value = this.resolveValue(
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
authenticate.properties.value,
{ $credentials: credentials },
workflow,
node,
defaultTimezone,
);
if (!requestOptions.qs) {
requestOptions.qs = {};
}
requestOptions.qs[key] = value;
// @ts-ignore
if (!requestOptions[outerKey]) {
// @ts-ignore
requestOptions[outerKey] = {};
}
// @ts-ignore
requestOptions[outerKey][keyResolved] = valueResolved;
});
});
}
}
}

View file

@ -1,10 +1,7 @@
import { CredentialsHelper, CredentialTypes } from '../../src';
import * as Helpers from './Helpers';
import {
IAuthenticateBasicAuth,
IAuthenticateBearer,
IAuthenticateHeaderAuth,
IAuthenticateQueryAuth,
IAuthenticateGeneric,
ICredentialDataDecryptedObject,
ICredentialType,
ICredentialTypeData,
@ -27,7 +24,7 @@ describe('CredentialsHelper', () => {
output: IHttpRequestOptions;
}> = [
{
description: 'built-in basicAuth, default property names',
description: 'basicAuth, default property names',
input: {
credentials: {
user: 'user1',
@ -51,10 +48,15 @@ describe('CredentialsHelper', () => {
},
];
authenticate = {
type: 'basicAuth',
properties: {},
} as IAuthenticateBasicAuth;
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
auth: {
username: '={{$credentials.user}}',
password: '={{$credentials.password}}',
},
},
};
})(),
},
output: {
@ -65,48 +67,67 @@ describe('CredentialsHelper', () => {
},
},
{
description: 'built-in basicAuth, custom property names',
description: 'headerAuth',
input: {
credentials: {
customUser: 'user2',
customPassword: 'password2',
accessToken: 'test',
},
credentialType: new (class TestApi implements ICredentialType {
name = 'testApi';
displayName = 'Test API';
properties: INodeProperties[] = [
{
displayName: 'User',
name: 'user',
type: 'string',
default: '',
},
{
displayName: 'Password',
name: 'password',
displayName: 'Access Token',
name: 'accessToken',
type: 'string',
default: '',
},
];
authenticate = {
type: 'basicAuth',
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
userPropertyName: 'customUser',
passwordPropertyName: 'customPassword',
headers: {
Authorization: '=Bearer {{$credentials.accessToken}}',
},
},
} as IAuthenticateBasicAuth;
};
})(),
},
output: {
url: '',
headers: {},
auth: { username: 'user2', password: 'password2' },
qs: {},
},
output: { url: '', headers: { Authorization: 'Bearer test' }, qs: {} },
},
{
description: 'built-in headerAuth',
description: 'headerAuth, key and value expressions',
input: {
credentials: {
accessToken: 'test',
},
credentialType: new (class TestApi implements ICredentialType {
name = 'testApi';
displayName = 'Test API';
properties: INodeProperties[] = [
{
displayName: 'Access Token',
name: 'accessToken',
type: 'string',
default: '',
},
];
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
'={{$credentials.accessToken}}': '=Bearer {{$credentials.accessToken}}',
},
},
};
})(),
},
output: { url: '', headers: { test: 'Bearer test' }, qs: {} },
},
{
description: 'queryAuth',
input: {
credentials: {
accessToken: 'test',
@ -124,95 +145,13 @@ describe('CredentialsHelper', () => {
];
authenticate = {
type: 'headerAuth',
type: 'generic',
properties: {
name: 'Authorization',
value: '=Bearer {{$credentials.accessToken}}',
qs: {
accessToken: '={{$credentials.accessToken}}',
},
},
} as IAuthenticateHeaderAuth;
})(),
},
output: { url: '', headers: { Authorization: 'Bearer test' }, qs: {} },
},
{
description: 'built-in bearer, default property name',
input: {
credentials: {
accessToken: 'test',
},
credentialType: new (class TestApi implements ICredentialType {
name = 'testApi';
displayName = 'Test API';
properties: INodeProperties[] = [
{
displayName: 'Access Token',
name: 'accessToken',
type: 'string',
default: '',
},
];
authenticate = {
type: 'bearer',
properties: {},
} as IAuthenticateBearer;
})(),
},
output: { url: '', headers: { Authorization: 'Bearer test' }, qs: {} },
},
{
description: 'built-in bearer, custom property name',
input: {
credentials: {
myToken: 'test',
},
credentialType: new (class TestApi implements ICredentialType {
name = 'testApi';
displayName = 'Test API';
properties: INodeProperties[] = [
{
displayName: 'My Token',
name: 'myToken',
type: 'string',
default: '',
},
];
authenticate = {
type: 'bearer',
properties: {
tokenPropertyName: 'myToken',
},
} as IAuthenticateBearer;
})(),
},
output: { url: '', headers: { Authorization: 'Bearer test' }, qs: {} },
},
{
description: 'built-in queryAuth',
input: {
credentials: {
accessToken: 'test',
},
credentialType: new (class TestApi implements ICredentialType {
name = 'testApi';
displayName = 'Test API';
properties: INodeProperties[] = [
{
displayName: 'Access Token',
name: 'accessToken',
type: 'string',
default: '',
},
];
authenticate = {
type: 'queryAuth',
properties: {
key: 'accessToken',
value: '={{$credentials.accessToken}}',
},
} as IAuthenticateQueryAuth;
} as IAuthenticateGeneric;
})(),
},
output: { url: '', headers: {}, qs: { accessToken: 'test' } },

View file

@ -1,5 +1,5 @@
import {
IAuthenticateBearer,
IAuthenticateGeneric,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
@ -17,9 +17,13 @@ export class AsanaApi implements ICredentialType {
},
];
authenticate = {
type: 'bearer',
properties: {},
} as IAuthenticateBearer;
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
Authorization: '=Bearer {{$credentials.accessToken}}',
},
},
};
}

View file

@ -1,5 +1,5 @@
import {
IAuthenticateQueryAuth,
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
@ -24,13 +24,14 @@ export class CalApi implements ICredentialType {
},
];
authenticate = {
type: 'queryAuth',
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
key: 'apiKey',
value: '={{$credentials.apiKey}}',
qs: {
apiKey: '={{$credentials.apiKey}}',
},
},
} as IAuthenticateQueryAuth;
};
test: ICredentialTestRequest = {
request: {

View file

@ -1,5 +1,5 @@
import {
IAuthenticateHeaderAuth,
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
@ -30,13 +30,16 @@ export class GithubApi implements ICredentialType {
default: '',
},
];
authenticate: IAuthenticateHeaderAuth = {
type: 'headerAuth',
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
name: 'Authorization',
value: '=token {{$credentials?.accessToken}}',
headers: {
Authorization: '=token {{$credentials?.accessToken}}',
},
},
};
test: ICredentialTestRequest = {
request: {
baseURL: '={{$credentials?.server}}',

View file

@ -1,5 +1,5 @@
import {
IAuthenticateHeaderAuth,
IAuthenticateGeneric,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
@ -26,11 +26,13 @@ export class HttpHeaderAuth implements ICredentialType {
default: '',
},
];
authenticate = {
type: 'headerAuth',
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
name: '={{credentials.name}}',
value: '={{credentials.value}}',
headers: {
'={{$credentials.name}}': '={{$credentials.value}}',
},
},
} as IAuthenticateHeaderAuth;
};
}

View file

@ -1,5 +1,5 @@
import {
IAuthenticateBearer,
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
@ -30,8 +30,12 @@ export class Magento2Api implements ICredentialType {
},
};
authenticate = {
type: 'bearer',
properties: {},
} as IAuthenticateBearer;
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
Authorization: '=Bearer {{$credentials.accessToken}}',
},
},
};
}

View file

@ -1,5 +1,5 @@
import {
IAuthenticateBasicAuth,
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
@ -30,13 +30,17 @@ export class MailjetEmailApi implements ICredentialType {
description: 'Whether to allow to run the API call in a Sandbox mode, where all validations of the payload will be done without delivering the message',
},
];
authenticate: IAuthenticateBasicAuth = {
type: 'basicAuth',
properties: {
userPropertyName: 'apiKey',
passwordPropertyName: 'secretKey',
},
};
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
auth: {
username: '={{$credentials.apiKey}}',
password: '={{$credentials.secretKey}}',
},
},
};
test: ICredentialTestRequest = {
request: {
baseURL: `https://api.mailjet.com`,

View file

@ -1,5 +1,5 @@
import {
IAuthenticateHeaderAuth,
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
@ -17,13 +17,16 @@ export class MailjetSmsApi implements ICredentialType {
default: '',
},
];
authenticate: IAuthenticateHeaderAuth = {
type: 'headerAuth',
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
name: 'Authorization',
value: '=Bearer {{$credentials.token}}',
headers: {
Authorization: '=Bearer {{$credentials.token}}',
},
},
};
test: ICredentialTestRequest = {
request: {
baseURL: `https://api.mailjet.com`,

View file

@ -1,6 +1,5 @@
import {
IAuthenticateQueryAuth,
ICredentialTestRequest,
IAuthenticateGeneric,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
@ -19,11 +18,12 @@ export class PipedriveApi implements ICredentialType {
},
];
authenticate = {
type: 'queryAuth',
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
key: 'api_token',
value: '={{$credentials.apiToken}}',
qs: {
api_token: '={{$credentials.apiToken}}',
},
},
} as IAuthenticateQueryAuth;
};
}

View file

@ -1,5 +1,5 @@
import {
IAuthenticateBasicAuth,
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
@ -40,10 +40,17 @@ export class ServiceNowBasicApi implements ICredentialType {
required: true,
},
];
authenticate: IAuthenticateBasicAuth = {
type: 'basicAuth',
properties: {},
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
auth: {
username: '={{$credentials.user}}',
password: '={{$credentials.password}}',
},
},
};
test: ICredentialTestRequest = {
request: {
baseURL: '=https://{{$credentials?.subdomain}}.service-now.com',

View file

@ -1,6 +1,5 @@
import {
IAuthenticateBearer,
IAuthenticateQueryAuth,
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
@ -19,12 +18,16 @@ export class SlackApi implements ICredentialType {
required: true,
},
];
authenticate: IAuthenticateBearer = {
type: 'bearer',
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
tokenPropertyName: 'accessToken',
headers: {
Authorization: '=Bearer {{$credentials.accessToken}}',
},
},
};
test: ICredentialTestRequest = {
request: {
baseURL: 'https://slack.com',

View file

@ -1,5 +1,5 @@
import {
IAuthenticateBearer,
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
@ -18,12 +18,14 @@ export class TodoistApi implements ICredentialType {
];
authenticate = {
type: 'bearer',
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
tokenPropertyName: 'apiKey',
headers: {
Authorization: '=Bearer {{$credentials.apiKey}}',
},
},
} as IAuthenticateBearer;
};
test: ICredentialTestRequest = {
request: {

View file

@ -1,5 +1,5 @@
import {
IAuthenticateBearer,
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
@ -17,12 +17,15 @@ export class TwakeCloudApi implements ICredentialType {
default: '',
},
];
authenticate = {
type: 'bearer',
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
tokenPropertyName: 'workspaceKey',
headers: {
Authorization: '=Bearer {{$credentials.workspaceKey}}',
},
},
} as IAuthenticateBearer;
};
test: ICredentialTestRequest = {
request: {

View file

@ -150,6 +150,16 @@ export interface IRequestOptionsSimplified {
qs: IDataObject;
}
export interface IRequestOptionsSimplifiedAuth {
auth?: {
username: string;
password: string;
};
body?: IDataObject;
headers?: IDataObject;
qs?: IDataObject;
}
export abstract class ICredentialsHelper {
encryptionKey: string;
@ -191,40 +201,16 @@ export abstract class ICredentialsHelper {
export interface IAuthenticateBase {
type: string;
properties: {
[key: string]: string;
};
properties:
| {
[key: string]: string;
}
| IRequestOptionsSimplifiedAuth;
}
export interface IAuthenticateBasicAuth extends IAuthenticateBase {
type: 'basicAuth';
properties: {
userPropertyName?: string;
passwordPropertyName?: string;
};
}
export interface IAuthenticateBearer extends IAuthenticateBase {
type: 'bearer';
properties: {
tokenPropertyName?: string;
};
}
export interface IAuthenticateHeaderAuth extends IAuthenticateBase {
type: 'headerAuth';
properties: {
name: string;
value: string;
};
}
export interface IAuthenticateQueryAuth extends IAuthenticateBase {
type: 'queryAuth';
properties: {
key: string;
value: string;
};
export interface IAuthenticateGeneric extends IAuthenticateBase {
type: 'generic';
properties: IRequestOptionsSimplifiedAuth;
}
export type IAuthenticate =
@ -232,10 +218,7 @@ export type IAuthenticate =
credentials: ICredentialDataDecryptedObject,
requestOptions: IHttpRequestOptions,
) => Promise<IHttpRequestOptions>)
| IAuthenticateBasicAuth
| IAuthenticateBearer
| IAuthenticateHeaderAuth
| IAuthenticateQueryAuth;
| IAuthenticateGeneric;
export interface IAuthenticateRuleBase {
type: string;