Skip to content

Commit 545f5c4

Browse files
committed
extract Clock interface. Make ClockImpl package private
1 parent 0abcebe commit 545f5c4

File tree

7 files changed

+28
-14
lines changed

7 files changed

+28
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ If you need to test this behaviour in your lib/app cast the `Verification` insta
158158
BaseVerification verification = (BaseVerification) JWT.require(Algorithm.RSA256(key))
159159
.acceptLeeway(1)
160160
.acceptExpiresAt(5);
161-
Clock clock = new Clock();
161+
Clock clock = new CustomClock(); //Must implement Clock interface
162162
JWTVerifier verifier = verification.build(clock);
163163
```
164164

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.auth0.jwt;
2+
3+
import com.auth0.jwt.interfaces.Clock;
4+
5+
import java.util.Date;
6+
7+
final class ClockImpl implements Clock {
8+
9+
ClockImpl() {
10+
}
11+
12+
@Override
13+
public Date getToday() {
14+
return new Date();
15+
}
16+
}

lib/src/main/java/com/auth0/jwt/JWTVerifier.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.auth0.jwt.exceptions.SignatureVerificationException;
88
import com.auth0.jwt.impl.PublicClaims;
99
import com.auth0.jwt.interfaces.Claim;
10+
import com.auth0.jwt.interfaces.Clock;
1011
import com.auth0.jwt.interfaces.DecodedJWT;
1112
import com.auth0.jwt.interfaces.Verification;
1213
import org.apache.commons.codec.binary.Base64;
@@ -278,7 +279,7 @@ public Verification withArrayClaim(String name, Integer... items) throws Illegal
278279
*/
279280
@Override
280281
public JWTVerifier build() {
281-
return this.build(new Clock());
282+
return this.build(new ClockImpl());
282283
}
283284

284285
/**
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
1-
package com.auth0.jwt;
1+
package com.auth0.jwt.interfaces;
22

33
import java.util.Date;
44

55
/**
66
* The Clock class is used to wrap calls to Date class.
77
*/
8-
public class Clock {
9-
10-
public Clock() {
11-
}
8+
public interface Clock {
129

1310
/**
1411
* Returns a new Date representing Today's time.
1512
*
1613
* @return a new Date representing Today's time.
1714
*/
18-
public Date getToday() {
19-
return new Date();
20-
}
15+
Date getToday();
2116
}

lib/src/test/java/com/auth0/jwt/ClockTest.java renamed to lib/src/test/java/com/auth0/jwt/ClockImplTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
package com.auth0.jwt;
22

3+
import com.auth0.jwt.interfaces.Clock;
34
import org.junit.Test;
45

56
import java.util.Date;
67

7-
import static org.hamcrest.Matchers.equalTo;
88
import static org.hamcrest.Matchers.is;
99
import static org.hamcrest.Matchers.notNullValue;
1010
import static org.junit.Assert.*;
1111

12-
public class ClockTest {
12+
public class ClockImplTest {
1313

1414
@Test
1515
public void shouldGetToday() throws Exception{
16-
Clock clock = new Clock();
16+
Clock clock = new ClockImpl();
1717
Date clockToday = clock.getToday();
1818
assertThat(clockToday, is(notNullValue()));
1919
}

lib/src/test/java/com/auth0/jwt/JWTTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.auth0.jwt;
22

33
import com.auth0.jwt.algorithms.Algorithm;
4+
import com.auth0.jwt.interfaces.Clock;
45
import com.auth0.jwt.interfaces.DecodedJWT;
56
import org.hamcrest.collection.IsCollectionWithSize;
67
import org.hamcrest.core.IsCollectionContaining;

lib/src/test/java/com/auth0/jwt/JWTVerifierTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.auth0.jwt.algorithms.Algorithm;
44
import com.auth0.jwt.exceptions.AlgorithmMismatchException;
55
import com.auth0.jwt.exceptions.InvalidClaimException;
6+
import com.auth0.jwt.interfaces.Clock;
67
import com.auth0.jwt.interfaces.DecodedJWT;
78
import org.junit.Rule;
89
import org.junit.Test;
@@ -195,7 +196,7 @@ public void shouldThrowOnInvalidCustomClaimValue() throws Exception {
195196
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjpbInNvbWV0aGluZyJdfQ.3ENLez6tU_fG0SVFrGmISltZPiXLSHaz_dyn-XFTEGQ";
196197
Map<String, Object> map = new HashMap<>();
197198
map.put("name", new Object());
198-
JWTVerifier verifier = new JWTVerifier(Algorithm.HMAC256("secret"), map, new Clock());
199+
JWTVerifier verifier = new JWTVerifier(Algorithm.HMAC256("secret"), map, new ClockImpl());
199200
verifier.verify(token);
200201
}
201202

0 commit comments

Comments
 (0)