Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ compileJava {

dependencies {
implementation 'com.fasterxml.jackson.core:jackson-databind:2.10.0.pr3'
implementation 'commons-codec:commons-codec:1.12'
testImplementation 'org.bouncycastle:bcprov-jdk15on:1.60'
testImplementation 'junit:junit:4.12'
testImplementation 'net.jodah:concurrentunit:0.4.3'
Expand Down
12 changes: 4 additions & 8 deletions lib/src/main/java/com/auth0/jwt/JWTCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.apache.commons.codec.binary.Base64;

import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;

/**
Expand Down Expand Up @@ -480,11 +476,11 @@ private void addClaim(String name, Object value) {
}

private String sign() throws SignatureGenerationException {
String header = Base64.encodeBase64URLSafeString(headerJson.getBytes(StandardCharsets.UTF_8));
String payload = Base64.encodeBase64URLSafeString(payloadJson.getBytes(StandardCharsets.UTF_8));
String header = Base64.getUrlEncoder().withoutPadding().encodeToString(headerJson.getBytes(StandardCharsets.UTF_8));
String payload = Base64.getUrlEncoder().withoutPadding().encodeToString(payloadJson.getBytes(StandardCharsets.UTF_8));

byte[] signatureBytes = algorithm.sign(header.getBytes(StandardCharsets.UTF_8), payload.getBytes(StandardCharsets.UTF_8));
String signature = Base64.encodeBase64URLSafeString((signatureBytes));
String signature = Base64.getUrlEncoder().withoutPadding().encodeToString((signatureBytes));

return String.format("%s.%s.%s", header, payload, signature);
}
Expand Down
12 changes: 6 additions & 6 deletions lib/src/main/java/com/auth0/jwt/JWTDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.Header;
import com.auth0.jwt.interfaces.Payload;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.StringUtils;

import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.Base64;
import java.util.Date;
import java.util.List;
import java.util.Map;
Expand All @@ -35,10 +35,10 @@ final class JWTDecoder implements DecodedJWT, Serializable {
String headerJson;
String payloadJson;
try {
headerJson = StringUtils.newStringUtf8(Base64.decodeBase64(parts[0]));
payloadJson = StringUtils.newStringUtf8(Base64.decodeBase64(parts[1]));
} catch (NullPointerException e) {
throw new JWTDecodeException("The UTF-8 Charset isn't initialized.", e);
headerJson = new String(Base64.getUrlDecoder().decode(parts[0]), StandardCharsets.UTF_8);
payloadJson = new String(Base64.getUrlDecoder().decode(parts[1]), StandardCharsets.UTF_8);
} catch (NullPointerException | IllegalArgumentException e) {
throw new JWTDecodeException("The UTF-8 Charset isn't initialized or the token is not valid Base64.", e);
}
header = converter.parseHeader(headerJson);
payload = converter.parsePayload(payloadJson);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.ECDSAKeyProvider;
import org.apache.commons.codec.binary.Base64;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.util.Base64;

class ECDSAAlgorithm extends Algorithm {
private final ECDSAKeyProvider keyProvider;
Expand All @@ -36,9 +36,8 @@ class ECDSAAlgorithm extends Algorithm {

@Override
public void verify(DecodedJWT jwt) throws SignatureVerificationException {
byte[] signatureBytes = Base64.decodeBase64(jwt.getSignature());

try {
byte[] signatureBytes = Base64.getUrlDecoder().decode(jwt.getSignature());
ECPublicKey publicKey = keyProvider.getPublicKeyById(jwt.getKeyId());
if (publicKey == null) {
throw new IllegalStateException("The given Public Key is null.");
Expand All @@ -48,7 +47,7 @@ public void verify(DecodedJWT jwt) throws SignatureVerificationException {
if (!valid) {
throw new SignatureVerificationException(this);
}
} catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException | IllegalStateException e) {
} catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException | IllegalStateException | IllegalArgumentException e) {
throw new SignatureVerificationException(this, e);
}
}
Expand Down
7 changes: 3 additions & 4 deletions lib/src/main/java/com/auth0/jwt/algorithms/HMACAlgorithm.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import com.auth0.jwt.exceptions.SignatureGenerationException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.apache.commons.codec.binary.Base64;

import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

class HMACAlgorithm extends Algorithm {
private final CryptoHelper crypto;
Expand Down Expand Up @@ -41,14 +41,13 @@ static byte[] getSecretBytes(String secret) throws IllegalArgumentException {

@Override
public void verify(DecodedJWT jwt) throws SignatureVerificationException {
byte[] signatureBytes = Base64.decodeBase64(jwt.getSignature());

try {
byte[] signatureBytes = Base64.getUrlDecoder().decode(jwt.getSignature());
boolean valid = crypto.verifySignatureFor(getDescription(), secret, jwt.getHeader(), jwt.getPayload(), signatureBytes);
if (!valid) {
throw new SignatureVerificationException(this);
}
} catch (IllegalStateException | InvalidKeyException | NoSuchAlgorithmException e) {
} catch (IllegalStateException | InvalidKeyException | NoSuchAlgorithmException | IllegalArgumentException e) {
throw new SignatureVerificationException(this, e);
}
}
Expand Down
14 changes: 10 additions & 4 deletions lib/src/main/java/com/auth0/jwt/algorithms/NoneAlgorithm.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import com.auth0.jwt.exceptions.SignatureGenerationException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.apache.commons.codec.binary.Base64;

import java.util.Base64;

class NoneAlgorithm extends Algorithm {

Expand All @@ -13,9 +14,14 @@ class NoneAlgorithm extends Algorithm {

@Override
public void verify(DecodedJWT jwt) throws SignatureVerificationException {
byte[] signatureBytes = Base64.decodeBase64(jwt.getSignature());
if (signatureBytes.length > 0) {
throw new SignatureVerificationException(this);
try {
byte[] signatureBytes = Base64.getUrlDecoder().decode(jwt.getSignature());

if (signatureBytes.length > 0) {
throw new SignatureVerificationException(this);
}
} catch (IllegalArgumentException e) {
throw new SignatureVerificationException(this, e);
}
}

Expand Down
7 changes: 3 additions & 4 deletions lib/src/main/java/com/auth0/jwt/algorithms/RSAAlgorithm.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.RSAKeyProvider;
import org.apache.commons.codec.binary.Base64;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Base64;

class RSAAlgorithm extends Algorithm {
private final RSAKeyProvider keyProvider;
Expand All @@ -32,9 +32,8 @@ class RSAAlgorithm extends Algorithm {

@Override
public void verify(DecodedJWT jwt) throws SignatureVerificationException {
byte[] signatureBytes = Base64.decodeBase64(jwt.getSignature());

try {
byte[] signatureBytes = Base64.getUrlDecoder().decode(jwt.getSignature());
RSAPublicKey publicKey = keyProvider.getPublicKeyById(jwt.getKeyId());
if (publicKey == null) {
throw new IllegalStateException("The given Public Key is null.");
Expand All @@ -43,7 +42,7 @@ public void verify(DecodedJWT jwt) throws SignatureVerificationException {
if (!valid) {
throw new SignatureVerificationException(this);
}
} catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException | IllegalStateException e) {
} catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException | IllegalArgumentException | IllegalStateException e) {
throw new SignatureVerificationException(this, e);
}
}
Expand Down
30 changes: 15 additions & 15 deletions lib/src/test/java/com/auth0/jwt/JWTCreatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.auth0.jwt.interfaces.ECDSAKeyProvider;
import com.auth0.jwt.interfaces.RSAKeyProvider;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.codec.binary.Base64;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand Down Expand Up @@ -48,7 +48,7 @@ public void shouldAddHeaderClaim() {

assertThat(signed, is(notNullValue()));
String[] parts = signed.split("\\.");
String headerJson = new String(Base64.decodeBase64(parts[0]), StandardCharsets.UTF_8);
String headerJson = new String(Base64.getUrlDecoder().decode(parts[0]), StandardCharsets.UTF_8);
assertThat(headerJson, JsonMatcher.hasEntry("asd", 123));
}

Expand All @@ -73,7 +73,7 @@ public void shouldOverwriteExistingHeaderIfHeaderMapContainsTheSameKey() {

assertThat(signed, is(notNullValue()));
String[] parts = signed.split("\\.");
String headerJson = new String(Base64.decodeBase64(parts[0]), StandardCharsets.UTF_8);
String headerJson = new String(Base64.getUrlDecoder().decode(parts[0]), StandardCharsets.UTF_8);
assertThat(headerJson, JsonMatcher.hasEntry(PublicClaims.KEY_ID, "xyz"));
}

Expand All @@ -89,7 +89,7 @@ public void shouldOverwriteExistingHeadersWhenSettingSameHeaderKey() {

assertThat(signed, is(notNullValue()));
String[] parts = signed.split("\\.");
String headerJson = new String(Base64.decodeBase64(parts[0]), StandardCharsets.UTF_8);
String headerJson = new String(Base64.getUrlDecoder().decode(parts[0]), StandardCharsets.UTF_8);
assertThat(headerJson, JsonMatcher.hasEntry(PublicClaims.KEY_ID, "abc"));
}

Expand All @@ -106,7 +106,7 @@ public void shouldRemoveHeaderIfTheValueIsNull() {

assertThat(signed, is(notNullValue()));
String[] parts = signed.split("\\.");
String headerJson = new String(Base64.decodeBase64(parts[0]), StandardCharsets.UTF_8);
String headerJson = new String(Base64.getUrlDecoder().decode(parts[0]), StandardCharsets.UTF_8);
assertThat(headerJson, JsonMatcher.isNotPresent(PublicClaims.KEY_ID));
assertThat(headerJson, JsonMatcher.hasEntry("test2", "isSet"));
}
Expand All @@ -119,7 +119,7 @@ public void shouldAddKeyId() {

assertThat(signed, is(notNullValue()));
String[] parts = signed.split("\\.");
String headerJson = new String(Base64.decodeBase64(parts[0]), StandardCharsets.UTF_8);
String headerJson = new String(Base64.getUrlDecoder().decode(parts[0]), StandardCharsets.UTF_8);
assertThat(headerJson, JsonMatcher.hasEntry("kid", "56a8bd44da435300010000015f5ed"));
}

Expand All @@ -135,7 +135,7 @@ public void shouldAddKeyIdIfAvailableFromRSAAlgorithms() throws Exception {

assertThat(signed, is(notNullValue()));
String[] parts = signed.split("\\.");
String headerJson = new String(Base64.decodeBase64(parts[0]), StandardCharsets.UTF_8);
String headerJson = new String(Base64.getUrlDecoder().decode(parts[0]), StandardCharsets.UTF_8);
assertThat(headerJson, JsonMatcher.hasEntry("kid", "my-key-id"));
}

Expand All @@ -152,7 +152,7 @@ public void shouldNotOverwriteKeyIdIfAddedFromRSAAlgorithms() throws Exception {

assertThat(signed, is(notNullValue()));
String[] parts = signed.split("\\.");
String headerJson = new String(Base64.decodeBase64(parts[0]), StandardCharsets.UTF_8);
String headerJson = new String(Base64.getUrlDecoder().decode(parts[0]), StandardCharsets.UTF_8);
assertThat(headerJson, JsonMatcher.hasEntry("kid", "my-key-id"));
}

Expand All @@ -168,7 +168,7 @@ public void shouldAddKeyIdIfAvailableFromECDSAAlgorithms() throws Exception {

assertThat(signed, is(notNullValue()));
String[] parts = signed.split("\\.");
String headerJson = new String(Base64.decodeBase64(parts[0]), StandardCharsets.UTF_8);
String headerJson = new String(Base64.getUrlDecoder().decode(parts[0]), StandardCharsets.UTF_8);
assertThat(headerJson, JsonMatcher.hasEntry("kid", "my-key-id"));
}

Expand All @@ -185,7 +185,7 @@ public void shouldNotOverwriteKeyIdIfAddedFromECDSAAlgorithms() throws Exception

assertThat(signed, is(notNullValue()));
String[] parts = signed.split("\\.");
String headerJson = new String(Base64.decodeBase64(parts[0]), StandardCharsets.UTF_8);
String headerJson = new String(Base64.getUrlDecoder().decode(parts[0]), StandardCharsets.UTF_8);
assertThat(headerJson, JsonMatcher.hasEntry("kid", "my-key-id"));
}

Expand Down Expand Up @@ -315,7 +315,7 @@ public void shouldSetCorrectAlgorithmInTheHeader() {

assertThat(signed, is(notNullValue()));
String[] parts = signed.split("\\.");
String headerJson = new String(Base64.decodeBase64(parts[0]), StandardCharsets.UTF_8);
String headerJson = new String(Base64.getUrlDecoder().decode(parts[0]), StandardCharsets.UTF_8);
assertThat(headerJson, JsonMatcher.hasEntry("alg", "HS256"));
}

Expand All @@ -326,7 +326,7 @@ public void shouldSetDefaultTypeInTheHeader() {

assertThat(signed, is(notNullValue()));
String[] parts = signed.split("\\.");
String headerJson = new String(Base64.decodeBase64(parts[0]), StandardCharsets.UTF_8);
String headerJson = new String(Base64.getUrlDecoder().decode(parts[0]), StandardCharsets.UTF_8);
assertThat(headerJson, JsonMatcher.hasEntry("typ", "JWT"));
}

Expand All @@ -339,7 +339,7 @@ public void shouldSetCustomTypeInTheHeader() {

assertThat(signed, is(notNullValue()));
String[] parts = signed.split("\\.");
String headerJson = new String(Base64.decodeBase64(parts[0]), StandardCharsets.UTF_8);
String headerJson = new String(Base64.getUrlDecoder().decode(parts[0]), StandardCharsets.UTF_8);
assertThat(headerJson, JsonMatcher.hasEntry("typ", "passport"));
}

Expand Down Expand Up @@ -531,7 +531,7 @@ public void shouldAcceptCustomMapClaimOfBasicObjectTypes() throws Exception {
assertThat(jwt, is(notNullValue()));
String[] parts = jwt.split("\\.");

String body = new String(Base64.decodeBase64(parts[1]), StandardCharsets.UTF_8);
String body = new String(Base64.getUrlDecoder().decode(parts[1]), StandardCharsets.UTF_8);
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> map = (Map<String, Object>) mapper.readValue(body, Map.class).get("data");

Expand Down Expand Up @@ -593,7 +593,7 @@ public void shouldAcceptCustomListClaimOfBasicObjectTypes() throws Exception {
assertThat(jwt, is(notNullValue()));
String[] parts = jwt.split("\\.");

String body = new String(Base64.decodeBase64(parts[1]), StandardCharsets.UTF_8);
String body = new String(Base64.getUrlDecoder().decode(parts[1]), StandardCharsets.UTF_8);
ObjectMapper mapper = new ObjectMapper();
List<Object> list = (List<Object>) mapper.readValue(body, Map.class).get("data");

Expand Down
24 changes: 21 additions & 3 deletions lib/src/test/java/com/auth0/jwt/JWTDecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.auth0.jwt.impl.NullClaim;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.apache.commons.codec.binary.Base64;
import org.hamcrest.collection.IsCollectionWithSize;
import org.hamcrest.core.IsCollectionContaining;
import org.junit.Assert;
Expand All @@ -15,6 +14,7 @@
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.Base64;
import java.util.Date;
import java.util.Map;

Expand Down Expand Up @@ -65,6 +65,24 @@ public void shouldThrowIfHeaderHasInvalidJSONFormat() {
customJWT(invalidJson, validJson, "signature");
}

@Test
public void shouldThrowWhenHeaderNotValidBase64() {
exception.expect(JWTDecodeException.class);
exception.expectCause(isA(IllegalArgumentException.class));

String jwt = "eyJhbGciOiJub25l+IiwiY3R5IjoiSldUIn0.eyJpc3MiOiJhdXRoMCJ9.Ox-WRXRaGAuWt2KfPvWiGcCrPqZtbp_4OnQzZXaTfss";
JWT.decode(jwt);
}

@Test
public void shouldThrowWhenPayloadNotValidBase64() {
exception.expect(JWTDecodeException.class);
exception.expectCause(isA(IllegalArgumentException.class));

String jwt = "eyJhbGciOiJub25lIiwiY3R5IjoiSldUIn0.eyJpc3MiOiJhdXRo+MCJ9.Ox-WRXRaGAuWt2KfPvWiGcCrPqZtbp_4OnQzZXaTfss";
JWT.decode(jwt);
}

// Parts

@Test
Expand Down Expand Up @@ -364,8 +382,8 @@ public void shouldSerializeAndDeserialize() throws Exception {
//Helper Methods

private DecodedJWT customJWT(String jsonHeader, String jsonPayload, String signature) {
String header = Base64.encodeBase64URLSafeString(jsonHeader.getBytes(StandardCharsets.UTF_8));
String body = Base64.encodeBase64URLSafeString(jsonPayload.getBytes(StandardCharsets.UTF_8));
String header = Base64.getUrlEncoder().withoutPadding().encodeToString(jsonHeader.getBytes(StandardCharsets.UTF_8));
String body = Base64.getUrlEncoder().withoutPadding().encodeToString(jsonPayload.getBytes(StandardCharsets.UTF_8));
return JWT.decode(String.format("%s.%s.%s", header, body, signature));
}

Expand Down
Loading