X Tutup
Skip to content

Commit 8da0a0a

Browse files
author
Justin Dahmubed
committed
Adding decoder for base16,32,64 and working on jsonencoder
1 parent 31d3230 commit 8da0a0a

22 files changed

+474
-187
lines changed

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

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

3+
import com.auth0.jwt.creators.EncodeType;
34
import com.auth0.jwt.exceptions.JWTDecodeException;
45
import com.auth0.jwt.impl.JWTParser;
56
import com.auth0.jwt.interfaces.Claim;
67
import com.auth0.jwt.interfaces.DecodedJWT;
78
import com.auth0.jwt.interfaces.Header;
89
import com.auth0.jwt.interfaces.Payload;
10+
import org.apache.commons.codec.binary.Base32;
911
import org.apache.commons.codec.binary.Base64;
12+
import org.apache.commons.codec.binary.Hex;
1013
import org.apache.commons.codec.binary.StringUtils;
1114

1215
import java.util.Date;
@@ -23,17 +26,34 @@ public final class JWTDecoder implements DecodedJWT {
2326
private final Header header;
2427
private final Payload payload;
2528

26-
public JWTDecoder(String jwt) throws JWTDecodeException {
29+
public JWTDecoder(String jwt, EncodeType encodeType) throws Exception {
2730
parts = TokenUtils.splitToken(jwt);
2831
final JWTParser converter = new JWTParser();
29-
String headerJson;
30-
String payloadJson;
31-
try {
32-
headerJson = StringUtils.newStringUtf8(Base64.decodeBase64(parts[0]));
33-
payloadJson = StringUtils.newStringUtf8(Base64.decodeBase64(parts[1]));
34-
} catch (NullPointerException e) {
35-
throw new JWTDecodeException("The UTF-8 Charset isn't initialized.", e);
32+
String headerJson = null;
33+
String payloadJson = null;
34+
switch (encodeType) {
35+
case Base16:
36+
headerJson = StringUtils.newStringUtf8(Hex.decodeHex(parts[0]));
37+
payloadJson = StringUtils.newStringUtf8(Hex.decodeHex(parts[1]));
38+
break;
39+
case Base32: {
40+
Base32 base32 = new Base32();
41+
headerJson = StringUtils.newStringUtf8(base32.decode(parts[0]));
42+
payloadJson = StringUtils.newStringUtf8(base32.decode(parts[1]));
43+
break;
44+
}
45+
case Base64:
46+
headerJson = StringUtils.newStringUtf8(Base64.decodeBase64(parts[0]));
47+
payloadJson = StringUtils.newStringUtf8(Base64.decodeBase64(parts[1]));
48+
break;
49+
case JsonEncode:
50+
break;
51+
//token = jwtCreator.signJsonEncode();
3652
}
53+
//headerJson = StringUtils.newStringUtf8(Base64.decodeBase64(parts[0]));
54+
//headerJson = StringUtils.newStringUtf8(Hex.decodeHex(parts[0]));
55+
//payloadJson = StringUtils.newStringUtf8(Base64.decodeBase64(parts[1]));
56+
//payloadJson = StringUtils.newStringUtf8(Hex.decodeHex(parts[1]));
3757
header = converter.parseHeader(headerJson);
3858
payload = converter.parsePayload(payloadJson);
3959
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.auth0.jwt.algorithms;
22

3+
import com.auth0.jwt.creators.EncodeType;
34
import com.auth0.jwt.exceptions.SignatureGenerationException;
45
import com.auth0.jwt.exceptions.SignatureVerificationException;
56
import com.auth0.jwt.interfaces.DecodedJWT;
@@ -363,7 +364,7 @@ public String toString() {
363364
* @param jwt the already decoded JWT that it's going to be verified.
364365
* @throws SignatureVerificationException if the Token's Signature is invalid, meaning that it doesn't match the signatureBytes, or if the Key is invalid.
365366
*/
366-
public abstract void verify(DecodedJWT jwt) throws SignatureVerificationException;
367+
public abstract void verify(DecodedJWT jwt, EncodeType encodeType) throws Exception;
367368

368369
/**
369370
* Sign the given content using this Algorithm instance.

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

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

3+
import com.auth0.jwt.creators.EncodeType;
34
import com.auth0.jwt.exceptions.SignatureGenerationException;
45
import com.auth0.jwt.exceptions.SignatureVerificationException;
56
import com.auth0.jwt.interfaces.DecodedJWT;
67
import com.auth0.jwt.interfaces.ECDSAKeyProvider;
8+
import org.apache.commons.codec.binary.Base32;
79
import org.apache.commons.codec.binary.Base64;
10+
import org.apache.commons.codec.binary.Hex;
11+
import org.apache.commons.codec.binary.StringUtils;
812

913
import java.nio.charset.StandardCharsets;
1014
import java.security.InvalidKeyException;
@@ -35,9 +39,26 @@ class ECDSAAlgorithm extends Algorithm {
3539
}
3640

3741
@Override
38-
public void verify(DecodedJWT jwt) throws SignatureVerificationException {
42+
public void verify(DecodedJWT jwt, EncodeType encodeType) throws Exception {
3943
byte[] contentBytes = String.format("%s.%s", jwt.getHeader(), jwt.getPayload()).getBytes(StandardCharsets.UTF_8);
40-
byte[] signatureBytes = Base64.decodeBase64(jwt.getSignature());
44+
byte[] signatureBytes = null;
45+
String signature = jwt.getSignature();
46+
switch (encodeType) {
47+
case Base16:
48+
signatureBytes = Hex.decodeHex(signature);
49+
break;
50+
case Base32: {
51+
Base32 base32 = new Base32();
52+
signatureBytes = base32.decode(signature);
53+
break;
54+
}
55+
case Base64:
56+
signatureBytes = Base64.decodeBase64(signature);
57+
break;
58+
case JsonEncode:
59+
break;
60+
//token = jwtCreator.signJsonEncode();
61+
}
4162

4263
try {
4364
ECPublicKey publicKey = keyProvider.getPublicKeyById(jwt.getKeyId());

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.auth0.jwt.algorithms;
22

3+
import com.auth0.jwt.creators.EncodeType;
34
import com.auth0.jwt.exceptions.SignatureGenerationException;
45
import com.auth0.jwt.exceptions.SignatureVerificationException;
56
import com.auth0.jwt.interfaces.DecodedJWT;
67
import org.apache.commons.codec.CharEncoding;
8+
import org.apache.commons.codec.binary.Base32;
79
import org.apache.commons.codec.binary.Base64;
10+
import org.apache.commons.codec.binary.Hex;
811

912
import java.io.UnsupportedEncodingException;
1013
import java.nio.charset.StandardCharsets;
@@ -43,9 +46,26 @@ static byte[] getSecretBytes(String secret) throws IllegalArgumentException, Uns
4346
}
4447

4548
@Override
46-
public void verify(DecodedJWT jwt) throws SignatureVerificationException {
49+
public void verify(DecodedJWT jwt, EncodeType encodeType) throws Exception {
4750
byte[] contentBytes = String.format("%s.%s", jwt.getHeader(), jwt.getPayload()).getBytes(StandardCharsets.UTF_8);
48-
byte[] signatureBytes = Base64.decodeBase64(jwt.getSignature());
51+
byte[] signatureBytes = null;
52+
String signature = jwt.getSignature();
53+
switch (encodeType) {
54+
case Base16:
55+
signatureBytes = Hex.decodeHex(signature);
56+
break;
57+
case Base32: {
58+
Base32 base32 = new Base32();
59+
signatureBytes = base32.decode(signature);
60+
break;
61+
}
62+
case Base64:
63+
signatureBytes = Base64.decodeBase64(signature);
64+
break;
65+
case JsonEncode:
66+
break;
67+
//token = jwtCreator.signJsonEncode();
68+
}
4969

5070
try {
5171
boolean valid = crypto.verifySignatureFor(getDescription(), secret, contentBytes, signatureBytes);

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.auth0.jwt.algorithms;
22

3+
import com.auth0.jwt.creators.EncodeType;
34
import com.auth0.jwt.exceptions.SignatureGenerationException;
45
import com.auth0.jwt.exceptions.SignatureVerificationException;
56
import com.auth0.jwt.interfaces.DecodedJWT;
7+
import org.apache.commons.codec.binary.Base32;
68
import org.apache.commons.codec.binary.Base64;
9+
import org.apache.commons.codec.binary.Hex;
710

811
class NoneAlgorithm extends Algorithm {
912

@@ -12,8 +15,25 @@ class NoneAlgorithm extends Algorithm {
1215
}
1316

1417
@Override
15-
public void verify(DecodedJWT jwt) throws SignatureVerificationException {
16-
byte[] signatureBytes = Base64.decodeBase64(jwt.getSignature());
18+
public void verify(DecodedJWT jwt, EncodeType encodeType) throws Exception {
19+
byte[] signatureBytes = null;
20+
String signature = jwt.getSignature();
21+
switch (encodeType) {
22+
case Base16:
23+
signatureBytes = Hex.decodeHex(signature);
24+
break;
25+
case Base32: {
26+
Base32 base32 = new Base32();
27+
signatureBytes = base32.decode(signature);
28+
break;
29+
}
30+
case Base64:
31+
signatureBytes = Base64.decodeBase64(signature);
32+
break;
33+
case JsonEncode:
34+
break;
35+
//token = jwtCreator.signJsonEncode();
36+
}
1737
if (signatureBytes.length > 0) {
1838
throw new SignatureVerificationException(this);
1939
}

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.auth0.jwt.algorithms;
22

3+
import com.auth0.jwt.creators.EncodeType;
34
import com.auth0.jwt.exceptions.SignatureGenerationException;
45
import com.auth0.jwt.exceptions.SignatureVerificationException;
56
import com.auth0.jwt.interfaces.DecodedJWT;
67
import com.auth0.jwt.interfaces.RSAKeyProvider;
8+
import org.apache.commons.codec.binary.Base32;
79
import org.apache.commons.codec.binary.Base64;
10+
import org.apache.commons.codec.binary.Hex;
811

912
import java.nio.charset.StandardCharsets;
1013
import java.security.InvalidKeyException;
@@ -33,9 +36,26 @@ class RSAAlgorithm extends Algorithm {
3336
}
3437

3538
@Override
36-
public void verify(DecodedJWT jwt) throws SignatureVerificationException {
39+
public void verify(DecodedJWT jwt, EncodeType encodeType) throws Exception {
3740
byte[] contentBytes = String.format("%s.%s", jwt.getHeader(), jwt.getPayload()).getBytes(StandardCharsets.UTF_8);
38-
byte[] signatureBytes = Base64.decodeBase64(jwt.getSignature());
41+
byte[] signatureBytes = null;
42+
String signature = jwt.getSignature();
43+
switch (encodeType) {
44+
case Base16:
45+
signatureBytes = Hex.decodeHex(signature);
46+
break;
47+
case Base32: {
48+
Base32 base32 = new Base32();
49+
signatureBytes = base32.decode(signature);
50+
break;
51+
}
52+
case Base64:
53+
signatureBytes = Base64.decodeBase64(signature);
54+
break;
55+
case JsonEncode:
56+
break;
57+
//token = jwtCreator.signJsonEncode();
58+
}
3959

4060
try {
4161
RSAPublicKey publicKey = keyProvider.getPublicKeyById(jwt.getKeyId());
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package com.auth0.jwt.creators;
22

33
public enum EncodeType {
4-
Base16, Base32, Base64;
4+
Base16, Base32, Base64, JsonEncode;
55
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,24 @@ public String signBase32Encoding(Algorithm algorithm) throws Exception {
243243
return JWS;
244244
}
245245

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

0 commit comments

Comments
 (0)
X Tutup