Skip to content

Commit

Permalink
Support serialization of DecodedJWT
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyjames committed Jan 2, 2020
1 parent 2a0c022 commit d89d746
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lib/src/main/java/com/auth0/jwt/JWTDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.StringUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;
Expand All @@ -17,7 +18,9 @@
* The JWTDecoder class holds the decode method to parse a given JWT token into it's JWT representation.
*/
@SuppressWarnings("WeakerAccess")
final class JWTDecoder implements DecodedJWT {
final class JWTDecoder implements DecodedJWT, Serializable {

private static final long serialVersionUID = 1873362438023312895L;

private final String[] parts;
private final Header header;
Expand Down
5 changes: 4 additions & 1 deletion lib/src/main/java/com/auth0/jwt/impl/BasicHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectReader;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -14,7 +15,9 @@
/**
* The BasicHeader class implements the Header interface.
*/
class BasicHeader implements Header {
class BasicHeader implements Header, Serializable {
private static final long serialVersionUID = -4659137688548605095L;

private final String algorithm;
private final String type;
private final String contentType;
Expand Down
6 changes: 5 additions & 1 deletion lib/src/main/java/com/auth0/jwt/impl/PayloadImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectReader;

import java.io.Serializable;
import java.util.*;

import static com.auth0.jwt.impl.JsonNodeClaim.extractClaim;

/**
* The PayloadImpl class implements the Payload interface.
*/
class PayloadImpl implements Payload {
class PayloadImpl implements Payload, Serializable {

private static final long serialVersionUID = 1659021498824562311L;

private final String issuer;
private final String subject;
private final List<String> audience;
Expand Down
42 changes: 42 additions & 0 deletions lib/src/test/java/com/auth0/jwt/JWTDecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Map;
Expand Down Expand Up @@ -289,6 +290,34 @@ public void shouldGetAvailableClaims() throws Exception {
assertThat(jwt.getClaims().get("extraClaim"), is(notNullValue()));
}

@Test
public void shouldSerializeAndDeserialize() throws Exception {
DecodedJWT originalJwt = JWT.decode("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjEyMzQ1Njc4OTAsImlhdCI6MTIzNDU2Nzg5MCwibmJmIjoxMjM0NTY3ODkwLCJqdGkiOiJodHRwczovL2p3dC5pby8iLCJhdWQiOiJodHRwczovL2RvbWFpbi5hdXRoMC5jb20iLCJzdWIiOiJsb2dpbiIsImlzcyI6ImF1dGgwIiwiZXh0cmFDbGFpbSI6IkpvaG4gRG9lIn0.2_0nxDPJwOk64U5V5V9pt8U92jTPJbGsHYQ35HYhbdE");

assertThat(originalJwt, is(instanceOf(Serializable.class)));

byte[] serialized = serialize(originalJwt);
DecodedJWT deserializedJwt = (DecodedJWT) deserialize(serialized);

assertThat(originalJwt.getHeader(), is(equalTo(deserializedJwt.getHeader())));
assertThat(originalJwt.getPayload(), is(equalTo(deserializedJwt.getPayload())));
assertThat(originalJwt.getSignature(), is(equalTo(deserializedJwt.getSignature())));
assertThat(originalJwt.getToken(), is(equalTo(deserializedJwt.getToken())));
assertThat(originalJwt.getAlgorithm(), is(equalTo(deserializedJwt.getAlgorithm())));
assertThat(originalJwt.getAudience(), is(equalTo(deserializedJwt.getAudience())));
assertThat(originalJwt.getContentType(), is(equalTo(deserializedJwt.getContentType())));
assertThat(originalJwt.getExpiresAt(), is(equalTo(deserializedJwt.getExpiresAt())));
assertThat(originalJwt.getId(), is(equalTo(deserializedJwt.getId())));
assertThat(originalJwt.getIssuedAt(), is(equalTo(deserializedJwt.getIssuedAt())));
assertThat(originalJwt.getIssuer(), is(equalTo(deserializedJwt.getIssuer())));
assertThat(originalJwt.getKeyId(), is(equalTo(deserializedJwt.getKeyId())));
assertThat(originalJwt.getNotBefore(), is(equalTo(deserializedJwt.getNotBefore())));
assertThat(originalJwt.getSubject(), is(equalTo(deserializedJwt.getSubject())));
assertThat(originalJwt.getType(), is(equalTo(deserializedJwt.getType())));
assertThat(originalJwt.getClaims().get("extraClaim").asString(),
is(equalTo(deserializedJwt.getClaims().get("extraClaim").asString())));
}

//Helper Methods

private DecodedJWT customJWT(String jsonHeader, String jsonPayload, String signature) {
Expand All @@ -297,4 +326,17 @@ private DecodedJWT customJWT(String jsonHeader, String jsonPayload, String signa
return JWT.decode(String.format("%s.%s.%s", header, body, signature));
}

private static byte[] serialize(Object obj) throws IOException {
ByteArrayOutputStream b = new ByteArrayOutputStream();
ObjectOutputStream o = new ObjectOutputStream(b);
o.writeObject(obj);
return b.toByteArray();
}

private static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
ByteArrayInputStream b = new ByteArrayInputStream(bytes);
ObjectInputStream o = new ObjectInputStream(b);
return o.readObject();
}

}

0 comments on commit d89d746

Please sign in to comment.