Skip to content

Commit 3dbc678

Browse files
author
Kelvin Wijaya
authored
Merge pull request #6 from GovTechSG/development
Development
2 parents 27fa7e9 + 50a0c5f commit 3dbc678

File tree

6 files changed

+29
-43
lines changed

6 files changed

+29
-43
lines changed

CHANGELOG.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@
1717
+ Update templates
1818
### V1.1.2-SNAPSHOT
1919
+ Minor refactoring
20-
+ Update documentation
20+
+ Update documentation
21+
### V1.2.0-SNAPSHOT
22+
+ Bug fixes for null value checking
23+
+ Update basestring method to suppport use-case where value of queryparam or form value is empty
24+
+ Update nonce method to generate base64 encoded string value of 32 bytes characters

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ mvn install
6565
<dependency>
6666
<groupId>com.api.util</groupId>
6767
<artifactId>ApiSecurity</artifactId>
68-
<version>1.1.2-SNAPSHOT</version>
68+
<version>1.2.0-SNAPSHOT</version>
6969
</dependency>
7070

7171
```

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
id 'com.github.kt3k.coveralls' version '2.6.3'
55
}
66

7-
version '1.1.1-SNAPSHOT'
7+
version '1.2.0-SNAPSHOT'
88

99
tasks.withType(JavaCompile) {
1010
options.encoding = "UTF-8"

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>com.api.util</groupId>
44
<artifactId>ApiSecurity</artifactId>
5-
<version>1.1.1-SNAPSHOT</version>
5+
<version>1.2.0-SNAPSHOT</version>
66
<build>
77
<plugins>
88
<plugin>

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ public void add(String key, String value)
2121
this.add(item);
2222
}
2323

24-
public String toString() {
24+
public String toString(Boolean isBaseString) {
2525
String delimiter = "&";
2626
Boolean sort = true;
2727
Boolean quote = false;
2828

29-
return this.toString(delimiter, sort, quote);
29+
return this.toString(delimiter, sort, quote, isBaseString);
3030
}
3131

32-
public String toString(String delimiter, Boolean sort, Boolean quote)
32+
public String toString(String delimiter, Boolean sort, Boolean quote, Boolean isBaseString)
3333
{
3434
List<String> list = new ArrayList<String>();
3535

@@ -43,7 +43,7 @@ public String toString(String delimiter, Boolean sort, Boolean quote)
4343
return l1.getKey().equals(l2.getKey()) ? l1.getValue().compareTo(l2.getValue())
4444
: l1.getKey().compareTo(l2.getKey());
4545
})
46-
.map(e -> String.format(format, e.getKey(), e.getValue()))
46+
.map(e -> (null!= e.getValue() && e.getValue().equals("") && isBaseString) ? e.getKey() : String.format(format, e.getKey(), e.getValue()) )
4747
.collect(Collectors.toList());
4848
} else{
4949
list = this.stream().map(e -> String.format(format, e.getKey(), e.getValue()))

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

+17-35
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.isEmpty()) ? 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)