mirror of
https://github.com/donl/plivo-node.git
synced 2026-06-30 06:12:08 -06:00
add jwt helpers
This commit is contained in:
parent
324a8139b4
commit
48aeba2fef
4 changed files with 87 additions and 0 deletions
|
|
@ -13,6 +13,7 @@ import { NumberInterface } from "../resources/numbers";
|
|||
import { PricingInterface } from "../resources/pricings";
|
||||
import { RecordingInterface } from "../resources/recordings";
|
||||
import { Response } from "../utils/plivoxml";
|
||||
import { AccessToken } from "../utils/jwt";
|
||||
import { validateSignature } from "../utils/security";
|
||||
import { stringify } from "./../utils/jsonStrinfigier";
|
||||
import { CallFeedbackInterface } from "../resources/callFeedback";
|
||||
|
|
@ -22,6 +23,10 @@ exports.Response = function() {
|
|||
return new Response();
|
||||
};
|
||||
|
||||
exports.AccessToken = function(authId, authToken, username, validityOptions, uid) {
|
||||
return new AccessToken(authId, authToken, username, validityOptions, uid);
|
||||
};
|
||||
|
||||
exports.validateSignature = function(uri, nonce, signature, auth_token) {
|
||||
return validateSignature(uri, nonce, signature, auth_token);
|
||||
};
|
||||
|
|
|
|||
69
lib/utils/jwt.js
Normal file
69
lib/utils/jwt.js
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
var jwt = require('jsonwebtoken');
|
||||
|
||||
export function AccessToken(authId, authToken, username, validityOptions = {}, uid = null) {
|
||||
this.authId = authId || process.env.PLIVO_AUTH_ID;
|
||||
this.key = authToken || process.env.PLIVO_AUTH_TOKEN;
|
||||
this.username = username;
|
||||
|
||||
if (this.authId == null) {
|
||||
throw new Error("Please provide authId1");
|
||||
}
|
||||
if (this.key == null) {
|
||||
throw new Error("Please provide authToken");
|
||||
}
|
||||
|
||||
if (this.username == null) {
|
||||
throw new Error("Please provide username");
|
||||
}
|
||||
|
||||
|
||||
if (validityOptions.validFrom != null) {
|
||||
if (validityOptions.lifetime != null && validityOptions.validTill != null) {
|
||||
throw new Error("Please define at maximum any two of validFrom, lifetime and validTill");
|
||||
}
|
||||
this.validFrom = validityOptions.validFrom;
|
||||
if (validityOptions.validTill != null) {
|
||||
this.lifetime = validityOptions.validTill - this.validFrom;
|
||||
} else {
|
||||
this.lifetime = 84600;
|
||||
}
|
||||
} else {
|
||||
this.lifetime = validityOptions.lifetime || 84600;
|
||||
if (validityOptions.validTill != null) {
|
||||
this.validFrom = validityOptions.validTill - this.lifetime;
|
||||
} else {
|
||||
this.validFrom = (new Date()).getTime() / 1000;
|
||||
}
|
||||
}
|
||||
this.uid = uid || this.username + "-" + (new Date()).getTime();
|
||||
}
|
||||
|
||||
AccessToken.prototype = {
|
||||
addVoiceGrants: function(incoming = false, outgoing = false) {
|
||||
this.grants = {
|
||||
voice: {
|
||||
incoming_allow: incoming,
|
||||
outgoing_allow: outgoing
|
||||
}
|
||||
};
|
||||
|
||||
},
|
||||
toJwt: function() {
|
||||
let payload = {
|
||||
jti: this.uid,
|
||||
iss: this.authId,
|
||||
sub: this.username,
|
||||
nbf: Math.floor(this.validFrom),
|
||||
exp: Math.floor(this.validFrom + this.lifetime),
|
||||
grants: this.grants
|
||||
};
|
||||
let options = {
|
||||
header: {
|
||||
typ: "JWT",
|
||||
cty: "plivo;v=1"
|
||||
},
|
||||
noTimestamp: true,
|
||||
};
|
||||
return jwt.sign(payload, this.key, options);
|
||||
}
|
||||
}
|
||||
|
|
@ -59,6 +59,7 @@
|
|||
"dependencies": {
|
||||
"base-64": "^0.1.0",
|
||||
"build-url": "^1.0.10",
|
||||
"jsonwebtoken": "^8.5.1",
|
||||
"lodash": "^4.17.4",
|
||||
"querystring": "^0.2.0",
|
||||
"request": "^2.81.0",
|
||||
|
|
|
|||
12
test/jwt.js
Normal file
12
test/jwt.js
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import assert from 'assert';
|
||||
import sinon from 'sinon';
|
||||
|
||||
import {AccessToken} from '../lib/utils/jwt';
|
||||
|
||||
describe('Jwt', function () {
|
||||
it('should be created', function () {
|
||||
let acctkn = new AccessToken('MADADADADADADADADADA', 'qwerty', 'username', {validFrom: 12121212, lifetime: 300}, 'username-12345');
|
||||
acctkn.addVoiceGrants(true, true);
|
||||
assert.equal(acctkn.toJwt(), 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImN0eSI6InBsaXZvO3Y9MSJ9.eyJqdGkiOiJ1c2VybmFtZS0xMjM0NSIsImlzcyI6Ik1BREFEQURBREFEQURBREFEQURBIiwic3ViIjoidXNlcm5hbWUiLCJuYmYiOjEyMTIxMjEyLCJleHAiOjEyMjA1ODEyLCJncmFudHMiOnsidm9pY2UiOnsiaW5jb21pbmdfYWxsb3ciOnRydWUsIm91dGdvaW5nX2FsbG93Ijp0cnVlfX19.T0li-AM7WAhioMwRdwYuIA-N7BRkhf8o9g366py7w1s');
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue