1515 */
1616package com .here .account .auth ;
1717
18- import com .ning .http .client .FluentStringsMap ;
19- import com .ning .http .client .oauth .ConsumerKey ;
20- import com .ning .http .client .oauth .OAuthSignatureCalculator ;
21- import com .ning .http .client .oauth .RequestToken ;
18+ import org .asynchttpclient .Param ;
19+ import org .asynchttpclient .oauth .ConsumerKey ;
20+ import org .asynchttpclient .oauth .OAuthSignatureCalculatorInstance ;
21+ import org .asynchttpclient .oauth .RequestToken ;
22+ import org .asynchttpclient .uri .Uri ;
23+ import org .asynchttpclient .util .Utf8UrlEncoder ;
2224import org .junit .Test ;
2325
26+ import java .lang .reflect .Method ;
2427import java .security .*;
2528import java .security .spec .*;
2629import java .util .*;
@@ -49,7 +52,7 @@ public class SignatureCalculatorTest {
4952
5053 /////////////////////////////// HMAC-SHA1 //////////////////////////////////////////
5154 @ Test
52- public void testSignatureHmacSha1 () {
55+ public void testSignatureHmacSha1 () throws Exception {
5356 String expectedSignature = computeSHA1SignatureUsingLibrary (baseURL , null , null );
5457
5558 SignatureCalculator sc = new SignatureCalculator (consumerKey , consumerSecret );
@@ -59,7 +62,7 @@ public void testSignatureHmacSha1() {
5962 }
6063
6164 @ Test
62- public void testSignatureHmacSha1WithFormParams () {
65+ public void testSignatureHmacSha1WithFormParams () throws Exception {
6366 String expectedSignature = computeSHA1SignatureUsingLibrary (baseURL , params , null );
6467
6568 SignatureCalculator sc = new SignatureCalculator (consumerKey , consumerSecret );
@@ -69,7 +72,7 @@ public void testSignatureHmacSha1WithFormParams() {
6972 }
7073
7174 @ Test
72- public void testSignatureHmacSha1WithFormParamsWithSpacesInValue () {
75+ public void testSignatureHmacSha1WithFormParamsWithSpacesInValue () throws Exception {
7376
7477 Map <String , List <String >> nestedParams = new HashMap <>();
7578 nestedParams .put ("http_method" , Arrays .asList ("POST" ));
@@ -87,7 +90,7 @@ public void testSignatureHmacSha1WithFormParamsWithSpacesInValue() {
8790 }
8891
8992 @ Test
90- public void testSignatureHmacSha1WithQueryParams () {
93+ public void testSignatureHmacSha1WithQueryParams () throws Exception {
9194 String expectedSignature = computeSHA1SignatureUsingLibrary (baseURL , null , params );
9295
9396 SignatureCalculator sc = new SignatureCalculator (consumerKey , consumerSecret );
@@ -97,7 +100,7 @@ public void testSignatureHmacSha1WithQueryParams() {
97100 }
98101
99102 @ Test
100- public void testSignatureHmacSha1WithFormAndQueryParams () {
103+ public void testSignatureHmacSha1WithFormAndQueryParams () throws Exception {
101104 String expectedSignature = computeSHA1SignatureUsingLibrary (baseURL , params , params );
102105
103106 SignatureCalculator sc = new SignatureCalculator (consumerKey , consumerSecret );
@@ -107,7 +110,7 @@ public void testSignatureHmacSha1WithFormAndQueryParams() {
107110 }
108111
109112 @ Test
110- public void testSignatureHmacSha1WithBaseURLWithPort () {
113+ public void testSignatureHmacSha1WithBaseURLWithPort () throws Exception {
111114 String expectedSignature = computeSHA1SignatureUsingLibrary (baseURLWithPort , params , params );
112115
113116 SignatureCalculator sc = new SignatureCalculator (consumerKey , consumerSecret );
@@ -117,7 +120,7 @@ public void testSignatureHmacSha1WithBaseURLWithPort() {
117120 }
118121
119122 @ Test
120- public void testSignatureHmacSha1WithBaseURLWithNonStandardPort () {
123+ public void testSignatureHmacSha1WithBaseURLWithNonStandardPort () throws Exception {
121124 String expectedSignature = computeSHA1SignatureUsingLibrary (baseURLWithNonStandardPort , params , params );
122125
123126 SignatureCalculator sc = new SignatureCalculator (consumerKey , consumerSecret );
@@ -127,7 +130,7 @@ public void testSignatureHmacSha1WithBaseURLWithNonStandardPort() {
127130 }
128131
129132 @ Test
130- public void testVerifySha1Signature () {
133+ public void testVerifySha1Signature () throws Exception {
131134 String expectedSignature = computeSHA1SignatureUsingLibrary (baseURLWithNonStandardPort , params , params );
132135
133136 boolean verified = SignatureCalculator .verifySignature (consumerKey , method , baseURLWithNonStandardPort , timestamp , nonce ,
@@ -260,23 +263,21 @@ public static KeyPair generateES512KeyPair() {
260263 }
261264 }
262265
263- private static String computeSHA1SignatureUsingLibrary (String url , Map <String , List <String >> formParams , Map <String , List <String >> queryParams ) {
264- RequestToken emptyUserAuth = new RequestToken (null , "" );
265- OAuthSignatureCalculator calculator = new OAuthSignatureCalculator (new ConsumerKey (consumerKey , consumerSecret ), emptyUserAuth );
266-
267- FluentStringsMap fluentFormParams = null ;
268- if (null != formParams && !formParams .isEmpty ()) {
269- fluentFormParams = new FluentStringsMap ();
270- fluentFormParams .putAll (formParams );
271- }
266+ private static String computeSHA1SignatureUsingLibrary (String url , Map <String , List <String >> formParams , Map <String , List <String >> queryParams ) throws Exception {
267+ Method computeSignature = OAuthSignatureCalculatorInstance .class .getDeclaredMethod ("computeSignature" , ConsumerKey .class , RequestToken .class , Uri .class , String .class , List .class , List .class , long .class , String .class );
268+ computeSignature .setAccessible (true );
269+ return (String ) computeSignature .invoke (new OAuthSignatureCalculatorInstance (), new ConsumerKey (consumerKey , consumerSecret ), new RequestToken (null , "" ), Uri .create (url ), method , toParamList (formParams ), toParamList (queryParams ), timestamp , Utf8UrlEncoder .percentEncodeQueryElement (nonce ));
270+ }
272271
273- FluentStringsMap fluentQueryParams = null ;
274- if (null != queryParams && !queryParams .isEmpty ()) {
275- fluentQueryParams = new FluentStringsMap ();
276- fluentQueryParams .putAll (queryParams );
272+ private static List <Param > toParamList (Map <String , List <String >> paramMap ) {
273+ if (paramMap == null || paramMap .isEmpty ()) return null ;
274+ List <Param > paramList = new ArrayList <>();
275+ for (Map .Entry <String , List <String >> entry : paramMap .entrySet ()) {
276+ for (String value : entry .getValue ()) {
277+ paramList .add (new Param (entry .getKey (), value ));
278+ }
277279 }
278-
279- return calculator .calculateSignature (method , url , timestamp , nonce , fluentFormParams , fluentQueryParams );
280+ return paramList ;
280281 }
281282
282283 private static Map <String , List <String >> createParamsList () {
0 commit comments