Skip to content

Commit 2525498

Browse files
committed
rename delta to leeway
1 parent d8064d2 commit 2525498

File tree

3 files changed

+67
-64
lines changed

3 files changed

+67
-64
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,19 +125,19 @@ The JWT token may include DateNumber fields that can be used to validate that:
125125

126126
When verifying a token the time validation occurs automatically, resulting in a `JWTVerificationException` being throw when the values are invalid. If any of the previous fields are missing they won't be considered in this validation.
127127

128-
To specify a **delta window** or leeway in which the Token should still be considered valid, use the `acceptTimeDelta()` method in the `JWTVerifier` builder and pass a positive milliseconds value. This applies to every item listed above.
128+
To specify a **leeway window** in which the Token should still be considered valid, use the `acceptLeeway()` method in the `JWTVerifier` builder and pass a positive milliseconds value. This applies to every item listed above.
129129

130130
```java
131131
JWTVerifier verifier = JWT.require(Algorithm.RSA256(key))
132-
.acceptTimeDelta(100) //nbf, iat and exp
132+
.acceptLeeway(100) //nbf, iat and exp
133133
.build();
134134
```
135135

136136
You can also specify a custom value for a given Date claim and override the default one for only that claim.
137137

138138
```java
139139
JWTVerifier verifier = JWT.require(Algorithm.RSA256(key))
140-
.acceptTimeDelta(100) //nbf and iat
140+
.acceptLeeway(100) //nbf and iat
141141
.acceptExpiresAt(500) //exp
142142
.build();
143143
```

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

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

33
import com.auth0.jwt.algorithms.Algorithm;
4-
import com.auth0.jwt.exceptions.*;
4+
import com.auth0.jwt.exceptions.AlgorithmMismatchException;
5+
import com.auth0.jwt.exceptions.InvalidClaimException;
6+
import com.auth0.jwt.exceptions.JWTVerificationException;
7+
import com.auth0.jwt.exceptions.SignatureVerificationException;
58
import com.auth0.jwt.impl.PublicClaims;
69
import com.auth0.jwt.interfaces.Claim;
710
import org.apache.commons.codec.binary.Base64;
@@ -39,7 +42,7 @@ static JWTVerifier.Verification init(Algorithm algorithm) throws IllegalArgument
3942
static class Verification {
4043
private final Algorithm algorithm;
4144
private final Map<String, Object> claims;
42-
private long defaultDelta;
45+
private long defaultLeeway;
4346

4447
Verification(Algorithm algorithm) throws IllegalArgumentException {
4548
if (algorithm == null) {
@@ -48,7 +51,7 @@ static class Verification {
4851

4952
this.algorithm = algorithm;
5053
this.claims = new HashMap<>();
51-
this.defaultDelta = 0;
54+
this.defaultLeeway = 0;
5255
}
5356

5457
/**
@@ -86,65 +89,65 @@ public Verification withAudience(String... audience) {
8689

8790
/**
8891
* Define the default window in milliseconds in which the Not Before, Issued At and Expires At Claims will still be valid.
89-
* Setting a specific delta value on a given Claim will override this value for that Claim.
92+
* Setting a specific leeway value on a given Claim will override this value for that Claim.
9093
*
91-
* @param delta the window in milliseconds in which the Not Before, Issued At and Expires At Claims will still be valid.
94+
* @param leeway the window in milliseconds in which the Not Before, Issued At and Expires At Claims will still be valid.
9295
* @return this same Verification instance.
93-
* @throws IllegalArgumentException if delta is negative.
96+
* @throws IllegalArgumentException if leeway is negative.
9497
*/
95-
public Verification acceptTimeDelta(long delta) throws IllegalArgumentException {
96-
if (delta < 0) {
97-
throw new IllegalArgumentException("Delta value can't be negative.");
98+
public Verification acceptLeeway(long leeway) throws IllegalArgumentException {
99+
if (leeway < 0) {
100+
throw new IllegalArgumentException("Leeway value can't be negative.");
98101
}
99-
this.defaultDelta = delta;
102+
this.defaultLeeway = leeway;
100103
return this;
101104
}
102105

103106
/**
104-
* Set a specific delta window in milliseconds in which the Expires At ("exp") Claim will still be valid.
105-
* Expiration Date is always verified when the value is present. This method overrides the value set with acceptTimeDelta
107+
* Set a specific leeway window in milliseconds in which the Expires At ("exp") Claim will still be valid.
108+
* Expiration Date is always verified when the value is present. This method overrides the value set with acceptLeeway
106109
*
107-
* @param delta the window in milliseconds in which the Expires At Claim will still be valid.
110+
* @param leeway the window in milliseconds in which the Expires At Claim will still be valid.
108111
* @return this same Verification instance.
109-
* @throws IllegalArgumentException if delta is negative.
112+
* @throws IllegalArgumentException if leeway is negative.
110113
*/
111-
public Verification acceptExpiresAt(long delta) throws IllegalArgumentException {
112-
if (delta < 0) {
113-
throw new IllegalArgumentException("Delta value can't be negative.");
114+
public Verification acceptExpiresAt(long leeway) throws IllegalArgumentException {
115+
if (leeway < 0) {
116+
throw new IllegalArgumentException("Leeway value can't be negative.");
114117
}
115-
requireClaim(PublicClaims.EXPIRES_AT, delta);
118+
requireClaim(PublicClaims.EXPIRES_AT, leeway);
116119
return this;
117120
}
118121

119122
/**
120-
* Set a specific delta window in milliseconds in which the Not Before ("nbf") Claim will still be valid.
121-
* Not Before Date is always verified when the value is present. This method overrides the value set with acceptTimeDelta
123+
* Set a specific leeway window in milliseconds in which the Not Before ("nbf") Claim will still be valid.
124+
* Not Before Date is always verified when the value is present. This method overrides the value set with acceptLeeway
122125
*
123-
* @param delta the window in milliseconds in which the Not Before Claim will still be valid.
126+
* @param leeway the window in milliseconds in which the Not Before Claim will still be valid.
124127
* @return this same Verification instance.
125-
* @throws IllegalArgumentException if delta is negative.
128+
* @throws IllegalArgumentException if leeway is negative.
126129
*/
127-
public Verification acceptNotBefore(long delta) throws IllegalArgumentException {
128-
if (delta < 0) {
129-
throw new IllegalArgumentException("Delta value can't be negative.");
130+
public Verification acceptNotBefore(long leeway) throws IllegalArgumentException {
131+
if (leeway < 0) {
132+
throw new IllegalArgumentException("Leeway value can't be negative.");
130133
}
131-
requireClaim(PublicClaims.NOT_BEFORE, delta);
134+
requireClaim(PublicClaims.NOT_BEFORE, leeway);
132135
return this;
133136
}
134137

135138
/**
136-
* Set a specific delta window in milliseconds in which the Issued At ("iat") Claim will still be valid.
137-
* Issued At Date is always verified when the value is present. This method overrides the value set with acceptTimeDelta
139+
* Set a specific leeway window in milliseconds in which the Issued At ("iat") Claim will still be valid.
140+
* Issued At Date is always verified when the value is present. This method overrides the value set with acceptLeeway
138141
*
139-
* @param delta the window in milliseconds in which the Issued At Claim will still be valid.
142+
* @param leeway the window in milliseconds in which the Issued At Claim will still be valid.
140143
* @return this same Verification instance.
141-
* @throws IllegalArgumentException if delta is negative.
144+
* @throws IllegalArgumentException if leeway is negative.
142145
*/
143-
public Verification acceptIssuedAt(long delta) throws IllegalArgumentException {
144-
if (delta < 0) {
145-
throw new IllegalArgumentException("Delta value can't be negative.");
146+
public Verification acceptIssuedAt(long leeway) throws IllegalArgumentException {
147+
if (leeway < 0) {
148+
throw new IllegalArgumentException("Leeway value can't be negative.");
146149
}
147-
requireClaim(PublicClaims.ISSUED_AT, delta);
150+
requireClaim(PublicClaims.ISSUED_AT, leeway);
148151
return this;
149152
}
150153

@@ -204,13 +207,13 @@ JWTVerifier build(Clock clock) {
204207

205208
private void addDeltaToDateClaims() {
206209
if (!claims.containsKey(PublicClaims.EXPIRES_AT)) {
207-
claims.put(PublicClaims.EXPIRES_AT, defaultDelta);
210+
claims.put(PublicClaims.EXPIRES_AT, defaultLeeway);
208211
}
209212
if (!claims.containsKey(PublicClaims.NOT_BEFORE)) {
210-
claims.put(PublicClaims.NOT_BEFORE, defaultDelta);
213+
claims.put(PublicClaims.NOT_BEFORE, defaultLeeway);
211214
}
212215
if (!claims.containsKey(PublicClaims.ISSUED_AT)) {
213-
claims.put(PublicClaims.ISSUED_AT, defaultDelta);
216+
claims.put(PublicClaims.ISSUED_AT, defaultLeeway);
214217
}
215218
}
216219

@@ -307,16 +310,16 @@ private void assertValidStringClaim(String claimName, String value, String expec
307310
}
308311
}
309312

310-
private void assertValidDateClaim(Date date, long delta, boolean shouldBeFuture) {
313+
private void assertValidDateClaim(Date date, long leeway, boolean shouldBeFuture) {
311314
Date today = clock.getToday();
312315
boolean isValid;
313316
String errMessage;
314317
if (shouldBeFuture) {
315-
today.setTime(today.getTime() - delta);
318+
today.setTime(today.getTime() - leeway);
316319
isValid = date == null || !today.after(date);
317320
errMessage = String.format("The Token has expired on %s.", date);
318321
} else {
319-
today.setTime(today.getTime() + delta);
322+
today.setTime(today.getTime() + leeway);
320323
isValid = date == null || !today.before(date);
321324
errMessage = String.format("The Token can't be used before %s.", date);
322325
}

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public void shouldValidateCustomClaimOfTypeDate() throws Exception {
255255
// Generic Delta
256256
@SuppressWarnings("RedundantCast")
257257
@Test
258-
public void shouldAddDefaultTimeDeltaToDateClaims() throws Exception {
258+
public void shouldAddDefaultLeewayToDateClaims() throws Exception {
259259
Algorithm algorithm = mock(Algorithm.class);
260260
JWTVerifier verifier = JWTVerifier.init(algorithm)
261261
.build();
@@ -268,10 +268,10 @@ public void shouldAddDefaultTimeDeltaToDateClaims() throws Exception {
268268

269269
@SuppressWarnings("RedundantCast")
270270
@Test
271-
public void shouldAddCustomTimeDeltaToDateClaims() throws Exception {
271+
public void shouldAddCustomLeewayToDateClaims() throws Exception {
272272
Algorithm algorithm = mock(Algorithm.class);
273273
JWTVerifier verifier = JWTVerifier.init(algorithm)
274-
.acceptTimeDelta(1234L)
274+
.acceptLeeway(1234L)
275275
.build();
276276

277277
assertThat(verifier.claims, is(notNullValue()));
@@ -282,10 +282,10 @@ public void shouldAddCustomTimeDeltaToDateClaims() throws Exception {
282282

283283
@SuppressWarnings("RedundantCast")
284284
@Test
285-
public void shouldOverrideDefaultIssuedAtTimeDelta() throws Exception {
285+
public void shouldOverrideDefaultIssuedAtLeeway() throws Exception {
286286
Algorithm algorithm = mock(Algorithm.class);
287287
JWTVerifier verifier = JWTVerifier.init(algorithm)
288-
.acceptTimeDelta(1234L)
288+
.acceptLeeway(1234L)
289289
.acceptIssuedAt(9999L)
290290
.build();
291291

@@ -297,10 +297,10 @@ public void shouldOverrideDefaultIssuedAtTimeDelta() throws Exception {
297297

298298
@SuppressWarnings("RedundantCast")
299299
@Test
300-
public void shouldOverrideDefaultExpiresAtTimeDelta() throws Exception {
300+
public void shouldOverrideDefaultExpiresAtLeeway() throws Exception {
301301
Algorithm algorithm = mock(Algorithm.class);
302302
JWTVerifier verifier = JWTVerifier.init(algorithm)
303-
.acceptTimeDelta(1234L)
303+
.acceptLeeway(1234L)
304304
.acceptExpiresAt(9999L)
305305
.build();
306306

@@ -312,10 +312,10 @@ public void shouldOverrideDefaultExpiresAtTimeDelta() throws Exception {
312312

313313
@SuppressWarnings("RedundantCast")
314314
@Test
315-
public void shouldOverrideDefaultNotBeforeTimeDelta() throws Exception {
315+
public void shouldOverrideDefaultNotBeforeLeeway() throws Exception {
316316
Algorithm algorithm = mock(Algorithm.class);
317317
JWTVerifier verifier = JWTVerifier.init(algorithm)
318-
.acceptTimeDelta(1234L)
318+
.acceptLeeway(1234L)
319319
.acceptNotBefore(9999L)
320320
.build();
321321

@@ -326,17 +326,17 @@ public void shouldOverrideDefaultNotBeforeTimeDelta() throws Exception {
326326
}
327327

328328
@Test
329-
public void shouldThrowOnNegativeCustomTimeDelta() throws Exception {
329+
public void shouldThrowOnNegativeCustomLeeway() throws Exception {
330330
exception.expect(IllegalArgumentException.class);
331-
exception.expectMessage("Delta value can't be negative.");
331+
exception.expectMessage("Leeway value can't be negative.");
332332
Algorithm algorithm = mock(Algorithm.class);
333333
JWTVerifier.init(algorithm)
334-
.acceptTimeDelta(-1);
334+
.acceptLeeway(-1);
335335
}
336336

337337
// Expires At
338338
@Test
339-
public void shouldValidateExpiresAtWithDelta() throws Exception {
339+
public void shouldValidateExpiresAtWithLeeway() throws Exception {
340340
Clock clock = mock(Clock.class);
341341
when(clock.getToday()).thenReturn(new Date(DATE_TOKEN_MS_VALUE + 299));
342342

@@ -376,17 +376,17 @@ public void shouldThrowOnInvalidExpiresAtIfPresent() throws Exception {
376376
}
377377

378378
@Test
379-
public void shouldThrowOnNegativeExpiresAtDelta() throws Exception {
379+
public void shouldThrowOnNegativeExpiresAtLeeway() throws Exception {
380380
exception.expect(IllegalArgumentException.class);
381-
exception.expectMessage("Delta value can't be negative.");
381+
exception.expectMessage("Leeway value can't be negative.");
382382
Algorithm algorithm = mock(Algorithm.class);
383383
JWTVerifier.init(algorithm)
384384
.acceptExpiresAt(-1);
385385
}
386386

387387
// Not before
388388
@Test
389-
public void shouldValidateNotBeforeWithDelta() throws Exception {
389+
public void shouldValidateNotBeforeWithLeeway() throws Exception {
390390
Clock clock = mock(Clock.class);
391391
when(clock.getToday()).thenReturn(new Date(DATE_TOKEN_MS_VALUE - 299));
392392

@@ -426,17 +426,17 @@ public void shouldValidateNotBeforeIfPresent() throws Exception {
426426
}
427427

428428
@Test
429-
public void shouldThrowOnNegativeNotBeforeDelta() throws Exception {
429+
public void shouldThrowOnNegativeNotBeforeLeeway() throws Exception {
430430
exception.expect(IllegalArgumentException.class);
431-
exception.expectMessage("Delta value can't be negative.");
431+
exception.expectMessage("Leeway value can't be negative.");
432432
Algorithm algorithm = mock(Algorithm.class);
433433
JWTVerifier.init(algorithm)
434434
.acceptNotBefore(-1);
435435
}
436436

437437
// Issued At
438438
@Test
439-
public void shouldValidateIssuedAtWithDelta() throws Exception {
439+
public void shouldValidateIssuedAtWithLeeway() throws Exception {
440440
Clock clock = mock(Clock.class);
441441
when(clock.getToday()).thenReturn(new Date(DATE_TOKEN_MS_VALUE - 299));
442442

@@ -476,9 +476,9 @@ public void shouldValidateIssuedAtIfPresent() throws Exception {
476476
}
477477

478478
@Test
479-
public void shouldThrowOnNegativeIssuedAtDelta() throws Exception {
479+
public void shouldThrowOnNegativeIssuedAtLeeway() throws Exception {
480480
exception.expect(IllegalArgumentException.class);
481-
exception.expectMessage("Delta value can't be negative.");
481+
exception.expectMessage("Leeway value can't be negative.");
482482
Algorithm algorithm = mock(Algorithm.class);
483483
JWTVerifier.init(algorithm)
484484
.acceptIssuedAt(-1);

0 commit comments

Comments
 (0)