Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HmacAuthGuard Test #503

Merged
merged 2 commits into from
Sep 8, 2024
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
17 changes: 17 additions & 0 deletions violet-server/package-lock.json

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

1 change: 1 addition & 0 deletions violet-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@golevelup/ts-jest": "^0.5.5",
"@nestjs/common": "^10.0.0",
"@nestjs/config": "^3.1.1",
"@nestjs/core": "^10.0.0",
Expand Down
9 changes: 4 additions & 5 deletions violet-server/src/auth/guards/hmac.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,13 @@ export class HmacAuthGuard implements CanActivate {
throw new BadRequestException();
}

return this.authTest(token, valid, this.configService.get<string>('SALT'));
return this.buildHmac(token) == valid;
}

authTest(token, valid, salt) {
buildHmac(token: string): string {
const mac = crypto.createHash('sha512');
const salt = this.configService.get<string>('SALT');
const hmac = mac.update(token + salt);
const hash = hmac.digest('hex').substr(0, 7);

return hash == valid;
return hmac.digest('hex').slice(0, 7);
}
}
65 changes: 65 additions & 0 deletions violet-server/src/auth/guards/hmac.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { ConfigModule, ConfigService } from '@nestjs/config';
import { HmacAuthGuard } from './hmac.guard';
import { Test, TestingModule } from '@nestjs/testing';
import { envValidationSchema } from 'src/app.module';
import { createMock } from '@golevelup/ts-jest';
import { BadRequestException, ExecutionContext } from '@nestjs/common';

describe('HmacAuthGuard', () => {
let guard: HmacAuthGuard;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath: '.test.env',
validationSchema: envValidationSchema,
}),
],
providers: [HmacAuthGuard],
}).compile();

guard = module.get<HmacAuthGuard>(HmacAuthGuard);
});

it('should be defined', () => {
expect(guard).toBeDefined();
});

function mockContext(token: string, valid: string) {
return createMock<ExecutionContext>({
switchToHttp: () => ({
getRequest: () => ({
headers: {
'v-token': token,
'v-valid': valid,
},
}),
}),
});
}

it('hmac success', () => {
const token = new Date().getTime().toString();
const context = mockContext(token, guard.buildHmac(token));
expect(guard.canActivate(context)).toBeTruthy();
});

it('hmac fail timestamp invalid format', () => {
const context = mockContext('abcd', 'edfg');
expect(() => guard.canActivate(context)).toThrow(BadRequestException);
});

it('hmac fail timestamp diff less', () => {
const token = (new Date().getTime() - 40000).toString();
const context = mockContext(token, guard.buildHmac(token));
expect(() => guard.canActivate(context)).toThrow(BadRequestException);
});

it('hmac fail timestamp diff greater', () => {
const token = (new Date().getTime() + 40000).toString();
const context = mockContext(token, guard.buildHmac(token));
expect(() => guard.canActivate(context)).toThrow(BadRequestException);
});
});
Loading