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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"qs": "^6.16.1",
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1",
"tc-core-library-js": "github:topcoder-platform/tc-core-library-js#v4.0.0-rc.2",
"tc-core-library-js": "https://codeload.github.com/topcoder-platform/tc-core-library-js/tar.gz/c2df92b2227089909ab6e229df0ec6b658bb431a",
"uuid": "^14.0.1"
},
"devDependencies": {
Expand Down
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

110 changes: 71 additions & 39 deletions src/shared/modules/global/jwt.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ describe('JwtService validation', () => {
});
const token = sign(
{
emailVerified: true,
roles: [UserRole.Admin],
userId: '12345',
scope: 'openid profile offline_access',
Expand Down Expand Up @@ -197,6 +198,7 @@ describe('JwtService validation', () => {
});
const token = sign(
{
emailVerified: true,
scope: Scope.ReadGroups,
gty: 'client-credentials',
roles: [UserRole.Admin],
Expand Down Expand Up @@ -249,7 +251,7 @@ describe('JwtService validation', () => {
getPublicKey: () => publicKey.export({ type: 'spki', format: 'pem' }),
});
const token = sign(
{ roles: [UserRole.Admin], userId: '12345' },
{ emailVerified: true, roles: [UserRole.Admin], userId: '12345' },
privateKey,
{
algorithm: 'RS256',
Expand All @@ -271,13 +273,17 @@ describe('JwtService validation', () => {

it('rejects an unlisted issuer before requesting its signing key', async () => {
const signingKeySpy = jest.spyOn(JwksClient.prototype, 'getSigningKey');
const token = sign({ roles: [UserRole.Admin] }, privateKey, {
algorithm: 'RS256',
keyid: 'test-key-id',
issuer: 'https://unlisted-issuer.example/',
audience: AUDIENCE,
expiresIn: 300,
});
const token = sign(
{ emailVerified: true, roles: [UserRole.Admin] },
privateKey,
{
algorithm: 'RS256',
keyid: 'test-key-id',
issuer: 'https://unlisted-issuer.example/',
audience: AUDIENCE,
expiresIn: 300,
},
);

await expect(service.validateToken(token)).rejects.toBeInstanceOf(
UnauthorizedException,
Expand All @@ -287,12 +293,16 @@ describe('JwtService validation', () => {

it('rejects an RS256 token without a key ID', async () => {
const signingKeySpy = jest.spyOn(JwksClient.prototype, 'getSigningKey');
const token = sign({ roles: [UserRole.Admin] }, privateKey, {
algorithm: 'RS256',
issuer: ALLOWED_ISSUER,
audience: AUDIENCE,
expiresIn: 300,
});
const token = sign(
{ emailVerified: true, roles: [UserRole.Admin] },
privateKey,
{
algorithm: 'RS256',
issuer: ALLOWED_ISSUER,
audience: AUDIENCE,
expiresIn: 300,
},
);

await expect(service.validateToken(token)).rejects.toBeInstanceOf(
UnauthorizedException,
Expand All @@ -303,6 +313,7 @@ describe('JwtService validation', () => {
it('accepts a legacy HS256 admin token without kid or audience', async () => {
const token = sign(
{
emailVerified: true,
roles: [UserRole.Admin],
userId: '12345',
handle: 'admin-user',
Expand All @@ -326,11 +337,15 @@ describe('JwtService validation', () => {
});

it('rejects an HS256 token signed with a different secret', async () => {
const token = sign({ roles: [UserRole.Admin] }, 'wrong-secret', {
algorithm: 'HS256',
issuer: ALLOWED_ISSUER,
expiresIn: 300,
});
const token = sign(
{ emailVerified: true, roles: [UserRole.Admin] },
'wrong-secret',
{
algorithm: 'HS256',
issuer: ALLOWED_ISSUER,
expiresIn: 300,
},
);

await expect(service.validateToken(token)).rejects.toBeInstanceOf(
UnauthorizedException,
Expand All @@ -339,11 +354,15 @@ describe('JwtService validation', () => {

it('still verifies signatures when NODE_ENV is missing', async () => {
delete process.env.NODE_ENV;
const token = sign({ roles: [UserRole.Admin] }, 'wrong-secret', {
algorithm: 'HS256',
issuer: ALLOWED_ISSUER,
expiresIn: 300,
});
const token = sign(
{ emailVerified: true, roles: [UserRole.Admin] },
'wrong-secret',
{
algorithm: 'HS256',
issuer: ALLOWED_ISSUER,
expiresIn: 300,
},
);

await expect(service.validateToken(token)).rejects.toBeInstanceOf(
UnauthorizedException,
Expand All @@ -352,23 +371,31 @@ describe('JwtService validation', () => {

it('rejects an HS256 token when AUTH_SECRET is unavailable', async () => {
AuthConfig.authSecret = undefined;
const token = sign({ roles: [UserRole.Admin] }, AUTH_SECRET, {
algorithm: 'HS256',
issuer: ALLOWED_ISSUER,
expiresIn: 300,
});
const token = sign(
{ emailVerified: true, roles: [UserRole.Admin] },
AUTH_SECRET,
{
algorithm: 'HS256',
issuer: ALLOWED_ISSUER,
expiresIn: 300,
},
);

await expect(service.validateToken(token)).rejects.toBeInstanceOf(
UnauthorizedException,
);
});

it('rejects an expired HS256 admin token', async () => {
const token = sign({ roles: [UserRole.Admin] }, AUTH_SECRET, {
algorithm: 'HS256',
issuer: ALLOWED_ISSUER,
expiresIn: -60,
});
const token = sign(
{ emailVerified: true, roles: [UserRole.Admin] },
AUTH_SECRET,
{
algorithm: 'HS256',
issuer: ALLOWED_ISSUER,
expiresIn: -60,
},
);

await expect(service.validateToken(token)).rejects.toBeInstanceOf(
UnauthorizedException,
Expand All @@ -378,6 +405,7 @@ describe('JwtService validation', () => {
it('accepts an HS256 admin token with a non-M2M audience', async () => {
const token = sign(
{
emailVerified: true,
roles: [UserRole.Admin],
userId: '12345',
scope: 'openid profile offline_access',
Expand Down Expand Up @@ -423,11 +451,15 @@ describe('JwtService validation', () => {
});

it('rejects unsupported signing algorithms', async () => {
const token = sign({ roles: [UserRole.Admin] }, AUTH_SECRET, {
algorithm: 'HS384',
issuer: ALLOWED_ISSUER,
expiresIn: 300,
});
const token = sign(
{ emailVerified: true, roles: [UserRole.Admin] },
AUTH_SECRET,
{
algorithm: 'HS384',
issuer: ALLOWED_ISSUER,
expiresIn: 300,
},
);

await expect(service.validateToken(token)).rejects.toBeInstanceOf(
UnauthorizedException,
Expand Down
Loading