Skip to content

Commit 12297f7

Browse files
committed
add Apache Commons Codec Base64 provider as alternative to java 8+ (and
remove decoding from main code, it was used only in unit tests)
1 parent fe3e14a commit 12297f7

File tree

8 files changed

+118
-36
lines changed

8 files changed

+118
-36
lines changed

scribejava-apis/src/main/java/com/github/scribejava/apis/MediaWikiApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public MediaWikiApi(String indexUrl, String niceUrlBase) {
3636
}
3737

3838
/**
39-
* The instance for wikis hosted by the Wikimedia Foundation.Consumers are requested on
39+
* The instance for wikis hosted by the Wikimedia Foundation. Consumers are requested on
4040
* <a href="https://meta.wikimedia.org/wiki/Special:OAuthConsumerRegistration/propose">
4141
* Special:OAuthConsumerRegistration/propose
4242
* </a>.

scribejava-core/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
<artifactId>scribejava-java8</artifactId>
2121
<version>${project.version}</version>
2222
</dependency>
23+
<dependency>
24+
<groupId>commons-codec</groupId>
25+
<artifactId>commons-codec</artifactId>
26+
<version>1.15</version>
27+
<optional>true</optional>
28+
</dependency>
2329
</dependencies>
2430

2531
<build>

scribejava-core/src/main/java/com/github/scribejava/core/base64/Base64.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ private static Base64 createInstance() {
2222
if (Java8Base64.isAvailable()) {
2323
return new Java8Base64();
2424
}
25+
if (CommonsCodecBase64.isAvailable()) {
26+
return new CommonsCodecBase64();
27+
}
2528
throw new IllegalStateException(
2629
"No Base64 implementation was provided. Java 8 Base64, Apache Commons Codec or JAXB is needed");
2730
}
@@ -40,13 +43,7 @@ public static String encodeUrlWithoutPadding(byte[] bytes) {
4043
return getInstance().internalEncodeUrlWithoutPadding(bytes);
4144
}
4245

43-
public static byte[] decodeMime(String string) {
44-
return getInstance().internalDecodeMime(string);
45-
}
46-
4746
protected abstract String internalEncode(byte[] bytes);
4847

4948
protected abstract String internalEncodeUrlWithoutPadding(byte[] bytes);
50-
51-
protected abstract byte[] internalDecodeMime(String string);
5249
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.github.scribejava.core.base64;
2+
3+
public class CommonsCodecBase64 extends Base64 {
4+
5+
private static final org.apache.commons.codec.binary.Base64 BASE64_ENCODER
6+
= new org.apache.commons.codec.binary.Base64();
7+
private static final org.apache.commons.codec.binary.Base64 BASE64_URL_ENCODER_WITHOUT_PADDING
8+
= new org.apache.commons.codec.binary.Base64(0, null, true);
9+
10+
@Override
11+
protected String internalEncode(byte[] bytes) {
12+
return BASE64_ENCODER.encodeToString(bytes);
13+
}
14+
15+
@Override
16+
protected String internalEncodeUrlWithoutPadding(byte[] bytes) {
17+
return BASE64_URL_ENCODER_WITHOUT_PADDING.encodeToString(bytes);
18+
}
19+
20+
static boolean isAvailable() {
21+
try {
22+
Class.forName("org.apache.commons.codec.binary.Base64", false, CommonsCodecBase64.class.getClassLoader());
23+
return true;
24+
} catch (ClassNotFoundException cnfE) {
25+
return false;
26+
}
27+
}
28+
}

scribejava-core/src/main/java/com/github/scribejava/core/base64/Java8Base64.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ protected String internalEncodeUrlWithoutPadding(byte[] bytes) {
1515
return JAVA8_BASE64.internalEncodeUrlWithoutPadding(bytes);
1616
}
1717

18-
@Override
19-
protected byte[] internalDecodeMime(String string) {
20-
return JAVA8_BASE64.internalDecodeMime(string);
21-
}
22-
2318
static boolean isAvailable() {
2419
try {
2520
Class.forName("java.util.Base64", false, Java8Base64.class.getClassLoader());

scribejava-core/src/test/java/com/github/scribejava/core/base64/Base64Test.java

Lines changed: 78 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
import org.junit.Test;
66
import static org.junit.Assert.assertTrue;
77
import static org.junit.Assert.assertEquals;
8-
import static org.junit.Assert.assertArrayEquals;
98

109
public class Base64Test {
1110

1211
private Base64 java8Base64;
12+
private Base64 commonsCodecBase64;
1313
private byte[] helloWorldBytes;
1414
private byte[] helloWorldTwoLinesBytes;
1515
private byte[] helloWorldTwoLinesAndNewLineBytes;
1616
private byte[] helloWorldDifferentCharsBytes;
17+
private byte[] bytes;
1718

1819
@Before
1920
public void setUp() throws UnsupportedEncodingException {
@@ -23,12 +24,43 @@ public void setUp() throws UnsupportedEncodingException {
2324
helloWorldDifferentCharsBytes = ("`1234567890-=~!@#$%^&*()_+ёЁ\"№;:?qwertyuiop[]asdfghjkl;'zxcvbnm,./QWERTYUIOP"
2425
+ "{}|ASDFGHJKL:ZXCVBNM<>?йфяцычувскамепинртгоьшлбщдюзж.хэъ\\ЙФЯЦЫЧУВСКАМЕПИНРТГОЬШЛБЩДЮЗЖ,ХЭЪ/\r\t\f\'"
2526
+ "\b\n").getBytes("UTF-8");
27+
bytes = new byte[]{48, -126, 2, 118, 2, 1, 0, 48, 13, 6, 9, 42, -122, 72, -122, -9, 13, 1, 1, 1, 5, 0, 4, -126,
28+
2, 96, 48, -126, 2, 92, 2, 1, 0, 2, -127, -127, 0, -61, -48, -28, 16, -116, -58, 85, 42, -39, 54, 50, -119,
29+
18, 40, 17, 75, 51, -24, 113, -109, 38, 17, -18, 106, -60, -74, -97, 29, 82, 123, -128, -88, -34, 92, 112,
30+
-57, 43, -101, 85, -47, 99, -16, 11, -95, 28, -46, 82, -104, -101, -29, -106, -106, -45, -80, 99, -93, 45,
31+
-102, 107, 31, 32, -60, 13, -46, 102, 127, 81, 94, -98, -56, 117, 50, 21, 39, 5, -98, 26, -18, -30, -21,
32+
102, -78, -77, 20, 113, -55, 117, -87, -105, -10, -100, 90, -92, 31, 61, -68, -73, -121, -108, 42, 45, -10,
33+
21, 87, 118, -74, 71, -100, -37, 96, -24, 87, 102, 68, -95, -1, -14, 6, -20, -14, 32, -14, 33, -84, -123,
34+
-65, 54, 3, 2, 3, 1, 0, 1, 2, -127, -128, 62, 115, -45, 41, 76, 28, -67, 113, 11, 17, -12, 16, 47, -112, 67,
35+
-29, -66, 76, 118, 92, -66, 25, -99, -10, -61, -126, -109, 64, -32, -37, -82, -17, 44, -20, 66, -77, -29,
36+
62, -119, -94, 92, -61, 100, -110, 32, 5, 28, 126, -69, -55, 92, 112, 2, 88, 17, -113, 43, -82, 66, 88, 13,
37+
53, 58, 74, -65, 36, 45, 93, -63, -15, 125, -7, -44, -45, -51, -76, 86, 97, 54, -36, -49, -117, -18, 56, 54,
38+
78, 80, 119, -6, -75, 39, 16, 57, -125, -68, 42, 50, -114, 92, 6, 13, 30, -91, 53, -66, -19, -20, 88, 32,
39+
-38, 36, 126, -119, -86, 47, -46, 37, 115, -49, -23, 125, -61, 75, 37, 70, 92, -122, -79, 2, 65, 0, -11,
40+
-105, 91, 105, -73, 54, 97, 96, -87, -16, -15, -73, 15, 31, -80, -96, -74, -53, -54, 53, -17, -9, 39, 62,
41+
58, 51, 68, 107, 86, 111, -62, -48, -125, 117, 66, 111, -55, 27, 56, 81, -50, 96, -47, -102, -50, -83, -52,
42+
-17, -20, 3, -42, -94, 11, 23, 104, 127, 29, -25, 32, 43, -41, -112, -83, -99, 2, 65, 0, -52, 29, 122, 9,
43+
49, -14, -118, 110, -79, 107, 76, -88, 4, -49, 40, 32, 59, 88, 45, -71, 62, 78, 93, -121, -123, 123, 3, 4,
44+
111, -112, 27, 12, -115, -123, 125, 39, 54, 96, -2, -46, 30, 40, -4, -119, 13, -121, 118, -23, 1, -83, -76,
45+
-26, -117, -86, -79, -121, 113, -26, 33, 30, 124, 35, -16, 31, 2, 65, 0, -47, -113, 111, -81, 75, 104, -103,
46+
-69, 20, 7, -57, 25, -65, 75, -7, 57, -118, 1, 102, -16, -109, 108, -64, 13, -73, 55, -37, -32, 3, -121,
47+
-90, 34, -86, -87, -70, 33, 12, -25, -81, 45, 14, -1, 74, -101, -32, 84, 41, -107, 104, 60, -10, 62, -101,
48+
92, 68, 12, -124, 5, -98, 76, 10, -53, 39, 121, 2, 64, 7, 106, 102, -67, -96, -57, -20, 9, -101, 126, -121,
49+
121, 111, 59, 75, 124, -24, 75, 10, -42, 57, 18, 69, -55, -97, -86, -39, 112, 54, -47, 104, 122, 43, 70, 23,
50+
70, -18, 109, -43, -76, 50, -114, 80, -90, 118, 12, 94, -32, -106, 68, 6, 87, 125, -23, -124, -85, -92, 18,
51+
-75, 79, 83, 57, 71, 7, 2, 64, 73, -64, -3, 78, -90, -122, -64, -99, -29, -71, 75, 21, -74, -24, -43, -37,
52+
116, -89, 31, -115, -30, 50, 8, 23, 79, -71, -68, -39, 36, -23, 60, 102, -90, -42, 19, -33, -102, -85, -74,
53+
103, 73, -30, 120, -15, 104, -9, 110, -24, -127, 14, -57, -44, 67, 9, 80, 120, 42, 94, 107, -81, -109, 101,
54+
-1, 91};
55+
2656
java8Base64 = new Java8Base64();
57+
commonsCodecBase64 = new CommonsCodecBase64();
2758
}
2859

2960
@Test
3061
public void allImplementationsAreAvailable() {
3162
assertTrue(Java8Base64.isAvailable());
63+
assertTrue(CommonsCodecBase64.isAvailable());
3264
}
3365

3466
@Test
@@ -40,12 +72,34 @@ public void testEncode() {
4072
+ "bXWFzZGZnaGprbDsnenhjdmJubSwuL1FXRVJUWVVJT1B7fXxBU0RGR0hKS0w6WlhDVkJOTTw+P9C50YTRj9GG0YvRh9GD0LLRgdC"
4173
+ "60LDQvNC10L/QuNC90YDRgtCz0L7RjNGI0LvQsdGJ0LTRjtC30LYu0YXRjdGKXNCZ0KTQr9Cm0KvQp9Cj0JLQodCa0JDQnNCV0J/"
4274
+ "QmNCd0KDQotCT0J7QrNCo0JvQkdCp0JTQrtCX0JYs0KXQrdCqLw0JDCcICg==";
75+
final String str = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAMPQ5BCMxlUq2TYy"
76+
+ "iRIoEUsz6HGTJhHuasS2nx1Se4Co3lxwxyubVdFj8AuhHNJSmJvjlpbTsGOjLZpr"
77+
+ "HyDEDdJmf1Fensh1MhUnBZ4a7uLrZrKzFHHJdamX9pxapB89vLeHlCot9hVXdrZH"
78+
+ "nNtg6FdmRKH/8gbs8iDyIayFvzYDAgMBAAECgYA+c9MpTBy9cQsR9BAvkEPjvkx2"
79+
+ "XL4ZnfbDgpNA4Nuu7yzsQrPjPomiXMNkkiAFHH67yVxwAlgRjyuuQlgNNTpKvyQt"
80+
+ "XcHxffnU0820VmE23M+L7jg2TlB3+rUnEDmDvCoyjlwGDR6lNb7t7Fgg2iR+iaov"
81+
+ "0iVzz+l9w0slRlyGsQJBAPWXW2m3NmFgqfDxtw8fsKC2y8o17/cnPjozRGtWb8LQ"
82+
+ "g3VCb8kbOFHOYNGazq3M7+wD1qILF2h/HecgK9eQrZ0CQQDMHXoJMfKKbrFrTKgE"
83+
+ "zyggO1gtuT5OXYeFewMEb5AbDI2FfSc2YP7SHij8iQ2HdukBrbTmi6qxh3HmIR58"
84+
+ "I/AfAkEA0Y9vr0tombsUB8cZv0v5OYoBZvCTbMANtzfb4AOHpiKqqbohDOevLQ7/"
85+
+ "SpvgVCmVaDz2PptcRAyEBZ5MCssneQJAB2pmvaDH7Ambfod5bztLfOhLCtY5EkXJ"
86+
+ "n6rZcDbRaHorRhdG7m3VtDKOUKZ2DF7glkQGV33phKukErVPUzlHBwJAScD9TqaG"
87+
+ "wJ3juUsVtujV23SnH43iMggXT7m82STpPGam1hPfmqu2Z0niePFo927ogQ7H1EMJ"
88+
+ "UHgqXmuvk2X/Ww==";
4389

4490
assertEquals(helloWorldEncoded, java8Base64.internalEncode(helloWorldBytes));
4591
assertEquals(helloWorldTwoLinesEncoded, java8Base64.internalEncode(helloWorldTwoLinesBytes));
4692
assertEquals(helloWorldTwoLinesAndNewLineEncoded,
4793
java8Base64.internalEncode(helloWorldTwoLinesAndNewLineBytes));
4894
assertEquals(helloWorldDifferentCharsEncoded, java8Base64.internalEncode(helloWorldDifferentCharsBytes));
95+
assertEquals(str, java8Base64.internalEncode(bytes));
96+
97+
assertEquals(helloWorldEncoded, commonsCodecBase64.internalEncode(helloWorldBytes));
98+
assertEquals(helloWorldTwoLinesEncoded, commonsCodecBase64.internalEncode(helloWorldTwoLinesBytes));
99+
assertEquals(helloWorldTwoLinesAndNewLineEncoded,
100+
commonsCodecBase64.internalEncode(helloWorldTwoLinesAndNewLineBytes));
101+
assertEquals(helloWorldDifferentCharsEncoded, commonsCodecBase64.internalEncode(helloWorldDifferentCharsBytes));
102+
assertEquals(str, commonsCodecBase64.internalEncode(bytes));
49103
}
50104

51105
@Test
@@ -57,31 +111,36 @@ public void testEncodeUrlWithoutPadding() {
57111
+ "bXWFzZGZnaGprbDsnenhjdmJubSwuL1FXRVJUWVVJT1B7fXxBU0RGR0hKS0w6WlhDVkJOTTw-P9C50YTRj9GG0YvRh9GD0LLRgdC"
58112
+ "60LDQvNC10L_QuNC90YDRgtCz0L7RjNGI0LvQsdGJ0LTRjtC30LYu0YXRjdGKXNCZ0KTQr9Cm0KvQp9Cj0JLQodCa0JDQnNCV0J_"
59113
+ "QmNCd0KDQotCT0J7QrNCo0JvQkdCp0JTQrtCX0JYs0KXQrdCqLw0JDCcICg";
114+
final String str = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAMPQ5BCMxlUq2TYy"
115+
+ "iRIoEUsz6HGTJhHuasS2nx1Se4Co3lxwxyubVdFj8AuhHNJSmJvjlpbTsGOjLZpr"
116+
+ "HyDEDdJmf1Fensh1MhUnBZ4a7uLrZrKzFHHJdamX9pxapB89vLeHlCot9hVXdrZH"
117+
+ "nNtg6FdmRKH_8gbs8iDyIayFvzYDAgMBAAECgYA-c9MpTBy9cQsR9BAvkEPjvkx2"
118+
+ "XL4ZnfbDgpNA4Nuu7yzsQrPjPomiXMNkkiAFHH67yVxwAlgRjyuuQlgNNTpKvyQt"
119+
+ "XcHxffnU0820VmE23M-L7jg2TlB3-rUnEDmDvCoyjlwGDR6lNb7t7Fgg2iR-iaov"
120+
+ "0iVzz-l9w0slRlyGsQJBAPWXW2m3NmFgqfDxtw8fsKC2y8o17_cnPjozRGtWb8LQ"
121+
+ "g3VCb8kbOFHOYNGazq3M7-wD1qILF2h_HecgK9eQrZ0CQQDMHXoJMfKKbrFrTKgE"
122+
+ "zyggO1gtuT5OXYeFewMEb5AbDI2FfSc2YP7SHij8iQ2HdukBrbTmi6qxh3HmIR58"
123+
+ "I_AfAkEA0Y9vr0tombsUB8cZv0v5OYoBZvCTbMANtzfb4AOHpiKqqbohDOevLQ7_"
124+
+ "SpvgVCmVaDz2PptcRAyEBZ5MCssneQJAB2pmvaDH7Ambfod5bztLfOhLCtY5EkXJ"
125+
+ "n6rZcDbRaHorRhdG7m3VtDKOUKZ2DF7glkQGV33phKukErVPUzlHBwJAScD9TqaG"
126+
+ "wJ3juUsVtujV23SnH43iMggXT7m82STpPGam1hPfmqu2Z0niePFo927ogQ7H1EMJ"
127+
+ "UHgqXmuvk2X_Ww";
60128

61129
assertEquals(helloWorldEncoded, java8Base64.internalEncodeUrlWithoutPadding(helloWorldBytes));
62130
assertEquals(helloWorldTwoLinesEncoded, java8Base64.internalEncodeUrlWithoutPadding(helloWorldTwoLinesBytes));
63131
assertEquals(helloWorldTwoLinesAndNewLineEncoded,
64132
java8Base64.internalEncodeUrlWithoutPadding(helloWorldTwoLinesAndNewLineBytes));
65133
assertEquals(helloWorldDifferentCharsEncoded,
66134
java8Base64.internalEncodeUrlWithoutPadding(helloWorldDifferentCharsBytes));
67-
}
68-
69-
@Test
70-
public void testDecodeMime() {
71-
final String helloWorldEncoded = "SGVsbG8gV29ybGQ=";
72-
final String helloWorldTwoLinesEncoded = "SGVsbG8gV29ybGQNCk5ldyBMaW5lMg==";
73-
final String helloWorldTwoLinesAndNewLineEncoded = "SGVsbG8gV29ybGQNClNlY29uZCBMaW5lDQo=";
74-
final String helloWorldDifferentCharsEncoded = "YDEyMzQ1Njc4OTAtPX4hQCMkJV4mKigpXyvRkdCBIuKEljs6P3F3ZXJ0eXVpb3B"
75-
+ "bXWFzZGZnaGprbDsnenhjdmJubSwuL1FXRVJUWVVJT1B7fXxBU0RGR0hKS0w6WlhDVkJOTTw+P9C50YTRj9GG0YvRh9GD0LLRgdC"
76-
+ "60LDQvNC10L/QuNC90YDRgtCz0L7RjNGI0LvQsdGJ0LTRjtC30LYu0YXRjdGKXNCZ0KTQr9Cm0KvQp9Cj0JLQodCa0JDQnNCV0J/"
77-
+ "QmNCd0KDQotCT0J7QrNCo0JvQkdCp0JTQrtCX0JYs0KXQrdCqLw0JDCcICg==";
135+
assertEquals(str, java8Base64.internalEncodeUrlWithoutPadding(bytes));
78136

79-
assertArrayEquals(helloWorldBytes, java8Base64.internalDecodeMime(helloWorldEncoded));
80-
assertArrayEquals(helloWorldTwoLinesBytes, java8Base64.internalDecodeMime(helloWorldTwoLinesEncoded));
81-
assertArrayEquals(helloWorldTwoLinesAndNewLineBytes,
82-
java8Base64.internalDecodeMime(helloWorldTwoLinesAndNewLineEncoded));
83-
assertArrayEquals(helloWorldDifferentCharsBytes,
84-
java8Base64.internalDecodeMime(helloWorldDifferentCharsEncoded));
137+
assertEquals(helloWorldEncoded, commonsCodecBase64.internalEncodeUrlWithoutPadding(helloWorldBytes));
138+
assertEquals(helloWorldTwoLinesEncoded,
139+
commonsCodecBase64.internalEncodeUrlWithoutPadding(helloWorldTwoLinesBytes));
140+
assertEquals(helloWorldTwoLinesAndNewLineEncoded,
141+
commonsCodecBase64.internalEncodeUrlWithoutPadding(helloWorldTwoLinesAndNewLineBytes));
142+
assertEquals(helloWorldDifferentCharsEncoded,
143+
commonsCodecBase64.internalEncodeUrlWithoutPadding(helloWorldDifferentCharsBytes));
144+
assertEquals(str, commonsCodecBase64.internalEncodeUrlWithoutPadding(bytes));
85145
}
86-
87146
}

scribejava-core/src/test/java/com/github/scribejava/core/services/RSASha1SignatureServiceTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.github.scribejava.core.services;
22

3-
import com.github.scribejava.core.base64.Base64;
43
import java.security.KeyFactory;
54
import java.security.NoSuchAlgorithmException;
65
import java.security.PrivateKey;
76
import java.security.spec.InvalidKeySpecException;
87
import java.security.spec.PKCS8EncodedKeySpec;
8+
import org.apache.commons.codec.binary.Base64;
99
import static org.junit.Assert.assertEquals;
1010
import org.junit.Test;
1111

@@ -53,7 +53,7 @@ private static PrivateKey getPrivateKey() {
5353

5454
try {
5555
final KeyFactory fac = KeyFactory.getInstance("RSA");
56-
final PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(Base64.decodeMime(str));
56+
final PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(str));
5757
return fac.generatePrivate(privKeySpec);
5858
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
5959
throw new RuntimeException(e);

scribejava-java8/src/main/java/com/github/scribejava/java8/base64/Java8Base64.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,4 @@ public String internalEncodeUrlWithoutPadding(byte[] bytes) {
1414
return BASE64_URL_ENCODER_WITHOUT_PADDING.encodeToString(bytes);
1515
}
1616

17-
public byte[] internalDecodeMime(String string) {
18-
return java.util.Base64.getMimeDecoder().decode(string);
19-
}
2017
}

0 commit comments

Comments
 (0)