Skip to content

Commit aca25eb

Browse files
committed
When User and/or Password are too long, then the Authorization Header is broken because of sun Base64Encoder impl.
Changed for another Base64 implementation
1 parent bc5011b commit aca25eb

4 files changed

Lines changed: 179 additions & 2 deletions

File tree

‎CHANGES.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### Version 6.0.2
2+
* Fix for BasicAuthRequestInterceptor when username and/or password are long.
3+
14
### Version 6.0
25
* Support binary request and response bodies.
36
* Don't throw http status code exceptions when return type is `Response`.
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
* Copyright 2013 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package feign.auth;
17+
18+
import java.io.UnsupportedEncodingException;
19+
20+
/**
21+
* copied from <a href="https://github.com/square/okhttp/blob/master/okhttp-protocols/src/main/java/com/squareup/okhttp/internal/Base64.java">okhttp</a>
22+
* @author Alexander Y. Kleymenov
23+
*/
24+
final class Base64 {
25+
26+
public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
27+
28+
private Base64() {
29+
}
30+
31+
public static byte[] decode(byte[] in) {
32+
return decode(in, in.length);
33+
}
34+
35+
public static byte[] decode(byte[] in, int len) {
36+
// approximate output length
37+
int length = len / 4 * 3;
38+
// return an empty array on empty or short input without padding
39+
if (length == 0) {
40+
return EMPTY_BYTE_ARRAY;
41+
}
42+
// temporary array
43+
byte[] out = new byte[length];
44+
// number of padding characters ('=')
45+
int pad = 0;
46+
byte chr;
47+
// compute the number of the padding characters
48+
// and adjust the length of the input
49+
for (; ; len--) {
50+
chr = in[len - 1];
51+
// skip the neutral characters
52+
if ((chr == '\n') || (chr == '\r') || (chr == ' ') || (chr == '\t')) {
53+
continue;
54+
}
55+
if (chr == '=') {
56+
pad++;
57+
} else {
58+
break;
59+
}
60+
}
61+
// index in the output array
62+
int outIndex = 0;
63+
// index in the input array
64+
int inIndex = 0;
65+
// holds the value of the input character
66+
int bits = 0;
67+
// holds the value of the input quantum
68+
int quantum = 0;
69+
for (int i = 0; i < len; i++) {
70+
chr = in[i];
71+
// skip the neutral characters
72+
if ((chr == '\n') || (chr == '\r') || (chr == ' ') || (chr == '\t')) {
73+
continue;
74+
}
75+
if ((chr >= 'A') && (chr <= 'Z')) {
76+
// char ASCII value
77+
// A 65 0
78+
// Z 90 25 (ASCII - 65)
79+
bits = chr - 65;
80+
} else if ((chr >= 'a') && (chr <= 'z')) {
81+
// char ASCII value
82+
// a 97 26
83+
// z 122 51 (ASCII - 71)
84+
bits = chr - 71;
85+
} else if ((chr >= '0') && (chr <= '9')) {
86+
// char ASCII value
87+
// 0 48 52
88+
// 9 57 61 (ASCII + 4)
89+
bits = chr + 4;
90+
} else if (chr == '+') {
91+
bits = 62;
92+
} else if (chr == '/') {
93+
bits = 63;
94+
} else {
95+
return null;
96+
}
97+
// append the value to the quantum
98+
quantum = (quantum << 6) | (byte) bits;
99+
if (inIndex % 4 == 3) {
100+
// 4 characters were read, so make the output:
101+
out[outIndex++] = (byte) (quantum >> 16);
102+
out[outIndex++] = (byte) (quantum >> 8);
103+
out[outIndex++] = (byte) quantum;
104+
}
105+
inIndex++;
106+
}
107+
if (pad > 0) {
108+
// adjust the quantum value according to the padding
109+
quantum = quantum << (6 * pad);
110+
// make output
111+
out[outIndex++] = (byte) (quantum >> 16);
112+
if (pad == 1) {
113+
out[outIndex++] = (byte) (quantum >> 8);
114+
}
115+
}
116+
// create the resulting array
117+
byte[] result = new byte[outIndex];
118+
System.arraycopy(out, 0, result, 0, outIndex);
119+
return result;
120+
}
121+
122+
private static final byte[] MAP = new byte[] {
123+
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
124+
'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
125+
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4',
126+
'5', '6', '7', '8', '9', '+', '/'
127+
};
128+
129+
public static String encode(byte[] in) {
130+
int length = (in.length + 2) * 4 / 3;
131+
byte[] out = new byte[length];
132+
int index = 0, end = in.length - in.length % 3;
133+
for (int i = 0; i < end; i += 3) {
134+
out[index++] = MAP[(in[i] & 0xff) >> 2];
135+
out[index++] = MAP[((in[i] & 0x03) << 4) | ((in[i + 1] & 0xff) >> 4)];
136+
out[index++] = MAP[((in[i + 1] & 0x0f) << 2) | ((in[i + 2] & 0xff) >> 6)];
137+
out[index++] = MAP[(in[i + 2] & 0x3f)];
138+
}
139+
switch (in.length % 3) {
140+
case 1:
141+
out[index++] = MAP[(in[end] & 0xff) >> 2];
142+
out[index++] = MAP[(in[end] & 0x03) << 4];
143+
out[index++] = '=';
144+
out[index++] = '=';
145+
break;
146+
case 2:
147+
out[index++] = MAP[(in[end] & 0xff) >> 2];
148+
out[index++] = MAP[((in[end] & 0x03) << 4) | ((in[end + 1] & 0xff) >> 4)];
149+
out[index++] = MAP[((in[end + 1] & 0x0f) << 2)];
150+
out[index++] = '=';
151+
break;
152+
}
153+
try {
154+
return new String(out, 0, index, "US-ASCII");
155+
} catch (UnsupportedEncodingException e) {
156+
throw new AssertionError(e);
157+
}
158+
}
159+
}
160+

‎core/src/main/java/feign/auth/BasicAuthRequestInterceptor.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import feign.RequestInterceptor;
1919
import feign.RequestTemplate;
20-
import sun.misc.BASE64Encoder;
2120

2221
import java.nio.charset.Charset;
2322

@@ -64,6 +63,7 @@ public BasicAuthRequestInterceptor(String username, String password, Charset cha
6463
* response would be to pull the necessary portions of Guava's BaseEncoding class into Util.
6564
*/
6665
private static String base64Encode(byte[] bytes) {
67-
return new BASE64Encoder().encode(bytes);
66+
return Base64.encode(bytes);
6867
}
6968
}
69+

‎core/src/test/java/feign/auth/BasicAuthRequestInterceptorTest.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,18 @@ public class BasicAuthRequestInterceptorTest {
3838
Collection<String> expectedValue = Collections.singletonList("Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
3939
assertEquals(actualValue, expectedValue);
4040
}
41+
42+
/**
43+
* Tests that requests headers are added as expected when user and pass are too long
44+
*/
45+
@Test public void testAuthenticationWhenUserPassAreTooLong() {
46+
RequestTemplate template = new RequestTemplate();
47+
BasicAuthRequestInterceptor interceptor = new BasicAuthRequestInterceptor("IOIOIOIOIOIOIOIOIOIOIOIOIOIOIOIOIOIOIO",
48+
"101010101010101010101010101010101010101010");
49+
interceptor.apply(template);
50+
Collection<String> actualValue = template.headers().get("Authorization");
51+
Collection<String> expectedValue = Collections.
52+
singletonList("Basic SU9JT0lPSU9JT0lPSU9JT0lPSU9JT0lPSU9JT0lPSU9JT0lPSU86MTAxMDEwMTAxMDEwMTAxMDEwMTAxMDEwMTAxMDEwMTAxMDEwMTAxMDEw");
53+
assertEquals(actualValue, expectedValue);
54+
}
4155
}

0 commit comments

Comments
 (0)