Skip to content

Commit

Permalink
feat: add regional twr header in the access token (#623)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliesantos authored Oct 30, 2020
1 parent 5a10bfd commit a128488
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
4 changes: 4 additions & 0 deletions lib/jwt/AccessToken.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ declare namespace AccessToken {
* Time from epoch in seconds for not before value
*/
nbf?: number;
/**
* The region value associated with this account
*/
region?: string;
}
}

Expand Down
16 changes: 12 additions & 4 deletions lib/jwt/AccessToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ _.extend(VoiceGrant.prototype, {
* @param {number} [options.ttl=3600] - Time to live in seconds
* @param {string} [options.identity] - The identity of the first person
* @param {number} [options.nbf] - Time from epoch in seconds for not before value
* @param {string} [options.region] - The region value associated with this account
*/
function AccessToken(accountSid, keySid, secret, options) {
if (!accountSid) { throw new Error('accountSid is required'); }
Expand All @@ -221,6 +222,7 @@ function AccessToken(accountSid, keySid, secret, options) {
this.ttl = options.ttl || 3600;
this.identity = options.identity;
this.nbf = options.nbf;
this.region = options.region;
this.grants = [];
}

Expand Down Expand Up @@ -264,11 +266,17 @@ _.extend(AccessToken.prototype, {
};
if (_.isNumber(this.nbf)) { payload.nbf = this.nbf; }

var header = {
cty: 'twilio-fpa;v=1',
typ: 'JWT'
};

if (this.region && _.isString(this.region)) {
header.twr = this.region;
}

return jwt.sign(payload, this.secret, {
header: {
cty: 'twilio-fpa;v=1',
typ: 'JWT'
},
header: header,
algorithm: algorithm,
issuer: this.keySid,
subject: this.accountSid,
Expand Down
23 changes: 22 additions & 1 deletion spec/unit/jwt/AccessToken.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ describe('AccessToken', function() {
});

describe('generate', function() {

describe('home region', function() {
var secret = 'aTBl1PhJnykIjWll4TOiXKtD1ugxiz6f';

it('should add twr header when region is provided', function() {
var token = new twilio.jwt.AccessToken(accountSid, keySid, secret, {region: 'foo'});
var decoded = jwt.decode(token.toJwt(), {complete: true});

expect(decoded.header.twr).toBe('foo');
});

['', undefined, null, {}, 1, 0].forEach(function(value) {
it('should not add twr header if region is ' + value, function() {
var token = new twilio.jwt.AccessToken(accountSid, keySid, secret, {region: value});
var decoded = jwt.decode(token.toJwt(), {complete: true});

expect(decoded.header.twr).toBe(undefined);
});
});
});

it('should generate the correct headers', function() {
var token = new twilio.jwt.AccessToken(accountSid, keySid, 'aTBl1PhJnykIjWll4TOiXKtD1ugxiz6f');
var decoded = jwt.decode(token.toJwt(), {complete: true});
Expand All @@ -46,7 +67,7 @@ describe('AccessToken', function() {
});
});

it('should accept different algorithsm', function() {
it('should accept different algorithms', function() {
var validateAlg = function(alg) {
var token = new twilio.jwt.AccessToken(accountSid, keySid, 'secret');
var decoded = jwt.decode(token.toJwt(alg), {
Expand Down

0 comments on commit a128488

Please sign in to comment.