Skip to content

Commit

Permalink
Eliminated usage of Guava and Commons-Lang libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
tkrullmann committed Nov 2, 2017
1 parent d2944a9 commit 7cd583c
Show file tree
Hide file tree
Showing 27 changed files with 152 additions and 128 deletions.
4 changes: 0 additions & 4 deletions gradle/dependency-management.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@ dependencyManagement {

dependency 'com.google.code.findbugs:jsr305:3.0.2'

dependency 'com.google.guava:guava:21.0'

dependency 'nf.fr.eraasoft:objectpool:1.1.2'

dependency 'org.apache.commons:commons-lang3:3.5'

dependencySet(group: 'org.bouncycastle', version: '1.55') {
entry 'bcpkix-jdk15on'
entry 'bcprov-jdk15on'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package org.unbrokendome.jsonwebtoken.spring.autoconfigure;

import com.google.common.io.ByteSource;
import com.google.common.io.CharSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.io.Resource;
import org.springframework.util.StringUtils;
import org.unbrokendome.jsonwebtoken.IOSupplier;
import org.unbrokendome.jsonwebtoken.JwtDecodeOnlyProcessorBuilder;
import org.unbrokendome.jsonwebtoken.JwtDecodingProcessorBuilderBase;
import org.unbrokendome.jsonwebtoken.JwtEncodeOnlyProcessorBuilder;
Expand All @@ -18,7 +17,7 @@
import org.unbrokendome.jsonwebtoken.signature.VerificationKeyResolver;
import org.unbrokendome.jsonwebtoken.spring.JwtProcessorMode;
import org.unbrokendome.jsonwebtoken.spring.io.Base64DecodingByteSource;
import org.unbrokendome.jsonwebtoken.spring.io.ResourceByteSourceAdapter;
import org.unbrokendome.jsonwebtoken.spring.io.ResourceBinaryDataSupplier;

import javax.annotation.Nullable;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -185,13 +184,13 @@ public void setKey(String key) {


@Nullable
protected ByteSource getKeyByteSource() {
protected IOSupplier<byte[]> getKeyByteSource() {
if (key != null && !key.trim().isEmpty()) {
return new Base64DecodingByteSource(CharSource.wrap(key), StandardCharsets.UTF_8);
return new Base64DecodingByteSource(() -> key, StandardCharsets.UTF_8);

} else if (keyResource != null) {
ByteSource encodedByteSource = new ResourceByteSourceAdapter(keyResource);
return new Base64DecodingByteSource(encodedByteSource);
return new Base64DecodingByteSource(
new ResourceBinaryDataSupplier(keyResource));

} else {
return null;
Expand All @@ -202,7 +201,7 @@ protected ByteSource getKeyByteSource() {
@Nullable
protected Key loadSigningKey(SignatureAlgorithm<?, ?> algorithm) {

ByteSource keyByteSource = getKeyByteSource();
IOSupplier<byte[]> keyByteSource = getKeyByteSource();
if (keyByteSource == null) {
if (algorithm != SignatureAlgorithms.NONE) {
throw new IllegalStateException("A signature algorithm other than NONE was specified, but no signing " +
Expand Down Expand Up @@ -234,7 +233,7 @@ protected Key loadSigningKey(SignatureAlgorithm<?, ?> algorithm) {
@Nullable
protected Key loadVerificationKey(SignatureAlgorithm<?, ?> algorithm) {

ByteSource keyByteSource = getKeyByteSource();
IOSupplier<byte[]> keyByteSource = getKeyByteSource();
if (keyByteSource == null) {
if (algorithm != SignatureAlgorithms.NONE) {
throw new IllegalStateException("A signature algorithm other than NONE was specified, but no verification " +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
package org.unbrokendome.jsonwebtoken.spring.io;

import com.google.common.io.ByteSource;
import com.google.common.io.CharSource;
import org.unbrokendome.jsonwebtoken.IOSupplier;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Base64;


public final class Base64DecodingByteSource extends ByteSource {
public final class Base64DecodingByteSource implements IOSupplier<byte[]> {

private final ByteSource encoded;
private final IOSupplier<byte[]> encoded;


public Base64DecodingByteSource(ByteSource encoded) {
public Base64DecodingByteSource(IOSupplier<byte[]> encoded) {
this.encoded = encoded;
}


public Base64DecodingByteSource(CharSource encoded, Charset charset) {
this(encoded.asByteSource(charset));
public Base64DecodingByteSource(IOSupplier<String> encoded, Charset charset) {
this(() -> encoded.get().getBytes(charset));
}


@Override
public byte[] read() throws IOException {
return Base64.getMimeDecoder().decode(encoded.read());
}


@Override
public InputStream openStream() throws IOException {
InputStream encodedStream = encoded.openStream();
return Base64.getDecoder().wrap(encodedStream);
public byte[] get() throws IOException {
byte[] encodedBytes = encoded.get();
return Base64.getMimeDecoder().decode(encodedBytes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.unbrokendome.jsonwebtoken.spring.io;

import org.springframework.core.io.Resource;
import org.springframework.util.StreamUtils;
import org.unbrokendome.jsonwebtoken.IOSupplier;

import java.io.IOException;
import java.io.InputStream;


public class ResourceBinaryDataSupplier implements IOSupplier<byte[]> {

private final Resource resource;


public ResourceBinaryDataSupplier(Resource resource) {
this.resource = resource;
}


@Override
public byte[] get() throws IOException {
try (InputStream input = resource.getInputStream()) {
return StreamUtils.copyToByteArray(input);
}
}
}

This file was deleted.

3 changes: 0 additions & 3 deletions jwt/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ apply plugin: 'nebula.optional-base'

dependencies {
api 'com.fasterxml.jackson.core:jackson-databind'
api 'com.google.guava:guava'

implementation 'org.apache.commons:commons-lang3'
implementation 'org.slf4j:slf4j-api'
implementation 'nf.fr.eraasoft:objectpool'

implementation 'org.bouncycastle:bcpkix-jdk15on', optional
implementation 'org.bouncycastle:bcprov-jdk15on', optional

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package org.unbrokendome.jsonwebtoken;

import org.apache.commons.lang3.builder.HashCodeBuilder;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Objects;


/**
Expand Down Expand Up @@ -88,7 +87,7 @@ private boolean equals(BinaryData other) {

@Override
public int hashCode() {
return new HashCodeBuilder().append(buffer).toHashCode();
return Objects.hash(buffer);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.unbrokendome.jsonwebtoken;

import com.google.common.collect.ImmutableSet;

import javax.annotation.Nonnull;
import java.time.Instant;
import java.util.Set;
Expand Down Expand Up @@ -57,7 +55,7 @@ default ClaimsBuilder setAudience(String audience) {
*/
@Nonnull
default ClaimsBuilder setAudiences(Set<String> audiences) {
return set(AUDIENCE, ImmutableSet.copyOf(audiences));
return set(AUDIENCE, audiences);
}


Expand Down
18 changes: 18 additions & 0 deletions jwt/src/main/java/org/unbrokendome/jsonwebtoken/IOSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.unbrokendome.jsonwebtoken;

import java.io.IOException;


/**
* Represents a supplier of results which involves some I/O operation.
*
* <p>Similar to {@link java.util.function.Supplier}, but the {@link #get()} method may throw an
* {@link IOException}.
*
* @param <T> the type of results supplied by this supplier
*/
@FunctionalInterface
public interface IOSupplier<T> {

T get() throws IOException;
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package org.unbrokendome.jsonwebtoken.encoding;

import com.google.common.base.Splitter;
import org.apache.commons.lang3.StringUtils;
import org.unbrokendome.jsonwebtoken.BinaryData;
import org.unbrokendome.jsonwebtoken.Jws;
import org.unbrokendome.jsonwebtoken.encoding.text.Base64TextEncoding;
import org.unbrokendome.jsonwebtoken.impl.DefaultJws;

import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
import java.util.function.Function;


public final class JwsCompactDecoder implements JwsDecoder {

private static final char SEPARATOR = '.';

private final Splitter splitter = Splitter.on(SEPARATOR);
private final Function<String, BinaryData> textDecoder;


Expand All @@ -31,7 +30,15 @@ public JwsCompactDecoder() {

@Override
public Jws decode(String encoded) throws JwtMalformedTokenException {
List<String> parts = splitter.splitToList(encoded);
StringTokenizer tokenizer = new StringTokenizer(encoded, String.valueOf(SEPARATOR));
@SuppressWarnings({ "unchecked", "rawtypes" })
List<String> parts = (List) Collections.list(tokenizer);

// For the NONE algorithm, the token ends with a dot - the signature part is empty. StringTokenizer won't
// add a token for this, so let's add an empty third part manually
if (parts.size() == 2 && encoded.endsWith(".")) {
parts.add("");
}

if (parts.size() != 3) {
throw new JwtMalformedTokenException("Token must contain exactly 3 parts separated by a period");
Expand All @@ -52,7 +59,7 @@ private BinaryData decodePayload(String encoded) throws JwtMalformedTokenExcepti


private BinaryData decodeSignature(String encoded) throws JwtMalformedTokenException {
return StringUtils.isNotEmpty(encoded) ? decodeText(encoded) : BinaryData.EMPTY;
return !encoded.isEmpty() ? decodeText(encoded) : BinaryData.EMPTY;
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package org.unbrokendome.jsonwebtoken.impl;

import com.fasterxml.jackson.annotation.JsonValue;
import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import java.util.Map;
import java.util.Objects;


abstract class AbstractMapData {
Expand All @@ -13,7 +12,7 @@ abstract class AbstractMapData {


protected AbstractMapData(Map<String, Object> map) {
this.map = ImmutableMap.copyOf(map);
this.map = map;
}


Expand Down Expand Up @@ -41,7 +40,7 @@ private boolean equals(AbstractMapData other) {

@Override
public int hashCode() {
return new HashCodeBuilder().append(map).toHashCode();
return Objects.hash(map);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package org.unbrokendome.jsonwebtoken.impl;

import com.google.common.collect.ImmutableMap;
import org.unbrokendome.jsonwebtoken.MapData;
import org.unbrokendome.jsonwebtoken.MapDataBuilder;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

Expand All @@ -26,7 +27,7 @@ public Object get(String key) {
@Override
@Nonnull
public Map<String, Object> asMap() {
return ImmutableMap.copyOf(map);
return Collections.unmodifiableMap(new HashMap<>(map));
}


Expand Down
Loading

0 comments on commit 7cd583c

Please sign in to comment.