Skip to content

Commit f73bc80

Browse files
author
Justin Dahmubed
committed
Remove avro and create Message class
1 parent f7563f9 commit f73bc80

19 files changed

+33
-488
lines changed

lib/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ compileJava {
3636
dependencies {
3737
compile 'com.fasterxml.jackson.core:jackson-databind:2.9.2'
3838
compile 'commons-codec:commons-codec:1.11'
39-
compile "org.apache.avro:avro:1.8.1"
39+
compile 'com.google.code.gson:gson:2.8.2'
4040
testCompile 'org.bouncycastle:bcprov-jdk15on:1.58'
4141
testCompile 'junit:junit:4.12'
4242
testCompile 'net.jodah:concurrentunit:0.4.3'

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import com.auth0.jwt.interfaces.DecodedJWT;
99
import com.auth0.jwt.interfaces.Header;
1010
import com.auth0.jwt.interfaces.Payload;
11-
import org.apache.avro.Schema;
12-
import org.apache.avro.SchemaBuilder;
1311
import org.apache.commons.codec.binary.Base32;
1412
import org.apache.commons.codec.binary.Base64;
1513
import org.apache.commons.codec.binary.Hex;
@@ -55,15 +53,6 @@ public JWTDecoder(String jwt, EncodeType encodeType) throws Exception {
5553
payload = converter.parsePayload(payloadJson);
5654
}
5755

58-
public JWTDecoder(String jwt, Schema schemaForHeader, Schema schemaForPayload) throws Exception {
59-
parts = TokenUtils.splitToken(jwt);
60-
final JWTParser converter = new JWTParser();
61-
String headerJson = JWTCreator.avroToJson(JWTCreator.schemaToHeaderAndPayloadByteArray.get(schemaForHeader), schemaForHeader);
62-
String payloadJson = JWTCreator.avroToJson(JWTCreator.schemaToHeaderAndPayloadByteArray.get(schemaForPayload), schemaForPayload);
63-
header = converter.parseHeader(headerJson);
64-
payload = converter.parsePayload(payloadJson);
65-
}
66-
6756
@Override
6857
public String getAlgorithm() {
6958
return header.getAlgorithm();

lib/src/main/java/com/auth0/jwt/algorithms/HMACAlgorithm.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import com.auth0.jwt.exceptions.SignatureGenerationException;
66
import com.auth0.jwt.exceptions.SignatureVerificationException;
77
import com.auth0.jwt.interfaces.DecodedJWT;
8-
import org.apache.avro.Schema;
9-
import org.apache.avro.SchemaBuilder;
108
import org.apache.commons.codec.CharEncoding;
119
import org.apache.commons.codec.binary.Base32;
1210
import org.apache.commons.codec.binary.Base64;

lib/src/main/java/com/auth0/jwt/creators/AccessJwtCreator.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.auth0.jwt.exceptions.JWTCreationException;
55
import com.auth0.jwt.impl.PublicClaims;
66
import com.auth0.jwt.jwts.JWT;
7-
import org.apache.avro.Schema;
87

98
import java.util.Date;
109
import java.util.HashMap;
@@ -258,24 +257,6 @@ public String signBase32Encoding(Algorithm algorithm) throws Exception {
258257
return JWS;
259258
}
260259

261-
/**
262-
* Creates a new JWT and signs it with the given algorithm.
263-
*
264-
* @param algorithm used to sign the JWT
265-
* @return a new JWT token
266-
* @throws IllegalAccessException if the developer didn't want NONE algorithm to be allowed and it was passed in
267-
* @throws IllegalArgumentException if the provided algorithm is null.
268-
* @throws JWTCreationException if the claims could not be converted to a valid JSON or there was a problem with the signing key.
269-
*/
270-
public String signJSONEncoding(Algorithm algorithm, Schema schemaHeader, Schema schemaPayload) throws Exception {
271-
if(!jwt.getIsNoneAlgorithmAllowed() && algorithm.equals(Algorithm.none())) {
272-
throw new IllegalAccessException("None algorithm isn't allowed");
273-
}
274-
String JWS = jwt.signJSON(algorithm, schemaHeader, schemaPayload);
275-
verifyClaims();
276-
return JWS;
277-
}
278-
279260
/**
280261
* Verifies that all the standard claims were provided
281262
* @throws Exception if all the standard claims weren't provided

lib/src/main/java/com/auth0/jwt/creators/FbJwtCreator.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.auth0.jwt.exceptions.JWTCreationException;
55
import com.auth0.jwt.impl.PublicClaims;
66
import com.auth0.jwt.jwts.JWT;
7-
import org.apache.avro.Schema;
87

98
import java.util.Date;
109
import java.util.HashMap;
@@ -239,24 +238,6 @@ public String signBase32Encoding(Algorithm algorithm) throws Exception {
239238
return JWS;
240239
}
241240

242-
/**
243-
* Creates a new JWT and signs it with the given algorithm.
244-
*
245-
* @param algorithm used to sign the JWT
246-
* @return a new JWT token
247-
* @throws IllegalAccessException if the developer didn't want NONE algorithm to be allowed and it was passed in
248-
* @throws IllegalArgumentException if the provided algorithm is null.
249-
* @throws JWTCreationException if the claims could not be converted to a valid JSON or there was a problem with the signing key.
250-
*/
251-
public String signJSONEncoding(Algorithm algorithm, Schema schemaHeader, Schema schemaPayload) throws Exception {
252-
if(!jwt.getIsNoneAlgorithmAllowed() && algorithm.equals(Algorithm.none())) {
253-
throw new IllegalAccessException("None algorithm isn't allowed");
254-
}
255-
String JWS = jwt.signJSON(algorithm, schemaHeader, schemaPayload);
256-
verifyClaims();
257-
return JWS;
258-
}
259-
260241
/**
261242
* Verifies that all the standard claims were provided
262243
* @throws Exception if all the standard claims weren't provided

lib/src/main/java/com/auth0/jwt/creators/GoogleJwtCreator.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.auth0.jwt.exceptions.JWTCreationException;
55
import com.auth0.jwt.impl.PublicClaims;
66
import com.auth0.jwt.jwts.JWT;
7-
import org.apache.avro.Schema;
87

98
import java.util.Date;
109
import java.util.HashMap;
@@ -298,24 +297,6 @@ public String signBase32Encoding(Algorithm algorithm) throws Exception {
298297
return JWS;
299298
}
300299

301-
/**
302-
* Creates a new JWT and signs it with the given algorithm.
303-
*
304-
* @param algorithm used to sign the JWT
305-
* @return a new JWT token
306-
* @throws IllegalAccessException if the developer didn't want NONE algorithm to be allowed and it was passed in
307-
* @throws IllegalArgumentException if the provided algorithm is null.
308-
* @throws JWTCreationException if the claims could not be converted to a valid JSON or there was a problem with the signing key.
309-
*/
310-
public String signJSONEncoding(Algorithm algorithm, Schema schemaHeader, Schema schemaPayload) throws Exception {
311-
if(!jwt.getIsNoneAlgorithmAllowed() && algorithm.equals(Algorithm.none())) {
312-
throw new IllegalAccessException("None algorithm isn't allowed");
313-
}
314-
String JWS = jwt.signJSON(algorithm, schemaHeader, schemaPayload);
315-
verifyClaims();
316-
return JWS;
317-
}
318-
319300
/**
320301
* Verifies that all the standard claims were provided
321302
* @throws Exception if all the standard claims weren't provided

lib/src/main/java/com/auth0/jwt/creators/ImplicitJwtCreator.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.auth0.jwt.exceptions.JWTCreationException;
55
import com.auth0.jwt.impl.PublicClaims;
66
import com.auth0.jwt.jwts.JWT;
7-
import org.apache.avro.Schema;
87

98
import java.util.Date;
109
import java.util.HashMap;
@@ -244,24 +243,6 @@ public String signBase32Encoding(Algorithm algorithm) throws Exception {
244243
return JWS;
245244
}
246245

247-
/**
248-
* Creates a new JWT and signs it with the given algorithm.
249-
*
250-
* @param algorithm used to sign the JWT
251-
* @return a new JWT token
252-
* @throws IllegalAccessException if the developer didn't want NONE algorithm to be allowed and it was passed in
253-
* @throws IllegalArgumentException if the provided algorithm is null.
254-
* @throws JWTCreationException if the claims could not be converted to a valid JSON or there was a problem with the signing key.
255-
*/
256-
public String signJSONEncoding(Algorithm algorithm, Schema schemaHeader, Schema schemaPayload) throws Exception {
257-
if(!jwt.getIsNoneAlgorithmAllowed() && algorithm.equals(Algorithm.none())) {
258-
throw new IllegalAccessException("None algorithm isn't allowed");
259-
}
260-
String JWS = jwt.signJSON(algorithm, schemaHeader, schemaPayload);
261-
verifyClaims();
262-
return JWS;
263-
}
264-
265246
/**
266247
* Verifies that all the standard claims were provided
267248
* @throws Exception if all the standard claims weren't provided

lib/src/main/java/com/auth0/jwt/creators/JWTCreator.java

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,6 @@
1010
import com.fasterxml.jackson.databind.MapperFeature;
1111
import com.fasterxml.jackson.databind.ObjectMapper;
1212
import com.fasterxml.jackson.databind.module.SimpleModule;
13-
import org.apache.avro.Schema;
14-
import org.apache.avro.SchemaBuilder;
15-
import org.apache.avro.file.DataFileStream;
16-
import org.apache.avro.file.DataFileWriter;
17-
import org.apache.avro.generic.GenericDatumReader;
18-
import org.apache.avro.generic.GenericDatumWriter;
19-
import org.apache.avro.generic.GenericRecord;
20-
import org.apache.avro.io.*;
2113
import org.apache.commons.codec.Encoder;
2214
import org.apache.commons.codec.binary.Base32;
2315
import org.apache.commons.codec.binary.Base64;
@@ -41,7 +33,6 @@ public final class JWTCreator {
4133
private final Algorithm algorithm;
4234
private final String headerJson;
4335
private final String payloadJson;
44-
public static Map<Schema, byte[]> schemaToHeaderAndPayloadByteArray;
4536

4637
private JWTCreator(Algorithm algorithm, Map<String, Object> headerClaims, Map<String, Object> payloadClaims) throws JWTCreationException {
4738
this.algorithm = algorithm;
@@ -53,7 +44,6 @@ private JWTCreator(Algorithm algorithm, Map<String, Object> headerClaims, Map<St
5344
mapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
5445
headerJson = mapper.writeValueAsString(headerClaims);
5546
payloadJson = mapper.writeValueAsString(new ClaimsHolder(payloadClaims));
56-
schemaToHeaderAndPayloadByteArray = new HashMap<>();
5747
} catch (JsonProcessingException e) {
5848
throw new JWTCreationException("Some of the Claims couldn't be converted to a valid JSON format.", e);
5949
}
@@ -380,21 +370,6 @@ public String sign(Algorithm algorithm, EncodeType encodeType) throws Exception
380370
return token;
381371
}
382372

383-
public String signJSON(Algorithm algorithm, Schema schemaHeader, Schema schemaPayload) throws Exception {
384-
if (algorithm == null) {
385-
throw new IllegalArgumentException("The Algorithm cannot be null.");
386-
}
387-
headerClaims.put(PublicClaims.ALGORITHM, algorithm.getName());
388-
headerClaims.put(PublicClaims.TYPE, "JWT");
389-
String signingKeyId = algorithm.getSigningKeyId();
390-
if (signingKeyId != null) {
391-
withKeyId(signingKeyId);
392-
}
393-
JWTCreator jwtCreator = new JWTCreator(algorithm, headerClaims, payloadClaims);
394-
395-
return jwtCreator.signJsonEncode(schemaHeader, schemaPayload);
396-
}
397-
398373
protected void assertNonNull(String name) {
399374
if (name == null) {
400375
throw new IllegalArgumentException("The Custom Claim's name can't be null.");
@@ -410,63 +385,6 @@ private void addClaim(String name, Object value) {
410385
}
411386
}
412387

413-
private String signJsonEncode(Schema schemaForHeader, Schema schemaForPayload) throws Exception {
414-
byte[] bHeader = jsonToAvro(headerJson, schemaForHeader.toString());
415-
schemaToHeaderAndPayloadByteArray.put(schemaForHeader, bHeader);
416-
byte[] bPayload = jsonToAvro(payloadJson, schemaForPayload.toString());
417-
schemaToHeaderAndPayloadByteArray.put(schemaForPayload, bPayload);
418-
String content = String.format("%s.%s", new String(bHeader), new String(bPayload));
419-
420-
byte[] signatureBytes = algorithm.sign(content.getBytes(StandardCharsets.UTF_8));
421-
String signature = Base64.encodeBase64URLSafeString(signatureBytes);
422-
423-
return String.format("%s.%s", content, signature);
424-
}
425-
426-
public static String avroToJson(byte[] avro, Schema schema) throws IOException {
427-
boolean pretty = false;
428-
GenericDatumReader<Object> reader = new GenericDatumReader<>(schema);
429-
DatumWriter<Object> writer = new GenericDatumWriter<>(schema);
430-
ByteArrayOutputStream output = new ByteArrayOutputStream();
431-
JsonEncoder encoder = EncoderFactory.get().jsonEncoder(schema, output, pretty);
432-
Decoder decoder = DecoderFactory.get().binaryDecoder(avro, null);
433-
Object datum = reader.read(null, decoder);
434-
writer.write(datum, encoder);
435-
encoder.flush();
436-
output.flush();
437-
return new String(output.toByteArray(), "UTF-8");
438-
}
439-
440-
public static byte[] jsonToAvro(String json, String schemaStr) throws Exception {
441-
InputStream input = null;
442-
GenericDatumWriter<GenericRecord> writer = null;
443-
BinaryEncoder encoder = null;
444-
ByteArrayOutputStream output = null;
445-
try {
446-
Schema schema = new Schema.Parser().parse(schemaStr);
447-
DatumReader<GenericRecord> reader = new GenericDatumReader<GenericRecord>(schema);
448-
input = new ByteArrayInputStream(json.getBytes());
449-
output = new ByteArrayOutputStream();
450-
DataInputStream din = new DataInputStream(input);
451-
writer = new GenericDatumWriter<GenericRecord>(schema);
452-
Decoder decoder = DecoderFactory.get().jsonDecoder(schema, din);
453-
encoder = EncoderFactory.get().binaryEncoder(output, null);
454-
GenericRecord datum;
455-
while (true) {
456-
try {
457-
datum = reader.read(null, decoder);
458-
} catch (EOFException eofe) {
459-
break;
460-
}
461-
writer.write(datum, encoder);
462-
}
463-
encoder.flush();
464-
return output.toByteArray();
465-
} finally {
466-
try { input.close(); } catch (Exception e) { }
467-
}
468-
}
469-
470388
private String signBase16Encoding() throws UnsupportedEncodingException {
471389
String header = URLEncoder.encode(headerJson, "UTF-8");
472390
String payload = URLEncoder.encode(payloadJson, "UTF-8");
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.auth0.jwt.creators;
2+
3+
import com.fasterxml.jackson.core.type.TypeReference;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.google.gson.Gson;
6+
7+
import java.io.IOException;
8+
import java.io.UnsupportedEncodingException;
9+
import java.net.URLDecoder;
10+
import java.net.URLEncoder;
11+
import java.util.HashMap;
12+
import java.util.Map;
13+
14+
public class Message {
15+
16+
public String toUrlEncoded(String json) throws UnsupportedEncodingException {
17+
return URLEncoder.encode(json, "UTF-8");
18+
}
19+
20+
public String toUrlDecoded(String urlEncoded) throws UnsupportedEncodingException {
21+
return URLDecoder.decode(urlEncoded, "UTF-8");
22+
}
23+
24+
public String toJSON(HashMap<String,Object> hashMap) {
25+
return new Gson().toJson(hashMap);
26+
}
27+
28+
public HashMap<String,Object> fromJSON(String json) throws IOException {
29+
return new ObjectMapper().readValue(json, new TypeReference<Map<String, Object>>(){});
30+
}
31+
32+
}

lib/src/main/java/com/auth0/jwt/creators/RiscJwtCreator.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.auth0.jwt.impl.PublicClaims;
66
import com.auth0.jwt.interfaces.Verification;
77
import com.auth0.jwt.jwts.JWT;
8-
import org.apache.avro.Schema;
98

109
import java.util.Date;
1110
import java.util.HashMap;
@@ -283,24 +282,6 @@ public String signBase32Encoding(Algorithm algorithm) throws Exception {
283282
return JWS;
284283
}
285284

286-
/**
287-
* Creates a new JWT and signs it with the given algorithm.
288-
*
289-
* @param algorithm used to sign the JWT
290-
* @return a new JWT token
291-
* @throws IllegalAccessException if the developer didn't want NONE algorithm to be allowed and it was passed in
292-
* @throws IllegalArgumentException if the provided algorithm is null.
293-
* @throws JWTCreationException if the claims could not be converted to a valid JSON or there was a problem with the signing key.
294-
*/
295-
public String signJSONEncoding(Algorithm algorithm, Schema schemaHeader, Schema schemaPayload) throws Exception {
296-
if(!jwt.getIsNoneAlgorithmAllowed() && algorithm.equals(Algorithm.none())) {
297-
throw new IllegalAccessException("None algorithm isn't allowed");
298-
}
299-
String JWS = jwt.signJSON(algorithm, schemaHeader, schemaPayload);
300-
verifyClaims();
301-
return JWS;
302-
}
303-
304285
/**
305286
* Verifies that all the standard claims were provided
306287
* @throws Exception if all the standard claims weren't provided

0 commit comments

Comments
 (0)