Skip to content

Commit edf21ca

Browse files
committed
Fix bugs and Remove TrustManager method
1 parent 19bada0 commit edf21ca

File tree

1 file changed

+17
-35
lines changed

1 file changed

+17
-35
lines changed

src/main/java/com/api/util/ApiSecurity/ApiSigning.java

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
import javax.crypto.Mac;
77
import javax.crypto.spec.SecretKeySpec;
8-
import javax.net.ssl.TrustManager;
9-
import javax.net.ssl.X509TrustManager;
108
import java.io.FileInputStream;
119
import java.io.FileNotFoundException;
1210
import java.io.IOException;
@@ -27,7 +25,6 @@
2725
public class ApiSigning {
2826

2927
private static final Logger log = LoggerFactory.getLogger(ApiSigning.class);
30-
private final static String USER_AGENT = "Mozilla/5.0";
3128

3229
/**
3330
* Create HMACRSA256 Signature (L1) with a given basestring
@@ -411,16 +408,21 @@ public static String getBaseString(String authPrefix
411408
ApiList paramList = new ApiList();
412409

413410
// process QueryString from url by transfering it to paramList
414-
if (siteUri.getQuery().length() > 1) {
411+
if (null != siteUri.getQuery()) {
415412
String queryString = siteUri.getRawQuery();
416413
log.debug("queryString:: {}", queryString);
417414

418415
String[] paramArr = queryString.split("&");
419416
for (String item : paramArr) {
420-
log.debug("item:: {}", item);
417+
log.debug("queryItem:: {}", item);
421418
String[] itemArr = item.split("=");
422419
try {
423-
paramList.add(itemArr[0], java.net.URLDecoder.decode(itemArr[1], StandardCharsets.UTF_8.toString()));
420+
if(itemArr.length == 1) {
421+
paramList.add(itemArr[0], "");
422+
}else {
423+
paramList.add(itemArr[0], java.net.URLDecoder.decode(itemArr[1], StandardCharsets.UTF_8.toString()));
424+
}
425+
//paramList.add(itemArr[0], java.net.URLDecoder.decode(itemArr[1], StandardCharsets.UTF_8.toString()));
424426
} catch (UnsupportedEncodingException e) {
425427
throw e;
426428
}
@@ -439,7 +441,7 @@ public static String getBaseString(String authPrefix
439441
paramList.add(authPrefix + "_signature_method", signatureMethod);
440442
paramList.add(authPrefix + "_version", "1.0");
441443

442-
baseString = httpMethod.toUpperCase() + "&" + url + "&" + paramList.toString();
444+
baseString = httpMethod.toUpperCase() + "&" + url + "&" + paramList.toString(true);
443445

444446
} catch (ApiUtilException ae) {
445447
log.error("Error :: getBaseString :: " + ae.getMessage());
@@ -499,7 +501,7 @@ public static String getSignatureToken(
499501

500502
// Generate the nonce value
501503
try {
502-
nonce = nonce != null ? nonce : Long.toString(getNewNonce());
504+
nonce = nonce != null ? nonce : getNewNonce();
503505
} catch (NoSuchAlgorithmException nsae) {
504506
throw nsae;
505507
}
@@ -534,7 +536,7 @@ public static String getSignatureToken(
534536
tokenList.add(authPrefix + "_signature", base64Token);
535537
tokenList.add(authPrefix + "_version", "1.0");
536538

537-
authorizationToken = String.format("%s %s", authPrefix.substring(0, 1).toUpperCase() + authPrefix.substring(1), tokenList.toString(", ", false, true));
539+
authorizationToken = String.format("%s %s", authPrefix.substring(0, 1).toUpperCase() + authPrefix.substring(1), tokenList.toString(", ", false, true, false));
538540

539541
} catch (ApiUtilException ae) {
540542
log.error("Error :: getToken :: " + ae.getMessage());
@@ -553,33 +555,13 @@ private static long getNewTimestamp() {
553555
return System.currentTimeMillis();
554556
}
555557

556-
private static long getNewNonce() throws NoSuchAlgorithmException {
557-
long nonce = 0;
558-
559-
nonce = SecureRandom.getInstance("SHA1PRNG").nextLong();
560-
558+
private static String getNewNonce() throws NoSuchAlgorithmException {
559+
String nonce = null;
560+
byte[] b = new byte[32];
561+
SecureRandom.getInstance("SHA1PRNG").nextBytes(b);
562+
nonce = Base64.getEncoder().encodeToString(b);
563+
561564
return nonce;
562565
}
563566

564-
private static TrustManager[] getTrustManager() {
565-
// Create a trust manager that does not validate certificate chains
566-
TrustManager[] trustAllCerts = new TrustManager[]{
567-
new X509TrustManager() {
568-
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
569-
return null;
570-
}
571-
572-
public void checkClientTrusted(
573-
java.security.cert.X509Certificate[] certs, String authType) {
574-
}
575-
576-
public void checkServerTrusted(
577-
java.security.cert.X509Certificate[] certs, String authType) {
578-
}
579-
}
580-
};
581-
582-
return trustAllCerts;
583-
}
584-
585567
}

0 commit comments

Comments
 (0)