Skip to content

Commit

Permalink
finalized risk,log,filter
Browse files Browse the repository at this point in the history
  • Loading branch information
bartes committed Jun 2, 2021
1 parent 9c66163 commit 055b773
Show file tree
Hide file tree
Showing 20 changed files with 140 additions and 130 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ Response format
| --------------- | --------------------------------------------------------------------------------------------------- |
| action | `string` - The recommended action for the given event. Options: `allow`, `challenge`, `deny`. |
| user_id | `string` - The `user_id` of the end user. |
| risk_policy | `object` - object containing risk policy information, such as `id`,`revision_id`, `name` and `type` |
| policy | `object` - object containing risk policy information, such as `id`,`revision_id`, `name` |
| signals | `object` - object containing hash with signals names |
| device_token | `string` - Our token for the device that generated the event. |
| failover | `boolean` - An optional property indicating the request failed and the response is a failover. |
| failover_reason | `string` - A message indicating why the request failed. |
Expand Down
10 changes: 5 additions & 5 deletions src/api/services/api-filter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ const handleFailover = (
throw err;
}

return FailoverResponsePrepareService.call(
userId,
reason,
configuration.failoverStrategy
);
return {
action: configuration.failoverStrategy,
failover: true,
failover_reason: reason,
};
};

const isTimeoutError = (e: Error) => e.name === 'AbortError';
Expand Down
15 changes: 6 additions & 9 deletions src/api/services/api-risk.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { Configuration } from '../../configuraton';
import { InternalServerError } from '../../errors';
import { CommandRiskService } from '../../command/command.module';
import {
FailoverResponsePrepareService,
FailoverStrategy,
} from '../../failover/failover.module';
import { FailoverStrategy } from '../../failover/failover.module';
import { APIService } from './api.service';
import AbortController from 'abort-controller';

Expand All @@ -21,11 +18,11 @@ const handleFailover = (
throw err;
}

return FailoverResponsePrepareService.call(
userId,
reason,
configuration.failoverStrategy
);
return {
action: configuration.failoverStrategy,
failover: true,
failover_reason: reason,
};
};

const isTimeoutError = (e: Error) => e.name === 'AbortError';
Expand Down
5 changes: 4 additions & 1 deletion src/models/authenticate-result.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { RiskPolicy } from './risk-policy';
import { Verdict } from './verdict';
import { Signals } from './signals';

export type AuthenticateResult = {
action: Verdict | string;
risk?: number;
user_id?: string;
user?: { email?: string; username?: string };
device_token?: string;
failover?: boolean;
failover_reason?: string;
risk_policy?: RiskPolicy;
policy?: RiskPolicy;
signals?: Signals;
internal?: string;
};
3 changes: 3 additions & 0 deletions src/models/device.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type Device = {
token: string;
};
4 changes: 3 additions & 1 deletion src/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export * from './authenticate-result';
export * from './risk-policy';
export * from './risk-policy-result';
export * from './risk-result';
export * from './signals';
export * from './device';
export * from './verdict';
8 changes: 0 additions & 8 deletions src/models/risk-policy-result.ts

This file was deleted.

6 changes: 5 additions & 1 deletion src/models/risk-policy.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export type RiskPolicy = 'bot' | 'authentication';
export type RiskPolicy = {
id: string;
revision_id: string;
name: string;
};
16 changes: 13 additions & 3 deletions src/models/risk-result.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { RiskPolicy } from './risk-policy';
import { Verdict } from './verdict';
import type { RiskPolicy } from './risk-policy';
import type { Verdict } from './verdict';
import type { Signals } from './signals';
import type { Device } from './device';

export type RiskResult = {};
export type RiskResult = {
action: Verdict | string;
risk?: number;
failover?: boolean;
failover_reason?: string;
policy?: RiskPolicy;
signals?: Signals;
device?: Device;
};
3 changes: 3 additions & 0 deletions src/models/signals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type Signals = {
[name: string]: any;
};
12 changes: 6 additions & 6 deletions src/payload/models/filter_payload.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { IncomingHttpHeaders } from 'http2';

export type FilterPayload = {
request_token?: string;
event?: string;
request_token: string;
event: string;
status?: string;
user?: {
id: string;
email: string;
id?: string;
email?: string;
};
properties?: object;
created_at?: string;
context?: {
context: {
ip: string;
headers: IncomingHttpHeaders;
};
Expand Down
15 changes: 9 additions & 6 deletions src/payload/models/log_payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ import { IncomingHttpHeaders } from 'http2';

export type LogPayload = {
request_token?: string;
event?: string;
event: string;
status?: string;
user?: {
id: string;
email: string;
id?: string;
email?: string;
registered_at?: string;
traits?: object;
name?: string;
};
properties?: object;
created_at?: string;
properties?: object;
context?: {
ip: string;
headers: IncomingHttpHeaders;
ip?: string;
headers?: IncomingHttpHeaders;
};
};
14 changes: 8 additions & 6 deletions src/payload/models/risk_payload.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { IncomingHttpHeaders } from 'http2';

export type RiskPayload = {
request_token?: string;
event?: string;
status?: string;
user?: {
request_token: string;
event: string;
status: string;
user: {
id: string;
email: string;
registered_at?: string;
traits?: object;
name?: string;
};
properties?: object;
created_at?: string;
context?: {
context: {
ip: string;
headers: IncomingHttpHeaders;
};
Expand Down
13 changes: 4 additions & 9 deletions test/Castle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,10 @@ describe('Castle', () => {
action: 'allow',
device_token: 'device_token',
user_id: 'user_id',
risk_policy: {
policy: {
id: 'q-rbeMzBTdW2Fd09sbz55A',
revision_id: 'pke4zqO2TnqVr-NHJOAHEg',
name: 'Block Users from X',
type: 'bot',
},
}
);
Expand All @@ -239,16 +238,12 @@ describe('Castle', () => {
expect(response).toHaveProperty('action', 'allow');
expect(response).toHaveProperty('device_token', 'device_token');
expect(response).toHaveProperty('user_id', 'user_id');
expect(response.risk_policy).toHaveProperty(
'id',
'q-rbeMzBTdW2Fd09sbz55A'
);
expect(response.risk_policy).toHaveProperty(
expect(response.policy).toHaveProperty('id', 'q-rbeMzBTdW2Fd09sbz55A');
expect(response.policy).toHaveProperty(
'revision_id',
'pke4zqO2TnqVr-NHJOAHEg'
);
expect(response.risk_policy).toHaveProperty('type', 'bot');
expect(response.risk_policy).toHaveProperty('name', 'Block Users from X');
expect(response.policy).toHaveProperty('name', 'Block Users from X');

const lastOptions: any = fetch.lastOptions();
const payload = JSON.parse(lastOptions.body.toString());
Expand Down
13 changes: 4 additions & 9 deletions test/api/services/api-authenticate.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,10 @@ describe('APIAuthenticateService', () => {
action: 'deny',
device_token: 'device_token',
user_id: 'user_id',
risk_policy: {
policy: {
id: 'q-rbeMzBTdW2Fd09sbz55A',
revision_id: 'pke4zqO2TnqVr-NHJOAHEg',
name: 'Block Users from X',
type: 'bot',
},
});

Expand All @@ -97,16 +96,12 @@ describe('APIAuthenticateService', () => {
expect(response).toHaveProperty('action', 'deny');
expect(response).toHaveProperty('device_token', 'device_token');
expect(response).toHaveProperty('user_id', 'user_id');
expect(response.risk_policy).toHaveProperty(
'id',
'q-rbeMzBTdW2Fd09sbz55A'
);
expect(response.risk_policy).toHaveProperty(
expect(response.policy).toHaveProperty('id', 'q-rbeMzBTdW2Fd09sbz55A');
expect(response.policy).toHaveProperty(
'revision_id',
'pke4zqO2TnqVr-NHJOAHEg'
);
expect(response.risk_policy).toHaveProperty('type', 'bot');
expect(response.risk_policy).toHaveProperty('name', 'Block Users from X');
expect(response.policy).toHaveProperty('name', 'Block Users from X');
});
});
});
57 changes: 27 additions & 30 deletions test/api/services/api-filter.service.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { APIFilterService } from '../../../src/api/api.module';
import { Configuration } from '../../../src/configuraton';
import type { FilterPayload } from '../../../src/payload/payload.module';
import MockDate from 'mockdate';
import fetchMock from 'fetch-mock';

Expand All @@ -12,13 +13,13 @@ describe('APIFilterService', () => {
MockDate.reset();
});

const sampleRequestData = {
event: '$login.succeeded',
created_at: 'now',
user_id: 'userid',
user_traits: {
const sampleRequestData: FilterPayload = {
event: '$login',
request_token: 'token',
status: '$succeeded',
user: {
id: 'userid',
email: 'myemail',
updated_at: 'today',
},
context: {
ip: '8.8.8.8',
Expand All @@ -30,8 +31,8 @@ describe('APIFilterService', () => {
it('handles allow response', async () => {
const fetch = fetchMock.sandbox().mock('*', {
action: 'allow',
device_token: 'device_token',
user_id: 'user_id',
risk: 0.85,
device: { token: 'device_token' },
});

const config = new Configuration({
Expand All @@ -40,18 +41,18 @@ describe('APIFilterService', () => {
logger: { info: () => {} },
});

const response = await APIFilterService.call(sampleRequestData, config);
const response = await (<any>(
APIFilterService.call(sampleRequestData, config)
));
expect(response).toHaveProperty('action', 'allow');
expect(response).toHaveProperty('device_token', 'device_token');
expect(response).toHaveProperty('user_id', 'user_id');
// expect(response).toEqual({'action': 'allow', 'risk': 0.41, 'policy': {'id': ..., 'revision_id': ..., 'name': ...}, 'signals': {'datacenter_ip': {}}
expect(response).toHaveProperty('device.token', 'device_token');
});

it('handles deny response without risk policy', async () => {
const fetch = fetchMock.sandbox().mock('*', {
action: 'deny',
device_token: 'device_token',
user_id: 'user_id',
risk: 0.85,
device: { token: 'device_token' },
});

const config = new Configuration({
Expand All @@ -62,20 +63,19 @@ describe('APIFilterService', () => {

const response = await APIFilterService.call(sampleRequestData, config);
expect(response).toHaveProperty('action', 'deny');
expect(response).toHaveProperty('device_token', 'device_token');
expect(response).toHaveProperty('user_id', 'user_id');
expect(response).toHaveProperty('device.token', 'device_token');
});

it('handles deny response with risk policy', async () => {
const fetch = fetchMock.sandbox().mock('*', {
action: 'deny',
device_token: 'device_token',
user_id: 'user_id',
risk_policy: {
device: {
token: 'device_token',
},
policy: {
id: 'q-rbeMzBTdW2Fd09sbz55A',
revision_id: 'pke4zqO2TnqVr-NHJOAHEg',
name: 'Block Users from X',
type: 'bot',
},
});

Expand All @@ -85,20 +85,17 @@ describe('APIFilterService', () => {
logger: { info: () => {} },
});

const response = await APIFilterService.call(sampleRequestData, config);
const response = await (<any>(
APIFilterService.call(sampleRequestData, config)
));
expect(response).toHaveProperty('action', 'deny');
expect(response).toHaveProperty('device_token', 'device_token');
expect(response).toHaveProperty('user_id', 'user_id');
expect(response.risk_policy).toHaveProperty(
'id',
'q-rbeMzBTdW2Fd09sbz55A'
);
expect(response.risk_policy).toHaveProperty(
expect(response).toHaveProperty('device.token', 'device_token');
expect(response.policy).toHaveProperty('id', 'q-rbeMzBTdW2Fd09sbz55A');
expect(response.policy).toHaveProperty(
'revision_id',
'pke4zqO2TnqVr-NHJOAHEg'
);
expect(response.risk_policy).toHaveProperty('type', 'bot');
expect(response.risk_policy).toHaveProperty('name', 'Block Users from X');
expect(response.policy).toHaveProperty('name', 'Block Users from X');
});
});
});
Loading

0 comments on commit 055b773

Please sign in to comment.