Skip to content

Commit 14a0b76

Browse files
committedAug 12, 2021
fix: use OAUTH token if set for analytics
1 parent fa5bc2e commit 14a0b76

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed
 

‎src/lib/analytics/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const snyk = require('../../lib');
2+
import { someTokenExists, getAuthHeader } from '../api-token';
23
const config = require('../config');
34
import { makeRequest } from '../request';
45
const debug = require('debug')('snyk');
@@ -66,8 +67,8 @@ export async function postAnalytics(
6667
debug('analytics', JSON.stringify(analyticsData, null, ' '));
6768

6869
const headers = {};
69-
if (snyk.api) {
70-
headers['authorization'] = 'token ' + snyk.api;
70+
if (someTokenExists()) {
71+
headers['authorization'] = getAuthHeader();
7172
}
7273

7374
const queryStringParams = {};

‎src/lib/api-token.ts

+4
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,7 @@ export function getAuthHeader(): string {
4444
}
4545
return `token ${api()}`;
4646
}
47+
48+
export function someTokenExists(): boolean {
49+
return Boolean(getOAuthToken() || getDockerToken() || api());
50+
}

‎test/jest/acceptance/analytics.spec.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ describe('analytics module', () => {
2222
SNYK_INTEGRATION_NAME: 'JENKINS',
2323
SNYK_INTEGRATION_VERSION: '1.2.3',
2424
};
25-
2625
server = fakeServer(baseApi, env.SNYK_TOKEN);
2726
server.listen(port, () => {
2827
done();
@@ -300,6 +299,25 @@ describe('analytics module', () => {
300299
});
301300
});
302301

302+
it('uses OAUTH token if set', async () => {
303+
const project = await createProjectFromWorkspace('npm-package');
304+
const { code } = await runSnykCLI('version', {
305+
cwd: project.path(),
306+
env: {
307+
...env,
308+
SNYK_OAUTH_TOKEN: 'oauth-jwt-token',
309+
},
310+
});
311+
expect(code).toBe(0);
312+
313+
const lastRequest = server.popRequest();
314+
expect(lastRequest).toMatchObject({
315+
headers: {
316+
authorization: 'Bearer oauth-jwt-token',
317+
},
318+
});
319+
});
320+
303321
it("won't send analytics if disable analytics is set", async () => {
304322
const { code } = await runSnykCLI(`version`, {
305323
env: {
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import * as analytics from '../../../../../src/lib/analytics';
2+
import * as request from '../../../../../src/lib/request';
3+
import { argsFrom } from './utils';
4+
import * as apiTokenModule from '../../../../../src/lib/api-token';
5+
6+
describe('analytics module', () => {
7+
it('sends anaytics with no token set', async () => {
8+
analytics.add('k1', 'v1');
9+
const requestSpy = jest.spyOn(request, 'makeRequest');
10+
const someTokenExistsSpy = jest.spyOn(apiTokenModule, 'someTokenExists');
11+
someTokenExistsSpy.mockReturnValue(false);
12+
requestSpy.mockImplementation(jest.fn());
13+
await analytics.addDataAndSend({
14+
args: argsFrom({}),
15+
});
16+
expect(requestSpy).toBeCalledTimes(1);
17+
expect(requestSpy.mock.calls[0][0]).not.toHaveProperty(
18+
'headers.authorization',
19+
);
20+
});
21+
});

0 commit comments

Comments
 (0)
Please sign in to comment.