Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,69 @@ describe('OAuth2CredentialController', () => {
expect(oauthService.encryptAndSaveData).toHaveBeenCalled();
});

it('should include client_id and client_secret in body for PKCE flow with body authentication', async () => {
const { ClientOAuth2 } = await import('@n8n/client-oauth2');
const mockGetToken = jest.fn().mockResolvedValue({
data: { access_token: 'new_token' },
});
jest.mocked(ClientOAuth2).mockImplementation(
() =>
({
code: {
getToken: mockGetToken,
},
}) as any,
);

const mockResolvedCredential = mock<CredentialsEntity>({ id: '1' });
const mockState = {
token: 'token',
cid: '1',
userId: '123',
origin: 'static-credential' as const,
createdAt: timestamp,
data: 'encrypted-data',
};
oauthService.resolveCredential.mockResolvedValueOnce([
mockResolvedCredential,
{ csrfSecret: 'csrf-secret', codeVerifier: 'code_verifier' },
{
clientId: 'client_id',
clientSecret: 'client_secret',
authUrl: 'https://example.domain/oauth2/auth',
accessTokenUrl: 'https://example.domain/oauth2/token',
scope: 'openid',
grantType: 'pkce',
authentication: 'body',
},
mockState,
]);
oauthService.getBaseUrl.mockReturnValue('http://localhost:5678/rest/oauth2-credential');
externalHooks.run.mockResolvedValue(undefined);

const req = mock<OAuthRequest.OAuth2Credential.Callback>({
query: {
code: 'auth_code',
state: validState,
},
originalUrl: '/oauth2-credential/callback?code=auth_code&state=state',
});

await controller.handleCallback(req, res);

expect(mockGetToken).toHaveBeenCalledWith(
expect.stringContaining('code=auth_code'),
expect.objectContaining({
body: expect.objectContaining({
code_verifier: 'code_verifier',
client_id: 'client_id',
client_secret: 'client_secret',
}),
}),
);
expect(oauthService.encryptAndSaveData).toHaveBeenCalled();
});

it('should handle body authentication method', async () => {
const { ClientOAuth2 } = await import('@n8n/client-oauth2');
const mockGetToken = jest.fn().mockResolvedValue({
Expand Down
35 changes: 21 additions & 14 deletions packages/cli/src/controllers/oauth/oauth2-credential.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,32 @@ export class OAuth2CredentialController {
const [credential, decryptedDataOriginal, oauthCredentials, state] =
await this.oauthService.resolveCredential<OAuth2CredentialData>(req);

let options: Partial<ClientOAuth2Options> = {};

const oAuthOptions = this.convertCredentialToOptions(oauthCredentials);

if (oauthCredentials.grantType === 'pkce') {
options = {
body: { code_verifier: decryptedDataOriginal.codeVerifier },
};
} else if (oauthCredentials.authentication === 'body') {
options = {
body: {
...(oAuthOptions.body ?? {}),
client_id: oAuthOptions.clientId,
client_secret: oAuthOptions.clientSecret,
},
};
const isPkce = oauthCredentials.grantType === 'pkce';
const isBodyAuth = oauthCredentials.authentication === 'body';

const body: Record<string, string> = { ...(oAuthOptions.body ?? {}) };

if (isPkce) {
body.code_verifier = decryptedDataOriginal.codeVerifier as string;
}

if (isBodyAuth) {
body.client_id = oAuthOptions.clientId;
if (oAuthOptions.clientSecret) {
body.client_secret = oAuthOptions.clientSecret;
}
// Remove clientSecret so code-flow.ts won't also send it
// via the Authorization header
delete oAuthOptions.clientSecret;
}

let options: Partial<ClientOAuth2Options> = {};
if (isPkce || isBodyAuth) {
options = { body };
}

await this.externalHooks.run('oauth2.callback', [oAuthOptions]);

const oAuthObj = new ClientOAuth2(oAuthOptions);
Expand Down
Loading