Skip to content

Commit

Permalink
Added support for java.time Instant instead of java.util.Date
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Zagniotov committed Mar 14, 2018
1 parent 44faaca commit 0907ae9
Show file tree
Hide file tree
Showing 17 changed files with 235 additions and 190 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ dist: trusty
sudo: required
language: java
jdk:
- openjdk7
- oraclejdk8
- oraclejdk9

Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@
<version>${jackson.version}</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>

<!-- Optional Dependencies: -->
<dependency>
<groupId>org.bouncycastle</groupId>
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/io/jsonwebtoken/Claims.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.jsonwebtoken;

import java.time.Instant;
import java.util.Date;
import java.util.Map;

Expand Down Expand Up @@ -111,13 +112,13 @@ public interface Claims extends Map<String, Object>, ClaimsMutator<Claims> {
*
* @return the JWT {@code exp} value or {@code null} if not present.
*/
Date getExpiration();
Instant getExpiration();

/**
* {@inheritDoc}
*/
@Override //only for better/targeted JavaDoc
Claims setExpiration(Date exp);
Claims setExpiration(Instant exp);

/**
* Returns the JWT <a href="https://tools.ietf.org/html/draft-ietf-oauth-json-web-token-25#section-4.1.5">
Expand All @@ -127,13 +128,13 @@ public interface Claims extends Map<String, Object>, ClaimsMutator<Claims> {
*
* @return the JWT {@code nbf} value or {@code null} if not present.
*/
Date getNotBefore();
Instant getNotBefore();

/**
* {@inheritDoc}
*/
@Override //only for better/targeted JavaDoc
Claims setNotBefore(Date nbf);
Claims setNotBefore(Instant nbf);

/**
* Returns the JWT <a href="https://tools.ietf.org/html/draft-ietf-oauth-json-web-token-25#section-4.1.6">
Expand All @@ -143,13 +144,13 @@ public interface Claims extends Map<String, Object>, ClaimsMutator<Claims> {
*
* @return the JWT {@code nbf} value or {@code null} if not present.
*/
Date getIssuedAt();
Instant getIssuedAt();

/**
* {@inheritDoc}
*/
@Override //only for better/targeted JavaDoc
Claims setIssuedAt(Date iat);
Claims setIssuedAt(Instant iat);

/**
* Returns the JWTs <a href="https://tools.ietf.org/html/draft-ietf-oauth-json-web-token-25#section-4.1.7">
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/io/jsonwebtoken/ClaimsMutator.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.jsonwebtoken;

import java.time.Instant;
import java.util.Date;

/**
Expand Down Expand Up @@ -63,7 +64,7 @@ public interface ClaimsMutator<T extends ClaimsMutator> {
* @param exp the JWT {@code exp} value or {@code null} to remove the property from the JSON map.
* @return the {@code Claims} instance for method chaining.
*/
T setExpiration(Date exp);
T setExpiration(Instant exp);

/**
* Sets the JWT <a href="https://tools.ietf.org/html/draft-ietf-oauth-json-web-token-25#section-4.1.5">
Expand All @@ -74,7 +75,7 @@ public interface ClaimsMutator<T extends ClaimsMutator> {
* @param nbf the JWT {@code nbf} value or {@code null} to remove the property from the JSON map.
* @return the {@code Claims} instance for method chaining.
*/
T setNotBefore(Date nbf);
T setNotBefore(Instant nbf);

/**
* Sets the JWT <a href="https://tools.ietf.org/html/draft-ietf-oauth-json-web-token-25#section-4.1.6">
Expand All @@ -85,7 +86,7 @@ public interface ClaimsMutator<T extends ClaimsMutator> {
* @param iat the JWT {@code iat} value or {@code null} to remove the property from the JSON map.
* @return the {@code Claims} instance for method chaining.
*/
T setIssuedAt(Date iat);
T setIssuedAt(Instant iat);

/**
* Sets the JWT <a href="https://tools.ietf.org/html/draft-ietf-oauth-json-web-token-25#section-4.1.7">
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/jsonwebtoken/Clock.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package io.jsonwebtoken;

import java.util.Date;
import java.time.Instant;

/**
* A clock represents a time source that can be used when creating and verifying JWTs.
*
* @since 0.7.0
* @since 0.10.0
*/
public interface Clock {

Expand All @@ -14,5 +14,5 @@ public interface Clock {
*
* @return the clock's current timestamp at the instant the method is invoked.
*/
Date now();
Instant now();
}
23 changes: 12 additions & 11 deletions src/main/java/io/jsonwebtoken/JwtBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.jsonwebtoken;

import java.security.Key;
import java.time.Instant;
import java.util.Date;
import java.util.Map;

Expand Down Expand Up @@ -198,16 +199,16 @@ public interface JwtBuilder extends ClaimsMutator<JwtBuilder> {
* <p>A JWT obtained after this timestamp should not be used.</p>
*
* <p>This is a convenience method. It will first ensure a Claims instance exists as the JWT body and then set
* the Claims {@link Claims#setExpiration(java.util.Date) expiration} field with the specified value. This allows
* the Claims {@link Claims#setExpiration(java.time.Instant) expiration} field with the specified value. This allows
* you to write code like this:</p>
*
* <pre>
* String jwt = Jwts.builder().setExpiration(new Date(System.currentTimeMillis() + 3600000)).compact();
* String jwt = Jwts.builder().setExpiration(Instant.ofEpochMilli(System.currentTimeMillis() + 3600000)).compact();
* </pre>
*
* <p>instead of this:</p>
* <pre>
* Claims claims = Jwts.claims().setExpiration(new Date(System.currentTimeMillis() + 3600000));
* Claims claims = Jwts.claims().setExpiration(Instant.ofEpochMilli(System.currentTimeMillis() + 3600000));
* String jwt = Jwts.builder().setClaims(claims).compact();
* </pre>
* <p>if desired.</p>
Expand All @@ -217,7 +218,7 @@ public interface JwtBuilder extends ClaimsMutator<JwtBuilder> {
* @since 0.2
*/
@Override //only for better/targeted JavaDoc
JwtBuilder setExpiration(Date exp);
JwtBuilder setExpiration(Instant exp);

/**
* Sets the JWT Claims <a href="https://tools.ietf.org/html/draft-ietf-oauth-json-web-token-25#section-4.1.5">
Expand All @@ -226,16 +227,16 @@ public interface JwtBuilder extends ClaimsMutator<JwtBuilder> {
* <p>A JWT obtained before this timestamp should not be used.</p>
*
* <p>This is a convenience method. It will first ensure a Claims instance exists as the JWT body and then set
* the Claims {@link Claims#setNotBefore(java.util.Date) notBefore} field with the specified value. This allows
* the Claims {@link Claims#setNotBefore(java.time.Instant) notBefore} field with the specified value. This allows
* you to write code like this:</p>
*
* <pre>
* String jwt = Jwts.builder().setNotBefore(new Date()).compact();
* String jwt = Jwts.builder().setNotBefore(Instant.now()).compact();
* </pre>
*
* <p>instead of this:</p>
* <pre>
* Claims claims = Jwts.claims().setNotBefore(new Date());
* Claims claims = Jwts.claims().setNotBefore(Instant.now());
* String jwt = Jwts.builder().setClaims(claims).compact();
* </pre>
* <p>if desired.</p>
Expand All @@ -245,7 +246,7 @@ public interface JwtBuilder extends ClaimsMutator<JwtBuilder> {
* @since 0.2
*/
@Override //only for better/targeted JavaDoc
JwtBuilder setNotBefore(Date nbf);
JwtBuilder setNotBefore(Instant nbf);

/**
* Sets the JWT Claims <a href="https://tools.ietf.org/html/draft-ietf-oauth-json-web-token-25#section-4.1.6">
Expand All @@ -254,7 +255,7 @@ public interface JwtBuilder extends ClaimsMutator<JwtBuilder> {
* <p>The value is the timestamp when the JWT was created.</p>
*
* <p>This is a convenience method. It will first ensure a Claims instance exists as the JWT body and then set
* the Claims {@link Claims#setIssuedAt(java.util.Date) issuedAt} field with the specified value. This allows
* the Claims {@link Claims#setIssuedAt(java.time.Instant) issuedAt} field with the specified value. This allows
* you to write code like this:</p>
*
* <pre>
Expand All @@ -263,7 +264,7 @@ public interface JwtBuilder extends ClaimsMutator<JwtBuilder> {
*
* <p>instead of this:</p>
* <pre>
* Claims claims = Jwts.claims().setIssuedAt(new Date());
* Claims claims = Jwts.claims().setIssuedAt(Instant.now());
* String jwt = Jwts.builder().setClaims(claims).compact();
* </pre>
* <p>if desired.</p>
Expand All @@ -273,7 +274,7 @@ public interface JwtBuilder extends ClaimsMutator<JwtBuilder> {
* @since 0.2
*/
@Override //only for better/targeted JavaDoc
JwtBuilder setIssuedAt(Date iat);
JwtBuilder setIssuedAt(Instant iat);

/**
* Sets the JWT Claims <a href="https://tools.ietf.org/html/draft-ietf-oauth-json-web-token-25#section-4.1.7">
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/io/jsonwebtoken/JwtParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.jsonwebtoken.impl.DefaultClock;

import java.security.Key;
import java.time.Instant;
import java.util.Date;

/**
Expand Down Expand Up @@ -87,7 +88,7 @@ public interface JwtParser {
* @see MissingClaimException
* @see IncorrectClaimException
*/
JwtParser requireIssuedAt(Date issuedAt);
JwtParser requireIssuedAt(Instant issuedAt);

/**
* Ensures that the specified {@code exp} exists in the parsed JWT. If missing or if the parsed
Expand All @@ -99,7 +100,7 @@ public interface JwtParser {
* @see MissingClaimException
* @see IncorrectClaimException
*/
JwtParser requireExpiration(Date expiration);
JwtParser requireExpiration(Instant expiration);

/**
* Ensures that the specified {@code nbf} exists in the parsed JWT. If missing or if the parsed
Expand All @@ -111,7 +112,7 @@ public interface JwtParser {
* @see MissingClaimException
* @see IncorrectClaimException
*/
JwtParser requireNotBefore(Date notBefore);
JwtParser requireNotBefore(Instant notBefore);

/**
* Ensures that the specified {@code claimName} exists in the parsed JWT. If missing or if the parsed
Expand Down
32 changes: 16 additions & 16 deletions src/main/java/io/jsonwebtoken/impl/DefaultClaims.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.RequiredTypeException;

import java.util.Date;
import java.time.Instant;
import java.util.Map;

public class DefaultClaims extends JwtMap implements Claims {
Expand Down Expand Up @@ -65,35 +65,35 @@ public Claims setAudience(String aud) {
}

@Override
public Date getExpiration() {
return get(Claims.EXPIRATION, Date.class);
public Instant getExpiration() {
return get(Claims.EXPIRATION, Instant.class);
}

@Override
public Claims setExpiration(Date exp) {
setDate(Claims.EXPIRATION, exp);
public Claims setExpiration(Instant exp) {
setInstant(Claims.EXPIRATION, exp);
return this;
}

@Override
public Date getNotBefore() {
return get(Claims.NOT_BEFORE, Date.class);
public Instant getNotBefore() {
return get(Claims.NOT_BEFORE, Instant.class);
}

@Override
public Claims setNotBefore(Date nbf) {
setDate(Claims.NOT_BEFORE, nbf);
public Claims setNotBefore(Instant nbf) {
setInstant(Claims.NOT_BEFORE, nbf);
return this;
}

@Override
public Date getIssuedAt() {
return get(Claims.ISSUED_AT, Date.class);
public Instant getIssuedAt() {
return get(Claims.ISSUED_AT, Instant.class);
}

@Override
public Claims setIssuedAt(Date iat) {
setDate(Claims.ISSUED_AT, iat);
public Claims setIssuedAt(Instant iat) {
setInstant(Claims.ISSUED_AT, iat);
return this;
}

Expand All @@ -117,15 +117,15 @@ public <T> T get(String claimName, Class<T> requiredType) {
Claims.ISSUED_AT.equals(claimName) ||
Claims.NOT_BEFORE.equals(claimName)
) {
value = getDate(claimName);
value = getInstant(claimName);
}

return castClaimValue(value, requiredType);
}

private <T> T castClaimValue(Object value, Class<T> requiredType) {
if (requiredType == Date.class && value instanceof Long) {
value = new Date((Long)value);
if (requiredType == Instant.class && value instanceof Number) {
value = Instant.ofEpochSecond(Long.parseLong(value.toString()));
}

if (value instanceof Integer) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/io/jsonwebtoken/impl/DefaultClock.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.jsonwebtoken.Clock;

import java.time.Instant;
import java.util.Date;

/**
Expand All @@ -22,7 +23,7 @@ public class DefaultClock implements Clock {
* @return a new {@link Date} instance.
*/
@Override
public Date now() {
return new Date();
public Instant now() {
return java.time.Clock.systemUTC().instant();
}
}

0 comments on commit 0907ae9

Please sign in to comment.