-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #256 from plivo/kowshik_tokencreation
Added Token creation
- Loading branch information
Showing
9 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 have token object', function () { | ||
assert('object', typeof client.token); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters