Skip to content

Commit a1000f1

Browse files
test
Signed-off-by: ashikuma <ashish.kumar@here.com>
1 parent 7b6a3d9 commit a1000f1

4 files changed

Lines changed: 51 additions & 33 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
steps:
99
- uses: actions/checkout@v2
1010
- name: Cache local Maven repository
11-
uses: actions/cache@v2
11+
uses: actions/cache@v4
1212
with:
1313
path: $HOME/.m2
1414
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}

here-oauth-client/src/main/java/com/here/account/auth/SignatureCalculator.java

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
import javax.crypto.Mac;
2121
import javax.crypto.spec.SecretKeySpec;
2222
import java.io.UnsupportedEncodingException;
23+
import java.net.MalformedURLException;
24+
import java.net.URL;
2325
import java.net.URLEncoder;
26+
import java.nio.charset.StandardCharsets;
2427
import java.security.KeyFactory;
2528
import java.security.PrivateKey;
2629
import java.security.PublicKey;
@@ -291,14 +294,20 @@ private static String generateSignature(String signatureBaseString, String key,
291294
* not one.
292295
*/
293296
static String urlEncode(String s) {
294-
try {
295-
return URLEncoder.encode(s, OAuthConstants.UTF_8_STRING)
296-
.replace("+", "%20")
297-
.replace("*", "%2A")
298-
.replace("%7E", "~");
299-
} catch (UnsupportedEncodingException e) {
300-
throw new IllegalArgumentException(e);
297+
StringBuilder encoded = new StringBuilder();
298+
byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
299+
for (byte b : bytes) {
300+
char ch = (char) b;
301+
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || ch == '-' || ch == '_' || ch == '.' || ch == '~') {
302+
encoded.append(ch);
303+
} else {
304+
encoded.append('%');
305+
String hex = Integer.toHexString(b & 0xFF).toUpperCase();
306+
if (hex.length() == 1) encoded.append('0');
307+
encoded.append(hex);
308+
}
301309
}
310+
return encoded.toString();
302311
}
303312

304313
/**
@@ -357,20 +366,23 @@ private static PrivateKey consumerSecretToEllipticCurvePrivateKey(String key) {
357366
* Remove the default port from the baseURL
358367
*/
359368
private static String normalizeBaseURL(String baseURL) {
360-
int index;
361-
if (baseURL.startsWith("http:")) {
362-
index = baseURL.indexOf(":80/", 4);
363-
if (index > 0) {
364-
baseURL = baseURL.substring(0, index) + baseURL.substring(index + 3);
365-
}
366-
} else if (baseURL.startsWith("https:")) {
367-
index = baseURL.indexOf(":443/", 5);
368-
if (index > 0) {
369-
baseURL = baseURL.substring(0, index) + baseURL.substring(index + 4);
369+
try {
370+
URL url = new URL(baseURL);
371+
String scheme = url.getProtocol().toLowerCase();
372+
String host = url.getHost().toLowerCase();
373+
int port = url.getPort();
374+
boolean isDefaultPort = (scheme.equals("http") && port == 80) || (scheme.equals("https") && port == 443) || (port == -1);
375+
StringBuilder sb = new StringBuilder();
376+
sb.append(scheme).append("://").append(host);
377+
if (!isDefaultPort) {
378+
sb.append(":").append(port);
370379
}
380+
String path = url.getPath();
381+
sb.append((path == null || path.isEmpty()) ? "/" : path);
382+
return sb.toString();
383+
} catch (MalformedURLException e) {
384+
throw new IllegalArgumentException("Invalid URL: " + baseURL, e);
371385
}
372-
373-
return baseURL;
374386
}
375387

376388
/**

here-oauth-client/src/test/java/com/here/account/auth/SignatureCalculatorTest.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
package com.here.account.auth;
1717

1818
import com.ning.http.client.FluentStringsMap;
19+
import com.ning.http.client.Param;
1920
import com.ning.http.client.oauth.ConsumerKey;
2021
import com.ning.http.client.oauth.OAuthSignatureCalculator;
2122
import com.ning.http.client.oauth.RequestToken;
23+
import com.ning.http.client.uri.Uri;
2224
import org.junit.Test;
2325

2426
import java.security.*;
@@ -264,19 +266,23 @@ private static String computeSHA1SignatureUsingLibrary(String url, Map<String, L
264266
RequestToken emptyUserAuth = new RequestToken(null, "");
265267
OAuthSignatureCalculator calculator = new OAuthSignatureCalculator(new ConsumerKey(consumerKey, consumerSecret), emptyUserAuth);
266268

267-
FluentStringsMap fluentFormParams = null;
268-
if (null != formParams && !formParams.isEmpty()) {
269-
fluentFormParams = new FluentStringsMap();
270-
fluentFormParams.putAll(formParams);
271-
}
269+
List<Param> formParamList = convertToParamList(formParams);
270+
List<Param> queryParamList = convertToParamList(queryParams);
272271

273-
FluentStringsMap fluentQueryParams = null;
274-
if (null != queryParams && !queryParams.isEmpty()) {
275-
fluentQueryParams = new FluentStringsMap();
276-
fluentQueryParams.putAll(queryParams);
277-
}
272+
return calculator.calculateSignature(method, Uri.create(url), timestamp, nonce, formParamList, queryParamList);
273+
}
278274

279-
return calculator.calculateSignature(method, url, timestamp, nonce, fluentFormParams, fluentQueryParams);
275+
private static List<Param> convertToParamList(Map<String, List<String>> paramMap) {
276+
List<Param> paramList = new ArrayList<>();
277+
if (paramMap != null) {
278+
for (Map.Entry<String, List<String>> entry : paramMap.entrySet()) {
279+
String key = entry.getKey();
280+
for (String value : entry.getValue()) {
281+
paramList.add(new Param(key, value));
282+
}
283+
}
284+
}
285+
return paramList;
280286
}
281287

282288
private static Map<String, List<String>> createParamsList() {

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@
6666
<!-- Declare versions for dependencies -->
6767
<apache.httpclient.version>4.5.13</apache.httpclient.version>
6868
<ini4j.version>0.5.4</ini4j.version>
69-
<jackson.version>2.13.3</jackson.version>
69+
<jackson.version>2.13.4.2</jackson.version>
7070
<junit.version>4.13.1</junit.version>
7171
<mockito.version>1.10.19</mockito.version>
72-
<ning.version>1.8.17</ning.version>
72+
<ning.version>1.9.0</ning.version>
7373
<browsermob.version>2.1.5</browsermob.version>
7474

7575
<!-- configure surefire and maven to be individually skippable -->

0 commit comments

Comments
 (0)