Skip to content

Commit

Permalink
feat(core) Simplify authentication type (#3578)
Browse files Browse the repository at this point in the history
* ⚡ Add generic auth type

* ⚡ Remove queryAuth

* ⚡ Remove bearer

* ⚡ Remove headerAuth

* ⚡ Remove basicAuth

* ⚡ Adjust tests

* ⚡ Small improvements

* 👕 Fix lint issue
  • Loading branch information
janober committed Jun 26, 2022
1 parent 1e4fd9e commit 86721fc
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 256 deletions.
92 changes: 30 additions & 62 deletions packages/cli/src/CredentialsHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

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,
);

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;
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,
);

valueResolved = this.resolveValue(
value as string,
{ $credentials: credentials },
workflow,
node,
defaultTimezone,
);

// @ts-ignore
if (!requestOptions[outerKey]) {
// @ts-ignore
requestOptions[outerKey] = {};
}
// @ts-ignore
requestOptions[outerKey][keyResolved] = valueResolved;
});
});
}
}
}
Expand Down
123 changes: 31 additions & 92 deletions packages/cli/test/unit/CredentialsHelper.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { CredentialsHelper, CredentialTypes } from '../../src';
import * as Helpers from './Helpers';
import {
IAuthenticateBasicAuth,
IAuthenticateBearer,
IAuthenticateHeaderAuth,
IAuthenticateQueryAuth,
IAuthenticateGeneric,
ICredentialDataDecryptedObject,
ICredentialType,
ICredentialTypeData,
Expand All @@ -27,7 +24,7 @@ describe('CredentialsHelper', () => {
output: IHttpRequestOptions;
}> = [
{
description: 'built-in basicAuth, default property names',
description: 'basicAuth, default property names',
input: {
credentials: {
user: 'user1',
Expand All @@ -51,62 +48,26 @@ describe('CredentialsHelper', () => {
},
];

authenticate = {
type: 'basicAuth',
properties: {},
} as IAuthenticateBasicAuth;
})(),
},
output: {
url: '',
headers: {},
auth: { username: 'user1', password: 'password1' },
qs: {},
},
},
{
description: 'built-in basicAuth, custom property names',
input: {
credentials: {
customUser: 'user2',
customPassword: 'password2',
},
credentialType: new (class TestApi implements ICredentialType {
name = 'testApi';
displayName = 'Test API';
properties: INodeProperties[] = [
{
displayName: 'User',
name: 'user',
type: 'string',
default: '',
},
{
displayName: 'Password',
name: 'password',
type: 'string',
default: '',
},
];

authenticate = {
type: 'basicAuth',
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
userPropertyName: 'customUser',
passwordPropertyName: 'customPassword',
auth: {
username: '={{$credentials.user}}',
password: '={{$credentials.password}}',
},
},
} as IAuthenticateBasicAuth;
};
})(),
},
output: {
url: '',
headers: {},
auth: { username: 'user2', password: 'password2' },
auth: { username: 'user1', password: 'password1' },
qs: {},
},
},
{
description: 'built-in headerAuth',
description: 'headerAuth',
input: {
credentials: {
accessToken: 'test',
Expand All @@ -123,19 +84,20 @@ describe('CredentialsHelper', () => {
},
];

authenticate = {
type: 'headerAuth',
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
name: 'Authorization',
value: '=Bearer {{$credentials.accessToken}}',
headers: {
Authorization: '=Bearer {{$credentials.accessToken}}',
},
},
} as IAuthenticateHeaderAuth;
};
})(),
},
output: { url: '', headers: { Authorization: 'Bearer test' }, qs: {} },
},
{
description: 'built-in bearer, default property name',
description: 'headerAuth, key and value expressions',
input: {
credentials: {
accessToken: 'test',
Expand All @@ -152,44 +114,20 @@ describe('CredentialsHelper', () => {
},
];

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',
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
tokenPropertyName: 'myToken',
headers: {
'={{$credentials.accessToken}}': '=Bearer {{$credentials.accessToken}}',
},
},
} as IAuthenticateBearer;
};
})(),
},
output: { url: '', headers: { Authorization: 'Bearer test' }, qs: {} },
output: { url: '', headers: { test: 'Bearer test' }, qs: {} },
},
{
description: 'built-in queryAuth',
description: 'queryAuth',
input: {
credentials: {
accessToken: 'test',
Expand All @@ -207,12 +145,13 @@ describe('CredentialsHelper', () => {
];

authenticate = {
type: 'queryAuth',
type: 'generic',
properties: {
key: 'accessToken',
value: '={{$credentials.accessToken}}',
qs: {
accessToken: '={{$credentials.accessToken}}',
},
},
} as IAuthenticateQueryAuth;
} as IAuthenticateGeneric;
})(),
},
output: { url: '', headers: {}, qs: { accessToken: 'test' } },
Expand Down
14 changes: 9 additions & 5 deletions packages/nodes-base/credentials/AsanaApi.credentials.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
IAuthenticateBearer,
IAuthenticateGeneric,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
Expand All @@ -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}}',
},
},
};

}
13 changes: 7 additions & 6 deletions packages/nodes-base/credentials/CalApi.credentials.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
IAuthenticateQueryAuth,
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
Expand All @@ -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: {
Expand Down
13 changes: 8 additions & 5 deletions packages/nodes-base/credentials/GithubApi.credentials.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
IAuthenticateHeaderAuth,
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
Expand Down Expand Up @@ -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}}',
Expand Down
Loading

0 comments on commit 86721fc

Please sign in to comment.