Skip to content

Commit 3bc18d8

Browse files
committed
switch to use of long for expire and issue dates
1 parent 8f56584 commit 3bc18d8

File tree

5 files changed

+46
-53
lines changed

5 files changed

+46
-53
lines changed

core/src/main/java/redis/clients/authentication/core/SimpleToken.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package redis.clients.authentication.core;
22

3-
import java.util.Date;
43
import java.util.Map;
54

65
public class SimpleToken implements Token {
76

87
private String value;
9-
private Date expiresAt;
10-
private Date receivedAt;
8+
private long expiresAt;
9+
private long receivedAt;
1110
private Map<String, String> claims;
1211

13-
public SimpleToken(String value, Date expiresAt, Date receivedAt, Map<String, String> claims) {
12+
public SimpleToken(String value, long expiresAt, long receivedAt, Map<String, String> claims) {
1413
this.value = value;
1514
this.expiresAt = expiresAt;
1615
this.receivedAt = receivedAt;
@@ -19,12 +18,12 @@ public SimpleToken(String value, Date expiresAt, Date receivedAt, Map<String, St
1918

2019
@Override
2120
public boolean isExpired() {
22-
return new Date().after(expiresAt);
21+
return System.currentTimeMillis() > expiresAt;
2322
}
2423

2524
@Override
2625
public long ttl() {
27-
return expiresAt.getTime() - System.currentTimeMillis();
26+
return expiresAt - System.currentTimeMillis();
2827
}
2928

3029
@Override
@@ -33,12 +32,12 @@ public String getValue() {
3332
}
3433

3534
@Override
36-
public Date getExpiresAt() {
35+
public long getExpiresAt() {
3736
return expiresAt;
3837
}
3938

4039
@Override
41-
public Date getReceivedAt() {
40+
public long getReceivedAt() {
4241
return receivedAt;
4342
}
4443

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package redis.clients.authentication.core;
22

3-
import java.util.Date;
4-
53
public interface Token {
64

75
public boolean isExpired();
@@ -10,9 +8,9 @@ public interface Token {
108

119
public String getValue();
1210

13-
public Date getExpiresAt();
11+
public long getExpiresAt();
1412

15-
public Date getReceivedAt();
13+
public long getReceivedAt();
1614

1715
public String tryGet(String key);
1816
}

core/src/main/java/redis/clients/authentication/core/TokenManager.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package redis.clients.authentication.core;
22

3-
import java.util.Date;
43
import java.util.concurrent.ExecutionException;
54
import java.util.concurrent.ExecutorService;
65
import java.util.concurrent.Executors;
@@ -78,23 +77,23 @@ protected Token renewToken() {
7877
return null;
7978
}
8079

81-
public long calculateRenewalDelay(Date expireDate, Date issueDate) {
80+
public long calculateRenewalDelay(long expireDate, long issueDate) {
8281
long ttlLowerRefresh = ttlForLowerRefresh(expireDate);
8382
long ttlRatioRefresh = ttlForRatioRefresh(expireDate, issueDate);
8483
long delay = Math.min(ttlLowerRefresh, ttlRatioRefresh);
8584

8685
return delay < 0 ? 0 : delay;
8786
}
8887

89-
public long ttlForLowerRefresh(Date expireDate) {
90-
return expireDate.getTime() - tokenManagerConfig.getLowerRefreshBoundMillis()
88+
public long ttlForLowerRefresh(long expireDate) {
89+
return expireDate - tokenManagerConfig.getLowerRefreshBoundMillis()
9190
- System.currentTimeMillis();
9291
}
9392

94-
protected long ttlForRatioRefresh(Date expireDate, Date issueDate) {
95-
long validDuration = expireDate.getTime() - issueDate.getTime();
93+
protected long ttlForRatioRefresh(long expireDate, long issueDate) {
94+
long validDuration = expireDate - issueDate;
9695
long refreshBefore = validDuration
9796
- (long) (validDuration * tokenManagerConfig.getExpirationRefreshRatio());
98-
return expireDate.getTime() - refreshBefore - System.currentTimeMillis();
97+
return expireDate - refreshBefore - System.currentTimeMillis();
9998
}
10099
}

core/src/test/java/redis/clients/authentication/CoreAuthenticationUnitTests.java

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import static org.mockito.Mockito.verify;
1717

1818
import java.util.Collections;
19-
import java.util.Date;
2019
import java.util.concurrent.CountDownLatch;
2120
import java.util.concurrent.ExecutionException;
2221
import java.util.concurrent.TimeoutException;
@@ -60,10 +59,9 @@ public void testAuthXManagerFactory() {
6059
assertEquals(identityProvider, context.arguments().get(0));
6160
assertEquals(tokenManagerConfig, context.arguments().get(1));
6261
})) {
63-
AuthXManager manager = AuthXManagerFactory
64-
.create(new TokenAuthConfig(tokenManagerConfig, identityProviderConfig));
62+
AuthXManagerFactory.create(new TokenAuthConfig(tokenManagerConfig, identityProviderConfig));
6563

66-
CustomAuthXManager customManager = AuthXManagerFactory.create(CustomAuthXManager.class,
64+
AuthXManagerFactory.create(CustomAuthXManager.class,
6765
new TokenAuthConfig(tokenManagerConfig, identityProviderConfig));
6866
}
6967
}
@@ -91,17 +89,17 @@ public float getExpirationRefreshRatio() {
9189
public void testCalculateRenewalDelay() {
9290
long delay = 0;
9391
long duration = 0;
94-
Date issueDate;
95-
Date expireDate;
92+
long issueDate;
93+
long expireDate;
9694

9795
TokenManagerConfigWrapper config = new TokenManagerConfigWrapper();
9896
TokenManager manager = new TokenManager(() -> null, config);
9997

10098
duration = 5000;
10199
config.lower = 2000;
102100
config.ratio = 0.5F;
103-
issueDate = new Date();
104-
expireDate = new Date(issueDate.getTime() + duration);
101+
issueDate = System.currentTimeMillis();
102+
expireDate = issueDate + duration;
105103

106104
delay = manager.calculateRenewalDelay(expireDate, issueDate);
107105

@@ -111,8 +109,8 @@ public void testCalculateRenewalDelay() {
111109
duration = 10000;
112110
config.lower = 8000;
113111
config.ratio = 0.2F;
114-
issueDate = new Date();
115-
expireDate = new Date(issueDate.getTime() + duration);
112+
issueDate = System.currentTimeMillis();
113+
expireDate = issueDate + duration;
116114

117115
delay = manager.calculateRenewalDelay(expireDate, issueDate);
118116

@@ -122,8 +120,8 @@ public void testCalculateRenewalDelay() {
122120
duration = 10000;
123121
config.lower = 10000;
124122
config.ratio = 0.2F;
125-
issueDate = new Date();
126-
expireDate = new Date(issueDate.getTime() + duration);
123+
issueDate = System.currentTimeMillis();
124+
expireDate = issueDate + duration;
127125

128126
delay = manager.calculateRenewalDelay(expireDate, issueDate);
129127

@@ -132,8 +130,8 @@ public void testCalculateRenewalDelay() {
132130
duration = 0;
133131
config.lower = 5000;
134132
config.ratio = 0.2F;
135-
issueDate = new Date();
136-
expireDate = new Date(issueDate.getTime() + duration);
133+
issueDate = System.currentTimeMillis();
134+
expireDate = issueDate + duration;
137135

138136
delay = manager.calculateRenewalDelay(expireDate, issueDate);
139137

@@ -142,8 +140,8 @@ public void testCalculateRenewalDelay() {
142140
duration = 10000;
143141
config.lower = 1000;
144142
config.ratio = 0.00001F;
145-
issueDate = new Date();
146-
expireDate = new Date(issueDate.getTime() + duration);
143+
issueDate = System.currentTimeMillis();
144+
expireDate = issueDate + duration;
147145

148146
delay = manager.calculateRenewalDelay(expireDate, issueDate);
149147

@@ -152,8 +150,8 @@ public void testCalculateRenewalDelay() {
152150
duration = 10000;
153151
config.lower = 1000;
154152
config.ratio = 0.0001F;
155-
issueDate = new Date();
156-
expireDate = new Date(issueDate.getTime() + duration);
153+
issueDate = System.currentTimeMillis();
154+
expireDate = issueDate + duration;
157155

158156
delay = manager.calculateRenewalDelay(expireDate, issueDate);
159157

@@ -164,7 +162,7 @@ public void testCalculateRenewalDelay() {
164162
public void testAuthXManager() throws InterruptedException, ExecutionException, TimeoutException {
165163

166164
IdentityProvider identityProvider = () -> new SimpleToken("tokenVal",
167-
new Date(System.currentTimeMillis() + 5 * 1000), new Date(),
165+
System.currentTimeMillis() + 5 * 1000, System.currentTimeMillis(),
168166
Collections.singletonMap("oid", "user1"));
169167

170168
TokenManager tokenManager = new TokenManager(identityProvider,
@@ -232,8 +230,8 @@ public void testTokenManagerWithFailingTokenRequest()
232230
if (requesLatch.getCount() > 0) {
233231
throw new RuntimeException("Test exception from identity provider!");
234232
}
235-
return new SimpleToken("tokenValX", new Date(System.currentTimeMillis() + 50 * 1000),
236-
new Date(), Collections.singletonMap("oid", "user1"));
233+
return new SimpleToken("tokenValX", System.currentTimeMillis() + 50 * 1000,
234+
System.currentTimeMillis(), Collections.singletonMap("oid", "user1"));
237235
});
238236

239237
ArgumentCaptor<Token> argument = ArgumentCaptor.forClass(Token.class);
@@ -268,8 +266,8 @@ public void testTokenManagerWithHangingTokenRequest()
268266
}
269267
return null;
270268
}
271-
return new SimpleToken("tokenValX", new Date(System.currentTimeMillis() + tokenLifetime),
272-
new Date(), Collections.singletonMap("oid", "user1"));
269+
return new SimpleToken("tokenValX", System.currentTimeMillis() + tokenLifetime,
270+
System.currentTimeMillis(), Collections.singletonMap("oid", "user1"));
273271
};
274272

275273
TokenManager tokenManager = new TokenManager(identityProvider, new TokenManagerConfig(0.7F, 200,

entraid/src/main/java/redis/clients/authentication/entraid/JWToken.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package redis.clients.authentication.entraid;
22

3-
import java.util.Date;
43
import java.util.function.Function;
54

65
import com.auth0.jwt.interfaces.DecodedJWT;
@@ -10,26 +9,26 @@
109

1110
public class JWToken implements Token {
1211
private final String token;
13-
private final Date expiresAt;
14-
private final Date receivedAt;
15-
private final Function<String, String> claimQuery;
16-
12+
private final long expiresAt;
13+
private final long receivedAt;
14+
private final Function<String, String> claimQuery;
15+
1716
public JWToken(String token) {
1817
this.token = token;
1918
DecodedJWT jwt = JWT.decode(token);
20-
this.expiresAt = jwt.getExpiresAt();
21-
this.receivedAt = new Date();
19+
this.expiresAt = jwt.getExpiresAt().getTime();
20+
this.receivedAt = System.currentTimeMillis();
2221
this.claimQuery = key -> jwt.getClaim(key).asString();
2322
}
2423

2524
@Override
2625
public boolean isExpired() {
27-
return new Date().after(expiresAt);
26+
return System.currentTimeMillis() > expiresAt;
2827
}
2928

3029
@Override
3130
public long ttl() {
32-
return expiresAt.getTime() - System.currentTimeMillis();
31+
return expiresAt - System.currentTimeMillis();
3332
}
3433

3534
@Override
@@ -38,12 +37,12 @@ public String getValue() {
3837
}
3938

4039
@Override
41-
public Date getExpiresAt() {
40+
public long getExpiresAt() {
4241
return expiresAt;
4342
}
4443

4544
@Override
46-
public Date getReceivedAt() {
45+
public long getReceivedAt() {
4746
return receivedAt;
4847
}
4948

0 commit comments

Comments
 (0)