0

Is it possible to add a byte array in a JWT's body? I get a stream of bytes from a source and I want to package it into a JWT and send it off.

As a test, I tried converting a string to a byte array, added it to a jwt using the jjwt library, encoded it, decoded it, and the retrieved the bytes back. But they don't look the same.

String token = Jwts.builder().claim("key", "test".getBytes())                
              .signWith(<algorithm>, <signingkey>)
              .compact();

Claims claims = Jwts.parser().setSigningKey(<signingkey>).parseClaimsJws(token).getBody();
byte[] ret = claims.get("key", String.class).getBytes();

But ret != "test".getBytes()

nahzor
  • 460
  • 1
  • 4
  • 20
  • 2
    A JWT is just a JSON document (which is then base-64 encoded). So I think your question reduces to "how do I store a byte array in a JSON document?". – Oliver Charlesworth May 08 '18 at 19:17
  • Agreed. But i am not sure if its the way i am doing the byte conversion that i'm messing up. I've added a code sample to give it more context. Also, the jwt is further encoded and i am not sure if that makes a difference. – nahzor May 08 '18 at 19:29
  • Tried using java 8's Base64 encodeToString() and decode() and it works now. I'm guessing the jjwt library converts the bytes to a string internally and maybe the encoding is different. – nahzor May 08 '18 at 20:24

0 Answers0