@@ -145,27 +145,84 @@ dependencies {
145
145
146
146
#### Preparing HTTP Signature Token
147
147
148
- Append this signature token into the Authorization header of the HTTP request
148
+ Append this signature token into the Authorization header of the HTTP
149
+ request.
150
+
151
+ #### Example Generated Token -
152
+
153
+ ```
154
+ Apex_l1_eg realm="https://XYZ.api.gov.sg/abc/def", apex_l1_eg_app_id="APP_ID", apex_l1_eg_nonce="SOME_RANDOM_STRING", apex_l1_eg_signature_method="HMACSHA256", apex_l1_eg_timestamp="SOME_TIMESTAMP", apex_l1_eg_version="1.0", apex_l1_eg_signature="SOME_SIGNATURE"
155
+ ```
156
+
157
+ #### Example Authorization Header -
158
+
159
+ ```
160
+ Authorization: Apex_l1_eg realm="https://XYZ.api.gov.sg/abc/def", apex_l1_eg_app_id="APP_ID", apex_l1_eg_nonce="SOME_RANDOM_STRING", apex_l1_eg_signature_method="HMACSHA256", apex_l1_eg_timestamp="SOME_TIMESTAMP", apex_l1_eg_version="1.0", apex_l1_eg_signature="SOME_SIGNATURE"
161
+ ```
162
+
163
+ ### Parameters
164
+
165
+ #### realm
166
+ This is an identifier for the caller. Any value can be used here.
167
+
168
+ #### authPrefix
169
+
170
+ Authorization Header scheme prefix. There are 4 possible values for this
171
+ depending on the zone and the authentication method.
172
+
173
+ 1 . Apex_l1_ig
174
+ 2 . Apex_l1_eg
175
+ 3 . Apex_l2_ig
176
+ 4 . Apex_l2_eg
177
+
178
+ #### httpMethod
179
+
180
+ The HTTP method, i.e. ` GET ` , ` POST ` , etc.
181
+
182
+ #### signingUrl
183
+ The full API endpoint (with query parameters if any). This will be in
184
+ the form of ` https://<<tenant>>.e.api.gov.sg/xxx/yyy ` or
185
+ ` https://<<tenant>>-pvt.i.api.gov.sg/xxx/yyy ` .
186
+
187
+ ** Note:** Please note that you ** must** have ` .e ` or ` .i ` in the URL.
188
+ Otherwise you can encounter authorization failures.
189
+
190
+ #### appId
191
+ The APEX App ID.
192
+
193
+ #### secret
194
+ The APEX App secret. Set to value to ` null ` if you want to use L2
195
+ authentication with SHA256WITHRSA.
196
+
197
+ #### formData
198
+ Data which should be passed in the request (for ` POST ` requests
199
+ usually). For ` GET ` requests, set this value to ` null ` .
200
+
201
+ #### password
202
+ The password of the keystore. Set ` null ` for L1.
203
+
204
+ #### alias
205
+ The alias of the keystore. Set ` null ` for L1.
206
+
207
+ #### fileName
208
+ The p12 file path. Set ` null ` for L1.
209
+
210
+ #### nonce
211
+ The random generated string which to be used to generate the token. If
212
+ you set this to ` null ` , a new random string will be generated.
213
+
214
+ #### timestamp
215
+ Timestamp which should be used to generate the token. Set to ` null ` if
216
+ you want to use the current timestamp.
217
+
149
218
150
- Params:
151
- * realm
152
- * authPrefix - Authorization Header scheme prefix , i.e 'Apex_l2_eg'
153
- * httpMethod
154
- * urlPath - Signing URL, remember to append <<tenant >>.e.api.gov.sg or <<tenant >>-pvt.i.api.gov.sg in <<URL >>
155
- * appId - App ID created in Gateway
156
- * secret - set to null for REST L2 SHA256WITHRSA
157
- * formData - to support parameter for form data if any
158
- * password
159
- * alias
160
- * fileName
161
- * nonce - set to null for random generated number
162
- * timestamp - set to null for current timestamp
163
219
220
+ ### Example GET Request
164
221
165
222
``` java
166
- String realm = " <<your_client_host_url>>"
167
- String authPrefix = " <<authPrefix>>
168
- String httpMethod = " get "
223
+ String realm = " <<your_client_host_url>>" ;
224
+ String authPrefix = " <<authPrefix>>" ;
225
+ String httpMethod = " GET " ;
169
226
// Append the query param in the url or else add as ApiList
170
227
String signingUrl = " https://<<URL>>/api/v1/?param1=first¶m2=123" ;
171
228
String certFileName = " certificates/ssc.alpha.example.com.p12" ;
@@ -178,6 +235,33 @@ ApiList formData = null;
178
235
String nonce = null ;
179
236
String timestamp = null ;
180
237
238
+ try {
239
+ String signature = ApiSigning . getSignatureToken(authPrefix, authPrefix, httpMethod, signingUrl, appId, secret, formData, password, alias, certFileName, nonce, timestamp);
240
+ // Add this signature value to the authorization header when sending the request.
241
+ } catch (ApiUtilException e) {
242
+ e. printStackTrace();
243
+ }
244
+ ```
245
+
246
+
247
+ ### Example POST Request
248
+
249
+ ``` java
250
+ String realm = " <<your_client_host_url>>" ;
251
+ String authPrefix = " <<authPrefix>>" ;
252
+ String httpMethod = " POST" ;
253
+ // Append the query param in the url or else add as ApiList
254
+ String signingUrl = " https://<<URL>>/api/v1" ;
255
+ String certFileName = " certificates/ssc.alpha.example.com.p12" ;
256
+ String password = " <<passphrase>>" ;
257
+ String alias = " alpha" ;
258
+ String appId = " <<appId>>" ;
259
+ String secret = null ;
260
+ // only needed for Content-Type: application/x-www-form-urlencoded, else null
261
+ ApiList formData = null ;
262
+ String nonce = null ;
263
+ String timestamp = null ;
264
+
181
265
182
266
// optional for QueryParam - in-case not append the query parameters in the signingUrl
183
267
// Sring signingUrl = "https://<<tenant>>-pvt.i.api.gov.sg/api/v1"
@@ -193,6 +277,7 @@ formData.addAll(queryParam);
193
277
194
278
try {
195
279
String signature = ApiSigning . getSignatureToken(authPrefix, authPrefix, httpMethod, signingUrl, appId, secret, formData, password, alias, certFileName, nonce, timestamp);
280
+ // Add this signature value to the authorization header when sending the request.
196
281
} catch (ApiUtilException e) {
197
282
e. printStackTrace();
198
283
}
@@ -205,6 +290,9 @@ When your client program is making the actual HTTP POST call, the key value para
205
290
206
291
#### Constructing Signature BaseString (for reference only)
207
292
293
+ ** Please note that this section is for reference only. The actual token
294
+ generation is done using the ` ApiSigning.getSignatureToken() ` method.**
295
+
208
296
Method:
209
297
* getBaseString
210
298
@@ -302,12 +390,93 @@ try {
302
390
}
303
391
304
392
```
393
+
394
+ #### Sample HTTP GET Call with APEX L1 Security (for reference only)
395
+
396
+ ** Please note that this is for reference only. The actual implementation
397
+ might be different than this.**
398
+
399
+ ``` java
400
+
401
+ @Test
402
+ public void Http_GET_Test() throws ApiUtilException , IOException
403
+ {
404
+
405
+ String httpMethod = " GET" ;
406
+ // URL for actual HTTP API call
407
+ String url = " https://tenant.api.gov.sg:443/api14021live/resource" ;
408
+ // URL for passing as parameter for APEX Signature Token generation
409
+ String signUrl = " https://tenant.e.api.gov.sg:443/api14021live/resource" ;
410
+ String appId = " tenant-1X2w7NQPzjO2azDu904XI5AE" ;
411
+ String secret = " s0m3s3cr3t" ;
412
+
413
+ String authorizationToken = ApiSigning . getSignatureToken(
414
+ realm
415
+ , authPrefixL1
416
+ , httpMethod
417
+ , signUrl
418
+ , appId
419
+ , secret
420
+ , null ;
421
+ , null
422
+ , null
423
+ , null
424
+ , null
425
+ , null
426
+ );
427
+ System . out. println(" authorizationToken : " + authorizationToken);
428
+
429
+ try {
430
+ // ignore SSL
431
+ SSLContext sslContext = SSLContext . getInstance(" SSL" );
432
+ sslContext. init(null , getTrustManager(), new java.security. SecureRandom ());
433
+ HttpsURLConnection . setDefaultSSLSocketFactory(sslContext. getSocketFactory());
434
+
435
+ HttpURLConnection con = (HttpURLConnection ) new URL (url). openConnection();
436
+ con. setDoOutput(false );
437
+ con. setDoInput(true );
438
+ con. setRequestMethod(httpMethod);
439
+ con. setRequestProperty(" charset" , " utf-8" );
440
+ con. setRequestProperty(" Authorization" , authorizationToken);
441
+ con. setUseCaches(false );
442
+ con. setConnectTimeout(5000 );
443
+ con. setReadTimeout(5000 );
444
+
445
+ System . out. println(" Start http call ..." );
446
+ int status = - 1 ;
447
+ status = con. getResponseCode();
448
+ System . out. println(" HTTP Status:" + status);
449
+
450
+ System . out. println(" End http call ..." );
451
+ BufferedReader in = new BufferedReader (new InputStreamReader (con. getInputStream()));
452
+ String inputLine;
453
+ StringBuffer content = new StringBuffer ();
454
+ while ((inputLine = in. readLine()) != null ) {
455
+ content. append(inputLine);
456
+ }
457
+
458
+ System . out. println(" Content:" + content);
459
+ in. close();
460
+ con. disconnect();
461
+ }catch (Exception e){
462
+ System . out. println(" Error executing Http_Call_Test() : " + e);
463
+ }
464
+ // force to true to pass the test case
465
+ assertTrue(true );
466
+ }
467
+
468
+ ```
469
+
470
+
305
471
#### Sample HTTP POST Call for x-www-form-urlencoded with APEX L1 Security (for reference only)
306
472
473
+ ** Please note that this is for reference only. The actual implementation
474
+ might be different than this.**
475
+
307
476
``` java
308
477
309
478
@Test
310
- public void Http_Call_Test () throws ApiUtilException , IOException
479
+ public void Http_POST_Test () throws ApiUtilException , IOException
311
480
{
312
481
313
482
String httpMethod = " POST" ;
0 commit comments