Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): enable sending client credentials in body #4377

Merged
merged 3 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions packages/core/src/NodeExecuteFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ import {
PLACEHOLDER_EMPTY_EXECUTION_ID,
} from '.';
import { extractValue } from './ExtractValue';
import { getClientCredentialsToken } from './OAuth2Helper';

axios.defaults.timeout = 300000;
// Prevent axios from adding x-form-www-urlencoded headers by default
Expand Down Expand Up @@ -933,9 +934,10 @@ export async function requestOAuth2(
});

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 (credentials.grantType === OAuth2GrantType.clientCredentials && oauthTokenData === undefined) {
const { data } = await oAuthClient.credentials.getToken();
const { data } = await getClientCredentialsToken(oAuthClient, credentials);

// Find the credentials
if (!node.credentials?.[credentialsType]) {
Expand All @@ -950,7 +952,7 @@ export async function requestOAuth2(
await additionalData.credentialsHelper.updateCredentials(
nodeCredentials,
credentialsType,
credentials as unknown as ICredentialDataDecryptedObject,
Object.assign(credentials, { oauthTokenData: data }),
);

oauthTokenData = data;
Expand Down Expand Up @@ -1005,7 +1007,7 @@ export async function requestOAuth2(
// if it's OAuth2 with client credentials grant type, get a new token
// instead of refreshing it.
if (OAuth2GrantType.clientCredentials === credentials.grantType) {
newToken = await token.client.credentials.getToken();
newToken = await getClientCredentialsToken(token.client, credentials);
} else {
newToken = await token.refresh(tokenRefreshOptions);
}
Expand Down Expand Up @@ -1073,7 +1075,7 @@ export async function requestOAuth2(
// if it's OAuth2 with client credentials grant type, get a new token
// instead of refreshing it.
if (OAuth2GrantType.clientCredentials === credentials.grantType) {
newToken = await token.client.credentials.getToken();
newToken = await getClientCredentialsToken(token.client, credentials);
} else {
newToken = await token.refresh(tokenRefreshOptions);
}
Expand Down
22 changes: 22 additions & 0 deletions packages/core/src/OAuth2Helper.ts
Original file line number Diff line number Diff line change
@@ -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);
};
5 changes: 0 additions & 5 deletions packages/nodes-base/credentials/OAuth2Api.credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,6 @@ export class OAuth2Api implements ICredentialType {
displayName: 'Authentication',
name: 'authentication',
type: 'options',
displayOptions: {
show: {
grantType: ['authorizationCode'],
},
},
options: [
{
name: 'Body',
Expand Down