Skip to content

Commit

Permalink
fix(jans-auth-server): use duration class instead of custom util to c…
Browse files Browse the repository at this point in the history
…alculate seconds from date to now (#1249)
  • Loading branch information
Milton-Ch authored Apr 25, 2022
1 parent 369129d commit 5ae76ab
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.jans.orm.annotation.ObjectClass;

import java.io.Serializable;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
Expand Down Expand Up @@ -86,7 +87,8 @@ public void setTtl(Integer ttl) {
}

public void resetTtlFromExpirationDate() {
final Integer calculatedTtl = Util.getNumberOfSecondFromNow(getExpirationDate());
final Long duration = Duration.between(new Date().toInstant(), getExpirationDate().toInstant()).getSeconds();
final Integer calculatedTtl = duration.intValue();
if (calculatedTtl != null) {
setTtl(calculatedTtl);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import jakarta.validation.constraints.NotNull;
import java.io.Serializable;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -91,7 +92,8 @@ public void setTtl(Integer ttl) {
}

public void resetTtlFromExpirationDate() {
final Integer calculatedTtl = Util.getNumberOfSecondFromNow(getExpirationDate());
final Long duration = Duration.between(new Date().toInstant(), getExpirationDate().toInstant()).getSeconds();
final Integer calculatedTtl = duration.intValue();
if (calculatedTtl != null) {
setTtl(calculatedTtl);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
Expand Down Expand Up @@ -330,20 +331,6 @@ public static String byteArrayToHexString(byte[] b) {
return result.toString();
}

public static Integer getNumberOfSecondFromNow(Date date) {
if (date == null) {
return 0;
}

long now = new Date().getTime();
final long time = date.getTime();
if (time > now) {
return (int) (time - now) / 1000;
}

return null;
}

public static Date createExpirationDate(Integer lifetimeInSeconds) {
if (lifetimeInSeconds == null || lifetimeInSeconds == 0)
throw new IllegalArgumentException("lifetime can't be null or zero");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,33 +416,6 @@ public void byteArrayToHexString_byteArray_hexString() {
assertEquals(result, "48656c6c6f20776f726c64");
}

@Test
public void getNumberOfSecondFromNow_null_zero() {
showTitle("getNumberOfSecondFromNow_null_zero");
Integer seconds = Util.getNumberOfSecondFromNow(null);
assertNotNull(seconds);
assertEquals(seconds, Integer.valueOf(0));
}

@Test
public void getNumberOfSecondFromNow_dateInPast_null() {
showTitle("getNumberOfSecondFromNow_dateInPast_null");
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(cal.getTimeInMillis() - 10000L);
Integer seconds = Util.getNumberOfSecondFromNow(cal.getTime());
assertNull(seconds);
}

@Test
public void getNumberOfSecondFromNow_dateInFuture_intNumberOfSeconds() {
showTitle("getNumberOfSecondFromNow_dateInFuture_intNumberOfSeconds");
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(cal.getTimeInMillis() + 10999L);
Integer seconds = Util.getNumberOfSecondFromNow(cal.getTime());
assertNotNull(seconds);
assertTrue(seconds >= 10);
}

@Test
public void createExpirationDate_nullLifeTime_IllegalArgumentException() {
showTitle("createExpirationDate_nullLifeTime_IllegalArgumentException");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.jans.orm.model.base.Deletable;

import java.io.Serializable;
import java.time.Duration;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -105,7 +106,8 @@ private void initTtl() {
}

public void resetTtlFromExpirationDate() {
final Integer seconds = Util.getNumberOfSecondFromNow(getExpirationDate());
final Long duration = Duration.between(new Date().toInstant(), getExpirationDate().toInstant()).getSeconds();
final Integer seconds = duration.intValue();
if (seconds != null) {
this.ttl = seconds;
}
Expand Down

0 comments on commit 5ae76ab

Please sign in to comment.