Skip to content

Commit ef35334

Browse files
committed
Making verify method return Map<String, Object>
1 parent 2595999 commit ef35334

3 files changed

Lines changed: 38 additions & 29 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This was developed against `draft-ietf-oauth-json-web-token-08`.
1010
public class Application {
1111
public static void main (String [] args) {
1212
try {
13-
Map<String,String> decodedPayload =
13+
Map<String,Object> decodedPayload =
1414
new JWTVerifier("secret", "audience").verify("my-token");
1515

1616
// Get custom fields from decoded Payload

src/main/java/com/auth0/jwt/JWTVerifier.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.auth0.jwt;
22

33
import com.fasterxml.jackson.core.type.TypeReference;
4+
import com.fasterxml.jackson.databind.JsonNode;
45
import com.fasterxml.jackson.databind.ObjectMapper;
56
import org.apache.commons.codec.binary.Base64;
67

@@ -79,12 +80,12 @@ public Map<String, String> verify(String token)
7980
}
8081

8182
// get JWTHeader JSON object. Extract algorithm
82-
Map<String, String> jwtHeader = decodeAndParse(pieces[0]);
83+
JsonNode jwtHeader = decodeAndParse(pieces[0]);
8384

8485
String algorithm = getAlgorithm(jwtHeader);
8586

8687
// get JWTClaims JSON object
87-
Map<String, String> jwtPayload = decodeAndParse(pieces[1]);
88+
JsonNode jwtPayload = decodeAndParse(pieces[1]);
8889

8990
// check signature
9091
verifySignature(pieces, algorithm);
@@ -94,7 +95,7 @@ public Map<String, String> verify(String token)
9495
verifyIssuer(jwtPayload);
9596
verifyAudience(jwtPayload);
9697

97-
return jwtPayload;
98+
return mapper.treeToValue(jwtPayload, Map.class);
9899
}
99100

100101
void verifySignature(String[] pieces, String algorithm) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
@@ -107,31 +108,32 @@ void verifySignature(String[] pieces, String algorithm) throws NoSuchAlgorithmEx
107108
}
108109
}
109110

110-
void verifyExpiration(Map<String, String> jwtClaims) {
111-
long expiration = Long.parseLong(jwtClaims.get("exp"));
111+
void verifyExpiration(JsonNode jwtClaims) {
112+
final long expiration = jwtClaims.has("exp") ? jwtClaims.get("exp").asLong(0) : 0;
113+
112114
if (expiration != 0 && System.currentTimeMillis() / 1000L >= expiration) {
113115
throw new IllegalStateException("jwt expired");
114116
}
115117
}
116118

117-
void verifyIssuer(Map<String, String> jwtClaims) {
118-
String issuerFromToken = jwtClaims.get("iss");
119+
void verifyIssuer(JsonNode jwtClaims) {
120+
final String issuerFromToken = jwtClaims.has("iss") ? jwtClaims.get("iss").asText() : null;
119121

120122
if (issuerFromToken != null && issuer != null && !issuer.equals(issuerFromToken)) {
121123
throw new IllegalStateException("jwt issuer invalid");
122124
}
123125
}
124126

125-
void verifyAudience(Map<String, String> jwtClaims) {
126-
String audienceFromToken = jwtClaims.get("aud");
127+
void verifyAudience(JsonNode jwtClaims) {
128+
final String audienceFromToken = jwtClaims.has("aud") ? jwtClaims.get("aud").asText() : null;
127129

128130
if (audienceFromToken != null && !audience.equals(audienceFromToken)) {
129131
throw new IllegalStateException("jwt audience invalid");
130132
}
131133
}
132134

133-
String getAlgorithm(Map<String, String> jwtHeader) {
134-
String algorithmName = jwtHeader.get("alg");
135+
String getAlgorithm(JsonNode jwtHeader) {
136+
final String algorithmName = jwtHeader.has("alg") ? jwtHeader.get("alg").asText() : null;
135137

136138
if (jwtHeader.get("alg") == null) {
137139
throw new IllegalStateException("algorithm not set");
@@ -144,10 +146,9 @@ String getAlgorithm(Map<String, String> jwtHeader) {
144146
return algorithms.get(algorithmName);
145147
}
146148

147-
Map<String, String> decodeAndParse(String b64String) throws IOException {
149+
JsonNode decodeAndParse(String b64String) throws IOException {
148150
String jsonString = new String(decoder.decodeBase64(b64String), "UTF-8");
149-
TypeReference<HashMap<String,String>> typeRef = new TypeReference< HashMap<String,String> >() {};
150-
Map<String, String> jwtHeader = mapper.readValue(jsonString, typeRef);
151+
JsonNode jwtHeader = mapper.readValue(jsonString, JsonNode.class);
151152
return jwtHeader;
152153
}
153154
}

src/test/java/com/auth0/jwt/JWTVerifierTest.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.auth0.jwt;
22

3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
5+
import com.fasterxml.jackson.databind.node.ObjectNode;
36
import org.apache.commons.codec.binary.Base64;
47
import org.junit.Test;
58

@@ -48,17 +51,17 @@ public void shouldFailOnNullToken() throws Exception {
4851

4952
@Test(expected = IllegalStateException.class)
5053
public void shouldFailIfAlgorithmIsNotSetOnToken() throws Exception {
51-
new JWTVerifier("such secret").getAlgorithm(Collections.<String, String>emptyMap());
54+
new JWTVerifier("such secret").getAlgorithm(JsonNodeFactory.instance.objectNode());
5255
}
5356

5457
@Test(expected = IllegalStateException.class)
5558
public void shouldFailIfAlgorithmIsNotSupported() throws Exception {
56-
new JWTVerifier("such secret").getAlgorithm(Collections.singletonMap("alg", "doge-crypt"));
59+
new JWTVerifier("such secret").getAlgorithm(createSingletonJSONNode("alg", "doge-crypt"));
5760
}
5861

5962
@Test
6063
public void shouldWorkIfAlgorithmIsSupported() throws Exception {
61-
new JWTVerifier("such secret").getAlgorithm(Collections.singletonMap("alg", "HS256"));
64+
new JWTVerifier("such secret").getAlgorithm(createSingletonJSONNode("alg", "HS256"));
6265
}
6366

6467
@Test(expected = SignatureException.class)
@@ -89,49 +92,49 @@ public void shouldVerifySignature() throws Exception {
8992
@Test(expected = IllegalStateException.class)
9093
public void shouldFailWhenExpired1SecondAgo() throws Exception {
9194
new JWTVerifier("such secret").verifyExpiration(
92-
Collections.singletonMap("exp", Long.toString(System.currentTimeMillis() / 1000L - 1L)));
95+
createSingletonJSONNode("exp", Long.toString(System.currentTimeMillis() / 1000L - 1L)));
9396
}
9497

9598
@Test
9699
public void shouldVerifyExpiration() throws Exception {
97100
new JWTVerifier("such secret").verifyExpiration(
98-
Collections.singletonMap("exp", Long.toString(System.currentTimeMillis() / 1000L + 50L)));
101+
createSingletonJSONNode("exp", Long.toString(System.currentTimeMillis() / 1000L + 50L)));
99102
}
100103

101104
@Test
102105
public void shouldVerifyIssuer() throws Exception {
103106
new JWTVerifier("such secret", "amaze audience", "very issuer")
104-
.verifyIssuer(Collections.singletonMap("iss", "very issuer"));
107+
.verifyIssuer(createSingletonJSONNode("iss", "very issuer"));
105108
}
106109

107110
@Test(expected = IllegalStateException.class)
108111
public void shouldFailIssuer() throws Exception {
109112
new JWTVerifier("such secret", "amaze audience", "very issuer")
110-
.verifyIssuer(Collections.singletonMap("iss", "wow"));
113+
.verifyIssuer(createSingletonJSONNode("iss", "wow"));
111114
}
112115

113116
@Test
114117
public void shouldVerifyIssuerWhenNotFoundInClaimsSet() throws Exception {
115118
new JWTVerifier("such secret", "amaze audience", "very issuer")
116-
.verifyIssuer(Collections.<String, String>emptyMap());
119+
.verifyIssuer(JsonNodeFactory.instance.objectNode());
117120
}
118121

119122
@Test
120123
public void shouldVerifyAudience() throws Exception {
121124
new JWTVerifier("such secret", "amaze audience")
122-
.verifyAudience(Collections.singletonMap("aud", "amaze audience"));
125+
.verifyAudience(createSingletonJSONNode("aud", "amaze audience"));
123126
}
124127

125128
@Test(expected = IllegalStateException.class)
126129
public void shouldFailAudience() throws Exception {
127130
new JWTVerifier("such secret", "amaze audience")
128-
.verifyAudience(Collections.singletonMap("aud", "wow"));
131+
.verifyAudience(createSingletonJSONNode("aud", "wow"));
129132
}
130133

131134
@Test
132135
public void shouldVerifyAudienceWhenNotFoundInClaimsSet() throws Exception {
133136
new JWTVerifier("such secret", "amaze audience")
134-
.verifyAudience(Collections.<String, String>emptyMap());
137+
.verifyAudience(JsonNodeFactory.instance.objectNode());
135138
}
136139

137140
@Test
@@ -140,12 +143,17 @@ public void decodeAndParse() throws Exception {
140143
final String encodedJSON = new String(encoder.encode("{\"some\": \"json\", \"number\": 123}".getBytes()));
141144
final JWTVerifier jwtVerifier = new JWTVerifier("secret", "audience");
142145

143-
final Map<String,String> decodedJSON = jwtVerifier.decodeAndParse(encodedJSON);
146+
final JsonNode decodedJSON = jwtVerifier.decodeAndParse(encodedJSON);
144147

145-
assertEquals("json", decodedJSON.get("some"));
148+
assertEquals("json", decodedJSON.get("some").asText());
146149
assertEquals(null, decodedJSON.get("unexisting_property"));
147-
assertEquals("123", decodedJSON.get("number"));
150+
assertEquals("123", decodedJSON.get("number").asText());
148151
}
149152

150153

154+
public static JsonNode createSingletonJSONNode(String key, String value) {
155+
final ObjectNode jsonNodes = JsonNodeFactory.instance.objectNode();
156+
jsonNodes.put(key, value);
157+
return jsonNodes;
158+
}
151159
}

0 commit comments

Comments
 (0)