5
5
6
6
import javax .crypto .Mac ;
7
7
import javax .crypto .spec .SecretKeySpec ;
8
- import javax .net .ssl .TrustManager ;
9
- import javax .net .ssl .X509TrustManager ;
10
8
import java .io .FileInputStream ;
11
9
import java .io .FileNotFoundException ;
12
10
import java .io .IOException ;
27
25
public class ApiSigning {
28
26
29
27
private static final Logger log = LoggerFactory .getLogger (ApiSigning .class );
30
- private final static String USER_AGENT = "Mozilla/5.0" ;
31
28
32
29
/**
33
30
* Create HMACRSA256 Signature (L1) with a given basestring
@@ -411,16 +408,21 @@ public static String getBaseString(String authPrefix
411
408
ApiList paramList = new ApiList ();
412
409
413
410
// process QueryString from url by transfering it to paramList
414
- if (siteUri .getQuery (). length () > 1 ) {
411
+ if (null != siteUri .getQuery ()) {
415
412
String queryString = siteUri .getRawQuery ();
416
413
log .debug ("queryString:: {}" , queryString );
417
414
418
415
String [] paramArr = queryString .split ("&" );
419
416
for (String item : paramArr ) {
420
- log .debug ("item :: {}" , item );
417
+ log .debug ("queryItem :: {}" , item );
421
418
String [] itemArr = item .split ("=" );
422
419
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()));
424
426
} catch (UnsupportedEncodingException e ) {
425
427
throw e ;
426
428
}
@@ -439,7 +441,7 @@ public static String getBaseString(String authPrefix
439
441
paramList .add (authPrefix + "_signature_method" , signatureMethod );
440
442
paramList .add (authPrefix + "_version" , "1.0" );
441
443
442
- baseString = httpMethod .toUpperCase () + "&" + url + "&" + paramList .toString ();
444
+ baseString = httpMethod .toUpperCase () + "&" + url + "&" + paramList .toString (true );
443
445
444
446
} catch (ApiUtilException ae ) {
445
447
log .error ("Error :: getBaseString :: " + ae .getMessage ());
@@ -499,7 +501,7 @@ public static String getSignatureToken(
499
501
500
502
// Generate the nonce value
501
503
try {
502
- nonce = nonce != null ? nonce : Long . toString ( getNewNonce () );
504
+ nonce = ( nonce != null && ! nonce . isEmpty ()) ? nonce : getNewNonce ();
503
505
} catch (NoSuchAlgorithmException nsae ) {
504
506
throw nsae ;
505
507
}
@@ -534,7 +536,7 @@ public static String getSignatureToken(
534
536
tokenList .add (authPrefix + "_signature" , base64Token );
535
537
tokenList .add (authPrefix + "_version" , "1.0" );
536
538
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 ));
538
540
539
541
} catch (ApiUtilException ae ) {
540
542
log .error ("Error :: getToken :: " + ae .getMessage ());
@@ -553,33 +555,13 @@ private static long getNewTimestamp() {
553
555
return System .currentTimeMillis ();
554
556
}
555
557
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
+
561
564
return nonce ;
562
565
}
563
566
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
-
585
567
}
0 commit comments