Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add regional twr header in the access token #623

Merged
merged 6 commits into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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