-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Job Launcher] Exchange Oracle webhook (#849)
* Added signature verification logic, added exchange oracle webhook processing, added unit tests * Removed unused interface * Updated route name * Revert changes * Added oracle type to endpoint
- Loading branch information
1 parent
fab76cf
commit b21b382
Showing
12 changed files
with
265 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './jwt.auth'; | ||
export * from './signature.auth'; |
103 changes: 103 additions & 0 deletions
103
packages/apps/job-launcher/server/src/common/guards/signature.auth.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { ExecutionContext, UnauthorizedException, BadRequestException } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { SignatureAuthGuard } from './signature.auth'; | ||
import { verifySignature } from '../utils/signature'; | ||
import { MOCK_ADDRESS } from '../../../test/constants'; | ||
|
||
jest.mock('../../common/utils/signature'); | ||
|
||
describe('SignatureAuthGuard', () => { | ||
let guard: SignatureAuthGuard; | ||
let mockConfigService: Partial<ConfigService>; | ||
|
||
beforeEach(async () => { | ||
mockConfigService = { | ||
get: jest.fn(), | ||
}; | ||
|
||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
SignatureAuthGuard, | ||
{ provide: ConfigService, useValue: mockConfigService } | ||
], | ||
}).compile(); | ||
|
||
guard = module.get<SignatureAuthGuard>(SignatureAuthGuard); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(guard).toBeDefined(); | ||
}); | ||
|
||
describe('canActivate', () => { | ||
let context: ExecutionContext; | ||
let mockRequest: any; | ||
|
||
beforeEach(() => { | ||
mockRequest = { | ||
switchToHttp: jest.fn().mockReturnThis(), | ||
getRequest: jest.fn().mockReturnThis(), | ||
headers: {}, | ||
body: {}, | ||
originalUrl: '', | ||
}; | ||
context = { | ||
switchToHttp: jest.fn().mockReturnThis(), | ||
getRequest: jest.fn(() => mockRequest) | ||
} as any as ExecutionContext; | ||
}); | ||
|
||
it('should return true if signature is verified', async () => { | ||
mockRequest.headers['header-signature-key'] = 'validSignature'; | ||
jest.spyOn(guard, 'determineAddress').mockReturnValue('someAddress'); | ||
(verifySignature as jest.Mock).mockReturnValue(true); | ||
|
||
const result = await guard.canActivate(context as any); | ||
expect(result).toBeTruthy(); | ||
}); | ||
|
||
it('should throw unauthorized exception if signature is not verified', async () => { | ||
jest.spyOn(guard, 'determineAddress').mockReturnValue('someAddress'); | ||
(verifySignature as jest.Mock).mockReturnValue(false); | ||
|
||
await expect(guard.canActivate(context as any)).rejects.toThrow(UnauthorizedException); | ||
}); | ||
|
||
it('should throw unauthorized exception for unrecognized oracle type', async () => { | ||
mockRequest.originalUrl = '/some/random/path'; | ||
await expect(guard.canActivate(context as any)).rejects.toThrow(UnauthorizedException); | ||
}); | ||
}); | ||
|
||
describe('determineAddress', () => { | ||
it('should return the correct address if originalUrl contains the fortune oracle type', () => { | ||
const mockRequest = { originalUrl: '/somepath/fortune/anotherpath' }; | ||
const expectedAddress = MOCK_ADDRESS; | ||
mockConfigService.get = jest.fn().mockReturnValue(expectedAddress); | ||
|
||
const result = guard.determineAddress(mockRequest); | ||
|
||
expect(result).toEqual(expectedAddress); | ||
}); | ||
|
||
it('should return the correct address if originalUrl contains the cvat oracle type', () => { | ||
const mockRequest = { originalUrl: '/somepath/cvat/anotherpath' }; | ||
const expectedAddress = MOCK_ADDRESS; | ||
mockConfigService.get = jest.fn().mockReturnValue(expectedAddress); | ||
|
||
const result = guard.determineAddress(mockRequest); | ||
|
||
expect(result).toEqual(expectedAddress); | ||
}); | ||
|
||
it('should throw BadRequestException for unrecognized oracle type', () => { | ||
const mockRequest = { originalUrl: '/some/random/path' }; | ||
|
||
expect(() => { | ||
guard.determineAddress(mockRequest); | ||
}).toThrow(BadRequestException); | ||
}); | ||
|
||
}); | ||
}); |
52 changes: 52 additions & 0 deletions
52
packages/apps/job-launcher/server/src/common/guards/signature.auth.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
|
||
import { BadRequestException, CanActivate, ExecutionContext, Injectable, UnauthorizedException } from '@nestjs/common'; | ||
import { verifySignature } from '../utils/signature'; | ||
import { HEADER_SIGNATURE_KEY } from '../constants'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { ConfigNames } from '../config'; | ||
import { OracleType } from '../enums/webhook'; | ||
|
||
@Injectable() | ||
export class SignatureAuthGuard implements CanActivate { | ||
constructor( | ||
public readonly configService: ConfigService | ||
) {} | ||
|
||
public async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const request = context.switchToHttp().getRequest(); | ||
|
||
const data = request.body; | ||
const signature = request.headers[HEADER_SIGNATURE_KEY]; | ||
|
||
try { | ||
const address = this.determineAddress(request); | ||
const isVerified = verifySignature(data, signature, address) | ||
|
||
if (isVerified) { | ||
return true; | ||
} | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
|
||
throw new UnauthorizedException('Unauthorized'); | ||
} | ||
|
||
public determineAddress(request: any): string { | ||
const originalUrl = request.originalUrl; | ||
const parts = originalUrl.split('/'); | ||
const oracleType = parts[2]; | ||
|
||
if (oracleType === OracleType.FORTUNE) { | ||
return this.configService.get<string>( | ||
ConfigNames.FORTUNE_EXCHANGE_ORACLE_ADDRESS, | ||
)! | ||
} else if (oracleType === OracleType.CVAT) { | ||
return this.configService.get<string>( | ||
ConfigNames.CVAT_EXCHANGE_ORACLE_ADDRESS, | ||
)! | ||
} else { | ||
throw new BadRequestException('Unable to determine address from origin URL'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export interface RequestWithUser extends Request { | ||
user: any; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
b21b382
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
job-launcher-server – ./packages/apps/job-launcher/server
job-launcher-server-git-develop-humanprotocol.vercel.app
job-launcher-server-nine.vercel.app
job-launcher-server-humanprotocol.vercel.app