Skip to content

Commit a225152

Browse files
author
Kelvin Wijaya
authored
Merge pull request #8 from GovTechSG/development
Development
2 parents ad37ad3 + cef567c commit a225152

File tree

1 file changed

+100
-12
lines changed

1 file changed

+100
-12
lines changed

README.md

Lines changed: 100 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ Params:
151151
* urlPath - Signing URL, remember to append <<tenant>>.e.api.gov.sg or <<tenant>>-pvt.i.api.gov.sg in <<URL>>
152152
* appId - App ID created in Gateway
153153
* secret - set to null for REST L2 SHA256WITHRSA
154-
* formList - to support parameter for form data if any
154+
* formData - to support parameter for form data if any
155155
* password
156156
* alias
157157
* fileName
@@ -171,7 +171,7 @@ String alias = "alpha";
171171
String appId = "<<appId>>";
172172
String secret = null;
173173
//only needed for Content-Type: application/x-www-form-urlencoded, else null
174-
ApiList formList = null;
174+
ApiList formData = null;
175175
String nonce = null;
176176
String timestamp = null;
177177

@@ -181,19 +181,24 @@ String timestamp = null;
181181
ApiList queryParam = new ApiList();
182182
queryParam.add("query1","value1");
183183

184-
//optional for formList
185-
ApiList formList = new ApiList();
186-
formList.add("param1", "data1");
184+
//optional for formData
185+
formData = new ApiList();
186+
formData.add("param1", "data1");
187187

188-
//If queryParam and formList are both available, combine the list before submitting
189-
formList.addAll(queryParam);
188+
//If queryParam and formData are both available, combine the list before submitting
189+
formData.addAll(queryParam);
190190

191191
try {
192-
String signature = ApiSigning.getSignatureToken(authPrefix, authPrefix, httpMethod, signingUrl, appId, secret, formList, password, alias, certFileName, nonce, timestamp);
192+
String signature = ApiSigning.getSignatureToken(authPrefix, authPrefix, httpMethod, signingUrl, appId, secret, formData, password, alias, certFileName, nonce, timestamp);
193193
} catch (ApiUtilException e) {
194194
e.printStackTrace();
195195
}
196196
```
197+
**NOTE**
198+
199+
For **formData** parameter used for Signature generation, the key value parameters **do not** need to be URL encoded,
200+
When your client program is making the actual HTTP POST call, the key value parameters **has** to be URL encoded (refer to **formPostData**)
201+
197202

198203
#### Constructing Signature BaseString (for reference only)
199204

@@ -206,15 +211,15 @@ Params:
206211
* appId - App ID created in Gateway
207212
* urlPath
208213
* httpMethod
209-
* formList - only needed for Content-Type: application/x-www-form-urlencoded
214+
* formData - only needed for Content-Type: application/x-www-form-urlencoded
210215
* nonce - set to null for random generated number
211216
* timestamp - set to null for current timestamp
212217

213218
```java
214219
String signingUrl = "https://<<URL>>/api/v1/?param1=first&param2=123";
215220

216-
ApiList formList = new ApiList();
217-
formList.add("param1", "data1");
221+
ApiList formData = new ApiList();
222+
formData.add("param1", "data1");
218223

219224
String baseString;
220225

@@ -225,7 +230,7 @@ baseString = ApiSigning.getBaseString(
225230
"<<appId>>",
226231
signingUrl,
227232
"post",
228-
formList,
233+
formData,
229234
"6584351262900708156",
230235
"1502184161702"
231236
);
@@ -293,6 +298,89 @@ try {
293298
e.printStackTrace();
294299
}
295300

301+
```
302+
#### Sample HTTP POST Call for x-www-form-urlencoded with APEX L1 Security (for reference only)
303+
304+
```java
305+
306+
@Test
307+
public void Http_Call_Test() throws ApiUtilException, IOException
308+
{
309+
310+
String httpMethod = "POST";
311+
//URL for actual HTTP API call
312+
String url = "https://tenant.api.gov.sg:443/api14021live/resource";
313+
//URL for passing as parameter for APEX Signature Token generation
314+
String signUrl = "https://tenant.e.api.gov.sg:443/api14021live/resource";
315+
String appId = "tenant-1X2w7NQPzjO2azDu904XI5AE";
316+
String secret = "s0m3s3cr3t";
317+
ApiList formData = new ApiList();
318+
formData.add("key1", "value1");
319+
formData.add("key2","value2");
320+
321+
String authorizationToken = ApiSigning.getSignatureToken(
322+
realm
323+
, authPrefixL1
324+
, httpMethod
325+
, signUrl
326+
, appId
327+
, secret
328+
, formData
329+
, null
330+
, null
331+
, null
332+
, null
333+
, null
334+
);
335+
System.out.println("authorizationToken : "+authorizationToken);
336+
337+
try {
338+
//ignore SSL
339+
SSLContext sslContext = SSLContext.getInstance("SSL");
340+
sslContext.init(null, getTrustManager(), new java.security.SecureRandom());
341+
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
342+
343+
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
344+
con.setDoOutput(true);
345+
con.setDoInput(true);
346+
con.setRequestMethod(httpMethod);
347+
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
348+
con.setRequestProperty("charset", "utf-8");
349+
con.setRequestProperty("Authorization", authorizationToken);
350+
con.setUseCaches(false);
351+
con.setConnectTimeout(5000);
352+
con.setReadTimeout(5000);
353+
354+
DataOutputStream out = new DataOutputStream(con.getOutputStream());
355+
ApiList formPostData = new ApiList();
356+
formPostData.add("key1",URLEncoder.encode("value1", "UTF-8"));
357+
formPostData.add("key2",URLEncoder.encode("value2", "UTF-8"));
358+
out.writeBytes(formPostData.toString(false));
359+
out.flush();
360+
out.close();
361+
System.out.println("Start http call ...");
362+
int status = -1;
363+
status = con.getResponseCode();
364+
System.out.println("HTTP Status:" + status);
365+
366+
System.out.println("End http call ...");
367+
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
368+
String inputLine;
369+
StringBuffer content = new StringBuffer();
370+
while ((inputLine = in.readLine()) != null) {
371+
content.append(inputLine);
372+
}
373+
374+
System.out.println("Content:" + content);
375+
in.close();
376+
con.disconnect();
377+
}catch(Exception e){
378+
System.out.println("Error executing Http_Call_Test() : " + e);
379+
}
380+
//force to true to pass the test case
381+
assertTrue(true);
382+
}
383+
296384
```
297385

298386
## Contributing

0 commit comments

Comments
 (0)