Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for java.time Instant instead of java.util.Date #311

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
14 changes: 7 additions & 7 deletions src/main/java/io/jsonwebtoken/Claims.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package io.jsonwebtoken;

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

/**
Expand Down Expand Up @@ -111,13 +111,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 +127,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 +143,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
8 changes: 4 additions & 4 deletions src/main/java/io/jsonwebtoken/ClaimsMutator.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package io.jsonwebtoken;

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

/**
* Mutation (modifications) to a {@link io.jsonwebtoken.Claims Claims} instance.
Expand Down Expand Up @@ -63,7 +63,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 +74,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 +85,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
4 changes: 2 additions & 2 deletions src/main/java/io/jsonwebtoken/Clock.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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.
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();
}
24 changes: 12 additions & 12 deletions src/main/java/io/jsonwebtoken/JwtBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package io.jsonwebtoken;

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

/**
Expand Down Expand Up @@ -198,16 +198,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 +217,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 +226,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 +245,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 +254,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 +263,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 +273,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
8 changes: 4 additions & 4 deletions src/main/java/io/jsonwebtoken/JwtParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import io.jsonwebtoken.impl.DefaultClock;

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

/**
* A parser for reading JWT strings, used to convert them into a {@link Jwt} object representing the expanded JWT.
Expand Down Expand Up @@ -87,7 +87,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 +99,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 +111,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
10 changes: 5 additions & 5 deletions src/main/java/io/jsonwebtoken/impl/DefaultClock.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.jsonwebtoken.Clock;

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

/**
* Default {@link Clock} implementation.
Expand All @@ -17,12 +17,12 @@ public class DefaultClock implements Clock {
public static final Clock INSTANCE = new DefaultClock();

/**
* Simply returns <code>new {@link Date}()</code>.
* Simply returns <code>new {@link Instant}()</code>.
*
* @return a new {@link Date} instance.
* @return a new {@link Instant} instance.
*/
@Override
public Date now() {
return new Date();
public Instant now() {
return Instant.now();
}
}