-
Notifications
You must be signed in to change notification settings - Fork 520
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!: Making AccessToken Identity required #875
Merged
+44
−45
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -7,10 +7,21 @@ describe("AccessToken", function () { | |
var accountSid = "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; | ||
var keySid = "SKb5aed9ca12bf5890f37930e63cad6d38"; | ||
|
||
function getToken() { | ||
return new twilio.jwt.AccessToken(accountSid, keySid, "secret", { | ||
identity: "[email protected]", | ||
}); | ||
} | ||
|
||
describe("constructor", function () { | ||
var initWithoutIndex = function (index) { | ||
return function () { | ||
var constructorArgs = [accountSid, keySid, "secret"]; | ||
var constructorArgs = [ | ||
accountSid, | ||
keySid, | ||
"secret", | ||
{ identity: "foo" }, | ||
]; | ||
constructorArgs[index] = undefined; | ||
|
||
// add context | ||
|
@@ -30,6 +41,11 @@ describe("AccessToken", function () { | |
it("should require secret", function () { | ||
expect(initWithoutIndex(2)).toThrow(new Error("secret is required")); | ||
}); | ||
it("should require identity", function () { | ||
expect(initWithoutIndex(3)).toThrow( | ||
new Error("identity is required to be specified in options") | ||
); | ||
}); | ||
it("should convert identity from integer to string", function () { | ||
var token = new twilio.jwt.AccessToken(accountSid, keySid, "secret", { | ||
identity: 4444, | ||
|
@@ -41,22 +57,18 @@ 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 token = getToken(); | ||
token.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 token = getToken(); | ||
token.region = value; | ||
var decoded = jwt.decode(token.toJwt(), { complete: true }); | ||
|
||
expect(decoded.header.twr).toBe(undefined); | ||
|
@@ -68,7 +80,8 @@ describe("AccessToken", function () { | |
var token = new twilio.jwt.AccessToken( | ||
accountSid, | ||
keySid, | ||
"aTBl1PhJnykIjWll4TOiXKtD1ugxiz6f" | ||
"aTBl1PhJnykIjWll4TOiXKtD1ugxiz6f", | ||
{ identity: "foo" } | ||
); | ||
var decoded = jwt.decode(token.toJwt(), { complete: true }); | ||
|
||
|
@@ -81,7 +94,7 @@ describe("AccessToken", function () { | |
|
||
it("should accept different algorithms", function () { | ||
var validateAlg = function (alg) { | ||
var token = new twilio.jwt.AccessToken(accountSid, keySid, "secret"); | ||
var token = getToken(); | ||
var decoded = jwt.decode(token.toJwt(alg), { | ||
complete: true, | ||
algorithms: twilio.jwt.AccessToken.ALGORITHMS, | ||
|
@@ -97,7 +110,8 @@ describe("AccessToken", function () { | |
it("should throw on invalid algorithm", function () { | ||
var generateWithAlg = function (alg) { | ||
return function () { | ||
new twilio.jwt.AccessToken(accountSid, keySid, "secret").toJwt(alg); | ||
var token = getToken(); | ||
token.toJwt(alg); | ||
}; | ||
}; | ||
|
||
|
@@ -109,9 +123,7 @@ describe("AccessToken", function () { | |
}); | ||
|
||
it("should create a token without any grants", function () { | ||
var token = new twilio.jwt.AccessToken(accountSid, keySid, "secret"); | ||
token.identity = "[email protected]"; | ||
|
||
var token = getToken(); | ||
var decoded = jwt.verify(token.toJwt(), "secret"); | ||
expect(decoded.jti.indexOf(keySid)).toBe(0); | ||
expect(decoded.iss).toBe(keySid); | ||
|
@@ -123,11 +135,9 @@ describe("AccessToken", function () { | |
}); | ||
|
||
it("should accept nbf", function () { | ||
var token = getToken(); | ||
var nbf = Math.floor(Date.now() / 1000); | ||
var token = new twilio.jwt.AccessToken(accountSid, keySid, "secret", { | ||
nbf: nbf, | ||
}); | ||
token.identity = "[email protected]"; | ||
token.nbf = nbf; | ||
|
||
var decoded = jwt.verify(token.toJwt(), "secret"); | ||
expect(decoded.jti.indexOf(keySid)).toBe(0); | ||
|
@@ -144,18 +154,15 @@ describe("AccessToken", function () { | |
}); | ||
|
||
it("should accept user defined ttl", function () { | ||
var token = new twilio.jwt.AccessToken(accountSid, keySid, "secret"); | ||
var token = getToken(); | ||
token.ttl = 100; | ||
token.identity = "[email protected]"; | ||
|
||
var decoded = jwt.verify(token.toJwt(), "secret"); | ||
expect(decoded.exp - decoded.iat).toBe(100); | ||
}); | ||
|
||
it("should create token with chat grant", function () { | ||
var token = new twilio.jwt.AccessToken(accountSid, keySid, "secret"); | ||
token.identity = "[email protected]"; | ||
|
||
var token = getToken(); | ||
var grant = new twilio.jwt.AccessToken.ChatGrant(); | ||
grant.serviceSid = "SRaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; | ||
grant.endpointId = "endpointId"; | ||
|
@@ -176,9 +183,7 @@ describe("AccessToken", function () { | |
}); | ||
|
||
it("should create token with video grant", function () { | ||
var token = new twilio.jwt.AccessToken(accountSid, keySid, "secret"); | ||
token.identity = "[email protected]"; | ||
|
||
var token = getToken(); | ||
var grant = new twilio.jwt.AccessToken.VideoGrant(); | ||
grant.room = "room"; | ||
token.addGrant(grant); | ||
|
@@ -193,9 +198,7 @@ describe("AccessToken", function () { | |
}); | ||
|
||
it("should create token with sync grant", function () { | ||
var token = new twilio.jwt.AccessToken(accountSid, keySid, "secret"); | ||
token.identity = "[email protected]"; | ||
|
||
var token = getToken(); | ||
var grant = new twilio.jwt.AccessToken.SyncGrant(); | ||
grant.serviceSid = "ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; | ||
grant.endpointId = "endpointId"; | ||
|
@@ -212,9 +215,7 @@ describe("AccessToken", function () { | |
}); | ||
|
||
it("should create token with taskrouter grant", function () { | ||
var token = new twilio.jwt.AccessToken(accountSid, keySid, "secret"); | ||
token.identity = "[email protected]"; | ||
|
||
var token = getToken(); | ||
var grant = new twilio.jwt.AccessToken.TaskRouterGrant(); | ||
grant.workspaceSid = "WSxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; | ||
grant.workerSid = "WKxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; | ||
|
@@ -233,9 +234,7 @@ describe("AccessToken", function () { | |
}); | ||
|
||
it("should create token with playback grant", function () { | ||
var token = new twilio.jwt.AccessToken(accountSid, keySid, "secret"); | ||
token.identity = "[email protected]"; | ||
|
||
var token = getToken(); | ||
var playbackGrant = { | ||
requestCredentials: null, | ||
playbackUrl: | ||
|
@@ -255,9 +254,7 @@ describe("AccessToken", function () { | |
}); | ||
|
||
it("should create token with multiple grants", function () { | ||
var token = new twilio.jwt.AccessToken(accountSid, keySid, "secret"); | ||
token.identity = "[email protected]"; | ||
|
||
var token = getToken(); | ||
var grant = new twilio.jwt.AccessToken.ChatGrant(); | ||
grant.serviceSid = "SRaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; | ||
grant.endpointId = "endpointId"; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this first !-check needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually this breaks if options is undefined. https://github.com/twilio/twilio-node/actions/runs/3857410255/jobs/6574774047
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see. Or simply
!options?.identity