Skip to content

Commit

Permalink
Replaced internal Base64 by java.util.Base64 (#3803) (#3829)
Browse files Browse the repository at this point in the history
* Replaced core-common.src.main.java.org.glassfish.jersey.internal.util.Base64 with java.util.Base64
* fixed conversion to String after decoding of Base64 encode message
Signed-off-by: Michael Durawa <[email protected]>
  • Loading branch information
mdurawa authored and jansupol committed Jun 26, 2018
1 parent 4125a33 commit eafb9bd
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 224 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package org.glassfish.jersey.jdk.connector.internal;

import java.nio.charset.Charset;
import java.util.Base64;

import org.glassfish.jersey.internal.util.Base64;

/**
* @author Ondrej Kosatka (ondrej.kosatka at oracle.com)
Expand Down Expand Up @@ -46,6 +46,6 @@ static String generateAuthorizationHeader(String userName, String password) thro
System.arraycopy(prefix, 0, usernamePassword, 0, prefix.length);
System.arraycopy(passwordBytes, 0, usernamePassword, prefix.length, passwordBytes.length);

return "Basic " + Base64.encodeAsString(usernamePassword);
return "Basic " + Base64.getEncoder().encodeToString(usernamePassword);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Base64;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -31,7 +32,6 @@

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.internal.util.Base64;
import org.glassfish.jersey.jdk.connector.JdkConnectorProperties;
import org.glassfish.jersey.jdk.connector.JdkConnectorProvider;
import org.glassfish.jersey.server.ResourceConfig;
Expand Down Expand Up @@ -227,7 +227,7 @@ private boolean verifyBasicAuthorizationHeader(Response response, String authori
response.setStatus(400);
return false;
}
String decoded = new String(Base64.decode(authorizationHeader.substring(6).getBytes()),
String decoded = new String(Base64.getDecoder().decode(authorizationHeader.substring(6).getBytes()),
CHARACTER_SET);
final String[] split = decoded.split(":");
final String username = split[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

package org.glassfish.jersey.client.authentication;

import java.util.Base64;

import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientResponseContext;
import javax.ws.rs.core.HttpHeaders;

import org.glassfish.jersey.client.internal.LocalizationMessages;
import org.glassfish.jersey.internal.util.Base64;

/**
* Implementation of Basic Http Authentication method (RFC 2617).
Expand Down Expand Up @@ -61,7 +62,7 @@ private String calculateAuthentication(HttpAuthenticationFilter.Credentials cred
System.arraycopy(prefix, 0, usernamePassword, 0, prefix.length);
System.arraycopy(password, 0, usernamePassword, prefix.length, password.length);

return "Basic " + Base64.encodeAsString(usernamePassword);
return "Basic " + Base64.getEncoder().encodeToString(usernamePassword);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.glassfish.jersey.client.filter;


import java.util.Base64;
import java.util.concurrent.Future;

import javax.ws.rs.client.Client;
Expand All @@ -34,7 +35,7 @@
import org.glassfish.jersey.client.spi.AsyncConnectorCallback;
import org.glassfish.jersey.client.spi.Connector;
import org.glassfish.jersey.client.spi.ConnectorProvider;
import org.glassfish.jersey.internal.util.Base64;


import org.junit.Test;
import static org.junit.Assert.assertEquals;
Expand All @@ -53,7 +54,8 @@ public void testGet() {
Invocation.Builder invBuilder = client.target(UriBuilder.fromUri("/").build()).request();
Response r = invBuilder.get();

assertEquals("Basic " + Base64.encodeAsString("Uzivatelske jmeno:Heslo"), r.getHeaderString(HttpHeaders.AUTHORIZATION));
assertEquals("Basic " + Base64.getEncoder().encodeToString("Uzivatelske jmeno:Heslo".getBytes()),
r.getHeaderString(HttpHeaders.AUTHORIZATION));
}

@Test
Expand All @@ -63,7 +65,7 @@ public void testBlankUsernamePassword() {
Invocation.Builder invBuilder = client.target(UriBuilder.fromUri("/").build()).request();
Response r = invBuilder.get();

assertEquals("Basic " + Base64.encodeAsString(":"), r.getHeaderString(HttpHeaders.AUTHORIZATION));
assertEquals("Basic " + Base64.getEncoder().encodeToString(":".getBytes()), r.getHeaderString(HttpHeaders.AUTHORIZATION));
}

private static class TestConnector implements Connector, ConnectorProvider {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.glassfish.jersey.internal.util;

import java.util.Arrays;
import java.util.Base64;

import org.junit.Test;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -48,57 +49,57 @@ public class Base64Test {
@Test
public void testEncodeString() throws Exception {
for (int i = 0; i < decoded.length; i++) {
assertEquals(encoded[i], new String(Base64.encode(decoded[i].getBytes("ASCII")), "ASCII"));
assertEquals(encoded[i], new String(Base64.getEncoder().encode(decoded[i].getBytes("ASCII")), "ASCII"));
}
}

@Test
public void testDecodeString() throws Exception {
for (int i = 0; i < encoded.length; i++) {
assertEquals(decoded[i], new String(Base64.decode(encoded[i].getBytes("ASCII")), "ASCII"));
assertEquals(decoded[i], new String(Base64.getDecoder().decode(encoded[i].getBytes("ASCII")), "ASCII"));
}
}

@Test
public void testRoundtripLengthMod3Equals0() {
byte[] data = {0, 1, 2, 3, 4, 5, 6, 7, 8};
byte[] result = Base64.decode(Base64.encode(data));
byte[] result = Base64.getDecoder().decode(Base64.getEncoder().encode(data));
assertTrue("failed to roundtrip value to base64", Arrays.equals(data, result));
}

@Test
public void testRoundtripLengthMod3Equals1() {
byte[] data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
byte[] result = Base64.decode(Base64.encode(data));
byte[] result = Base64.getDecoder().decode(Base64.getEncoder().encode(data));
assertTrue("failed to roundtrip value to base64", Arrays.equals(data, result));
}

@Test
public void testRoundtripLengthMod3Equals2() {
byte[] data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
byte[] result = Base64.decode(Base64.encode(data));
byte[] result = Base64.getDecoder().decode(Base64.getEncoder().encode(data));
assertTrue("failed to roundtrip value to base64", Arrays.equals(data, result));
}

@Test
public void testRoundtripOneByteGreaterThan127() {
byte[] data = {(byte) 128};
try {
byte[] result = Base64.decode(Base64.encode(data));
fail();
} catch (Exception e) {
byte[] result = Base64.getDecoder().decode(Base64.getEncoder().encode(data));
// ok
} catch (Exception e) {
fail();
}
}

@Test
public void testRoundtripAssortedValues() {
byte[] data = {0, 1, 63, 64, 65, (byte) 127, (byte) 128, (byte) 1299, (byte) 254, (byte) 255};
try {
Base64.decode(Base64.encode(data));
fail();
} catch (Exception e) {
Base64.getDecoder().decode(Base64.getEncoder().encode(data));
// ok
} catch (Exception e) {
fail();
}
}

Expand All @@ -109,10 +110,10 @@ public void testEncodeByteArray() {
data[i] = (byte) (255 - i);
}
try {
new String(Base64.encode(data));
fail();
} catch (Exception e) {
new String(Base64.getEncoder().encode(data));
// ok
} catch (Exception e) {
fail();
}
}

Expand All @@ -122,7 +123,7 @@ public void testDecodeString2() {
+ "+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn"
+ "+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6"
+ "/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==";
byte[] result = Base64.decode(data.getBytes());
byte[] result = Base64.getDecoder().decode(data.getBytes());

assertEquals("incorrect length", result.length, 256);
for (int i = 0; i < 256; ++i) {
Expand Down
Loading

0 comments on commit eafb9bd

Please sign in to comment.