Skip to content

Commit 57b10e6

Browse files
committed
more jwt tests
1 parent 432ea50 commit 57b10e6

2 files changed

Lines changed: 36 additions & 5 deletions

File tree

‎src/JwtUtil.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default class JwtUtil {
3939
Log.info("JwtUtil.validateJwt");
4040

4141
try {
42-
if (key.kty === "RSA"){
42+
if (key.kty === "RSA") {
4343
if (key.e && key.n) {
4444
key = __global.KEYUTIL.getKey(key);
4545
}
@@ -51,7 +51,7 @@ export default class JwtUtil {
5151
return false;
5252
}
5353
}
54-
else if (key.kty === "EC"){
54+
else if (key.kty === "EC") {
5555
if (key.crv && key.x && key.y) {
5656
key = __global.KEYUTIL.getKey(key);
5757
}
@@ -71,13 +71,13 @@ export default class JwtUtil {
7171
alg: ['RS256', 'RS384', 'RS512', 'PS256', 'PS384', 'PS512', 'ES256', 'ES384', 'ES512'],
7272
iss: [issuer],
7373
aud: [audience],
74-
verifyAt : now
74+
verifyAt: now
7575
});
7676
}
7777
catch (e) {
7878
Log.error(e);
7979
}
80-
80+
8181
return false;
8282
}
8383

‎test/JwtUtil.spec.js‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ describe("JwtUtil", function() {
1818

1919
const expectedIssuer = "https://localhost:44333/core";
2020
const expectedAudience = "js.tokenmanager";
21-
const expectedNow = 1459129901;
21+
const expires = 1459130201;
22+
const notBefore = 1459129901;
23+
const expectedNow = notBefore;
2224

2325
beforeEach(function() {
2426
Log.setLogger(console);
@@ -137,6 +139,35 @@ describe("JwtUtil", function() {
137139

138140
});
139141

142+
it("should not validate after exp", function() {
143+
144+
var result = JwtUtil.validateJwt(jwtFromRsa, rsaKey, expectedIssuer, expectedAudience, expires + 1);
145+
result.should.be.false;
146+
147+
});
148+
149+
it("should not validate before nbf", function() {
150+
151+
var result = JwtUtil.validateJwt(jwtFromRsa, rsaKey, expectedIssuer, expectedAudience, notBefore - 1);
152+
result.should.be.false;
153+
154+
});
155+
156+
it("should not validate for invalid audience", function() {
157+
158+
var result = JwtUtil.validateJwt(jwtFromRsa, rsaKey, expectedIssuer, "invalid aud", expectedNow);
159+
result.should.be.false;
160+
161+
});
162+
163+
it("should not validate for invalid issuer", function() {
164+
165+
var result = JwtUtil.validateJwt(jwtFromRsa, rsaKey, "invalid issuer", expectedAudience, expectedNow);
166+
result.should.be.false;
167+
168+
});
169+
170+
140171
});
141172

142173
});

0 commit comments

Comments
 (0)