Skip to content

Commit

Permalink
fix(oauth2): credential in body for refresh token
Browse files Browse the repository at this point in the history
  • Loading branch information
RJiraya committed May 3, 2024
1 parent 6c63cd9 commit a9b06cc
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 10 deletions.
25 changes: 17 additions & 8 deletions packages/@n8n/client-oauth2/src/ClientOAuth2Token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,27 @@ export class ClientOAuth2Token {

if (!this.refreshToken) throw new Error('No refresh token');

const clientId = options.clientId;
const clientSecret = options.clientSecret;
const headers = { ...DEFAULT_HEADERS };
const body: Record<string, any> = {

Check failure on line 80 in packages/@n8n/client-oauth2/src/ClientOAuth2Token.ts

View workflow job for this annotation

GitHub Actions / Lint changes

Unexpected any. Specify a different type
refresh_token: this.refreshToken,
grant_type: 'refresh_token',
};

if (options.authentication === 'body') {
body.client_id = clientId;
body.client_secret = clientSecret;
} else {
headers.Authorization = auth(clientId, clientSecret);
}

const requestOptions = getRequestOptions(
{
url: options.accessTokenUri,
method: 'POST',
headers: {
...DEFAULT_HEADERS,
Authorization: auth(options.clientId, options.clientSecret),
},
body: {
refresh_token: this.refreshToken,
grant_type: 'refresh_token',
},
headers: headers,

Check failure on line 96 in packages/@n8n/client-oauth2/src/ClientOAuth2Token.ts

View workflow job for this annotation

GitHub Actions / Lint changes

Expected property shorthand
body: body,

Check failure on line 97 in packages/@n8n/client-oauth2/src/ClientOAuth2Token.ts

View workflow job for this annotation

GitHub Actions / Lint changes

Expected property shorthand
},
options,
);
Expand Down
49 changes: 47 additions & 2 deletions packages/@n8n/client-oauth2/test/CredentialsFlow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ describe('CredentialsFlow', () => {
});

describe('#refresh', () => {
const mockRefreshCall = () =>
nock(config.baseUrl)
const mockRefreshCall = async () => {
const nockScope = nock(config.baseUrl)
.post(
'/login/oauth/access_token',
({ refresh_token, grant_type }) =>
Expand All @@ -142,6 +142,15 @@ describe('CredentialsFlow', () => {
access_token: config.refreshedAccessToken,
refresh_token: config.refreshedRefreshToken,
});
return await new Promise<{ headers: Headers; body: unknown }>((resolve) => {
nockScope.once('request', (req) => {
resolve({
headers: req.headers,
body: req.requestBodyBuffers.toString('utf-8'),
});
});
});
};

it('should make a request to get a new access token', async () => {
const authClient = createAuthClient({ scopes: ['notifications'] });
Expand All @@ -156,6 +165,42 @@ describe('CredentialsFlow', () => {
expect(token1.accessToken).toEqual(config.refreshedAccessToken);
expect(token1.tokenType).toEqual('bearer');
});

it('should make a request to get a new access token with authentication = "body"', async () => {
const authClient = createAuthClient({ scopes: ['notifications'], authentication: 'body' });
void mockTokenCall({ requestedScope: 'notifications' });

const token = await authClient.credentials.getToken();
expect(token.accessToken).toEqual(config.accessToken);

const requestPromise = mockRefreshCall();
const token1 = await token.refresh();
const { headers, body } = await requestPromise;

expect(token1).toBeInstanceOf(ClientOAuth2Token);
expect(token1.accessToken).toEqual(config.refreshedAccessToken);
expect(token1.tokenType).toEqual('bearer');
expect(headers?.authorization).toBe(undefined);
expect(body).toEqual('refresh_token=def456token&grant_type=refresh_token&client_id=abc&client_secret=123');

Check failure on line 184 in packages/@n8n/client-oauth2/test/CredentialsFlow.test.ts

View workflow job for this annotation

GitHub Actions / Lint changes

Replace `'refresh_token=def456token&grant_type=refresh_token&client_id=abc&client_secret=123'` with `⏎↹↹↹↹↹'refresh_token=def456token&grant_type=refresh_token&client_id=abc&client_secret=123',⏎↹↹↹↹`
});

it('should make a request to get a new access token with authentication = "header"', async () => {
const authClient = createAuthClient({ scopes: ['notifications'], authentication: 'header' });

Check failure on line 188 in packages/@n8n/client-oauth2/test/CredentialsFlow.test.ts

View workflow job for this annotation

GitHub Actions / Lint changes

Replace `·scopes:·['notifications'],·authentication:·'header'·` with `⏎↹↹↹↹↹scopes:·['notifications'],⏎↹↹↹↹↹authentication:·'header',⏎↹↹↹↹`
void mockTokenCall({ requestedScope: 'notifications' });

const token = await authClient.credentials.getToken();
expect(token.accessToken).toEqual(config.accessToken);

const requestPromise = mockRefreshCall();
const token1 = await token.refresh();
const { headers, body } = await requestPromise;

expect(token1).toBeInstanceOf(ClientOAuth2Token);
expect(token1.accessToken).toEqual(config.refreshedAccessToken);
expect(token1.tokenType).toEqual('bearer');
expect(headers?.authorization).toBe('Basic YWJjOjEyMw==');
expect(body).toEqual('refresh_token=def456token&grant_type=refresh_token');
});
});
});
});

0 comments on commit a9b06cc

Please sign in to comment.