diff --git a/CHANGELOG.md b/CHANGELOG.md index c029850b..7294f26b 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 new file mode 100644 index 00000000..b29af44a --- /dev/null +++ b/lib/resources/token.js @@ -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); + }); + }); + } + } + + + diff --git a/lib/rest/client-test.js b/lib/rest/client-test.js index d8ce65a9..dcf282c4 100644 --- a/lib/rest/client-test.js +++ b/lib/rest/client-test.js @@ -4,6 +4,9 @@ import { import { CallInterface } from '../resources/call.js'; +import { + TokenInterface +} from '../resources/token.js'; import { version } from '../../package.json'; @@ -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); diff --git a/lib/rest/client.js b/lib/rest/client.js index 8948cea4..5e8c6ca3 100644 --- a/lib/rest/client.js +++ b/lib/rest/client.js @@ -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"; @@ -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); diff --git a/package.json b/package.json index 61a9428a..eae67d22 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": [ diff --git a/run.sh b/run.sh new file mode 100755 index 00000000..271ab1a9 --- /dev/null +++ b/run.sh @@ -0,0 +1,4 @@ +npm run prepublish +npm pack +mv plivo-4.33.0.tgz ../../../token_test/ + diff --git a/test/token.js b/test/token.js new file mode 100644 index 00000000..e5eafb97 --- /dev/null +++ b/test/token.js @@ -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); + }); + +}); diff --git a/types/resources/token.d.ts b/types/resources/token.d.ts new file mode 100644 index 00000000..2f83fd6f --- /dev/null +++ b/types/resources/token.d.ts @@ -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"; diff --git a/types/rest/client-test.d.ts b/types/rest/client-test.d.ts index cd82e78d..29572f6b 100644 --- a/types/rest/client-test.d.ts +++ b/types/rest/client-test.d.ts @@ -1,6 +1,7 @@ export class Client { constructor(authid: string, authToken: string, proxy: string); calls: CallInterface; + token: TokenInterface; accounts: AccountInterface; subAccounts: SubaccountInterface; applications: ApplicationInterface; @@ -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"; diff --git a/types/rest/client.d.ts b/types/rest/client.d.ts index 499c046c..261bd4fb 100644 --- a/types/rest/client.d.ts +++ b/types/rest/client.d.ts @@ -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; @@ -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";