2020-11-15 11:14:09 +01:00
|
|
|
import { DEFAULT_REGISTRY } from '@verdaccio/config';
|
2021-10-29 17:33:05 +02:00
|
|
|
import { HEADERS, TOKEN_BASIC, TOKEN_BEARER, constants } from '@verdaccio/core';
|
|
|
|
import { setup } from '@verdaccio/logger';
|
|
|
|
import { buildToken } from '@verdaccio/utils';
|
2020-03-03 23:59:19 +01:00
|
|
|
|
|
|
|
import { ProxyStorage } from '../src/up-storage';
|
2018-06-19 22:02:29 +02:00
|
|
|
|
|
|
|
setup([]);
|
|
|
|
|
|
|
|
function createUplink(config) {
|
|
|
|
const defaultConfig = {
|
2020-08-13 23:27:00 +02:00
|
|
|
url: DEFAULT_REGISTRY,
|
2018-06-19 22:02:29 +02:00
|
|
|
};
|
2020-03-03 23:59:19 +01:00
|
|
|
const mergeConfig = Object.assign({}, defaultConfig, config);
|
2019-08-16 21:20:18 +02:00
|
|
|
// @ts-ignore
|
2018-06-19 22:02:29 +02:00
|
|
|
return new ProxyStorage(mergeConfig, {});
|
|
|
|
}
|
|
|
|
|
2019-08-16 21:20:18 +02:00
|
|
|
function setHeaders(config: unknown = {}, headers: unknown = {}) {
|
2018-06-19 22:02:29 +02:00
|
|
|
const uplink = createUplink(config);
|
2021-05-08 09:47:30 +02:00
|
|
|
// @ts-expect-error
|
2018-06-19 22:02:29 +02:00
|
|
|
return uplink._setHeaders({
|
2020-08-13 23:27:00 +02:00
|
|
|
headers,
|
2018-06-19 22:02:29 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-20 07:33:39 +02:00
|
|
|
describe('uplink headers auth test', () => {
|
2018-06-19 22:02:29 +02:00
|
|
|
test('if set headers empty should return default headers', () => {
|
|
|
|
const headers = setHeaders();
|
|
|
|
const keys = Object.keys(headers);
|
2019-05-19 21:37:43 +02:00
|
|
|
const keysExpected = [HEADERS.ACCEPT, HEADERS.ACCEPT_ENCODING, HEADERS.USER_AGENT];
|
2018-06-19 22:02:29 +02:00
|
|
|
|
|
|
|
expect(keys).toEqual(keysExpected);
|
|
|
|
expect(keys).toHaveLength(3);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('if assigns value invalid to attribute auth', () => {
|
|
|
|
const fnError = function () {
|
|
|
|
setHeaders({
|
2020-08-13 23:27:00 +02:00
|
|
|
auth: '',
|
2018-06-19 22:02:29 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-08-13 23:27:00 +02:00
|
|
|
expect(function () {
|
2018-06-19 22:02:29 +02:00
|
|
|
fnError();
|
|
|
|
}).toThrow(Error('Auth invalid'));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('if assigns the header authorization', () => {
|
2020-08-13 23:27:00 +02:00
|
|
|
const headers = setHeaders(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
[HEADERS.AUTHORIZATION]: buildToken(TOKEN_BASIC, 'Zm9vX2Jhcg=='),
|
|
|
|
}
|
|
|
|
);
|
2018-06-19 22:02:29 +02:00
|
|
|
|
|
|
|
expect(Object.keys(headers)).toHaveLength(4);
|
2019-05-19 21:37:43 +02:00
|
|
|
expect(headers[HEADERS.AUTHORIZATION]).toEqual(buildToken(TOKEN_BASIC, 'Zm9vX2Jhcg=='));
|
2018-06-19 22:02:29 +02:00
|
|
|
});
|
|
|
|
|
2020-08-13 23:27:00 +02:00
|
|
|
test('if assigns headers authorization and token the header precedes', () => {
|
|
|
|
const headers = setHeaders(
|
|
|
|
{
|
2018-06-19 22:02:29 +02:00
|
|
|
auth: {
|
|
|
|
type: TOKEN_BEARER,
|
2020-08-13 23:27:00 +02:00
|
|
|
token: 'tokenBearer',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[HEADERS.AUTHORIZATION]: buildToken(TOKEN_BASIC, 'tokenBasic'),
|
|
|
|
}
|
|
|
|
);
|
2018-06-19 22:02:29 +02:00
|
|
|
|
2020-08-13 23:27:00 +02:00
|
|
|
expect(headers[HEADERS.AUTHORIZATION]).toEqual(buildToken(TOKEN_BASIC, 'tokenBasic'));
|
|
|
|
});
|
2018-06-19 22:02:29 +02:00
|
|
|
|
|
|
|
test('set type auth basic', () => {
|
|
|
|
const headers = setHeaders({
|
|
|
|
auth: {
|
|
|
|
type: TOKEN_BASIC,
|
2020-08-13 23:27:00 +02:00
|
|
|
token: 'Zm9vX2Jhcg==',
|
|
|
|
},
|
2018-06-19 22:02:29 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(Object.keys(headers)).toHaveLength(4);
|
2019-05-19 21:37:43 +02:00
|
|
|
expect(headers[HEADERS.AUTHORIZATION]).toEqual(buildToken(TOKEN_BASIC, 'Zm9vX2Jhcg=='));
|
2018-06-19 22:02:29 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test('set type auth bearer', () => {
|
|
|
|
const headers = setHeaders({
|
|
|
|
auth: {
|
|
|
|
type: TOKEN_BEARER,
|
2020-08-13 23:27:00 +02:00
|
|
|
token: 'Zm9vX2Jhcf===',
|
|
|
|
},
|
2018-06-19 22:02:29 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(Object.keys(headers)).toHaveLength(4);
|
2019-05-19 21:37:43 +02:00
|
|
|
expect(headers[HEADERS.AUTHORIZATION]).toEqual(buildToken(TOKEN_BEARER, 'Zm9vX2Jhcf==='));
|
2018-06-19 22:02:29 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test('set auth type invalid', () => {
|
2020-08-13 23:27:00 +02:00
|
|
|
const fnError = function () {
|
2018-06-19 22:02:29 +02:00
|
|
|
setHeaders({
|
|
|
|
auth: {
|
|
|
|
type: 'null',
|
2020-08-13 23:27:00 +02:00
|
|
|
token: 'Zm9vX2Jhcf===',
|
|
|
|
},
|
|
|
|
});
|
2018-06-19 22:02:29 +02:00
|
|
|
};
|
|
|
|
|
2020-08-13 23:27:00 +02:00
|
|
|
expect(function () {
|
2018-06-19 22:02:29 +02:00
|
|
|
fnError();
|
|
|
|
}).toThrow(Error(`Auth type 'null' not allowed`));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('set auth with NPM_TOKEN', () => {
|
|
|
|
process.env.NPM_TOKEN = 'myToken';
|
|
|
|
const headers = setHeaders({
|
|
|
|
auth: {
|
2020-08-13 23:27:00 +02:00
|
|
|
type: TOKEN_BEARER,
|
|
|
|
},
|
2018-06-19 22:02:29 +02:00
|
|
|
});
|
|
|
|
|
2019-05-19 21:37:43 +02:00
|
|
|
expect(headers[HEADERS.AUTHORIZATION]).toBe(buildToken(TOKEN_BEARER, 'myToken'));
|
2018-06-19 22:02:29 +02:00
|
|
|
delete process.env.NPM_TOKEN;
|
|
|
|
});
|
|
|
|
|
|
|
|
test('set auth with token name and assigns in env', () => {
|
|
|
|
process.env.NPM_TOKEN_TEST = 'myTokenTest';
|
|
|
|
const headers = setHeaders({
|
|
|
|
auth: {
|
|
|
|
type: TOKEN_BASIC,
|
2020-08-13 23:27:00 +02:00
|
|
|
token_env: 'NPM_TOKEN_TEST',
|
|
|
|
},
|
2018-06-19 22:02:29 +02:00
|
|
|
});
|
|
|
|
|
2019-05-19 21:37:43 +02:00
|
|
|
expect(headers[HEADERS.AUTHORIZATION]).toBe(buildToken(TOKEN_BASIC, 'myTokenTest'));
|
2018-06-19 22:02:29 +02:00
|
|
|
delete process.env.NPM_TOKEN_TEST;
|
|
|
|
});
|
|
|
|
|
|
|
|
test('if token not set', () => {
|
2020-08-13 23:27:00 +02:00
|
|
|
const fnError = function () {
|
2018-06-19 22:02:29 +02:00
|
|
|
setHeaders({
|
|
|
|
auth: {
|
2020-08-13 23:27:00 +02:00
|
|
|
type: TOKEN_BASIC,
|
|
|
|
},
|
2018-06-19 22:02:29 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-08-13 23:27:00 +02:00
|
|
|
expect(function () {
|
2018-06-19 22:02:29 +02:00
|
|
|
fnError();
|
2021-09-26 00:08:00 +02:00
|
|
|
}).toThrow(constants.ERROR_CODE.token_required);
|
2018-06-19 22:02:29 +02:00
|
|
|
});
|
|
|
|
});
|