Skip to content

Commit 72bceb9

Browse files
committed
change base class of JWTDecoder
1 parent 5c9bcc5 commit 72bceb9

File tree

5 files changed

+32
-29
lines changed

5 files changed

+32
-29
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import com.auth0.jwt.interfaces.Verification;
77

88
@SuppressWarnings("WeakerAccess")
9-
public abstract class JWT implements DecodedJWT {
9+
public abstract class JWT {
1010

1111
/**
1212
* Decode a given Json Web Token.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.auth0.jwt.exceptions.JWTDecodeException;
44
import com.auth0.jwt.impl.JWTParser;
55
import com.auth0.jwt.interfaces.Claim;
6+
import com.auth0.jwt.interfaces.DecodedJWT;
67
import com.auth0.jwt.interfaces.Header;
78
import com.auth0.jwt.interfaces.Payload;
89
import org.apache.commons.codec.binary.Base64;
@@ -16,7 +17,7 @@
1617
* The JWTDecoder class holds the decode method to parse a given JWT token into it's JWT representation.
1718
*/
1819
@SuppressWarnings("WeakerAccess")
19-
final class JWTDecoder extends JWT {
20+
final class JWTDecoder implements DecodedJWT {
2021

2122
private final String token;
2223
private Header header;
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.auth0.jwt.exceptions;
22

33
public class TokenExpiredException extends JWTVerificationException {
4-
5-
private static final long serialVersionUID = -7076928975713577708L;
64

7-
public TokenExpiredException(String message) {
8-
super(message);
9-
}
5+
private static final long serialVersionUID = -7076928975713577708L;
6+
7+
public TokenExpiredException(String message) {
8+
super(message);
9+
}
1010
}

lib/src/main/java/com/auth0/jwt/impl/JsonNodeClaim.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ public Integer asInt() {
3636
}
3737

3838
@Override
39-
public Long asLong() { return !data.isNumber() ? null : data.asLong(); }
39+
public Long asLong() {
40+
return !data.isNumber() ? null : data.asLong();
41+
}
4042

4143
@Override
4244
public Double asDouble() {

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class JWTDecoderTest {
2525

2626
@Test
2727
public void getSubject() throws Exception {
28-
DecodedJWT jwt = JWTDecoder.decode("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ");
28+
DecodedJWT jwt = JWT.decode("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ");
2929
assertThat(jwt.getSubject(), is(notNullValue()));
3030
assertThat(jwt.getSubject(), is("1234567890"));
3131
}
@@ -35,14 +35,14 @@ public void getSubject() throws Exception {
3535
public void shouldThrowIfLessThan3Parts() throws Exception {
3636
exception.expect(JWTDecodeException.class);
3737
exception.expectMessage("The token was expected to have 3 parts, but got 2.");
38-
JWTDecoder.decode("two.parts");
38+
JWT.decode("two.parts");
3939
}
4040

4141
@Test
4242
public void shouldThrowIfMoreThan3Parts() throws Exception {
4343
exception.expect(JWTDecodeException.class);
4444
exception.expectMessage("The token was expected to have 3 parts, but got 4.");
45-
JWTDecoder.decode("this.has.four.parts");
45+
JWT.decode("this.has.four.parts");
4646
}
4747

4848
@Test
@@ -66,7 +66,7 @@ public void shouldThrowIfHeaderHasInvalidJSONFormat() throws Exception {
6666
// getToken
6767
@Test
6868
public void shouldGetStringToken() throws Exception {
69-
DecodedJWT jwt = JWTDecoder.decode("eyJhbGciOiJIUzI1NiJ9.e30.XmNK3GpH3Ys_7wsYBfq4C3M6goz71I7dTgUkuIa5lyQ");
69+
DecodedJWT jwt = JWT.decode("eyJhbGciOiJIUzI1NiJ9.e30.XmNK3GpH3Ys_7wsYBfq4C3M6goz71I7dTgUkuIa5lyQ");
7070
assertThat(jwt, is(notNullValue()));
7171
assertThat(jwt.getToken(), is(notNullValue()));
7272
assertThat(jwt.getToken(), is("eyJhbGciOiJIUzI1NiJ9.e30.XmNK3GpH3Ys_7wsYBfq4C3M6goz71I7dTgUkuIa5lyQ"));
@@ -76,14 +76,14 @@ public void shouldGetStringToken() throws Exception {
7676

7777
@Test
7878
public void shouldGetAlgorithm() throws Exception {
79-
DecodedJWT jwt = JWTDecoder.decode("eyJhbGciOiJIUzI1NiJ9.e30.XmNK3GpH3Ys_7wsYBfq4C3M6goz71I7dTgUkuIa5lyQ");
79+
DecodedJWT jwt = JWT.decode("eyJhbGciOiJIUzI1NiJ9.e30.XmNK3GpH3Ys_7wsYBfq4C3M6goz71I7dTgUkuIa5lyQ");
8080
assertThat(jwt, is(notNullValue()));
8181
assertThat(jwt.getAlgorithm(), is("HS256"));
8282
}
8383

8484
@Test
8585
public void shouldGetSignature() throws Exception {
86-
DecodedJWT jwt = JWTDecoder.decode("eyJhbGciOiJIUzI1NiJ9.e30.XmNK3GpH3Ys_7wsYBfq4C3M6goz71I7dTgUkuIa5lyQ");
86+
DecodedJWT jwt = JWT.decode("eyJhbGciOiJIUzI1NiJ9.e30.XmNK3GpH3Ys_7wsYBfq4C3M6goz71I7dTgUkuIa5lyQ");
8787
assertThat(jwt, is(notNullValue()));
8888
assertThat(jwt.getSignature(), is("XmNK3GpH3Ys_7wsYBfq4C3M6goz71I7dTgUkuIa5lyQ"));
8989
}
@@ -92,37 +92,37 @@ public void shouldGetSignature() throws Exception {
9292

9393
@Test
9494
public void shouldGetIssuer() throws Exception {
95-
DecodedJWT jwt = JWTDecoder.decode("eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJKb2huIERvZSJ9.SgXosfRR_IwCgHq5lF3tlM-JHtpucWCRSaVuoHTbWbQ");
95+
DecodedJWT jwt = JWT.decode("eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJKb2huIERvZSJ9.SgXosfRR_IwCgHq5lF3tlM-JHtpucWCRSaVuoHTbWbQ");
9696
assertThat(jwt, is(notNullValue()));
9797
assertThat(jwt.getIssuer(), is("John Doe"));
9898
}
9999

100100
@Test
101101
public void shouldGetSubject() throws Exception {
102-
DecodedJWT jwt = JWTDecoder.decode("eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJUb2szbnMifQ.RudAxkslimoOY3BLl2Ghny3BrUKu9I1ZrXzCZGDJtNs");
102+
DecodedJWT jwt = JWT.decode("eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJUb2szbnMifQ.RudAxkslimoOY3BLl2Ghny3BrUKu9I1ZrXzCZGDJtNs");
103103
assertThat(jwt, is(notNullValue()));
104104
assertThat(jwt.getSubject(), is("Tok3ns"));
105105
}
106106

107107
@Test
108108
public void shouldGetArrayAudience() throws Exception {
109-
DecodedJWT jwt = JWTDecoder.decode("eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOlsiSG9wZSIsIlRyYXZpcyIsIlNvbG9tb24iXX0.Tm4W8WnfPjlmHSmKFakdij0on2rWPETpoM7Sh0u6-S4");
109+
DecodedJWT jwt = JWT.decode("eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOlsiSG9wZSIsIlRyYXZpcyIsIlNvbG9tb24iXX0.Tm4W8WnfPjlmHSmKFakdij0on2rWPETpoM7Sh0u6-S4");
110110
assertThat(jwt, is(notNullValue()));
111111
assertThat(jwt.getAudience(), is(IsCollectionWithSize.hasSize(3)));
112112
assertThat(jwt.getAudience(), is(IsCollectionContaining.hasItems("Hope", "Travis", "Solomon")));
113113
}
114114

115115
@Test
116116
public void shouldGetStringAudience() throws Exception {
117-
DecodedJWT jwt = JWTDecoder.decode("eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJKYWNrIFJleWVzIn0.a4I9BBhPt1OB1GW67g2P1bEHgi6zgOjGUL4LvhE9Dgc");
117+
DecodedJWT jwt = JWT.decode("eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJKYWNrIFJleWVzIn0.a4I9BBhPt1OB1GW67g2P1bEHgi6zgOjGUL4LvhE9Dgc");
118118
assertThat(jwt, is(notNullValue()));
119119
assertThat(jwt.getAudience(), is(IsCollectionWithSize.hasSize(1)));
120120
assertThat(jwt.getAudience(), is(IsCollectionContaining.hasItems("Jack Reyes")));
121121
}
122122

123123
@Test
124124
public void shouldGetExpirationTime() throws Exception {
125-
DecodedJWT jwt = JWTDecoder.decode("eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0NzY3MjcwODZ9.L9dcPHEDQew2u9MkDCORFkfDGcSOsgoPqNY-LUMLEHg");
125+
DecodedJWT jwt = JWT.decode("eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0NzY3MjcwODZ9.L9dcPHEDQew2u9MkDCORFkfDGcSOsgoPqNY-LUMLEHg");
126126
assertThat(jwt, is(notNullValue()));
127127
assertThat(jwt.getExpiresAt(), is(instanceOf(Date.class)));
128128
long ms = 1476727086L * 1000;
@@ -133,7 +133,7 @@ public void shouldGetExpirationTime() throws Exception {
133133

134134
@Test
135135
public void shouldGetNotBefore() throws Exception {
136-
DecodedJWT jwt = JWTDecoder.decode("eyJhbGciOiJIUzI1NiJ9.eyJuYmYiOjE0NzY3MjcwODZ9.tkpD3iCPQPVqjnjpDVp2bJMBAgpVCG9ZjlBuMitass0");
136+
DecodedJWT jwt = JWT.decode("eyJhbGciOiJIUzI1NiJ9.eyJuYmYiOjE0NzY3MjcwODZ9.tkpD3iCPQPVqjnjpDVp2bJMBAgpVCG9ZjlBuMitass0");
137137
assertThat(jwt, is(notNullValue()));
138138
assertThat(jwt.getNotBefore(), is(instanceOf(Date.class)));
139139
long ms = 1476727086L * 1000;
@@ -144,7 +144,7 @@ public void shouldGetNotBefore() throws Exception {
144144

145145
@Test
146146
public void shouldGetIssuedAt() throws Exception {
147-
DecodedJWT jwt = JWTDecoder.decode("eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE0NzY3MjcwODZ9.KPjGoW665E8V5_27Jugab8qSTxLk2cgquhPCBfAP0_w");
147+
DecodedJWT jwt = JWT.decode("eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE0NzY3MjcwODZ9.KPjGoW665E8V5_27Jugab8qSTxLk2cgquhPCBfAP0_w");
148148
assertThat(jwt, is(notNullValue()));
149149
assertThat(jwt.getIssuedAt(), is(instanceOf(Date.class)));
150150
long ms = 1476727086L * 1000;
@@ -155,21 +155,21 @@ public void shouldGetIssuedAt() throws Exception {
155155

156156
@Test
157157
public void shouldGetId() throws Exception {
158-
DecodedJWT jwt = JWTDecoder.decode("eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIxMjM0NTY3ODkwIn0.m3zgEfVUFOd-CvL3xG5BuOWLzb0zMQZCqiVNQQOPOvA");
158+
DecodedJWT jwt = JWT.decode("eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIxMjM0NTY3ODkwIn0.m3zgEfVUFOd-CvL3xG5BuOWLzb0zMQZCqiVNQQOPOvA");
159159
assertThat(jwt, is(notNullValue()));
160160
assertThat(jwt.getId(), is("1234567890"));
161161
}
162162

163163
@Test
164164
public void shouldGetContentType() throws Exception {
165-
DecodedJWT jwt = JWTDecoder.decode("eyJhbGciOiJIUzI1NiIsImN0eSI6ImF3ZXNvbWUifQ.e30.AIm-pJDOaAyct9qKMlN-lQieqNDqc3d4erqUZc5SHAs");
165+
DecodedJWT jwt = JWT.decode("eyJhbGciOiJIUzI1NiIsImN0eSI6ImF3ZXNvbWUifQ.e30.AIm-pJDOaAyct9qKMlN-lQieqNDqc3d4erqUZc5SHAs");
166166
assertThat(jwt, is(notNullValue()));
167167
assertThat(jwt.getContentType(), is("awesome"));
168168
}
169169

170170
@Test
171171
public void shouldGetType() throws Exception {
172-
DecodedJWT jwt = JWTDecoder.decode("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.e30.WdFmrzx8b9v_a-r6EHC2PTAaWywgm_8LiP8RBRhYwkI");
172+
DecodedJWT jwt = JWT.decode("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.e30.WdFmrzx8b9v_a-r6EHC2PTAaWywgm_8LiP8RBRhYwkI");
173173
assertThat(jwt, is(notNullValue()));
174174
assertThat(jwt.getType(), is("JWS"));
175175
}
@@ -178,23 +178,23 @@ public void shouldGetType() throws Exception {
178178

179179
@Test
180180
public void shouldGetMissingClaimIfClaimDoesNotExist() throws Exception {
181-
DecodedJWT jwt = JWTDecoder.decode("eyJhbGciOiJIUzI1NiJ9.e30.K17vlwhE8FCMShdl1_65jEYqsQqBOVMPUU9IgG-QlTM");
181+
DecodedJWT jwt = JWT.decode("eyJhbGciOiJIUzI1NiJ9.e30.K17vlwhE8FCMShdl1_65jEYqsQqBOVMPUU9IgG-QlTM");
182182
assertThat(jwt, is(notNullValue()));
183183
assertThat(jwt.getClaim("notExisting"), is(notNullValue()));
184184
assertThat(jwt.getClaim("notExisting"), is(instanceOf(NullClaim.class)));
185185
}
186186

187187
@Test
188188
public void shouldGetValidClaim() throws Exception {
189-
DecodedJWT jwt = JWTDecoder.decode("eyJhbGciOiJIUzI1NiJ9.eyJvYmplY3QiOnsibmFtZSI6ImpvaG4ifX0.lrU1gZlOdlmTTeZwq0VI-pZx2iV46UWYd5-lCjy6-c4");
189+
DecodedJWT jwt = JWT.decode("eyJhbGciOiJIUzI1NiJ9.eyJvYmplY3QiOnsibmFtZSI6ImpvaG4ifX0.lrU1gZlOdlmTTeZwq0VI-pZx2iV46UWYd5-lCjy6-c4");
190190
assertThat(jwt, is(notNullValue()));
191191
assertThat(jwt.getClaim("object"), is(notNullValue()));
192192
assertThat(jwt.getClaim("object"), is(instanceOf(Claim.class)));
193193
}
194194

195195
@Test
196196
public void shouldNotGetNullClaimIfClaimIsEmptyObject() throws Exception {
197-
DecodedJWT jwt = JWTDecoder.decode("eyJhbGciOiJIUzI1NiJ9.eyJvYmplY3QiOnt9fQ.d3nUeeL_69QsrHL0ZWij612LHEQxD8EZg1rNoY3a4aI");
197+
DecodedJWT jwt = JWT.decode("eyJhbGciOiJIUzI1NiJ9.eyJvYmplY3QiOnt9fQ.d3nUeeL_69QsrHL0ZWij612LHEQxD8EZg1rNoY3a4aI");
198198
assertThat(jwt, is(notNullValue()));
199199
assertThat(jwt.getClaim("object"), is(notNullValue()));
200200
assertThat(jwt.getClaim("object").isNull(), is(false));
@@ -262,7 +262,7 @@ public void shouldGetCustomMapClaim() throws Exception {
262262

263263
@Test
264264
public void shouldGetAvailableClaims() throws Exception {
265-
DecodedJWT jwt = JWTDecoder.decode("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOiIxMjM0NTY3ODkwIiwiaWF0IjoiMTIzNDU2Nzg5MCIsIm5iZiI6IjEyMzQ1Njc4OTAiLCJqdGkiOiJodHRwczovL2p3dC5pby8iLCJhdWQiOiJodHRwczovL2RvbWFpbi5hdXRoMC5jb20iLCJzdWIiOiJsb2dpbiIsImlzcyI6ImF1dGgwIiwiZXh0cmFDbGFpbSI6IkpvaG4gRG9lIn0.TX9Ct4feGp9YyeGK9Zl91tO0YBOrguJ4As9jeqgHdZQ");
265+
DecodedJWT jwt = JWT.decode("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOiIxMjM0NTY3ODkwIiwiaWF0IjoiMTIzNDU2Nzg5MCIsIm5iZiI6IjEyMzQ1Njc4OTAiLCJqdGkiOiJodHRwczovL2p3dC5pby8iLCJhdWQiOiJodHRwczovL2RvbWFpbi5hdXRoMC5jb20iLCJzdWIiOiJsb2dpbiIsImlzcyI6ImF1dGgwIiwiZXh0cmFDbGFpbSI6IkpvaG4gRG9lIn0.TX9Ct4feGp9YyeGK9Zl91tO0YBOrguJ4As9jeqgHdZQ");
266266
assertThat(jwt, is(notNullValue()));
267267
assertThat(jwt.getClaims(), is(notNullValue()));
268268
assertThat(jwt.getClaims(), is(instanceOf(Map.class)));
@@ -281,7 +281,7 @@ public void shouldGetAvailableClaims() throws Exception {
281281
private DecodedJWT customJWT(String jsonHeader, String jsonPayload, String signature) {
282282
String header = Base64.encodeBase64URLSafeString(jsonHeader.getBytes(StandardCharsets.UTF_8));
283283
String body = Base64.encodeBase64URLSafeString(jsonPayload.getBytes(StandardCharsets.UTF_8));
284-
return JWTDecoder.decode(String.format("%s.%s.%s", header, body, signature));
284+
return JWT.decode(String.format("%s.%s.%s", header, body, signature));
285285
}
286286

287287
}

0 commit comments

Comments
 (0)