mirror of https://github.com/jwtk/jjwt.git
resolved Issue #7
This commit is contained in:
parent
35a42826ea
commit
5540d5ded0
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* Copyright (C) 2014 jsonwebtoken.io
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.jsonwebtoken;
|
||||
|
||||
/**
|
||||
* Exception indicating that a JWT was accepted before it is allowed to be accessed and must be rejected.
|
||||
*
|
||||
* @since 0.3
|
||||
*/
|
||||
public class PrematureJwtException extends JwtException {
|
||||
|
||||
public PrematureJwtException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
public PrematureJwtException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
|
@ -26,6 +26,7 @@ import io.jsonwebtoken.JwtHandler;
|
|||
import io.jsonwebtoken.JwtHandlerAdapter;
|
||||
import io.jsonwebtoken.JwtParser;
|
||||
import io.jsonwebtoken.MalformedJwtException;
|
||||
import io.jsonwebtoken.PrematureJwtException;
|
||||
import io.jsonwebtoken.SignatureAlgorithm;
|
||||
import io.jsonwebtoken.SignatureException;
|
||||
import io.jsonwebtoken.UnsupportedJwtException;
|
||||
|
@ -190,7 +191,6 @@ public class DefaultJwtParser implements JwtParser {
|
|||
}
|
||||
}
|
||||
|
||||
/*
|
||||
//https://tools.ietf.org/html/draft-ietf-oauth-json-web-token-30#section-4.1.5
|
||||
//token MUST NOT be accepted before any specified nbf time:
|
||||
Date nbf = claims.getNotBefore();
|
||||
|
@ -209,7 +209,6 @@ public class DefaultJwtParser implements JwtParser {
|
|||
throw new PrematureJwtException(msg);
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
// =============== Signature =================
|
||||
|
|
|
@ -174,7 +174,6 @@ class JwtParserTest {
|
|||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@Test
|
||||
void testParseWithPrematureJwt() {
|
||||
|
||||
|
@ -188,7 +187,6 @@ class JwtParserTest {
|
|||
assertTrue e.getMessage().startsWith('JWT must not be accepted before ')
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
// ========================================================================
|
||||
// parsePlaintextJwt tests
|
||||
|
@ -322,6 +320,20 @@ class JwtParserTest {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testParseClaimsJwtWithPrematureJwt() {
|
||||
|
||||
Date nbf = new Date(System.currentTimeMillis() + 100000);
|
||||
|
||||
String compact = Jwts.builder().setSubject('Joe').setNotBefore(nbf).compact();
|
||||
|
||||
try {
|
||||
Jwts.parser().parseClaimsJwt(compact);
|
||||
} catch (PrematureJwtException e) {
|
||||
assertTrue e.getMessage().startsWith('JWT must not be accepted before ')
|
||||
}
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// parsePlaintextJws tests
|
||||
// ========================================================================
|
||||
|
@ -425,6 +437,22 @@ class JwtParserTest {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testParseClaimsJwsWithPrematureJws() {
|
||||
|
||||
byte[] key = randomKey()
|
||||
|
||||
Date nbf = new Date(System.currentTimeMillis() + 100000);
|
||||
|
||||
String compact = Jwts.builder().setSubject('Joe').setNotBefore(nbf).signWith(SignatureAlgorithm.HS256, key).compact();
|
||||
|
||||
try {
|
||||
Jwts.parser().parseClaimsJws(compact);
|
||||
} catch (PrematureJwtException e) {
|
||||
assertTrue e.getMessage().startsWith('JWT must not be accepted before ')
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testParseClaimsJwsWithPlaintextJwt() {
|
||||
|
||||
|
|
Loading…
Reference in New Issue