2020-08-13 16:27:00 -05:00
|
|
|
import path from 'path';
|
2018-08-21 01:05:34 -05:00
|
|
|
import _ from 'lodash';
|
2020-09-25 13:16:16 -05:00
|
|
|
import { CHARACTER_ENCODING, TOKEN_BEARER, ROLES, API_ERROR } from '@verdaccio/dev-commons';
|
2020-03-03 17:59:19 -05:00
|
|
|
|
|
|
|
import { configExample } from '@verdaccio/mock';
|
2020-08-13 16:27:00 -05:00
|
|
|
import { Config as AppConfig } from '@verdaccio/config';
|
|
|
|
import { setup } from '@verdaccio/logger';
|
2020-03-03 17:59:19 -05:00
|
|
|
|
2018-08-21 01:05:34 -05:00
|
|
|
import {
|
|
|
|
getAuthenticatedMessage,
|
2020-03-08 03:19:12 -05:00
|
|
|
buildToken,
|
2020-03-08 04:13:56 -05:00
|
|
|
parseConfigFile,
|
|
|
|
createAnonymousRemoteUser,
|
|
|
|
createRemoteUser,
|
2020-09-25 13:16:16 -05:00
|
|
|
AllowActionCallbackResponse,
|
2020-10-03 07:47:04 -05:00
|
|
|
buildUserBuffer,
|
2020-03-03 17:59:19 -05:00
|
|
|
} from '@verdaccio/utils';
|
2018-08-21 01:05:34 -05:00
|
|
|
|
2020-08-13 16:27:00 -05:00
|
|
|
import { Config, Security, RemoteUser } from '@verdaccio/types';
|
2020-09-25 13:16:16 -05:00
|
|
|
import { VerdaccioError, getForbidden } from '@verdaccio/commons-api';
|
|
|
|
import {
|
|
|
|
IAuth,
|
|
|
|
Auth,
|
|
|
|
ActionsAllowed,
|
|
|
|
allow_action,
|
|
|
|
getDefaultPlugins,
|
|
|
|
getMiddlewareCredentials,
|
|
|
|
getApiToken,
|
|
|
|
verifyJWTPayload,
|
|
|
|
getSecurity,
|
|
|
|
aesDecrypt,
|
|
|
|
verifyPayload,
|
|
|
|
signPayload,
|
2020-10-03 07:47:04 -05:00
|
|
|
buildUser,
|
2020-09-25 13:16:16 -05:00
|
|
|
} from '../src';
|
2020-03-08 03:19:12 -05:00
|
|
|
|
2019-02-24 17:20:25 -05:00
|
|
|
setup([]);
|
2018-08-21 01:05:34 -05:00
|
|
|
|
2020-03-03 17:59:19 -05:00
|
|
|
const parseConfigurationFile = (conf) => {
|
|
|
|
const { name, ext } = path.parse(conf);
|
|
|
|
const format = ext.startsWith('.') ? ext.substring(1) : 'yaml';
|
|
|
|
|
|
|
|
return path.join(__dirname, `./partials/config/${format}/security/${name}.${format}`);
|
|
|
|
};
|
|
|
|
|
2018-08-21 01:05:34 -05:00
|
|
|
describe('Auth utilities', () => {
|
2019-07-27 00:20:30 -05:00
|
|
|
jest.setTimeout(20000);
|
|
|
|
|
2018-08-21 01:05:34 -05:00
|
|
|
const parseConfigurationSecurityFile = (name) => {
|
|
|
|
return parseConfigurationFile(`security/${name}`);
|
|
|
|
};
|
|
|
|
|
|
|
|
function getConfig(configFileName: string, secret: string) {
|
|
|
|
const conf = parseConfigFile(parseConfigurationSecurityFile(configFileName));
|
2020-08-11 00:21:51 -05:00
|
|
|
// @ts-ignore
|
2020-08-13 16:27:00 -05:00
|
|
|
const secConf = _.merge(configExample(), conf);
|
2018-08-21 01:05:34 -05:00
|
|
|
secConf.secret = secret;
|
|
|
|
const config: Config = new AppConfig(secConf);
|
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2020-10-03 07:47:04 -05:00
|
|
|
async function getTokenByConfiguration(
|
2018-08-21 01:05:34 -05:00
|
|
|
configFileName: string,
|
|
|
|
username: string,
|
|
|
|
password: string,
|
|
|
|
secret = '12345',
|
|
|
|
methodToSpy: string,
|
2020-08-13 16:27:00 -05:00
|
|
|
methodNotBeenCalled: string
|
|
|
|
): Promise<string> {
|
2019-07-16 01:40:01 -05:00
|
|
|
const config: Config = getConfig(configFileName, secret);
|
|
|
|
const auth: IAuth = new Auth(config);
|
|
|
|
// @ts-ignore
|
|
|
|
const spy = jest.spyOn(auth, methodToSpy);
|
|
|
|
// @ts-ignore
|
|
|
|
const spyNotCalled = jest.spyOn(auth, methodNotBeenCalled);
|
|
|
|
const user: RemoteUser = {
|
|
|
|
name: username,
|
|
|
|
real_groups: [],
|
2020-08-13 16:27:00 -05:00
|
|
|
groups: [],
|
2019-07-16 01:40:01 -05:00
|
|
|
};
|
|
|
|
const token = await getApiToken(auth, config, user, password);
|
|
|
|
expect(spy).toHaveBeenCalled();
|
|
|
|
expect(spy).toHaveBeenCalledTimes(1);
|
|
|
|
expect(spyNotCalled).not.toHaveBeenCalled();
|
|
|
|
expect(token).toBeDefined();
|
|
|
|
|
2020-10-03 07:47:04 -05:00
|
|
|
return token as string;
|
2018-08-21 01:05:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const verifyJWT = (token: string, user: string, password: string, secret: string) => {
|
|
|
|
const payload = verifyPayload(token, secret);
|
|
|
|
expect(payload.name).toBe(user);
|
|
|
|
expect(payload.groups).toBeDefined();
|
|
|
|
expect(payload.real_groups).toBeDefined();
|
|
|
|
};
|
|
|
|
|
|
|
|
const verifyAES = (token: string, user: string, password: string, secret: string) => {
|
2020-10-03 07:47:04 -05:00
|
|
|
const payload = aesDecrypt(token, secret).toString(
|
2020-09-25 13:16:16 -05:00
|
|
|
// @ts-ignore
|
2020-09-16 23:48:16 -05:00
|
|
|
CHARACTER_ENCODING.UTF8
|
|
|
|
);
|
2018-08-21 01:05:34 -05:00
|
|
|
const content = payload.split(':');
|
|
|
|
|
|
|
|
expect(content[0]).toBe(user);
|
|
|
|
expect(content[0]).toBe(password);
|
|
|
|
};
|
|
|
|
|
2020-09-25 13:16:16 -05:00
|
|
|
describe('getDefaultPlugins', () => {
|
|
|
|
test('authentication should fail by default (default)', () => {
|
|
|
|
const plugin = getDefaultPlugins({ trace: jest.fn() });
|
|
|
|
plugin.authenticate('foo', 'bar', (error: any) => {
|
|
|
|
expect(error).toEqual(getForbidden(API_ERROR.BAD_USERNAME_PASSWORD));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('add user should fail by default (default)', () => {
|
|
|
|
const plugin = getDefaultPlugins({ trace: jest.fn() });
|
|
|
|
// @ts-ignore
|
|
|
|
plugin.adduser('foo', 'bar', (error: any) => {
|
|
|
|
expect(error).toEqual(getForbidden(API_ERROR.BAD_USERNAME_PASSWORD));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('allow_action', () => {
|
|
|
|
describe('access/publish/unpublish and anonymous', () => {
|
|
|
|
const packageAccess = {
|
|
|
|
name: 'foo',
|
|
|
|
version: undefined,
|
|
|
|
access: ['foo'],
|
|
|
|
unpublish: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
// const type = 'access';
|
|
|
|
test.each(['access', 'publish', 'unpublish'])(
|
|
|
|
'should restrict %s to anonymous users',
|
|
|
|
(type) => {
|
|
|
|
allow_action(type as ActionsAllowed, { trace: jest.fn() })(
|
|
|
|
createAnonymousRemoteUser(),
|
|
|
|
{
|
|
|
|
...packageAccess,
|
|
|
|
[type]: ['foo'],
|
|
|
|
},
|
|
|
|
(error: VerdaccioError | null, allowed: AllowActionCallbackResponse) => {
|
|
|
|
expect(error).not.toBeNull();
|
|
|
|
expect(allowed).toBeUndefined();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
test.each(['access', 'publish', 'unpublish'])(
|
|
|
|
'should allow %s to anonymous users',
|
|
|
|
(type) => {
|
|
|
|
allow_action(type as ActionsAllowed, { trace: jest.fn() })(
|
|
|
|
createAnonymousRemoteUser(),
|
|
|
|
{
|
|
|
|
...packageAccess,
|
|
|
|
[type]: [ROLES.$ANONYMOUS],
|
|
|
|
},
|
|
|
|
(error: VerdaccioError | null, allowed: AllowActionCallbackResponse) => {
|
|
|
|
expect(error).toBeNull();
|
|
|
|
expect(allowed).toBe(true);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
test.each(['access', 'publish', 'unpublish'])(
|
|
|
|
'should allow %s only if user is anonymous if the logged user has groups',
|
|
|
|
(type) => {
|
|
|
|
allow_action(type as ActionsAllowed, { trace: jest.fn() })(
|
|
|
|
createRemoteUser('juan', ['maintainer', 'admin']),
|
|
|
|
{
|
|
|
|
...packageAccess,
|
|
|
|
[type]: [ROLES.$ANONYMOUS],
|
|
|
|
},
|
|
|
|
(error: VerdaccioError | null, allowed: AllowActionCallbackResponse) => {
|
|
|
|
expect(error).not.toBeNull();
|
|
|
|
expect(allowed).toBeUndefined();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
test.each(['access', 'publish', 'unpublish'])(
|
|
|
|
'should allow %s only if user is anonymous match any other groups',
|
|
|
|
(type) => {
|
|
|
|
allow_action(type as ActionsAllowed, { trace: jest.fn() })(
|
|
|
|
createRemoteUser('juan', ['maintainer', 'admin']),
|
|
|
|
{
|
|
|
|
...packageAccess,
|
|
|
|
[type]: ['admin', 'some-other-group', ROLES.$ANONYMOUS],
|
|
|
|
},
|
|
|
|
(error: VerdaccioError | null, allowed: AllowActionCallbackResponse) => {
|
|
|
|
expect(error).toBeNull();
|
|
|
|
expect(allowed).toBe(true);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
test.each(['access', 'publish', 'unpublish'])(
|
|
|
|
'should not allow %s anonymous if other groups are defined and does not match',
|
|
|
|
(type) => {
|
|
|
|
allow_action(type as ActionsAllowed, { trace: jest.fn() })(
|
|
|
|
createRemoteUser('juan', ['maintainer', 'admin']),
|
|
|
|
{
|
|
|
|
...packageAccess,
|
|
|
|
[type]: ['bla-bla-group', 'some-other-group', ROLES.$ANONYMOUS],
|
|
|
|
},
|
|
|
|
(error: VerdaccioError | null, allowed: AllowActionCallbackResponse) => {
|
|
|
|
expect(error).not.toBeNull();
|
|
|
|
expect(allowed).toBeUndefined();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-08-21 01:05:34 -05:00
|
|
|
describe('getApiToken test', () => {
|
|
|
|
test('should sign token with aes and security missing', async () => {
|
2020-10-03 07:47:04 -05:00
|
|
|
const token = await getTokenByConfiguration(
|
2020-09-16 23:48:16 -05:00
|
|
|
'security-missing',
|
|
|
|
'test',
|
|
|
|
'test',
|
2020-10-03 07:47:04 -05:00
|
|
|
'b2df428b9929d3ace7c598bbf4e496b2',
|
2020-09-16 23:48:16 -05:00
|
|
|
'aesEncrypt',
|
|
|
|
'jwtEncrypt'
|
|
|
|
);
|
2018-08-21 01:05:34 -05:00
|
|
|
|
2020-10-03 07:47:04 -05:00
|
|
|
verifyAES(token, 'test', 'test', 'b2df428b9929d3ace7c598bbf4e496b2');
|
2018-08-21 01:05:34 -05:00
|
|
|
expect(_.isString(token)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2018-09-21 10:34:12 -05:00
|
|
|
test('should sign token with aes and security empty', async () => {
|
2020-10-03 07:47:04 -05:00
|
|
|
const token = await getTokenByConfiguration(
|
2020-09-16 23:48:16 -05:00
|
|
|
'security-empty',
|
|
|
|
'test',
|
|
|
|
'test',
|
2020-10-03 07:47:04 -05:00
|
|
|
'b2df428b9929d3ace7c598bbf4e496b2',
|
2020-09-16 23:48:16 -05:00
|
|
|
'aesEncrypt',
|
|
|
|
'jwtEncrypt'
|
|
|
|
);
|
2018-08-21 01:05:34 -05:00
|
|
|
|
2020-10-03 07:47:04 -05:00
|
|
|
verifyAES(token, 'test', 'test', 'b2df428b9929d3ace7c598bbf4e496b2');
|
2018-08-21 01:05:34 -05:00
|
|
|
expect(_.isString(token)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should sign token with aes', async () => {
|
2020-10-03 07:47:04 -05:00
|
|
|
const token = await getTokenByConfiguration(
|
2020-09-16 23:48:16 -05:00
|
|
|
'security-basic',
|
|
|
|
'test',
|
|
|
|
'test',
|
2020-10-03 07:47:04 -05:00
|
|
|
'b2df428b9929d3ace7c598bbf4e496b2',
|
2020-09-16 23:48:16 -05:00
|
|
|
'aesEncrypt',
|
|
|
|
'jwtEncrypt'
|
|
|
|
);
|
2018-08-21 01:05:34 -05:00
|
|
|
|
2020-10-03 07:47:04 -05:00
|
|
|
verifyAES(token, 'test', 'test', 'b2df428b9929d3ace7c598bbf4e496b2');
|
2018-08-21 01:05:34 -05:00
|
|
|
expect(_.isString(token)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should sign token with legacy and jwt disabled', async () => {
|
2020-10-03 07:47:04 -05:00
|
|
|
const token = await getTokenByConfiguration(
|
2020-09-16 23:48:16 -05:00
|
|
|
'security-no-legacy',
|
|
|
|
'test',
|
|
|
|
'test',
|
2020-10-03 07:47:04 -05:00
|
|
|
'b2df428b9929d3ace7c598bbf4e496b2',
|
2020-09-16 23:48:16 -05:00
|
|
|
'aesEncrypt',
|
|
|
|
'jwtEncrypt'
|
|
|
|
);
|
2018-08-21 01:05:34 -05:00
|
|
|
|
|
|
|
expect(_.isString(token)).toBeTruthy();
|
2020-10-03 07:47:04 -05:00
|
|
|
verifyAES(token, 'test', 'test', 'b2df428b9929d3ace7c598bbf4e496b2');
|
2018-08-21 01:05:34 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should sign token with legacy enabled and jwt enabled', async () => {
|
2020-10-03 07:47:04 -05:00
|
|
|
const token = await getTokenByConfiguration(
|
2020-09-16 23:48:16 -05:00
|
|
|
'security-jwt-legacy-enabled',
|
|
|
|
'test',
|
|
|
|
'test',
|
2020-10-03 07:47:04 -05:00
|
|
|
'b2df428b9929d3ace7c598bbf4e496b2',
|
2020-09-16 23:48:16 -05:00
|
|
|
'jwtEncrypt',
|
|
|
|
'aesEncrypt'
|
|
|
|
);
|
2018-08-21 01:05:34 -05:00
|
|
|
|
2020-10-03 07:47:04 -05:00
|
|
|
verifyJWT(token, 'test', 'test', 'b2df428b9929d3ace7c598bbf4e496b2');
|
2018-08-21 01:05:34 -05:00
|
|
|
expect(_.isString(token)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should sign token with jwt enabled', async () => {
|
2020-10-03 07:47:04 -05:00
|
|
|
const token = await getTokenByConfiguration(
|
2020-09-16 23:48:16 -05:00
|
|
|
'security-jwt',
|
|
|
|
'test',
|
|
|
|
'test',
|
2020-10-03 07:47:04 -05:00
|
|
|
'b2df428b9929d3ace7c598bbf4e496b2',
|
2020-09-16 23:48:16 -05:00
|
|
|
'jwtEncrypt',
|
|
|
|
'aesEncrypt'
|
|
|
|
);
|
2018-08-21 01:05:34 -05:00
|
|
|
|
2019-07-16 01:40:01 -05:00
|
|
|
expect(_.isString(token)).toBeTruthy();
|
2020-10-03 07:47:04 -05:00
|
|
|
verifyJWT(token, 'test', 'test', 'b2df428b9929d3ace7c598bbf4e496b2');
|
2018-08-21 01:05:34 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should sign with jwt whether legacy is disabled', async () => {
|
2020-10-03 07:47:04 -05:00
|
|
|
const token = await getTokenByConfiguration(
|
2020-09-16 23:48:16 -05:00
|
|
|
'security-legacy-disabled',
|
|
|
|
'test',
|
|
|
|
'test',
|
2020-10-03 07:47:04 -05:00
|
|
|
'b2df428b9929d3ace7c598bbf4e496b2',
|
2020-09-16 23:48:16 -05:00
|
|
|
'jwtEncrypt',
|
|
|
|
'aesEncrypt'
|
|
|
|
);
|
2018-08-21 01:05:34 -05:00
|
|
|
|
|
|
|
expect(_.isString(token)).toBeTruthy();
|
2020-10-03 07:47:04 -05:00
|
|
|
verifyJWT(token, 'test', 'test', 'b2df428b9929d3ace7c598bbf4e496b2');
|
2018-08-21 01:05:34 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getAuthenticatedMessage test', () => {
|
|
|
|
test('should sign token with jwt enabled', () => {
|
2020-08-13 16:27:00 -05:00
|
|
|
expect(getAuthenticatedMessage('test')).toBe("you are authenticated as 'test'");
|
2018-08-21 01:05:34 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getMiddlewareCredentials test', () => {
|
|
|
|
describe('should get AES credentials', () => {
|
2020-10-03 07:47:04 -05:00
|
|
|
test.concurrent('should unpack aes token and credentials bearer auth', async () => {
|
|
|
|
const secret = 'b2df428b9929d3ace7c598bbf4e496b2';
|
2019-07-16 01:40:01 -05:00
|
|
|
const user = 'test';
|
|
|
|
const pass = 'test';
|
2020-10-03 07:47:04 -05:00
|
|
|
const token = await getTokenByConfiguration(
|
2020-09-16 23:48:16 -05:00
|
|
|
'security-legacy',
|
|
|
|
user,
|
|
|
|
pass,
|
|
|
|
secret,
|
|
|
|
'aesEncrypt',
|
|
|
|
'jwtEncrypt'
|
|
|
|
);
|
2018-08-21 01:05:34 -05:00
|
|
|
const config: Config = getConfig('security-legacy', secret);
|
|
|
|
const security: Security = getSecurity(config);
|
|
|
|
const credentials = getMiddlewareCredentials(security, secret, `Bearer ${token}`);
|
|
|
|
expect(credentials).toBeDefined();
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
2018-08-21 01:05:34 -05:00
|
|
|
expect(credentials.user).toEqual(user);
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
2018-08-21 01:05:34 -05:00
|
|
|
expect(credentials.password).toEqual(pass);
|
|
|
|
});
|
|
|
|
|
2020-10-03 07:47:04 -05:00
|
|
|
test.concurrent('should unpack aes token and credentials basic auth', async () => {
|
|
|
|
const secret = 'b2df428b9929d3ace7c598bbf4e496b2';
|
2019-07-16 01:40:01 -05:00
|
|
|
const user = 'test';
|
|
|
|
const pass = 'test';
|
2020-10-03 07:47:04 -05:00
|
|
|
// basic authentication need send user as base64
|
2018-08-21 01:05:34 -05:00
|
|
|
const token = buildUserBuffer(user, pass).toString('base64');
|
|
|
|
const config: Config = getConfig('security-legacy', secret);
|
|
|
|
const security: Security = getSecurity(config);
|
|
|
|
const credentials = getMiddlewareCredentials(security, secret, `Basic ${token}`);
|
|
|
|
expect(credentials).toBeDefined();
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
2018-08-21 01:05:34 -05:00
|
|
|
expect(credentials.user).toEqual(user);
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
2018-08-21 01:05:34 -05:00
|
|
|
expect(credentials.password).toEqual(pass);
|
|
|
|
});
|
|
|
|
|
|
|
|
test.concurrent('should return empty credential wrong secret key', async () => {
|
2020-10-03 07:47:04 -05:00
|
|
|
const secret = 'b2df428b9929d3ace7c598bbf4e496b2';
|
|
|
|
const token = await getTokenByConfiguration(
|
2020-09-16 23:48:16 -05:00
|
|
|
'security-legacy',
|
|
|
|
'test',
|
|
|
|
'test',
|
|
|
|
secret,
|
|
|
|
'aesEncrypt',
|
|
|
|
'jwtEncrypt'
|
|
|
|
);
|
2018-08-21 01:05:34 -05:00
|
|
|
const config: Config = getConfig('security-legacy', secret);
|
|
|
|
const security: Security = getSecurity(config);
|
2020-09-16 23:48:16 -05:00
|
|
|
const credentials = getMiddlewareCredentials(
|
|
|
|
security,
|
2020-10-03 07:47:04 -05:00
|
|
|
'b2df428b9929d3ace7c598bbf4e496_BAD_TOKEN',
|
2020-09-16 23:48:16 -05:00
|
|
|
buildToken(TOKEN_BEARER, token)
|
|
|
|
);
|
2018-08-21 01:05:34 -05:00
|
|
|
expect(credentials).not.toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
test.concurrent('should return empty credential wrong scheme', async () => {
|
2020-10-03 07:47:04 -05:00
|
|
|
const secret = 'b2df428b9929d3ace7c598bbf4e496b2';
|
|
|
|
const token = await getTokenByConfiguration(
|
2020-09-16 23:48:16 -05:00
|
|
|
'security-legacy',
|
|
|
|
'test',
|
|
|
|
'test',
|
|
|
|
secret,
|
|
|
|
'aesEncrypt',
|
|
|
|
'jwtEncrypt'
|
|
|
|
);
|
2018-08-21 01:05:34 -05:00
|
|
|
const config: Config = getConfig('security-legacy', secret);
|
|
|
|
const security: Security = getSecurity(config);
|
2020-09-16 23:48:16 -05:00
|
|
|
const credentials = getMiddlewareCredentials(
|
|
|
|
security,
|
|
|
|
secret,
|
|
|
|
buildToken('BAD_SCHEME', token)
|
|
|
|
);
|
2018-08-21 01:05:34 -05:00
|
|
|
expect(credentials).not.toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
test.concurrent('should return empty credential corrupted payload', async () => {
|
2020-10-03 07:47:04 -05:00
|
|
|
const secret = 'b2df428b9929d3ace7c598bbf4e496b2';
|
2018-08-21 01:05:34 -05:00
|
|
|
const config: Config = getConfig('security-legacy', secret);
|
|
|
|
const auth: IAuth = new Auth(config);
|
2020-10-03 07:47:04 -05:00
|
|
|
const token = auth.aesEncrypt(null);
|
2018-08-21 01:05:34 -05:00
|
|
|
const security: Security = getSecurity(config);
|
2020-09-16 23:48:16 -05:00
|
|
|
const credentials = getMiddlewareCredentials(
|
|
|
|
security,
|
|
|
|
secret,
|
2020-10-03 07:47:04 -05:00
|
|
|
buildToken(TOKEN_BEARER, token as string)
|
2020-09-16 23:48:16 -05:00
|
|
|
);
|
2018-08-21 01:05:34 -05:00
|
|
|
expect(credentials).not.toBeDefined();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-08 04:13:56 -05:00
|
|
|
describe('verifyJWTPayload', () => {
|
|
|
|
test('should fail on verify the token and return anonymous users', () => {
|
2020-10-03 07:47:04 -05:00
|
|
|
expect(verifyJWTPayload('fakeToken', 'b2df428b9929d3ace7c598bbf4e496b2')).toEqual(
|
|
|
|
createAnonymousRemoteUser()
|
|
|
|
);
|
2020-03-08 04:13:56 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should fail on verify the token and return anonymous users', async () => {
|
|
|
|
const remoteUser = createRemoteUser('foo', []);
|
|
|
|
const token = await signPayload(remoteUser, '12345');
|
|
|
|
const verifiedToken = verifyJWTPayload(token, '12345');
|
|
|
|
expect(verifiedToken.groups).toEqual(remoteUser.groups);
|
|
|
|
expect(verifiedToken.name).toEqual(remoteUser.name);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-08-21 01:05:34 -05:00
|
|
|
describe('should get JWT credentials', () => {
|
|
|
|
test('should return anonymous whether token is corrupted', () => {
|
|
|
|
const config: Config = getConfig('security-jwt', '12345');
|
|
|
|
const security: Security = getSecurity(config);
|
2020-09-16 23:48:16 -05:00
|
|
|
const credentials = getMiddlewareCredentials(
|
|
|
|
security,
|
|
|
|
'12345',
|
|
|
|
buildToken(TOKEN_BEARER, 'fakeToken')
|
|
|
|
);
|
2018-08-21 01:05:34 -05:00
|
|
|
|
|
|
|
expect(credentials).toBeDefined();
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
2018-08-21 01:05:34 -05:00
|
|
|
expect(credentials.name).not.toBeDefined();
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
2018-08-21 01:05:34 -05:00
|
|
|
expect(credentials.real_groups).toBeDefined();
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
2018-08-21 01:05:34 -05:00
|
|
|
expect(credentials.real_groups).toEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should return anonymous whether token and scheme are corrupted', () => {
|
|
|
|
const config: Config = getConfig('security-jwt', '12345');
|
|
|
|
const security: Security = getSecurity(config);
|
2020-09-16 23:48:16 -05:00
|
|
|
const credentials = getMiddlewareCredentials(
|
|
|
|
security,
|
|
|
|
'12345',
|
|
|
|
buildToken('FakeScheme', 'fakeToken')
|
|
|
|
);
|
2018-08-21 01:05:34 -05:00
|
|
|
|
|
|
|
expect(credentials).not.toBeDefined();
|
|
|
|
});
|
|
|
|
|
2020-10-03 07:47:04 -05:00
|
|
|
test('should verify successfully a JWT token', async () => {
|
|
|
|
const secret = 'b2df428b9929d3ace7c598bbf4e496b2';
|
2019-07-16 01:40:01 -05:00
|
|
|
const user = 'test';
|
2018-08-21 01:05:34 -05:00
|
|
|
const config: Config = getConfig('security-jwt', secret);
|
2020-10-03 07:47:04 -05:00
|
|
|
const token = await getTokenByConfiguration(
|
2020-09-16 23:48:16 -05:00
|
|
|
'security-jwt',
|
|
|
|
user,
|
|
|
|
'secretTest',
|
|
|
|
secret,
|
|
|
|
'jwtEncrypt',
|
|
|
|
'aesEncrypt'
|
|
|
|
);
|
2018-08-21 01:05:34 -05:00
|
|
|
const security: Security = getSecurity(config);
|
2020-09-16 23:48:16 -05:00
|
|
|
const credentials = getMiddlewareCredentials(
|
|
|
|
security,
|
|
|
|
secret,
|
|
|
|
buildToken(TOKEN_BEARER, token)
|
|
|
|
);
|
2018-08-21 01:05:34 -05:00
|
|
|
expect(credentials).toBeDefined();
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
2018-08-21 01:05:34 -05:00
|
|
|
expect(credentials.name).toEqual(user);
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
2018-08-21 01:05:34 -05:00
|
|
|
expect(credentials.real_groups).toBeDefined();
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
2018-08-21 01:05:34 -05:00
|
|
|
expect(credentials.real_groups).toEqual([]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|