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

Testtoken #260

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## [v4.34.0](https://github.com/plivo/plivo-go/tree/v4.34.0) (2022-08-07)
**Feature - Token Creation**
- `JWT Token Creation API` added API to create a new JWT token.

## [v4.33.0](https://github.com/plivo/plivo-go/tree/v4.33.0) (2022-07-11)
**Feature - STIR Attestation**
- Add stir attestation param as part of Get CDR and Get live call APIs Response
Expand Down
103 changes: 103 additions & 0 deletions lib/resources/token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import * as _ from "lodash";

import {
PlivoResourceInterface
} from '../base';
import {
extend,
validate
} from '../utils/common.js';


const clientKey = Symbol();
const action = 'JWT/Token/';


export class CreateTokenResponse {
constructor(params) {
params = params || {};
this.apiId = params.apiId;
this.token = params.token;
}
}


/**
* Represents a Token Interface
* @constructor
* @param {function} client - make api Token
* @param {object} [data] - data of Token
*/
export class TokenInterface extends PlivoResourceInterface {

constructor(client, data = {}) {
super(action, TokenInterface, client);
extend(this, data);

this[clientKey] = client;
}
/**
* Create a token
* @method
* @param {string} iss - Auth id of the user
* @param {object} optionalParams - Optional Params to send message
* @param {string} [optionalParams.sub] - subject of the token
* @param {string} [optionalParams.exp] - expiration time of the token
* @param {string} [optionalParams.nbf] - not before time of the token
* @param {boolean} [optionalParams.incoming_allow] - incoming allow of the token
* @param {boolean} [optionalParams.outgoing_allow] - outgoing allow of the token
* @param {string} [optionalParams.app] - app id of the token
* @param {json} [optionalParams.per] - permissions of the token
* @promise {object} return {@link PlivoGenericMessage} object if success
* @fail {Error} returns Error
*/
create(iss, optionalParams = {}) {

let errors = validate([{
field: 'iss',
value: iss,
validators: ['isRequired']
},
]);
if (errors) {
return errors;
}

let params = {};
params.per = {};
params.per.voice = {};
if(optionalParams.sub) {
params.sub = optionalParams.sub;
}
if(optionalParams.exp) {
params.exp = optionalParams.exp;
}
if(optionalParams.nbf) {
params.nbf = optionalParams.nbf;
}
if(optionalParams.incoming_allow) {
params.per.voice.incoming_allow = optionalParams.incoming_allow;
}
if(optionalParams.outgoing_allow) {
params.per.voice.outgoing_allow = optionalParams.outgoing_allow;
}
if(optionalParams.app) {
params.app = optionalParams.app;
}
params.iss = iss;

let client = this[clientKey];
return new Promise((resolve, reject) => {
client('POST', action, params)
.then(response => {
resolve(new CreateTokenResponse(response.body));
})
.catch(error => {
reject(error);
});
});
}
}



4 changes: 4 additions & 0 deletions lib/rest/client-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import {
import {
CallInterface
} from '../resources/call.js';
import {
TokenInterface
} from '../resources/token.js';
import {
version
} from '../../package.json';
Expand Down Expand Up @@ -96,6 +99,7 @@ export class Client {
let client = camelCaseRequestWrapper(Request(options));

this.calls = new CallInterface(client);
this.token = new TokenInterface(client);
this.accounts = new AccountInterface(client);
this.subAccounts = new SubaccountInterface(client);
this.applications = new ApplicationInterface(client);
Expand Down
2 changes: 2 additions & 0 deletions lib/rest/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { camelCaseRequestWrapper } from "./utils";
import { name, version } from "../../package.json";
import { Phlo, PhloInterface } from "../resources/phlo";
import { CallInterface } from "../resources/call";
import { TokenInterface } from "../resources/token.js";
import { SubaccountInterface, AccountInterface } from "../resources/accounts";
import { ApplicationInterface } from "../resources/applications";
import { ConferenceInterface } from "../resources/conferences";
Expand Down Expand Up @@ -85,6 +86,7 @@ export class Client {
let client = camelCaseRequestWrapper(Axios(options));

this.calls = new CallInterface(client);
this.token = new TokenInterface(client);
this.accounts = new AccountInterface(client);
this.subaccounts = this.subAccounts = new SubaccountInterface(client);
this.applications = new ApplicationInterface(client);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "plivo",
"version": "4.33.0",
"version": "4.34.0",
"description": "A Node.js SDK to make voice calls and send SMS using Plivo and to generate Plivo XML",
"homepage": "https://github.com/plivo/plivo-node",
"files": [
Expand Down
4 changes: 4 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
npm run prepublish
npm pack
mv plivo-4.33.0.tgz ../../../token_test/

14 changes: 14 additions & 0 deletions test/token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//unittesting for token creation
import assert from 'assert';
import sinon from 'sinon';
import {Client} from '../lib/rest/client-test';
import {PlivoGenericResponse} from '../lib/base.js';

let client = new Client('sampleid', 'sammpletoken', 'sampleproxy');

describe('Token', function () {
it('should create token via interface', function () {
assert('object', typeof client.token);
});

});
31 changes: 31 additions & 0 deletions types/resources/token.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export class CreateTokenResponse {
constructor(params: object);
apiId: string;
token: string;
}
/**
* Represents a Token Interface
* @constructor
* @param {function} client - make api call
* @param {object} [data] - data of token
*/
export class TokenInterface extends PlivoResourceInterface {
constructor(client: Function, data ? : {});
/**
* Get Token Detail
* @method
* @promise {object} returns Call Object
* @fail {Error} returns Error
*/
create( iss: string, sub: string, nbf: number, exp: number, incoming_allow: boolean, outgoing_allow: boolean, app: string, params ? : {}): Promise < CreateTokenResponse > ;
/**
* Create a token
* @method
* @promise {object} returns PlivoGenericResponse Object
* @fail {Error} returns Error
*/
}
declare const clientKey: unique symbol;
import {
PlivoResourceInterface
} from "../base";
2 changes: 2 additions & 0 deletions types/rest/client-test.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export class Client {
constructor(authid: string, authToken: string, proxy: string);
calls: CallInterface;
token: TokenInterface;
accounts: AccountInterface;
subAccounts: SubaccountInterface;
applications: ApplicationInterface;
Expand All @@ -26,6 +27,7 @@ export class PhloClient {
phlo: (phloid: string) => Phlo;
}
import { CallInterface } from "../resources/call.js";
import { TokenInterface } from "../resources/token.js";
import { AccountInterface } from "../resources/accounts.js";
import { SubaccountInterface } from "../resources/accounts.js";
import { ApplicationInterface } from "../resources/applications.js";
Expand Down
2 changes: 2 additions & 0 deletions types/rest/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export function validateSignature(uri: string, nonce: string, signature: string,
export class Client {
constructor(authId?: string, authToken?: string, options?: string);
calls: CallInterface;
token: TokenInterface;
accounts: AccountInterface;
subaccounts: SubaccountInterface;
subAccounts: SubaccountInterface;
Expand Down Expand Up @@ -42,6 +43,7 @@ export class PhloClient {
phlo: (phloId: any) => Phlo;
}
import { CallInterface } from "../resources/call.js";
import { TokenInterface } from "../resources/token.js";
import { AccountInterface } from "../resources/accounts.js";
import { SubaccountInterface } from "../resources/accounts.js";
import { ApplicationInterface } from "../resources/applications.js";
Expand Down