Skip to content

Commit f9ebc95

Browse files
committed
multiple audiences -- implements auth0#4
1 parent 708c4d8 commit f9ebc95

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ module.exports.verify = function(jwtString, secretOrPublicKey, options, callback
5353
}
5454

5555
if (options.audience) {
56-
if (payload.aud !== options.audience)
56+
var audiences = Array.isArray(options.audience)? options.audience : [options.audience];
57+
if (options.audience.indexOf(payload.aud) < 0)
5758
return callback(new Error('jwt audience invalid. expected: ' + payload.aud));
5859
}
5960

test/jwt.rs.tests.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ describe('RS256', function() {
7373
});
7474
});
7575

76+
it('should check audience in array', function(done) {
77+
jwt.verify(token, pub, { audience: ['urn:foo', 'urn:other'] }, function (err, decoded) {
78+
assert.isNotNull(decoded);
79+
assert.isNull(err);
80+
done();
81+
});
82+
});
83+
7684
it('should throw when invalid audience', function(done) {
7785
jwt.verify(token, pub, { audience: 'urn:wrong' }, function(err, decoded) {
7886
assert.isUndefined(decoded);
@@ -81,6 +89,14 @@ describe('RS256', function() {
8189
});
8290
});
8391

92+
it('should throw when invalid audience in array', function(done) {
93+
jwt.verify(token, pub, { audience: ['urn:wrong', 'urn:morewrong'] }, function(err, decoded) {
94+
assert.isUndefined(decoded);
95+
assert.isNotNull(err);
96+
done();
97+
});
98+
});
99+
84100
});
85101

86102
describe('when signing a token without audience', function() {
@@ -94,6 +110,14 @@ describe('RS256', function() {
94110
});
95111
});
96112

113+
it('should check audience in array', function(done) {
114+
jwt.verify(token, pub, { audience: ['urn:wrong', 'urn:morewrong'] }, function(err, decoded) {
115+
assert.isUndefined(decoded);
116+
assert.isNotNull(err);
117+
done();
118+
});
119+
});
120+
97121
});
98122

99123
describe('when signing a token with issuer', function() {

0 commit comments

Comments
 (0)