From c946ab3185bd35bdcfa29951d4a024b6a0714280 Mon Sep 17 00:00:00 2001 From: kowshiksiva5 Date: Sun, 7 Aug 2022 21:13:44 +0530 Subject: [PATCH] Added corrections in Token Creation --- CHANGELOG.md | 4 +++ lib/resources/token.js | 68 +++++++++++++++++++++++------------------- package.json | 2 +- 3 files changed, 43 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c029850..7294f26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/resources/token.js b/lib/resources/token.js index e2a0451..2bef20a 100644 --- a/lib/resources/token.js +++ b/lib/resources/token.js @@ -25,13 +25,13 @@ export class CreateTokenResponse { /** * Represents a Token Interface * @constructor - * @param {function} client - make api call + * @param {function} client - make api Token * @param {object} [data] - data of Token */ export class TokenInterface extends PlivoResourceInterface { constructor(client, data = {}) { - super(action, TokenInterface, idField, client); + super(action, TokenInterface, client); extend(this, data); this[clientKey] = client; @@ -40,49 +40,57 @@ export class CreateTokenResponse { * Create a token * @method * @param {string} iss - Auth id of the user - * @param {string} sub - Subject - * @param {number} nbf - Start time of the token - * @param {number} exp - Expiration time of the token - * @param {boolean} incoming_allowed - Incoming allowed - * @param {boolean} outgoing_allowed - Outgoing allowed - * @param {string} app - App id - * @promise {object} returns PlivoGenericResponse Object + * @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,sub,nbf,exp,incoming_allowed,outgoing_allowed,app, params = {}) { - let errors = validate([{ + create(iss, optionalParams = {}) { + + let errors = validate([{ field: 'iss', value: iss, validators: ['isRequired'] - } + }, ]); - if(incoming_allowed == true && sub == null){ - errors.push({ - field: 'sub', - value: sub, - validators: ['isRequired'] - }); - } - - if (errors) { - return errors; + return errors; } + let params = optionalParams || {}; + 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; - params.sub = sub; - params.nbf = nbf; - params.exp = exp; - params.incoming_allowed = incoming_allowed; - params.outgoing_allowed = outgoing_allowed; - params.app = app; - params.isVoiceRequest = 'true'; let client = this[clientKey]; return new Promise((resolve, reject) => { client('POST', action, params) .then(response => { - resolve(new CreateTokenResponse(response.body, idField)); + resolve(new CreateTokenResponse(response.body)); }) .catch(error => { reject(error); diff --git a/package.json b/package.json index 61a9428..eae67d2 100644 --- a/package.json +++ b/package.json @@ -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": [