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

Microoptimalization: Dates ++ #651

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions api/src/main/java/io/jsonwebtoken/Clock.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ public interface Clock {
* Returns the clock's current timestamp at the instant the method is invoked.
*
* @return the clock's current timestamp at the instant the method is invoked.
* @deprecated
*/
Date now();

/**
* Returns the clock's current timestamp at the instant the method is invoked.
*
* @return the clock's current timestamp at the instant the method is invoked.
*/

long millis();

}
8 changes: 6 additions & 2 deletions impl/src/main/java/io/jsonwebtoken/impl/DefaultClock.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ public class DefaultClock implements Clock {
*
* @return a new {@link Date} instance.
*/
@Override
public Date now() {
return new Date();
return new Date(millis());
}

@Override
public long millis() {
return System.currentTimeMillis();
}
}
25 changes: 8 additions & 17 deletions impl/src/main/java/io/jsonwebtoken/impl/DefaultJwtParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public Jwt parse(String jwt) throws ExpiredJwtException, MalformedJwtException,

int delimiterCount = 0;

StringBuilder sb = new StringBuilder(128);
StringBuilder sb = new StringBuilder(jwt.length());

for (char c : jwt.toCharArray()) {

Expand Down Expand Up @@ -421,26 +421,19 @@ public Jwt parse(String jwt) throws ExpiredJwtException, MalformedJwtException,
}
}

final boolean allowSkew = this.allowedClockSkewMillis > 0;

//since 0.3:
if (claims != null) {

final Date now = this.clock.now();
long nowTime = now.getTime();
final long nowTime = this.clock.millis();

//https://tools.ietf.org/html/draft-ietf-oauth-json-web-token-30#section-4.1.4
//token MUST NOT be accepted on or after any specified exp time:
Date exp = claims.getExpiration();
if (exp != null) {

long maxTime = nowTime - this.allowedClockSkewMillis;
Date max = allowSkew ? new Date(maxTime) : now;
if (max.after(exp)) {
if (nowTime > exp.getTime() + this.allowedClockSkewMillis) {
String expVal = DateFormats.formatIso8601(exp, false);
String nowVal = DateFormats.formatIso8601(now, false);
String nowVal = DateFormats.formatIso8601(new Date(nowTime), false);

long differenceMillis = maxTime - exp.getTime();
long differenceMillis = nowTime - exp.getTime();

String msg = "JWT expired at " + expVal + ". Current time: " + nowVal + ", a difference of " +
differenceMillis + " milliseconds. Allowed clock skew: " +
Expand All @@ -454,13 +447,11 @@ public Jwt parse(String jwt) throws ExpiredJwtException, MalformedJwtException,
Date nbf = claims.getNotBefore();
if (nbf != null) {

long minTime = nowTime + this.allowedClockSkewMillis;
Date min = allowSkew ? new Date(minTime) : now;
if (min.before(nbf)) {
if (nowTime < nbf.getTime() - this.allowedClockSkewMillis) {
String nbfVal = DateFormats.formatIso8601(nbf, false);
String nowVal = DateFormats.formatIso8601(now, false);
String nowVal = DateFormats.formatIso8601(new Date(nowTime), false);

long differenceMillis = nbf.getTime() - minTime;
long differenceMillis = nbf.getTime() - nowTime;

String msg = "JWT must not be accepted before " + nbfVal + ". Current time: " + nowVal +
", a difference of " +
Expand Down
16 changes: 12 additions & 4 deletions impl/src/main/java/io/jsonwebtoken/impl/FixedClock.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
*/
public class FixedClock implements Clock {

private final Date now;
private final long now;

/**
* Creates a new fixed clock using <code>new {@link Date Date}()</code> as the seed timestamp. All calls to
* {@link #now now()} will always return this seed Date.
*/
public FixedClock() {
this(new Date());
this(System.currentTimeMillis());
}

/**
Expand All @@ -43,12 +43,20 @@ public FixedClock() {
*
* @param now the specified Date to always return from all calls to {@link #now now()}.
*/
public FixedClock(Date now) {
public FixedClock(long now) {
this.now = now;
}

@Override
public FixedClock(Date now) {
this(now.getTime());
}

public Date now() {
return new Date(millis());
}

@Override
public long millis() {
return this.now;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ class FixedClockTest {
Thread.sleep(100)
def date2 = clock.now()

assertSame date1, date2
assertEquals date1, date2
}
}