Skip to content

Commit fc82d45

Browse files
committed
Making AccessToken Identity required
1 parent d0f5f2e commit fc82d45

File tree

2 files changed

+44
-45
lines changed

2 files changed

+44
-45
lines changed

lib/jwt/AccessToken.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ export interface AccessTokenOptions {
7979
*/
8080
ttl?: number;
8181
/**
82-
* The identity of the first person
82+
* The identity of the first person. Required.
8383
*/
84-
identity?: string;
84+
identity: string;
8585
/**
8686
* Time from epoch in seconds for not before value
8787
*/
@@ -329,7 +329,7 @@ export default class AccessToken implements AccessTokenOptions {
329329
keySid: string;
330330
secret: string;
331331
ttl: number;
332-
identity?: string;
332+
identity: string;
333333
nbf?: number;
334334
region?: string;
335335
grants: Grant<any, any, any>[];
@@ -340,15 +340,15 @@ export default class AccessToken implements AccessTokenOptions {
340340
* @param secret - The secret to sign the token with
341341
* @param options - ...
342342
* @param options.ttl - Time to live in seconds (default 3600)
343-
* @param options.identity - The identity of the first person
343+
* @param options.identity - The identity of the first person. Required.
344344
* @param options.nbf - Time from epoch in seconds for not before value
345345
* @param options.region - The region value associated with this account
346346
*/
347347
constructor(
348348
accountSid: string,
349349
keySid: string,
350350
secret: string,
351-
options?: AccessTokenOptions
351+
options: AccessTokenOptions
352352
) {
353353
if (!accountSid) {
354354
throw new Error("accountSid is required");
@@ -359,7 +359,9 @@ export default class AccessToken implements AccessTokenOptions {
359359
if (!secret) {
360360
throw new Error("secret is required");
361361
}
362-
options = options || {};
362+
if (!options || !options.identity) {
363+
throw new Error("identity is required to be specified in options");
364+
}
363365

364366
this.accountSid = accountSid;
365367
this.keySid = keySid;

spec/unit/jwt/AccessToken.spec.js

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,21 @@ describe("AccessToken", function () {
77
var accountSid = "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
88
var keySid = "SKb5aed9ca12bf5890f37930e63cad6d38";
99

10+
function getToken() {
11+
return new twilio.jwt.AccessToken(accountSid, keySid, "secret", {
12+
identity: "[email protected]",
13+
});
14+
}
15+
1016
describe("constructor", function () {
1117
var initWithoutIndex = function (index) {
1218
return function () {
13-
var constructorArgs = [accountSid, keySid, "secret"];
19+
var constructorArgs = [
20+
accountSid,
21+
keySid,
22+
"secret",
23+
{ identity: "foo" },
24+
];
1425
constructorArgs[index] = undefined;
1526

1627
// add context
@@ -30,6 +41,11 @@ describe("AccessToken", function () {
3041
it("should require secret", function () {
3142
expect(initWithoutIndex(2)).toThrow(new Error("secret is required"));
3243
});
44+
it("should require identity", function () {
45+
expect(initWithoutIndex(3)).toThrow(
46+
new Error("identity is required to be specified in options")
47+
);
48+
});
3349
it("should convert identity from integer to string", function () {
3450
var token = new twilio.jwt.AccessToken(accountSid, keySid, "secret", {
3551
identity: 4444,
@@ -41,22 +57,18 @@ describe("AccessToken", function () {
4157

4258
describe("generate", function () {
4359
describe("home region", function () {
44-
var secret = "aTBl1PhJnykIjWll4TOiXKtD1ugxiz6f";
45-
4660
it("should add twr header when region is provided", function () {
47-
var token = new twilio.jwt.AccessToken(accountSid, keySid, secret, {
48-
region: "foo",
49-
});
61+
var token = getToken();
62+
token.region = "foo";
5063
var decoded = jwt.decode(token.toJwt(), { complete: true });
5164

5265
expect(decoded.header.twr).toBe("foo");
5366
});
5467

5568
["", undefined, null, {}, 1, 0].forEach(function (value) {
5669
it("should not add twr header if region is " + value, function () {
57-
var token = new twilio.jwt.AccessToken(accountSid, keySid, secret, {
58-
region: value,
59-
});
70+
var token = getToken();
71+
token.region = value;
6072
var decoded = jwt.decode(token.toJwt(), { complete: true });
6173

6274
expect(decoded.header.twr).toBe(undefined);
@@ -68,7 +80,8 @@ describe("AccessToken", function () {
6880
var token = new twilio.jwt.AccessToken(
6981
accountSid,
7082
keySid,
71-
"aTBl1PhJnykIjWll4TOiXKtD1ugxiz6f"
83+
"aTBl1PhJnykIjWll4TOiXKtD1ugxiz6f",
84+
{ identity: "foo" }
7285
);
7386
var decoded = jwt.decode(token.toJwt(), { complete: true });
7487

@@ -81,7 +94,7 @@ describe("AccessToken", function () {
8194

8295
it("should accept different algorithms", function () {
8396
var validateAlg = function (alg) {
84-
var token = new twilio.jwt.AccessToken(accountSid, keySid, "secret");
97+
var token = getToken();
8598
var decoded = jwt.decode(token.toJwt(alg), {
8699
complete: true,
87100
algorithms: twilio.jwt.AccessToken.ALGORITHMS,
@@ -97,7 +110,8 @@ describe("AccessToken", function () {
97110
it("should throw on invalid algorithm", function () {
98111
var generateWithAlg = function (alg) {
99112
return function () {
100-
new twilio.jwt.AccessToken(accountSid, keySid, "secret").toJwt(alg);
113+
var token = getToken();
114+
token.toJwt(alg);
101115
};
102116
};
103117

@@ -109,9 +123,7 @@ describe("AccessToken", function () {
109123
});
110124

111125
it("should create a token without any grants", function () {
112-
var token = new twilio.jwt.AccessToken(accountSid, keySid, "secret");
113-
token.identity = "[email protected]";
114-
126+
var token = getToken();
115127
var decoded = jwt.verify(token.toJwt(), "secret");
116128
expect(decoded.jti.indexOf(keySid)).toBe(0);
117129
expect(decoded.iss).toBe(keySid);
@@ -123,11 +135,9 @@ describe("AccessToken", function () {
123135
});
124136

125137
it("should accept nbf", function () {
138+
var token = getToken();
126139
var nbf = Math.floor(Date.now() / 1000);
127-
var token = new twilio.jwt.AccessToken(accountSid, keySid, "secret", {
128-
nbf: nbf,
129-
});
130-
token.identity = "[email protected]";
140+
token.nbf = nbf;
131141

132142
var decoded = jwt.verify(token.toJwt(), "secret");
133143
expect(decoded.jti.indexOf(keySid)).toBe(0);
@@ -144,18 +154,15 @@ describe("AccessToken", function () {
144154
});
145155

146156
it("should accept user defined ttl", function () {
147-
var token = new twilio.jwt.AccessToken(accountSid, keySid, "secret");
157+
var token = getToken();
148158
token.ttl = 100;
149-
token.identity = "[email protected]";
150159

151160
var decoded = jwt.verify(token.toJwt(), "secret");
152161
expect(decoded.exp - decoded.iat).toBe(100);
153162
});
154163

155164
it("should create token with chat grant", function () {
156-
var token = new twilio.jwt.AccessToken(accountSid, keySid, "secret");
157-
token.identity = "[email protected]";
158-
165+
var token = getToken();
159166
var grant = new twilio.jwt.AccessToken.ChatGrant();
160167
grant.serviceSid = "SRaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
161168
grant.endpointId = "endpointId";
@@ -176,9 +183,7 @@ describe("AccessToken", function () {
176183
});
177184

178185
it("should create token with video grant", function () {
179-
var token = new twilio.jwt.AccessToken(accountSid, keySid, "secret");
180-
token.identity = "[email protected]";
181-
186+
var token = getToken();
182187
var grant = new twilio.jwt.AccessToken.VideoGrant();
183188
grant.room = "room";
184189
token.addGrant(grant);
@@ -193,9 +198,7 @@ describe("AccessToken", function () {
193198
});
194199

195200
it("should create token with sync grant", function () {
196-
var token = new twilio.jwt.AccessToken(accountSid, keySid, "secret");
197-
token.identity = "[email protected]";
198-
201+
var token = getToken();
199202
var grant = new twilio.jwt.AccessToken.SyncGrant();
200203
grant.serviceSid = "ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
201204
grant.endpointId = "endpointId";
@@ -212,9 +215,7 @@ describe("AccessToken", function () {
212215
});
213216

214217
it("should create token with taskrouter grant", function () {
215-
var token = new twilio.jwt.AccessToken(accountSid, keySid, "secret");
216-
token.identity = "[email protected]";
217-
218+
var token = getToken();
218219
var grant = new twilio.jwt.AccessToken.TaskRouterGrant();
219220
grant.workspaceSid = "WSxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
220221
grant.workerSid = "WKxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
@@ -233,9 +234,7 @@ describe("AccessToken", function () {
233234
});
234235

235236
it("should create token with playback grant", function () {
236-
var token = new twilio.jwt.AccessToken(accountSid, keySid, "secret");
237-
token.identity = "[email protected]";
238-
237+
var token = getToken();
239238
var playbackGrant = {
240239
requestCredentials: null,
241240
playbackUrl:
@@ -255,9 +254,7 @@ describe("AccessToken", function () {
255254
});
256255

257256
it("should create token with multiple grants", function () {
258-
var token = new twilio.jwt.AccessToken(accountSid, keySid, "secret");
259-
token.identity = "[email protected]";
260-
257+
var token = getToken();
261258
var grant = new twilio.jwt.AccessToken.ChatGrant();
262259
grant.serviceSid = "SRaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
263260
grant.endpointId = "endpointId";

0 commit comments

Comments
 (0)