mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
feat(core): enable sending client credentials in body (#4377)
* ⚡ Enable inject credentials in the body for OAuth2 CC * 🐛 Persist token data * 👕 Fix linting error
This commit is contained in:
@@ -102,6 +102,7 @@ import {
|
|||||||
PLACEHOLDER_EMPTY_EXECUTION_ID,
|
PLACEHOLDER_EMPTY_EXECUTION_ID,
|
||||||
} from '.';
|
} from '.';
|
||||||
import { extractValue } from './ExtractValue';
|
import { extractValue } from './ExtractValue';
|
||||||
|
import { getClientCredentialsToken } from './OAuth2Helper';
|
||||||
|
|
||||||
axios.defaults.timeout = 300000;
|
axios.defaults.timeout = 300000;
|
||||||
// Prevent axios from adding x-form-www-urlencoded headers by default
|
// Prevent axios from adding x-form-www-urlencoded headers by default
|
||||||
@@ -933,9 +934,10 @@ export async function requestOAuth2(
|
|||||||
});
|
});
|
||||||
|
|
||||||
let oauthTokenData = credentials.oauthTokenData as clientOAuth2.Data;
|
let oauthTokenData = credentials.oauthTokenData as clientOAuth2.Data;
|
||||||
|
|
||||||
// if it's the first time using the credentials, get the access token and save it into the DB.
|
// if it's the first time using the credentials, get the access token and save it into the DB.
|
||||||
if (credentials.grantType === OAuth2GrantType.clientCredentials && oauthTokenData === undefined) {
|
if (credentials.grantType === OAuth2GrantType.clientCredentials && oauthTokenData === undefined) {
|
||||||
const { data } = await oAuthClient.credentials.getToken();
|
const { data } = await getClientCredentialsToken(oAuthClient, credentials);
|
||||||
|
|
||||||
// Find the credentials
|
// Find the credentials
|
||||||
if (!node.credentials?.[credentialsType]) {
|
if (!node.credentials?.[credentialsType]) {
|
||||||
@@ -950,7 +952,7 @@ export async function requestOAuth2(
|
|||||||
await additionalData.credentialsHelper.updateCredentials(
|
await additionalData.credentialsHelper.updateCredentials(
|
||||||
nodeCredentials,
|
nodeCredentials,
|
||||||
credentialsType,
|
credentialsType,
|
||||||
credentials as unknown as ICredentialDataDecryptedObject,
|
Object.assign(credentials, { oauthTokenData: data }),
|
||||||
);
|
);
|
||||||
|
|
||||||
oauthTokenData = data;
|
oauthTokenData = data;
|
||||||
@@ -1005,7 +1007,7 @@ export async function requestOAuth2(
|
|||||||
// if it's OAuth2 with client credentials grant type, get a new token
|
// if it's OAuth2 with client credentials grant type, get a new token
|
||||||
// instead of refreshing it.
|
// instead of refreshing it.
|
||||||
if (OAuth2GrantType.clientCredentials === credentials.grantType) {
|
if (OAuth2GrantType.clientCredentials === credentials.grantType) {
|
||||||
newToken = await token.client.credentials.getToken();
|
newToken = await getClientCredentialsToken(token.client, credentials);
|
||||||
} else {
|
} else {
|
||||||
newToken = await token.refresh(tokenRefreshOptions);
|
newToken = await token.refresh(tokenRefreshOptions);
|
||||||
}
|
}
|
||||||
@@ -1073,7 +1075,7 @@ export async function requestOAuth2(
|
|||||||
// if it's OAuth2 with client credentials grant type, get a new token
|
// if it's OAuth2 with client credentials grant type, get a new token
|
||||||
// instead of refreshing it.
|
// instead of refreshing it.
|
||||||
if (OAuth2GrantType.clientCredentials === credentials.grantType) {
|
if (OAuth2GrantType.clientCredentials === credentials.grantType) {
|
||||||
newToken = await token.client.credentials.getToken();
|
newToken = await getClientCredentialsToken(token.client, credentials);
|
||||||
} else {
|
} else {
|
||||||
newToken = await token.refresh(tokenRefreshOptions);
|
newToken = await token.refresh(tokenRefreshOptions);
|
||||||
}
|
}
|
||||||
|
|||||||
22
packages/core/src/OAuth2Helper.ts
Normal file
22
packages/core/src/OAuth2Helper.ts
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
/* eslint-disable @typescript-eslint/naming-convention */
|
||||||
|
import { ICredentialDataDecryptedObject } from 'n8n-workflow';
|
||||||
|
import clientOAuth2 from 'client-oauth2';
|
||||||
|
|
||||||
|
export const getClientCredentialsToken = async (
|
||||||
|
oAuth2Client: clientOAuth2,
|
||||||
|
credentials: ICredentialDataDecryptedObject,
|
||||||
|
): Promise<clientOAuth2.Token> => {
|
||||||
|
const options = {};
|
||||||
|
if (credentials.authentication === 'body') {
|
||||||
|
Object.assign(options, {
|
||||||
|
headers: {
|
||||||
|
Authorization: '',
|
||||||
|
},
|
||||||
|
body: {
|
||||||
|
client_id: credentials.clientId as string,
|
||||||
|
client_secret: credentials.clientSecret as string,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return oAuth2Client.credentials.getToken(options);
|
||||||
|
};
|
||||||
@@ -82,11 +82,6 @@ export class OAuth2Api implements ICredentialType {
|
|||||||
displayName: 'Authentication',
|
displayName: 'Authentication',
|
||||||
name: 'authentication',
|
name: 'authentication',
|
||||||
type: 'options',
|
type: 'options',
|
||||||
displayOptions: {
|
|
||||||
show: {
|
|
||||||
grantType: ['authorizationCode'],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
options: [
|
options: [
|
||||||
{
|
{
|
||||||
name: 'Body',
|
name: 'Body',
|
||||||
|
|||||||
Reference in New Issue
Block a user