Skip to content

Commit

Permalink
feat(network): added sim swap and network client (#939)
Browse files Browse the repository at this point in the history
  • Loading branch information
manchuck authored Aug 29, 2024
1 parent bacbe72 commit 13438d6
Show file tree
Hide file tree
Showing 51 changed files with 1,903 additions and 24 deletions.
22 changes: 20 additions & 2 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ const config: Config.InitialOptions = {
testMatch: ['<rootDir>/packages/media/__tests__/**/*.test.ts'],
coveragePathIgnorePatterns: ['node_modules', '__tests__'],
},
{
...projectDefault,
displayName: 'MEETINGS',
testMatch: ['<rootDir>/packages/meetings/__tests__/**/*.test.ts'],
coveragePathIgnorePatterns: ['node_modules', '__tests__'],
},
{
...projectDefault,
displayName: 'MESSAGES',
Expand All @@ -68,8 +74,20 @@ const config: Config.InitialOptions = {
},
{
...projectDefault,
displayName: 'MEETINGS',
testMatch: ['<rootDir>/packages/meetings/__tests__/**/*.test.ts'],
displayName: 'NETWORK CLIENT',
testMatch: ['<rootDir>/packages/network-client/__tests__/**/*.test.ts'],
coveragePathIgnorePatterns: ['node_modules', '__tests__'],
},
{
...projectDefault,
displayName: 'NETWORK SIM SWAP',
testMatch: ['<rootDir>/packages/network-sim-swap/__tests__/**/*.test.ts'],
coveragePathIgnorePatterns: ['node_modules', '__tests__'],
},
{
...projectDefault,
displayName: 'NETWORK NUMBER VERIFICATION',
testMatch: ['<rootDir>/packages/network-number-verification/__tests__/**/*.test.ts'],
coveragePathIgnorePatterns: ['node_modules', '__tests__'],
},
{
Expand Down
68 changes: 68 additions & 0 deletions packages/network-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Vonage Server SDK for Node.js

![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/vonage/vonage-node-sdk/ci.yml?branch=3.x) [![Codecov](https://img.shields.io/codecov/c/github/vonage/vonage-node-sdk?label=Codecov&logo=codecov&style=flat-square)](https://codecov.io/gh/Vonage/vonage-server-sdk) ![Latest Release](https://img.shields.io/npm/v/@vonage/network-client?label=%40vonage%2Fnetwork-client&style=flat-square) [![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-v2.0%20adopted-ff69b4.svg?style=flat-square)](../../CODE_OF_CONDUCT.md) [![License](https://img.shields.io/npm/l/@vonage/accounts?label=License&style=flat-square)][license]

<img src="https://developer.nexmo.com/images/logos/vbc-logo.svg" height="48px" alt="Vonage" />

This is the Vonage Server Client SDK for Node.js used to wrap the authentication
headers/signatures for use with [Vonage APIs](https://www.vonage.com/). To use
it you will need a Vonage account. Sign up [for free at vonage.com][signup].

For full API documentation refer to [developer.vonage.com](https://developer.vonage.com/).

* [Installation](#installation)
* [Usage](#usage)
* [Options](#options)
* [Testing](#testing)

## Installation

### With NPM

```bash
npm install @vonage/network-client
```

### With Yarn

```bash
yarn add @vonage/network-client
```

## Usage

To create a client, you will need to pass in a `@vonage/auth` object.

```js
const { Auth } = require('@vonage/auth');
const { Client } = require('@vonage/network-client');

const vonageClient = new Client (new Auth({
apiKey: API_KEY,
apiSecret: API_SECRET,
applicationId: APP_ID,
privateKey: PRIVATE_KEY_PATH,
}),
options,
);
```

### Options

`options` is any option from [`@vonage/server-client`](https://github.com/Vonage/vonage-node-sdk/blob/3.x/packages/server-client/README.md#options)
along with the following that are specific for the network APIs

* `msisdn: string` - The MSISDN (phone number) you wish to authenticate to.
* `accessToken: string` (optional) - A pre generated Access token for making network API calls
* `expiresIn: string` (required if `accessToken` is passed) - Time until the access token expires

## Testing

Run:

```bash
npm run test
```

[signup]: https://dashboard.nexmo.com/sign-up?utm_source=DEV_REL&utm_medium=github&utm_campaign=node-server-sdk
[license]: ../../LICENSE.txt
288 changes: 288 additions & 0 deletions packages/network-client/__tests__/cibaClient.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,288 @@
import {
NetworkClient,
Purpose,
Scope,
} from '../lib';
import { SDKTestCase, testPrivateKey } from '../../../testHelpers';
import { AuthenticationType } from '@vonage/server-client';
import { VonageTest } from '../../../testHelpers';

class CIBATestClient extends NetworkClient {
protected authType?: AuthenticationType = AuthenticationType.CIBA;

protected _purpose = Purpose.FRAUD_PREVENTION_AND_DETECTION;
protected _scope = Scope.NUMBER_VERIFICATION_VERIFY_READ;

async makeTestCIBARequest() {
await this.sendGetRequest('https://api-eu.vonage.com/foo/bar');
}
}

const testCases = [
{
label: 'make CIBA request',
baseUrl: 'https://api-eu.vonage.com',
clientMethod: 'makeTestCIBARequest',
reqHeaders: {
authorization: (value) => value.startsWith('Bearer '),
},
client: new CIBATestClient(
{
privateKey: testPrivateKey,
applicationId: 'my-application',
msisdn: '447700900000',
},
),
requests: [
[
'/oauth2/bc-authorize',
'POST',
{
login_hint: 'tel:+447700900000',
scope: `openid dpv:${Purpose.FRAUD_PREVENTION_AND_DETECTION}#${Scope.NUMBER_VERIFICATION_VERIFY_READ}`,
},
],
[
'/oauth2/token',
'POST',
{
auth_req_id: 'auth-req-id',
grant_type: 'urn:openid:params:grant-type:ciba',
},
],
[
'/foo/bar',
'GET',
],
],
responses: [
[
200,
{
auth_req_id: 'auth-req-id',
}
],
[
200,
{
access_token: 'access',
expires: new Date().getTime() * 1500
}
],
[
204,
],
],
generator: false,
error: false,
expected: undefined,
},
{
label: 'error when getting request Id fails with 401',
baseUrl: 'https://api-eu.vonage.com',
clientMethod: 'makeTestCIBARequest',
reqHeaders: {
authorization: (value) => value.startsWith('Bearer '),
},
client: new CIBATestClient(
{
privateKey: testPrivateKey,
applicationId: 'my-application',
msisdn: '447700900000',
},
),
requests: [
[
'/oauth2/bc-authorize',
'POST',
{
login_hint: 'tel:+447700900000',
scope: `openid dpv:${Purpose.FRAUD_PREVENTION_AND_DETECTION}#${Scope.NUMBER_VERIFICATION_VERIFY_READ}`,
},
],
],
responses: [
[
401,
],
],
generator: false,
error: 'A 401 was returned when trying to get authorization. '
+ 'Some possible reasons for this:'
+ '- The application id or private key are invalid. '
+ '- You have not setup the Network API correctly for your application .',
expected: undefined,
},
{
label: 'error when getting request Id fails with 400',
baseUrl: 'https://api-eu.vonage.com',
clientMethod: 'makeTestCIBARequest',
reqHeaders: {
authorization: (value) => value.startsWith('Bearer '),
},
client: new CIBATestClient(
{
privateKey: testPrivateKey,
applicationId: 'my-application',
msisdn: '447700900000',
},
),
requests: [
[
'/oauth2/bc-authorize',
'POST',
{
login_hint: 'tel:+447700900000',
scope: `openid dpv:${Purpose.FRAUD_PREVENTION_AND_DETECTION}#${Scope.NUMBER_VERIFICATION_VERIFY_READ}`,
},
],
],
responses: [
[
400,
],
],
generator: false,
error: 'It appears you have not enabled the Network API for your account. '
+ 'Please contact your account manager to enable this feature. ',
expected: undefined,
},
{
label: 'error when getting request Id fails with 404',
baseUrl: 'https://api-eu.vonage.com',
clientMethod: 'makeTestCIBARequest',
reqHeaders: {
authorization: (value) => value.startsWith('Bearer '),
},
client: new CIBATestClient(
{
privateKey: testPrivateKey,
applicationId: 'my-application',
msisdn: '447700900000',
},
),
requests: [
[
'/oauth2/bc-authorize',
'POST',
{
login_hint: 'tel:+447700900000',
scope: `openid dpv:${Purpose.FRAUD_PREVENTION_AND_DETECTION}#${Scope.NUMBER_VERIFICATION_VERIFY_READ}`,
},
],
],
responses: [
[
404,
],
],
generator: false,
error: 'A 404 was returned when trying to get authorization. '
+ 'Some possible reasons for this:'
+ '- The network application is not setup correctly. '
+ '- The phone number is not associated with this network. ',
expected: undefined,
},
{
label: 'erro when getting token fails with 401',
baseUrl: 'https://api-eu.vonage.com',
clientMethod: 'makeTestCIBARequest',
reqHeaders: {
authorization: (value) => value.startsWith('Bearer '),
},
client: new CIBATestClient(
{
privateKey: testPrivateKey,
applicationId: 'my-application',
msisdn: '447700900000',
},
),
requests: [
[
'/oauth2/bc-authorize',
'POST',
{
login_hint: 'tel:+447700900000',
scope: `openid dpv:${Purpose.FRAUD_PREVENTION_AND_DETECTION}#${Scope.NUMBER_VERIFICATION_VERIFY_READ}`,
},
],
[
'/oauth2/token',
'POST',
{
auth_req_id: 'auth-req-id',
grant_type: 'urn:openid:params:grant-type:ciba',
},
],
],
responses: [
[
200,
{
auth_req_id: 'auth-req-id',
}
],
[
401,
],
],
generator: false,
error: 'Invalid credentials. Please check that the application id and private key are correct. '
+ 'This could also mean that you have not setup the Network API correctly for your application .',
expected: undefined,
},
{
label: 'erro when getting token fails with 400',
baseUrl: 'https://api-eu.vonage.com',
clientMethod: 'makeTestCIBARequest',
reqHeaders: {
authorization: (value) => value.startsWith('Bearer '),
},
client: new CIBATestClient(
{
privateKey: testPrivateKey,
applicationId: 'my-application',
msisdn: '447700900000',
},
),
requests: [
[
'/oauth2/bc-authorize',
'POST',
{
login_hint: 'tel:+447700900000',
scope: `openid dpv:${Purpose.FRAUD_PREVENTION_AND_DETECTION}#${Scope.NUMBER_VERIFICATION_VERIFY_READ}`,
},
],
[
'/oauth2/token',
'POST',
{
auth_req_id: 'auth-req-id',
grant_type: 'urn:openid:params:grant-type:ciba',
},
],
],
responses: [
[
200,
{
auth_req_id: 'auth-req-id',
}
],
[
400,
],
],
generator: false,
error: 'It appears you have not enabled the Network API for your account. '
+ 'Please contact your account manager to enable this feature. ',
expected: undefined,
}
] as SDKTestCase<CIBATestClient>[];

VonageTest([{
name: 'CIBA tests',
tests: testCases,
}]);

Loading

0 comments on commit 13438d6

Please sign in to comment.