-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(intel-service): add exception and guard
- Loading branch information
1 parent
21f8210
commit 8819c51
Showing
25 changed files
with
309 additions
and
122 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# ignore the .ts files | ||
*.ts | ||
|
||
# include the .d.ts files | ||
!*.d.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
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require('dotenv').config(); | ||
|
||
module.exports = { | ||
env: 'default', | ||
production: false, | ||
container: { | ||
intelService: { | ||
port: 6531, | ||
username: 'test', | ||
password: 'test2', | ||
} | ||
}, | ||
exchange: { | ||
crypto: { | ||
bitmex: { | ||
apiKey: process.env.SPEC_BITMEX_REAL_API_KEY, | ||
apiSecret: process.env.SPEC_BITMEX_REAL_API_SECRET, | ||
}, | ||
bitmexTestNet: { | ||
apiKey: process.env.SPEC_BITMEX_TEST_API_KEY, | ||
apiSecret: process.env.SPEC_BITMEX_TEST_API_SECRET, | ||
}, | ||
} | ||
} | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { ExchangeCryptoAuthConfig, SupportedExchange } from 'dripjs-types'; | ||
|
||
import { BitmexSpy, IntelFactory, Spy } from '../../..'; | ||
|
||
export function findSpy(exchange: string, config: ExchangeCryptoAuthConfig): Spy { | ||
switch (exchange) { | ||
case SupportedExchange.Bitmex: { | ||
return IntelFactory.create(BitmexSpy, { ...config, testnet: false }); | ||
} | ||
case SupportedExchange.BitmexTestNet: { | ||
return IntelFactory.create(BitmexSpy, { ...config, testnet: true }); | ||
} | ||
default: { | ||
return IntelFactory.create(BitmexSpy, { ...config, testnet: false }); | ||
} | ||
} | ||
} |
File renamed without changes.
8 changes: 0 additions & 8 deletions
8
...ligence/service/src/intel/common/types.ts → .../intelligence/service/src/common/types.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 |
---|---|---|
@@ -1,18 +1,10 @@ | ||
import { Symbol } from 'dripjs-types'; | ||
|
||
import { Spy } from '../../../..'; | ||
|
||
export interface IntelErrorResponse { | ||
name: string; | ||
message: string; | ||
} | ||
|
||
export interface IntelGetSymbolsResponse { | ||
symbols: Symbol[]; | ||
error?: IntelErrorResponse; | ||
} | ||
|
||
export interface SpyImpl { | ||
spy?: Spy; | ||
error?: IntelErrorResponse; | ||
} |
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,2 @@ | ||
export * from './intel-exception-filter'; | ||
export * from './intel-exception'; |
25 changes: 25 additions & 0 deletions
25
projects/intelligence/service/src/exceptions/intel-exception-filter.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,25 @@ | ||
import { ArgumentsHost } from '@nestjs/common'; | ||
import { WsArgumentsHost } from '@nestjs/common/interfaces'; | ||
|
||
import { IntelServiceException } from './intel-exception'; | ||
import { IntelServiceExceptionFilter } from './intel-exception-filter'; | ||
|
||
describe('IntelServiceExceptionFilter', () => { | ||
let intelServiceExceptionFilter: IntelServiceExceptionFilter; | ||
const host = <ArgumentsHost>{}; | ||
|
||
beforeAll(() => { | ||
intelServiceExceptionFilter = new IntelServiceExceptionFilter(); | ||
host.switchToWs = jest.fn(() => { | ||
return <WsArgumentsHost>{ | ||
getClient: () => { | ||
return { emit: (event: string, res: any) => {} }; | ||
}, | ||
}; | ||
}); | ||
}); | ||
|
||
it('catch', () => { | ||
expect(intelServiceExceptionFilter.catch(new IntelServiceException('test'), host)).toBeUndefined(); | ||
}); | ||
}); |
16 changes: 16 additions & 0 deletions
16
projects/intelligence/service/src/exceptions/intel-exception-filter.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,16 @@ | ||
import { ArgumentsHost, Catch, ExceptionFilter } from '@nestjs/common'; | ||
|
||
import { IntelErrorResponse } from '../common'; | ||
import { IntelServiceException } from './intel-exception'; | ||
|
||
@Catch(IntelServiceException) | ||
export class IntelServiceExceptionFilter implements ExceptionFilter { | ||
catch(exception: IntelServiceException, host: ArgumentsHost): void { | ||
const client = host.switchToWs().getClient(); | ||
const errorRespsonse: IntelErrorResponse = { | ||
name: exception.name, | ||
message: exception.message, | ||
}; | ||
client.emit('exception', errorRespsonse); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
projects/intelligence/service/src/exceptions/intel-exception.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,6 @@ | ||
export class IntelServiceException extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
this.name = 'IntelServiceException'; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
projects/intelligence/service/src/guards/auth.guard.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,46 @@ | ||
import { ExecutionContext } from '@nestjs/common'; | ||
|
||
import { AuthGuard } from './auth.guard'; | ||
import { getSwitchToWs } from './test-helpers'; | ||
|
||
describe('AuthGuard', () => { | ||
let auth: AuthGuard; | ||
const context: ExecutionContext = <any>{}; | ||
|
||
beforeAll(() => { | ||
auth = new AuthGuard(); | ||
context.switchToWs = getSwitchToWs({}); | ||
}); | ||
|
||
it('canActivate return true', () => { | ||
expect(auth.canActivate(context)).toBeTruthy(); | ||
}); | ||
|
||
describe('Auth info not found', () => { | ||
const ctx: ExecutionContext = <any>{}; | ||
|
||
it('username is undefined', () => { | ||
ctx.switchToWs = getSwitchToWs({ noUsername: true }); | ||
expect(auth.canActivate(ctx)).toBeFalsy(); | ||
}); | ||
|
||
it('password is undefined', () => { | ||
ctx.switchToWs = getSwitchToWs({ noPassword: true }); | ||
expect(auth.canActivate(ctx)).toBeFalsy(); | ||
}); | ||
}); | ||
|
||
describe('Authentication failed', () => { | ||
const ctx: ExecutionContext = <any>{}; | ||
|
||
it('username is wrong', () => { | ||
ctx.switchToWs = getSwitchToWs({ username: 'z' }); | ||
expect(auth.canActivate(ctx)).toBeFalsy(); | ||
}); | ||
|
||
it('password is wrong', () => { | ||
ctx.switchToWs = getSwitchToWs({ password: 'z' }); | ||
expect(auth.canActivate(ctx)).toBeFalsy(); | ||
}); | ||
}); | ||
}); |
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,29 @@ | ||
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'; | ||
import { ConfigIntelServer } from 'dripjs-types'; | ||
|
||
// tslint:disable-next-line | ||
const config: ConfigIntelServer = require('config').container.intelService; | ||
|
||
@Injectable() | ||
export class AuthGuard implements CanActivate { | ||
canActivate(context: ExecutionContext): boolean { | ||
try { | ||
const client = context.switchToWs().getClient(); | ||
const username = client.handshake.headers['username']; | ||
const password = client.handshake.headers['password']; | ||
|
||
if (!username || !password) { | ||
throw new Error('Auth info not found.'); | ||
} | ||
if (config.username !== username || config.password !== password) { | ||
throw new Error('Authentication failed.'); | ||
} | ||
} catch (e) { | ||
// TODO: output log | ||
// console.error(e.message); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
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 @@ | ||
export * from './auth.guard'; |
Oops, something went wrong.