mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-18 10:31:15 +00:00
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:
@@ -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;
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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' } },
|
||||
|
||||
Reference in New Issue
Block a user