Skip to content

Commit ad37ad3

Browse files
author
Kelvin Wijaya
authored
Merge pull request #7 from GovTechSG/development
Development
2 parents 3dbc678 + 6b66474 commit ad37ad3

26 files changed

+2421
-73
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@
2121
### V1.2.0-SNAPSHOT
2222
+ Bug fixes for null value checking
2323
+ 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
24+
+ Update nonce method to generate base64 encoded string value of 32 bytes characters
25+
### V1.3.0-SNAPSHOT
26+
+ Update test-suites framework to cover more use-case scenarios
27+
+ Add Utility to read from PKI Key with PEM format
28+
+ Update README documentation

README.md

Lines changed: 64 additions & 46 deletions
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.2.0-SNAPSHOT</version>
68+
<version>1.3.0-SNAPSHOT</version>
6969
</dependency>
7070

7171
```
@@ -133,20 +133,75 @@ repositories {
133133
mavenLocal()
134134
}
135135
dependencies {
136-
compile group: 'com.api.util', name: 'ApiSecurity', version: '1.0-SNAPSHOT'
136+
compile group: 'com.api.util', name: 'ApiSecurity', version: '1.3.0-SNAPSHOT'
137137
}
138138
139139
```
140140

141141
### Development
142142

143-
#### Constructing Signature BaseString
143+
#### Preparing HTTP Signature Token
144+
145+
Append this signature token into the Authorization header of the HTTP request
146+
147+
Params:
148+
* realm
149+
* authPrefix - Authorization Header scheme prefix , i.e 'Apex_l2_eg'
150+
* httpMethod
151+
* urlPath - Signing URL, remember to append <<tenant>>.e.api.gov.sg or <<tenant>>-pvt.i.api.gov.sg in <<URL>>
152+
* appId - App ID created in Gateway
153+
* secret - set to null for REST L2 SHA256WITHRSA
154+
* formList - to support parameter for form data if any
155+
* password
156+
* alias
157+
* fileName
158+
* nonce - set to null for random generated number
159+
* timestamp - set to null for current timestamp
160+
161+
162+
```java
163+
String realm = "<<your_client_host_url>>"
164+
String authPrefix = "<<authPrefix>>
165+
String httpMethod = "get"
166+
//Append the query param in the url or else add as ApiList
167+
String signingUrl = "https://<<URL>>/api/v1/?param1=first&param2=123";
168+
String certFileName = "certificates/ssc.alpha.example.com.p12";
169+
String password = "<<passphrase>>";
170+
String alias = "alpha";
171+
String appId = "<<appId>>";
172+
String secret = null;
173+
//only needed for Content-Type: application/x-www-form-urlencoded, else null
174+
ApiList formList = null;
175+
String nonce = null;
176+
String timestamp = null;
177+
178+
179+
//optional for QueryParam - in-case not append the query parameters in the signingUrl
180+
//Sring signingUrl = "https://<<tenant>>-pvt.i.api.gov.sg/api/v1"
181+
ApiList queryParam = new ApiList();
182+
queryParam.add("query1","value1");
183+
184+
//optional for formList
185+
ApiList formList = new ApiList();
186+
formList.add("param1", "data1");
187+
188+
//If queryParam and formList are both available, combine the list before submitting
189+
formList.addAll(queryParam);
190+
191+
try {
192+
String signature = ApiSigning.getSignatureToken(authPrefix, authPrefix, httpMethod, signingUrl, appId, secret, formList, password, alias, certFileName, nonce, timestamp);
193+
} catch (ApiUtilException e) {
194+
e.printStackTrace();
195+
}
196+
```
197+
198+
#### Constructing Signature BaseString (for reference only)
144199

145200
Method:
146201
* getBaseString
147202

148203
Params:
149-
* authPrefix - Authorization Header scheme prefix , i.e 'prefix_appId'
204+
* authPrefix - Authorization Header scheme prefix , i.e 'Apex_l2_eg'
150205
* signatureMethod
151206
* appId - App ID created in Gateway
152207
* urlPath
@@ -156,7 +211,7 @@ Params:
156211
* timestamp - set to null for current timestamp
157212

158213
```java
159-
String url = "https://<<URL>>/api/v1/?param1=first&ab-param2=123";
214+
String signingUrl = "https://<<URL>>/api/v1/?param1=first&param2=123";
160215

161216
ApiList formList = new ApiList();
162217
formList.add("param1", "data1");
@@ -168,7 +223,7 @@ baseString = ApiSigning.getBaseString(
168223
"<<authPrefix>>",
169224
"HMACSHA256",
170225
"<<appId>>",
171-
url,
226+
signingUrl,
172227
"post",
173228
formList,
174229
"6584351262900708156",
@@ -183,7 +238,7 @@ System.out.println(baseString);
183238

184239
```
185240

186-
#### Constructing HMAC256 L1 Header
241+
#### Constructing HMAC256 L1 Header (for reference only)
187242

188243
Method:
189244
* getHMACSignature
@@ -207,7 +262,7 @@ try {
207262

208263
```
209264

210-
#### Constructing RSA256 L2 Header
265+
#### Constructing RSA256 L2 Header (for reference only)
211266

212267
Method:
213268
* getRSASignature
@@ -217,7 +272,7 @@ Params:
217272
* privateKey
218273

219274
```java
220-
String baseString = "GET&https://<<URL>/api/v1/&ap=裕廊坊 心邻坊&<<authPrefix>>_app_id=<<appId>>&<<authPrefix>>_nonce=7231415196459608363&<<authPrefix>>_signature_method=SHA256withRSA&<<authPrefix>>_timestamp=1502164219425&<<authPrefix>>_version=1.0&oq=c# nunit mac&q=c# nunit mac";
275+
String baseString = "GET&https://<<URL>/api/v1/&ap=裕廊坊 心邻坊&<<authPrefix>>_app_id=<<appId>>&<<authPrefix>>_nonce=7231415196459608363&<<authPrefix>>_signature_method=SHA256withRSA&<<authPrefix>>_timestamp=1502164219425&<<authPrefix>>_version=1.0&oq=123&q=abc";
221276
String alias = "alpha";
222277
String password = "<<passphrase>>";
223278
String keyStoreFileName = "certificates/ssc.alpha.example.com.p12";
@@ -240,43 +295,6 @@ try {
240295

241296
```
242297

243-
#### Preparing HTTP Signature Token
244-
245-
Append this signature token into the Authorization header of the HTTP request
246-
247-
Params:
248-
* realm
249-
* authPrefix - Authorization Header scheme prefix , i.e 'prefix_appId'
250-
* httpMethod
251-
* urlPath
252-
* appId - App ID created in Gateway
253-
* secret - set to null for REST L2 SHA256WITHRSA
254-
* formList
255-
* password
256-
* alias
257-
* fileName
258-
* nonce - set to null for random generated number
259-
* timestamp - set to null for current timestamp
260-
261-
262-
```java
263-
String url = "https://<<URL>>/api/v1/?ap=裕廊坊%20心邻坊";
264-
String certFileName = "certificates/ssc.alpha.example.com.p12";
265-
String password = "<<passphrase>>";
266-
String alias = "alpha";
267-
String appId = "<<appId>>";
268-
String secret = null;
269-
ApiList formList = null;
270-
String nonce = null;
271-
String timestamp = null;
272-
273-
try {
274-
String signature = ApiSigning.getSignatureToken("http://api.test.io/l2", "<<authPrefix>>", "get", url, appId, null, null, password, alias, certFileName, nonce, timestamp);
275-
} catch (ApiUtilException e) {
276-
e.printStackTrace();
277-
}
278-
```
279-
280298
## Contributing
281299
For more information about contributing PRs and issues, see [CONTRIBUTING.md](.github/CONTRIBUTING.md).
282300

build.gradle

Lines changed: 6 additions & 2 deletions
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.2.0-SNAPSHOT'
7+
version '1.3.0-SNAPSHOT'
88

99
tasks.withType(JavaCompile) {
1010
options.encoding = "UTF-8"
@@ -15,8 +15,12 @@ repositories {
1515
}
1616

1717
dependencies {
18-
compile group: 'org.apache.commons', name: 'commons-collections4', version: '4.1'
18+
//compile group: 'org.apache.commons', name: 'commons-collections4', version: '4.1'
19+
compile group: 'commons-lang', name: 'commons-lang', version: '2.4'
1920
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
21+
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.7'
22+
compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
23+
compile group: 'org.bouncycastle', name: 'bcpkix-jdk15on', version: '1.60'
2024
testCompile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.25'
2125
testCompile group: 'junit', name: 'junit', version: '4.12'
2226
}

pom.xml

Lines changed: 42 additions & 1 deletion
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.2.0-SNAPSHOT</version>
5+
<version>1.3.0-SNAPSHOT</version>
66
<build>
77
<plugins>
88
<plugin>
@@ -29,6 +29,22 @@
2929
</descriptorRefs>
3030
</configuration>
3131
</plugin>
32+
<plugin>
33+
<groupId>org.jsonschema2pojo</groupId>
34+
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
35+
<version>0.5.1</version>
36+
<configuration>
37+
<sourceDirectory>src/main/resources/schema</sourceDirectory>
38+
<targetPackage>com.api.util.ApiSecurity</targetPackage>
39+
</configuration>
40+
<executions>
41+
<execution>
42+
<goals>
43+
<goal>generate</goal>
44+
</goals>
45+
</execution>
46+
</executions>
47+
</plugin>
3248
</plugins>
3349
</build>
3450
<dependencies>
@@ -56,8 +72,33 @@
5672
<artifactId>slf4j-log4j12</artifactId>
5773
<version>1.7.25</version>
5874
</dependency>
75+
<dependency>
76+
<groupId>commons-lang</groupId>
77+
<artifactId>commons-lang</artifactId>
78+
<version>2.4</version>
79+
</dependency>
80+
<dependency>
81+
<groupId>com.fasterxml.jackson.core</groupId>
82+
<artifactId>jackson-databind</artifactId>
83+
<version>2.9.7</version>
84+
</dependency>
85+
<dependency>
86+
<groupId>com.googlecode.json-simple</groupId>
87+
<artifactId>json-simple</artifactId>
88+
<version>1.1.1</version>
89+
</dependency>
90+
<dependency>
91+
<groupId>org.bouncycastle</groupId>
92+
<artifactId>bcpkix-jdk15on</artifactId>
93+
<version>1.60</version>
94+
</dependency>
5995
</dependencies>
6096
<properties>
6197
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
6298
</properties>
99+
<scm>
100+
<connection>scm:git:https://github.com/GovTechSG/test-suites-apex-api-security.git</connection>
101+
<developerConnection>scm:git:https://kelvinwijaya@github.com/GovTechSG/test-suites-apex-api-security.git</developerConnection>
102+
<url>https://github.com/GovTechSG</url>
103+
</scm>
63104
</project>

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.api.util.ApiSecurity;
22
import java.util.AbstractMap.SimpleEntry;
33
import java.util.ArrayList;
4-
import java.util.Collections;
54
import java.util.List;
65
import java.util.Map.Entry;
76
import java.util.stream.Collectors;
@@ -13,7 +12,7 @@
1312
public class ApiList extends ArrayList<Entry<String,String>>{
1413

1514
private static final long serialVersionUID = 1L;
16-
15+
1716
public void add(String key, String value)
1817
{
1918
Entry<String, String> item = new SimpleEntry<String, String>(key, value);
@@ -43,7 +42,7 @@ public String toString(String delimiter, Boolean sort, Boolean quote, Boolean is
4342
return l1.getKey().equals(l2.getKey()) ? l1.getValue().compareTo(l2.getValue())
4443
: l1.getKey().compareTo(l2.getKey());
4544
})
46-
.map(e -> (null!= e.getValue() && e.getValue().equals("") && isBaseString) ? e.getKey() : String.format(format, e.getKey(), e.getValue()) )
45+
.map(e -> (null== e.getValue() || (null!= e.getValue() && e.getValue().isEmpty()) && isBaseString) ? e.getKey() : String.format(format, e.getKey(), e.getValue()) )
4746
.collect(Collectors.toList());
4847
} else{
4948
list = this.stream().map(e -> String.format(format, e.getKey(), e.getValue()))

0 commit comments

Comments
 (0)