Skip to content

Commit 1216a0b

Browse files
several improvements
1 parent 12e9c07 commit 1216a0b

File tree

10 files changed

+106
-83
lines changed

10 files changed

+106
-83
lines changed

src/main/java/com/bastiaanjansen/jwt/Claims.java

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
import com.bastiaanjansen.jwt.utils.Base64Utils;
44
import org.json.JSONObject;
55

6-
import java.util.Arrays;
7-
import java.util.Date;
8-
import java.util.HashMap;
9-
import java.util.Map;
6+
import java.util.*;
7+
import java.util.stream.Collectors;
108

119
public class Claims {
12-
private final String[] registeredDateClaims = { Payload.Registered.EXPIRATION_TIME, Payload.Registered.ISSUED_AT, Payload.Registered.NOT_BEFORE };
10+
private final Registered[] registeredDateClaims = { Registered.EXPIRATION_TIME, Registered.ISSUED_AT, Registered.NOT_BEFORE };
1311
protected final Map<String, Object> claims;
1412

1513
protected Claims() {
@@ -37,14 +35,18 @@ public String base64Encoded() {
3735
* @param <T> type of the claim
3836
* @return claim value cast to specified type
3937
*/
38+
@SuppressWarnings("unchecked")
4039
public <T> T getClaim(String name, Class<T> type) {
4140
Object value = claims.get(name);
4241

43-
boolean isDateClaim = Arrays.asList(registeredDateClaims).contains(name);
42+
boolean isDateClaim = Arrays.stream(registeredDateClaims)
43+
.map(Claims.Registered::getValue)
44+
.collect(Collectors.toList())
45+
.contains(name);
4446

4547
if (isDateClaim) {
4648
long millisSinceEpoch = Long.parseLong(String.valueOf(value));
47-
value = new Date(millisSinceEpoch);
49+
return (T) new Date(millisSinceEpoch);
4850
}
4951

5052
return type.cast(value);
@@ -60,4 +62,27 @@ public void addClaim(String name, Object value) {
6062
if (value == null) throw new IllegalArgumentException("value cannot be null");
6163
claims.put(name, value);
6264
}
65+
66+
public enum Registered {
67+
ISSUER("iss"),
68+
SUBJECT("sub"),
69+
AUDIENCE("aud"),
70+
EXPIRATION_TIME("exp"),
71+
NOT_BEFORE("nbf"),
72+
ISSUED_AT("iat"),
73+
JWT_ID("jti"),
74+
TYPE("typ"),
75+
CONTENT_TYPE("cty"),
76+
ALGORITHM("alg");
77+
78+
private final String value;
79+
80+
Registered(String value) {
81+
this.value = value;
82+
}
83+
84+
public String getValue() {
85+
return value;
86+
}
87+
}
6388
}

src/main/java/com/bastiaanjansen/jwt/DefaultJWTValidator.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ private void verifyPayload(Payload payload) throws JWTValidationException {
6868

6969
private void validateNotBefore(Payload payload, Date currentDate) throws JWTValidationException {
7070
// Checks that if the not-before (nbf) claim is set, the current date is after or equal to the not-before date.
71-
if (payload.containsClaim(Payload.Registered.NOT_BEFORE)) {
71+
if (payload.containsClaim(Claims.Registered.NOT_BEFORE.getValue())) {
7272
Date notBefore = payload.getNotBefore();
7373
if (currentDate.getTime() <= notBefore.getTime())
7474
throw new JWTValidationException("JWT is only valid after " + notBefore);
7575
}
7676
}
7777

7878
private void validateExpirationTime(Payload payload, Date currentDate) throws JWTExpiredException {
79-
if (payload.containsClaim(Payload.Registered.EXPIRATION_TIME)) {
79+
if (payload.containsClaim(Claims.Registered.EXPIRATION_TIME.getValue())) {
8080
Date expirationTime = payload.getExpirationTime();
8181
if (currentDate.getTime() > expirationTime.getTime())
8282
throw new JWTExpiredException("JWT expired on " + expirationTime);
@@ -93,32 +93,32 @@ public Builder() {
9393
}
9494

9595
public Builder withType(String type) {
96-
withHeader(Header.Registered.TYPE, type::equals);
96+
withHeader(Claims.Registered.TYPE.getValue(), type::equals);
9797
return this;
9898
}
9999

100100
public Builder withContentType(String type) {
101-
withHeader(Header.Registered.CONTENT_TYPE, type::equals);
101+
withHeader(Claims.Registered.CONTENT_TYPE.getValue(), type::equals);
102102
return this;
103103
}
104104

105105
public Builder withAlgorithm(String algorithm) {
106-
withHeader(Header.Registered.ALGORITHM, algorithm::equals);
106+
withHeader(Claims.Registered.ALGORITHM.getValue(), algorithm::equals);
107107
return this;
108108
}
109109

110110
public Builder withIssuer(String issuer) {
111-
withClaim(Payload.Registered.ISSUER, issuer::equals);
111+
withClaim(Claims.Registered.ISSUER.getValue(), issuer::equals);
112112
return this;
113113
}
114114

115115
public Builder withSubject(String subject) {
116-
withClaim(Payload.Registered.SUBJECT, subject::equals);
116+
withClaim(Claims.Registered.SUBJECT.getValue(), subject::equals);
117117
return this;
118118
}
119119

120120
public Builder withOneOfAudience(String... audience) {
121-
withClaim(Payload.Registered.AUDIENCE, value -> {
121+
withClaim(Claims.Registered.AUDIENCE.getValue(), value -> {
122122
for (String audienceItem: audience) {
123123
if (Arrays.asList((Object[]) value).contains(audienceItem))
124124
return true;
@@ -130,7 +130,7 @@ public Builder withOneOfAudience(String... audience) {
130130
}
131131

132132
public Builder withAllOfAudience(String... audience) {
133-
withClaim(Payload.Registered.AUDIENCE, value -> {
133+
withClaim(Claims.Registered.AUDIENCE.getValue(), value -> {
134134
String[] values = (String[]) value;
135135
return Arrays.asList(values).containsAll(Arrays.asList(audience));
136136
});
@@ -139,37 +139,37 @@ public Builder withAllOfAudience(String... audience) {
139139
}
140140

141141
public Builder withExpirationTime(Date expirationTime) {
142-
withClaim(Payload.Registered.EXPIRATION_TIME, value -> value.equals(expirationTime.getTime()));
142+
withClaim(Claims.Registered.EXPIRATION_TIME.getValue(), value -> value.equals(expirationTime.getTime()));
143143
return this;
144144
}
145145

146146
public Builder withExpirationTime(long timeSinceEpoch) {
147-
withClaim(Payload.Registered.EXPIRATION_TIME, value -> value.equals(timeSinceEpoch));
147+
withClaim(Claims.Registered.EXPIRATION_TIME.getValue(), value -> value.equals(timeSinceEpoch));
148148
return this;
149149
}
150150

151151
public Builder withNotBefore(Date notBefore) {
152-
withClaim(Payload.Registered.NOT_BEFORE, value -> value.equals(notBefore.getTime()));
152+
withClaim(Claims.Registered.NOT_BEFORE.getValue(), value -> value.equals(notBefore.getTime()));
153153
return this;
154154
}
155155

156156
public Builder withNotBefore(long timeSinceEpoch) {
157-
withClaim(Payload.Registered.NOT_BEFORE, value -> value.equals(timeSinceEpoch));
157+
withClaim(Claims.Registered.NOT_BEFORE.getValue(), value -> value.equals(timeSinceEpoch));
158158
return this;
159159
}
160160

161161
public Builder withIssuedAt(Date issuedAt) {
162-
withClaim(Payload.Registered.ISSUED_AT, value -> value.equals(issuedAt.getTime()));
162+
withClaim(Claims.Registered.ISSUED_AT.getValue(), value -> value.equals(issuedAt.getTime()));
163163
return this;
164164
}
165165

166166
public Builder withIssuedAt(long timeSinceEpoch) {
167-
withClaim(Payload.Registered.ISSUED_AT, value -> value.equals(timeSinceEpoch));
167+
withClaim(Claims.Registered.ISSUED_AT.getValue(), value -> value.equals(timeSinceEpoch));
168168
return this;
169169
}
170170

171171
public Builder withID(String id) {
172-
withClaim(Payload.Registered.JWT_ID, id::equals);
172+
withClaim(Claims.Registered.JWT_ID.getValue(), id::equals);
173173
return this;
174174
}
175175

src/main/java/com/bastiaanjansen/jwt/Header.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@
55

66
import java.util.Map;
77

8-
public class Header extends Claims {
9-
public static class Registered {
10-
static String TYPE = "typ";
11-
static String CONTENT_TYPE = "cty";
12-
static String ALGORITHM = "alg";
13-
}
8+
public final class Header extends Claims {
149

1510
public Header() {
1611
setType("JWT");
@@ -28,26 +23,26 @@ public static Header fromBase64EncodedJSON(String encodedJSON) {
2823
}
2924

3025
public void setType(String type) {
31-
addClaim(Registered.TYPE, type);
26+
addClaim(Registered.TYPE.getValue(), type);
3227
}
3328

3429
public String getType() {
35-
return getClaim(Registered.TYPE, String.class);
30+
return getClaim(Registered.TYPE.getValue(), String.class);
3631
}
3732

3833
public void setContentType(String value) {
39-
addClaim(Registered.CONTENT_TYPE, value);
34+
addClaim(Registered.CONTENT_TYPE.getValue(), value);
4035
}
4136

4237
public String getContentType() {
43-
return getClaim(Registered.CONTENT_TYPE, String.class);
38+
return getClaim(Registered.CONTENT_TYPE.getValue(), String.class);
4439
}
4540

4641
public void setAlgorithm(String algorithm) {
47-
addClaim(Registered.ALGORITHM, algorithm);
42+
addClaim(Registered.ALGORITHM.getValue(), algorithm);
4843
}
4944

5045
public String getAlgorithm() {
51-
return getClaim(Registered.ALGORITHM, String.class);
46+
return getClaim(Registered.ALGORITHM.getValue(), String.class);
5247
}
5348
}

src/main/java/com/bastiaanjansen/jwt/JWT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
*
1818
* @author Bastiaan Jansen
1919
*/
20-
public class JWT {
20+
public final class JWT {
2121

22-
private final static int NUMBER_OF_SEGMENTS = 3;
22+
private static final int NUMBER_OF_SEGMENTS = 3;
2323

2424
private final Algorithm algorithm;
2525
private final Header header;
@@ -360,7 +360,7 @@ public Builder withClaim(Map<String, ?> claims) {
360360
* @throws JWTCreationException when the JWT could not be created
361361
*/
362362
public String sign() throws JWTCreationException {
363-
if (!header.containsClaim(Header.Registered.TYPE))
363+
if (!header.containsClaim(Claims.Registered.TYPE.getValue()))
364364
withType("JWT");
365365
return new JWT(this).sign();
366366
}

src/main/java/com/bastiaanjansen/jwt/Payload.java

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,7 @@
66
import java.util.Date;
77
import java.util.Map;
88

9-
public class Payload extends Claims {
10-
public static class Registered {
11-
static String ISSUER = "iss";
12-
static String SUBJECT = "sub";
13-
static String AUDIENCE = "aud";
14-
static String EXPIRATION_TIME = "exp";
15-
static String NOT_BEFORE = "nbf";
16-
static String ISSUED_AT = "iat";
17-
static String JWT_ID = "jti";
18-
}
9+
public final class Payload extends Claims {
1910

2011
public Payload() {}
2112

@@ -30,27 +21,27 @@ public static Payload fromBase64EncodedJSON(String encodedJSON) {
3021
}
3122

3223
public void setIssuer(String issuer) {
33-
addClaim(Registered.ISSUER, issuer);
24+
addClaim(Registered.ISSUER.getValue(), issuer);
3425
}
3526

3627
public String getIssuer() {
37-
return getClaim(Registered.ISSUER, String.class);
28+
return getClaim(Registered.ISSUER.getValue(), String.class);
3829
}
3930

4031
public void setSubject(String subject) {
41-
addClaim(Registered.SUBJECT, subject);
32+
addClaim(Registered.SUBJECT.getValue(), subject);
4233
}
4334

4435
public String getSubject() {
45-
return getClaim(Registered.SUBJECT, String.class);
36+
return getClaim(Claims.Registered.SUBJECT.getValue(), String.class);
4637
}
4738

4839
public void setAudience(String... audience) {
49-
addClaim(Registered.AUDIENCE, audience);
40+
addClaim(Registered.AUDIENCE.getValue(), audience);
5041
}
5142

5243
public String[] getAudience() {
53-
Object audience = getClaim(Registered.AUDIENCE, Object.class);
44+
Object audience = getClaim(Registered.AUDIENCE.getValue(), Object.class);
5445

5546
if (!(audience instanceof Object[]))
5647
return new String[] {(String) audience};
@@ -59,46 +50,46 @@ public String[] getAudience() {
5950
}
6051

6152
public void setExpirationTime(long timeSinceEpoch) {
62-
addClaim(Registered.EXPIRATION_TIME, timeSinceEpoch);
53+
addClaim(Registered.EXPIRATION_TIME.getValue(), timeSinceEpoch);
6354
}
6455

6556
public void setExpirationTime(Date expirationTime) {
6657
setExpirationTime(expirationTime.getTime());
6758
}
6859

6960
public Date getExpirationTime() {
70-
return getClaim(Registered.EXPIRATION_TIME, Date.class);
61+
return getClaim(Registered.EXPIRATION_TIME.getValue(), Date.class);
7162
}
7263

7364
public void setNotBefore(long timeSinceEpoch) {
74-
addClaim(Registered.NOT_BEFORE, timeSinceEpoch);
65+
addClaim(Registered.NOT_BEFORE.getValue(), timeSinceEpoch);
7566
}
7667

7768
public void setNotBefore(Date notBefore) {
7869
setNotBefore(notBefore.getTime());
7970
}
8071

8172
public Date getNotBefore() {
82-
return getClaim(Registered.NOT_BEFORE, Date.class);
73+
return getClaim(Registered.NOT_BEFORE.getValue(), Date.class);
8374
}
8475

8576
public void setIssuedAt(long timeSinceEpoch) {
86-
addClaim(Registered.ISSUED_AT, timeSinceEpoch);
77+
addClaim(Registered.ISSUED_AT.getValue(), timeSinceEpoch);
8778
}
8879

8980
public void setIssuedAt(Date issuedAt) {
9081
setIssuedAt(issuedAt.getTime());
9182
}
9283

9384
public Date getIssuedAt() {
94-
return getClaim(Registered.ISSUED_AT, Date.class);
85+
return getClaim(Registered.ISSUED_AT.getValue(), Date.class);
9586
}
9687

9788
public void setID(String id) {
98-
addClaim(Registered.JWT_ID, id);
89+
addClaim(Registered.JWT_ID.getValue(), id);
9990
}
10091

10192
public String getID() {
102-
return getClaim(Registered.JWT_ID, String.class);
93+
return getClaim(Registered.JWT_ID.getValue(), String.class);
10394
}
10495
}

src/main/java/com/bastiaanjansen/jwt/algorithms/HMACAlgorithm.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ public byte[] sign(String data) throws JWTSignException {
2727
@Override
2828
public byte[] sign(byte[] data) throws JWTSignException {
2929
try {
30-
Mac HMAC = Mac.getInstance(description);
30+
Mac mac = Mac.getInstance(description);
3131

3232
SecretKeySpec secretKey = new SecretKeySpec(secret, description);
33-
HMAC.init(secretKey);
33+
mac.init(secretKey);
3434

35-
return HMAC.doFinal(data);
35+
return mac.doFinal(data);
3636
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
3737
throw new JWTSignException(e.getMessage());
3838
}
@@ -48,6 +48,4 @@ public boolean verify(byte[] data, byte[] expected) throws JWTValidationExceptio
4848
throw new JWTValidationException(e.getMessage());
4949
}
5050
}
51-
52-
5351
}

src/main/java/com/bastiaanjansen/jwt/utils/Base64Utils.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
*/
1111
public class Base64Utils {
1212

13+
private Base64Utils() {}
14+
1315
/**
1416
* Encode string to base64URL string
1517
*

0 commit comments

Comments
 (0)