Skip to content

Commit

Permalink
resolved Issue #7
Browse files Browse the repository at this point in the history
  • Loading branch information
lhazlewood committed Oct 29, 2014
1 parent 35a4282 commit 5540d5d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
33 changes: 33 additions & 0 deletions src/main/java/io/jsonwebtoken/PrematureJwtException.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
3 changes: 1 addition & 2 deletions src/main/java/io/jsonwebtoken/impl/DefaultJwtParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;
Expand Down Expand Up @@ -190,7 +191,6 @@ public Jwt parse(String jwt) throws ExpiredJwtException, MalformedJwtException,
}
}

/*
//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();
Expand All @@ -209,7 +209,6 @@ public Jwt parse(String jwt) throws ExpiredJwtException, MalformedJwtException,
throw new PrematureJwtException(msg);
}
}
*/
}

// =============== Signature =================
Expand Down
32 changes: 30 additions & 2 deletions src/test/groovy/io/jsonwebtoken/JwtParserTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ class JwtParserTest {
}
}

/*
@Test
void testParseWithPrematureJwt() {

Expand All @@ -188,7 +187,6 @@ class JwtParserTest {
assertTrue e.getMessage().startsWith('JWT must not be accepted before ')
}
}
*/

// ========================================================================
// parsePlaintextJwt tests
Expand Down Expand Up @@ -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
// ========================================================================
Expand Down Expand Up @@ -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() {

Expand Down

0 comments on commit 5540d5d

Please sign in to comment.