@@ -257,6 +257,11 @@ public Jwt parse(String jwt) throws ExpiredJwtException, MalformedJwtException,
257
257
258
258
Assert .hasText (jwt , "JWT String argument cannot be null or empty." );
259
259
260
+ if (".." .equals (jwt )) {
261
+ String msg = "JWT string '..' is missing a header." ;
262
+ throw new MalformedJwtException (msg );
263
+ }
264
+
260
265
String base64UrlEncodedHeader = null ;
261
266
String base64UrlEncodedPayload = null ;
262
267
String base64UrlEncodedDigest = null ;
@@ -293,9 +298,6 @@ public Jwt parse(String jwt) throws ExpiredJwtException, MalformedJwtException,
293
298
base64UrlEncodedDigest = sb .toString ();
294
299
}
295
300
296
- if (base64UrlEncodedPayload == null ) {
297
- throw new MalformedJwtException ("JWT string '" + jwt + "' is missing a body/payload." );
298
- }
299
301
300
302
// =============== Header =================
301
303
Header header = null ;
@@ -317,15 +319,18 @@ public Jwt parse(String jwt) throws ExpiredJwtException, MalformedJwtException,
317
319
}
318
320
319
321
// =============== Body =================
320
- byte [] bytes = base64UrlDecoder .decode (base64UrlEncodedPayload );
321
- if (compressionCodec != null ) {
322
- bytes = compressionCodec .decompress (bytes );
322
+ String payload = "" ; // https://github.com/jwtk/jjwt/pull/540
323
+ if (base64UrlEncodedPayload != null ) {
324
+ byte [] bytes = base64UrlDecoder .decode (base64UrlEncodedPayload );
325
+ if (compressionCodec != null ) {
326
+ bytes = compressionCodec .decompress (bytes );
327
+ }
328
+ payload = new String (bytes , Strings .UTF_8 );
323
329
}
324
- String payload = new String (bytes , Strings .UTF_8 );
325
330
326
331
Claims claims = null ;
327
332
328
- if (payload .charAt (0 ) == '{' && payload .charAt (payload .length () - 1 ) == '}' ) { //likely to be json, parse it:
333
+ if (! payload . isEmpty () && payload .charAt (0 ) == '{' && payload .charAt (payload .length () - 1 ) == '}' ) { //likely to be json, parse it:
329
334
Map <String , Object > claimsMap = (Map <String , Object >) readValue (payload );
330
335
claims = new DefaultClaims (claimsMap );
331
336
}
@@ -385,7 +390,10 @@ public Jwt parse(String jwt) throws ExpiredJwtException, MalformedJwtException,
385
390
Assert .notNull (key , "A signing key must be specified if the specified JWT is digitally signed." );
386
391
387
392
//re-create the jwt part without the signature. This is what needs to be signed for verification:
388
- String jwtWithoutSignature = base64UrlEncodedHeader + SEPARATOR_CHAR + base64UrlEncodedPayload ;
393
+ String jwtWithoutSignature = base64UrlEncodedHeader + SEPARATOR_CHAR ;
394
+ if (base64UrlEncodedPayload != null ) {
395
+ jwtWithoutSignature += base64UrlEncodedPayload ;
396
+ }
389
397
390
398
JwtSignatureValidator validator ;
391
399
try {
0 commit comments