Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ This project use Maven or Gradle as its build and management tools
### Maven Guide

+ Download and Install Maven (3.5.0 or above)
+ Java (1.8)
+ Java (1.7)

#### Maven Build Option

Expand Down Expand Up @@ -63,7 +63,7 @@ mvn install
<dependency>
<groupId>com.api.util</groupId>
<artifactId>ApiSecurity</artifactId>
<version>1.3.2-SNAPSHOT</version>
<version>1.3.5-SNAPSHOT</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
id 'com.github.kt3k.coveralls' version '2.6.3'
}

version '1.3.4-SNAPSHOT'
version '1.3.5-SNAPSHOT'

tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.api.util</groupId>
<artifactId>ApiSecurity</artifactId>
<version>1.3.4-SNAPSHOT</version>
<version>1.3.5-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
Expand Down
48 changes: 31 additions & 17 deletions src/main/java/com/api/util/ApiSecurity/ApiList.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.api.util.ApiSecurity;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map.Entry;
import java.util.stream.Collectors;

/**
* @author GDS-PDD
Expand Down Expand Up @@ -33,22 +34,35 @@ public String toString(String delimiter, Boolean sort, Boolean quote, Boolean is
List<String> list = new ArrayList<String>();

final String format = (quote ? "%s=\"%s\"" : "%s=%s");

/* Sort key first then value*/
if (sort){
list = this.stream()
.sorted((Entry<String,String> l1, Entry<String,String> l2) ->
{
return l1.getKey().equals(l2.getKey()) ? l1.getValue().compareTo(l2.getValue())
: l1.getKey().compareTo(l2.getKey());
})
.map(e -> (null== e.getValue() || (null!= e.getValue() && e.getValue().isEmpty()) && isBaseString) ? e.getKey() : String.format(format, e.getKey(), e.getValue()) )
.collect(Collectors.toList());
} else{
list = this.stream().map(e -> String.format(format, e.getKey(), e.getValue()))
.collect(Collectors.toList());

// Sort key first then value
if (sort) {
List<Entry<String, String>> toSort = new ArrayList<>(this);
Collections.sort(toSort, new Comparator<Entry<String, String>>() {
@Override
public int compare(Entry<String, String> l1, Entry<String, String> l2) {
return l1.getKey().equals(l2.getKey()) ? l1.getValue().compareTo(l2.getValue()) :
l1.getKey().compareTo(l2.getKey());
}
});

for (Entry<String, String> e : toSort) {
String s = (e.getValue() == null || e.getValue().isEmpty() && isBaseString) ? e.getKey() :
String.format(format, e.getKey(), e.getValue());
list.add(s);
}
} else {
for (Entry<String, String> e : this) {
String s = String.format(format, e.getKey(), e.getValue());
list.add(s);
}
}

return String.join(delimiter, list);

StringBuilder stringBuilder = new StringBuilder();
for (String item : list) {
stringBuilder.append(item).append(delimiter);
}
String value = stringBuilder.toString();
return value.substring(0, value.length() - delimiter.length());
}
}
10 changes: 5 additions & 5 deletions src/main/java/com/api/util/ApiSecurity/ApiSigning.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
import org.bouncycastle.openssl.jcajce.JcePEMDecryptorProviderBuilder;
import org.bouncycastle.util.encoders.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -26,7 +27,6 @@
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Base64;


/**
Expand Down Expand Up @@ -91,7 +91,7 @@ public static String getHMACSignature(String baseString, String secret) throws A
}

// base64-encode the hmac
base64Token = new String(Base64.getEncoder().encodeToString(rawHmac));
base64Token = new String(Base64.encode(rawHmac), StandardCharsets.ISO_8859_1);

} catch (ApiUtilException ae) {
log.error("Error :: getHMACSignature :: " + ae.getMessage());
Expand Down Expand Up @@ -176,7 +176,7 @@ public static String getRSASignature(String baseString, PrivateKey privateKey) t
}
log.debug("encryptedData length:" + encryptedData.length);

base64Token = new String(Base64.getEncoder().encode(encryptedData));
base64Token = new String(Base64.encode(encryptedData), StandardCharsets.ISO_8859_1);

} catch (ApiUtilException ae) {
log.error("Error :: getRSASignature :: " + ae.getMessage());
Expand Down Expand Up @@ -223,7 +223,7 @@ public static boolean verifyRSASignature(String baseString, String signature, Pu
throw uee;
}

byte[] signatureBytes = Base64.getDecoder().decode(signature);
byte[] signatureBytes = Base64.decode(signature);

log.debug("Exit :: verifyRSASignature");
try {
Expand Down Expand Up @@ -637,7 +637,7 @@ private static String getNewNonce() throws NoSuchAlgorithmException {
String nonce = null;
byte[] b = new byte[32];
SecureRandom.getInstance("SHA1PRNG").nextBytes(b);
nonce = Base64.getEncoder().encodeToString(b);
nonce = new String(Base64.encode(b), StandardCharsets.ISO_8859_1);

return nonce;
}
Expand Down
39 changes: 35 additions & 4 deletions src/test/java/com/api/util/ApiSecurity/AuthorizationTokenTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.api.util.ApiSecurity;
import com.api.util.ApiSecurity.ApiSigning;
import com.api.util.ApiSecurity.ApiUtilException;
import org.hamcrest.CoreMatchers;
import org.junit.Assume;
import org.junit.Test;

import java.io.File;
Expand Down Expand Up @@ -99,10 +101,11 @@ public void Test_L2_Basic_Test() throws ApiUtilException
}

@Test
public void Test_L2_Wrong_Password_Test() throws ApiUtilException
public void Test_L2_Wrong_Password_Test_Java7() throws ApiUtilException
{
String expectedMessage = "keystore password was incorrect";

Assume.assumeThat(System.getProperty("java.version"), CoreMatchers.startsWith("1.7"));
String expectedMessage = "failed to decrypt safe contents entry: javax.crypto.BadPaddingException: Given final block not properly padded";

try {
ApiSigning.getSignatureToken(
realm
Expand All @@ -125,7 +128,35 @@ public void Test_L2_Wrong_Password_Test() throws ApiUtilException
}
}

@Test
@Test
public void Test_L2_Wrong_Password_Test_Java8() throws ApiUtilException
{
Assume.assumeThat(System.getProperty("java.version"), CoreMatchers.not(CoreMatchers.startsWith("1.7")));
String expectedMessage = "keystore password was incorrect";

try {
ApiSigning.getSignatureToken(
realm
, authPrefixL2
, httpMethod
, url
, appId
, null
, null
, passphrase + "x"
, alias
, privateCertNameP12
, null
, null
);
}
catch (ApiUtilException expected)
{
assertEquals(expectedMessage, expected.getCause().getMessage());
}
}

@Test
public void Test_L2_Not_Supported_Cert_Test() throws ApiUtilException
{
String fileName = getLocalPath("certificates/ssc.alpha.example.com.pem");
Expand Down