Skip to content

Commit c135304

Browse files
author
Justin Dahmubed
committed
Jsonencoding for the rest of the tokens and remove comments
1 parent 604a566 commit c135304

17 files changed

Lines changed: 505 additions & 294 deletions

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,8 @@ public JWTDecoder(String jwt, EncodeType encodeType) throws Exception {
5757
public JWTDecoder(String jwt, Schema schemaForHeader, Schema schemaForPayload) throws Exception {
5858
parts = TokenUtils.splitToken(jwt);
5959
final JWTParser converter = new JWTParser();
60-
/*Schema schemaForHeader = SchemaBuilder
61-
.record("record").namespace("namespace")
62-
.fields()
63-
.name("alg").type().stringType().noDefault()
64-
.name("typ").type().stringType().noDefault()
65-
.endRecord();
66-
67-
Schema schemaForPayload = SchemaBuilder
68-
.record("record").namespace("namespace")
69-
.fields()
70-
.name("sub").type().array().items().stringType().noDefault()
71-
.name("iss").type().array().items().stringType().noDefault()
72-
.name("aud").type().stringType().noDefault()
73-
.name("iat").type().intType().noDefault()
74-
.endRecord();*/
7560
String headerJson = JWTCreator.avroToJson(JWTCreator.schemaToHeaderAndPayloadByteArray.get(schemaForHeader), schemaForHeader);
7661
String payloadJson = JWTCreator.avroToJson(JWTCreator.schemaToHeaderAndPayloadByteArray.get(schemaForPayload), schemaForPayload);
77-
7862
header = converter.parseHeader(headerJson);
7963
payload = converter.parsePayload(payloadJson);
8064
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,13 @@ public void verify(DecodedJWT jwt, EncodeType encodeType) throws Exception {
4747
case Base16:
4848
signatureBytes = Hex.decodeHex(signature);
4949
break;
50-
case Base32: {
50+
case Base32:
5151
Base32 base32 = new Base32();
5252
signatureBytes = base32.decode(signature);
5353
break;
54-
}
5554
case Base64:
5655
signatureBytes = Base64.decodeBase64(signature);
5756
break;
58-
case JsonEncode:
59-
break;
60-
//token = jwtCreator.signJsonEncode();
6157
}
6258

6359
try {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,18 @@ public void verify(DecodedJWT jwt, EncodeType encodeType) throws Exception {
5757
case Base16:
5858
signatureBytes = Hex.decodeHex(signature);
5959
break;
60-
case Base32: {
60+
case Base32:
6161
Base32 base32 = new Base32();
6262
signatureBytes = base32.decode(signature);
6363
break;
64-
}
6564
case Base64:
6665
signatureBytes = Base64.decodeBase64(signature);
6766
break;
6867
case JsonEncode: {
6968
signatureBytes = Base64.decodeBase64(signature);
7069
break;
7170
}
71+
7272
}
7373

7474
try {

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ public void verify(DecodedJWT jwt, EncodeType encodeType) throws Exception {
3030
case Base64:
3131
signatureBytes = Base64.decodeBase64(signature);
3232
break;
33-
case JsonEncode:
34-
break;
35-
//token = jwtCreator.signJsonEncode();
3633
}
3734
if (signatureBytes.length > 0) {
3835
throw new SignatureVerificationException(this);

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ public void verify(DecodedJWT jwt, EncodeType encodeType) throws Exception {
5252
case Base64:
5353
signatureBytes = Base64.decodeBase64(signature);
5454
break;
55-
case JsonEncode:
56-
break;
57-
//token = jwtCreator.signJsonEncode();
5855
}
5956

6057
try {

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
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;
78

89
import java.util.Date;
910
import java.util.HashMap;
@@ -257,6 +258,24 @@ public String signBase32Encoding(Algorithm algorithm) throws Exception {
257258
return JWS;
258259
}
259260

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+
260279
/**
261280
* Verifies that all the standard claims were provided
262281
* @throws Exception if all the standard claims weren't provided

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
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;
78

89
import java.util.Date;
910
import java.util.HashMap;
@@ -238,6 +239,24 @@ public String signBase32Encoding(Algorithm algorithm) throws Exception {
238239
return JWS;
239240
}
240241

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+
241260
/**
242261
* Verifies that all the standard claims were provided
243262
* @throws Exception if all the standard claims weren't provided

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
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;
78

89
import java.util.Date;
910
import java.util.HashMap;
@@ -297,6 +298,23 @@ public String signBase32Encoding(Algorithm algorithm) throws Exception {
297298
return JWS;
298299
}
299300

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+
}
300318

301319
/**
302320
* Verifies that all the standard claims were provided

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

Lines changed: 1 addition & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -410,77 +410,18 @@ private void addClaim(String name, Object value) {
410410
}
411411

412412
private String signJsonEncode(Schema schemaForHeader, Schema schemaForPayload) throws Exception {
413-
/*Schema schemaForHeader = SchemaBuilder
414-
.record("record").namespace("namespace")
415-
.fields()
416-
.name("alg").type().stringType().noDefault()
417-
.name("typ").type().stringType().noDefault()
418-
.endRecord();*/
419-
/*String schema = "{" +
420-
"\"type\":\"record\"," +
421-
"\"namespace\":\"foo\"," +
422-
"\"name\":\"Person\"," +
423-
"\"fields\":" +
424-
"{" +
425-
"\"alg\":\"string\"," +
426-
"\"typ\":\"string\"" +
427-
"}" +
428-
"}";*/
429413
byte[] header = jsonToAvro(headerJson, schemaForHeader.toString());
430414
schemaToHeaderAndPayloadByteArray.put(schemaForHeader, header);
431-
System.out.println(avroToJson(header, schemaForHeader));
432-
//.name("aud").type().array().items().stringType().noDefault()
433-
/*Schema schemaForPayload = SchemaBuilder
434-
.record("record").namespace("namespace")
435-
.fields()
436-
.name("sub").type().array().items().stringType().noDefault()
437-
.name("iss").type().array().items().stringType().noDefault()
438-
.name("aud").type().stringType().noDefault()
439-
.name("iat").type().intType().noDefault()
440-
.endRecord();*/
441415
byte[] payload = jsonToAvro(payloadJson, schemaForPayload.toString());
442416
schemaToHeaderAndPayloadByteArray.put(schemaForPayload, payload);
443-
System.out.println(avroToJson(payload, schemaForPayload));
444417
String content = String.format("%s.%s", new String(header), new String(payload));
445418

446419
byte[] signatureBytes = algorithm.sign(content.getBytes(StandardCharsets.UTF_8));
447420
String signature = Base64.encodeBase64URLSafeString(signatureBytes);
448-
System.out.println(signature);
449-
System.out.println(Base64.decodeBase64(signature));
450421

451-
return String.format("%s.%s", content, signature); //for now
422+
return String.format("%s.%s", content, signature);
452423
}
453424

454-
/*public static String avroToJson(byte[] avro, String schemaStr) throws IOException {
455-
boolean pretty = false;
456-
GenericDatumReader<GenericRecord> reader = null;
457-
JsonEncoder encoder = null;
458-
ByteArrayOutputStream output = null;
459-
try {
460-
Schema schema = new Schema.Parser().parse(schemaStr);
461-
reader = new GenericDatumReader<GenericRecord>(schema);
462-
InputStream input = new ByteArrayInputStream(avro);
463-
output = new ByteArrayOutputStream();
464-
DatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(schema);
465-
encoder = EncoderFactory.get().jsonEncoder(schema, output, pretty);
466-
Decoder decoder = DecoderFactory.get().binaryDecoder(input, null);
467-
GenericRecord datum;
468-
while (true) {
469-
try {
470-
datum = reader.read(null, decoder);
471-
} catch (EOFException eofe) {
472-
break;
473-
}
474-
writer.write(datum, encoder);
475-
}
476-
encoder.flush();
477-
output.flush();
478-
return new String(output.toByteArray());
479-
} finally {
480-
try { if (output != null) output.close(); } catch (Exception e) { }
481-
}
482-
}*/
483-
484425
public static String avroToJson(byte[] avro, Schema schema) throws IOException {
485426
boolean pretty = false;
486427
GenericDatumReader<Object> reader = new GenericDatumReader<>(schema);
@@ -495,30 +436,6 @@ public static String avroToJson(byte[] avro, Schema schema) throws IOException {
495436
return new String(output.toByteArray(), "UTF-8");
496437
}
497438

498-
public static String avroToJson(byte[] avro) throws IOException {
499-
boolean pretty = false;
500-
GenericDatumReader<GenericRecord> reader = null;
501-
JsonEncoder encoder = null;
502-
ByteArrayOutputStream output = null;
503-
try {
504-
reader = new GenericDatumReader<GenericRecord>();
505-
InputStream input = new ByteArrayInputStream(avro);
506-
DataFileStream<GenericRecord> streamReader = new DataFileStream<GenericRecord>(input, reader);
507-
output = new ByteArrayOutputStream();
508-
Schema schema = streamReader.getSchema();
509-
DatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(schema);
510-
encoder = EncoderFactory.get().jsonEncoder(schema, output, pretty);
511-
for (GenericRecord datum : streamReader) {
512-
writer.write(datum, encoder);
513-
}
514-
encoder.flush();
515-
output.flush();
516-
return new String(output.toByteArray());
517-
} finally {
518-
try { if (output != null) output.close(); } catch (Exception e) { }
519-
}
520-
}
521-
522439
public static byte[] jsonToAvro(String json, String schemaStr) throws Exception {
523440
InputStream input = null;
524441
GenericDatumWriter<GenericRecord> writer = null;

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
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;
89

910
import java.util.Date;
1011
import java.util.HashMap;
@@ -282,6 +283,24 @@ public String signBase32Encoding(Algorithm algorithm) throws Exception {
282283
return JWS;
283284
}
284285

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+
285304
/**
286305
* Verifies that all the standard claims were provided
287306
* @throws Exception if all the standard claims weren't provided

0 commit comments

Comments
 (0)