-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdo.txt
More file actions
486 lines (346 loc) · 13 KB
/
do.txt
File metadata and controls
486 lines (346 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
https://github.com/manjime83/datapower-tools/blob/77dd05dc325bc978daa455dcf2db059e7c5181b2/jose-firewall/local/js/jose-verify-with-config.js
https://github.com/huqi1996/demo-collection/blob/f875b0640038f52df678cb84f546d4a377a9b16f/src/main/java/com/huqi/qs/crypto/MainCrypto.java
https://www.ibm.com/support/pages/howto-transform-non-xml-input-using-datapower
https://github.com/ibm-datapower/datapower-tools/blob/faf7271944916099bc2b9e03f9d8942b46b39266/coproc2/samples/toHex.xsl
--------------
var body = ' { "name": "test", "description": "test json", "website": "domain.com" } ';
JSON.stringify(JSON.parse(body))
-----
var urlopen = require('urlopen');
var sm = require ('service-metadata.js');
var hm = require ('header-metadata.js');
var uri = sm.getVar ('var://service/URI');
console.warn("URI: " + uri);
--------
var uri = sm.getVar('var://service/URI');
var routeURL = "http://"+destination.host+":"+destination.port+uri;
console.info("Routing to "+routeURL);
sm.setVar('var://service/routing-url', routeURL);
}
session.output.write(destination);
--
var uri = sm.getVar('var://service/URI');
uri=uri.split(?).slice(1);
var querystring = require ('querystring');
var params=querystring.parse(uri);
session.output.write(querystring.parse(uri));
-----
var abc = {
"myparam1": "1",
"myparam2": "2",
"myparam3": "3",
"myparam4": "4"
};
var a1='';
for (var p in abc) {
a1= a1+ p;
}
console.log(a1);
var names3 ='';
for (var p in abc) {
names3 = names3+abc[p];
}
console.log(names3);
var uri = sm.getVar('var://service/URI');
uri=uri.split(?).slice(1);
----------
<xsl:value-of
select="dp:variable('var://service/error-code')" />
<xsl:value-of
select="dp:variable('var://service/error-subcode')" />
<xsl:value-of
select="dp:variable('var://service/error-message')" />
---------
<dp:set-variable name="'var://context/sp/accessToken'"
value='string($acctkn)'/>
---
<xsl:variable name="acctkn">
<xsl:value-of select="//output/access_token/text()"/>
</xsl:variable>
---------
var hm = require('header-metadata');
var allHeaders = hm.current.get();
console.log(JSON.stringify(allHeaders));
var contentType = hm.current.get('Content-Type');
console.log("Content-Type header %s", contentType);
--------
UUID
Since you cannot use dp:generate-uuid in GatewayScript, you have to call it in GatewayScript using the XSLT transform function: https://www.ibm.com/docs/en/datapower-gateways/10.0.1?topic=apis-transform-module
uuid.js:
var transform = require('transform');
var options = {
"location": "local:///generate-uuid.xsl"
};
transform.xslt(options, function(error, nodelist, abortinfo) {
if (error) {
session.output.write("An error was returned when executing '" + options.location + "'");
}
else {
//write out the transformed result
session.output.write(nodelist);
}
});
generate-uuid.xsl:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dp="http://www.datapower.com/extensions"
extension-element-prefixes="dp">
<xsl:output omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:copy-of select="dp:generate-uuid()" />
</xsl:template>
</xsl:stylesheet>
result service output:
curl http://dpl2idg2.rtp.raleigh.ibm.com:9191/
050f68b7-4bae-4b20-9928-b6634d1db595
curl http://dpl2idg2.rtp.raleigh.ibm.com:9191/
8a289630-2195-4c97-8d6e-b6634d1ddd56
curl http://dpl2idg2.rtp.raleigh.ibm.com:9191/
8bc45b4b-37d5-4ac1-bf8f-b6634d1dd666
------------
var hm = require('header-metadata');
var allHeaders = hm.current.get();
console.log(JSON.stringify(allHeaders));
var contentType = hm.current.get('Content-Type');
console.log("Content-Type header %s", contentType);
----------
<xsl:text disable-output-escaping="yes"><![CDATA[CREATE TABLE IF NOT EXISTS `]]></xsl:text>
<ToUserName><![CDATA[toUserId]]></ToUserName>
-----------
var urlopen = require ('urlopen');
var sm = require ('service-metadata');
var dpuri = sm.getVar ('var://service/URI');
var endpoint = 'https://gw-apic.qiwa.info/tal/staging/ta-oauth/oauth2/token';
var options = {
target: endpoint,
method: 'POST',
headers:{'Content-Type' : 'application/x-www-form-urlencoded' },
contentType: 'application/x-www-form-urlencoded',
data : "client_id=ab8eb9a74291d8b627d99c9c328f0821&client_secret=965bf427d5b14de1aae4efdf87851547&grant_type=client_credentials&scope=IntegrationScope"
};
urlopen.open(options, function(error, response) {
if (error) {
// an error occurred during the request sending or response header parsing
session.output.write("urlopen error: "+JSON.stringify(error));
} else {
// get the response status code
var responseStatusCode = response.statusCode;
var responseReasonPhrase = response.reasonPhrase;
console.log("Response status code: " + responseStatusCode);
console.log("Response reason phrase: " + responseReasonPhrase);
// reading response data
response.readAsJSON(function(error, responseData){
if (error){
throw error ;
} else {
var token = "Bearer"+" "+responseData.access_token;
var options1 = {
target: 'https://gw-apic.qiwa.info'+dpuri,
method: 'POST',
headers:{'Authorization' : token },
contentType: 'application/json',
data : json
};
urlopen.open(options1, function(error, response) {
if (error) {
session.output.write("urlopen error: "+JSON.stringify(error));
} else {
response.readAsBuffer(function(error, responseData){
if (error){
throw error ;
} else {
session.output.write(responseData) ;
}
});
}
});
}
});
}
});
----------
if(context.get('request.headers.authorization')){
var vAuth = (context.get('request.headers.authorization').split(' ').pop());
------
get token
var urlopen = require ('urlopen');
var Token =session.name('sp').getVar('accessToken');
var accessToken="";
var sm = require ('service-metadata');
session.output.write({"test":"test"});
session.input.readAsJSON( function ( error, json) {
console.error('count INPUT: %j', JSON.stringify(json));
accessToken = "Bearer ";
accessToken = accessToken.concat(Token);
var options = {
target: 'https://api.ciscospark.com/v1/people/me',
sslClientProfile: 'ciscospark',
method: 'get',
headers: {"Authorization":accessToken}
};
urlopen.open (options, function (error, response) {
if (error) {
session.output.write ("urlopen connect error: " +
JSON.stringify(error));
} else {
var responseStatusCode = response.statusCode;
if (responseStatusCode == 200) {
response.readAsJSON(function(error, responseData)
{
if (error) {
session.output.write("readAsBuffer error:
" + JSON.stringify(error));
} else {
//console.error('count: %j',JSON.stringify(responseData));
session.output.write(JSON.stringify(responseData));
}
});
} else {
session.output.write ("urlopen target return
statusCode " + responseStatusCode);
}
}
});
});
----------------------
var inCtx = 'param1';
var inCtx2 = 'payload';
/**
*
* END: Modify Parameters
*
**/
// Read the input as a JSON object
session.input.readAsJSON(function (error, json) {
if (error) {
throw error;
}
console.notice('json', JSON.stringify(json));
var ctx = session.name('policyCtx') || session.createContext('policyCtx');
ctx.setVar(inCtx, json[inCtx]);
//modify (JSON.stringify | JSON.stringify) to reflect in the input payload
session.output.write(XML.parse(json[inCtx2]));
});
+++++++++=
var urlopen = require('urlopen');
var options = {
target: 'dpkafka://kafka-cluster?RequestTopic=test&ConsumerGroup=datapower&Key=4234234',
data: {"name":"adam"},
timeout: 10
};
urlopen.open(options, function (error, response) {
if (error) {
session.output.write ("urlopen connect error: " + JSON.stringify(error));
} else {
response.readAsBuffer(function(error, data) {
if (error) {
session.output.write("readAsBuffer error: " + JSON.stringify(error));
} else {
session.output.write(data);
}
});
}
});
-----------
console.alert("Starting Read/Write Payload Alex Catch The Session");
//session.input.readAsBuffer(function (error, buffer) {
session.input.readAsBuffer(function (error, buffer) {
if (error) {
// handle error
session.output.write(error.errorMessage);
console.error("readAsBuffer() failure: %s", error); //New
} else {
/* write the default output buffer */
session.output.write(buffer);
console.alert("readAsBuffer() success: %s", buffer); //New
//Find the json
var specifyJson = JSON.stringify(buffer);
console.alert("@@@ //Find the json" + specifyJson);
// var json = JSON.parse();
// console.alert("@@@ //Find the jsonParce " + json);
var responceJson = buffer.id;
console.alert("@@@ //Find the jsonType " + responceJson);
//session.output.write(specifyJson);
//Get variable
//var myInputVar = session.input.getVariable('myInputVar');
//console.alert("@@@ ////Get variable" + myInputVar)
}
});
-----------
var transform = require('transform');
session.input.readAsXML(function (error, nodelist) {
var dom = nodelist.item(0).parentNode;
console.info('dom %s', XML.stringify(dom));
// use xpath to find the username entry
//todo: xpath assumes a single entry but there could be multiple items
transform.xpath('/identity/entry/token/text()', dom, function (error, username) {
if (error) {
console.error('error while executing /identity/entry/* xpath - ' + error);
throw error;
} else {
var usernameNode = XML.stringify(username);
//remove the XML prolog
var usernameString = usernameNode.substring(usernameNode.indexOf('>') + 2);
console.info('username %s', usernameString);
var xmlString =
'<credentials>' +
'<entry type="custom" url="local:///custom_au.xsl">' +
usernameString +
'</entry>' +
'</credentials>'
console.info('xml %s', xmlString);
session.output.write(XML.parse(xmlString));
}
})
});
++++++=
--------------
if(context.get('request.headers.authorization')){
var vAuth = (context.get('request.headers.authorization').split(' ').pop());
title: gatewayscript
version: 1.0.0
source: "var reqauth = apim.getvariable('request.authorization').split(' ');\n
var splitval = new Buffer((reqauth[1] || ''), 'base64').toString('utf8').split(':');\n
var username = splitval[0] || '';\n
var password = splitval[1] || '';\napim.console.debug('user credential : [' + username + ':' + password + ']');\nif (username ===
apim.getvariable('request.parameters.username') && password === apim.getvariable('request.parameters.password')) {\n
\tsession.output.write({ \"authenticatedUser\": username });\n\tapim.setvariable('message.headers.api-authenticated-credential', 'cn='
+ username + ',email=' + username + '@poon.com');\n\tapim.setvariable('message.status.code', 200);\n\tapim.output
('application/json');\n}\nelse {\n\tapim.setvariable('message.status.code', 401);\n}"
- operations:
- verb: get
path: /ping
execute:
/*Base64 decode the token*/
var vDecodedAuthFULL = new Buffer(vAuth,'base64').toString('ascii');
console.debug('****Decoded.Auth.Header: ' + vDecodedAuthFULL);
/*Modify decoded token to fit json format*/
var vDecodedAuthSPLIT = JSON.parse(vDecodedAuthFULL.split('}.{'));
-----
const jose = require('jose');
const fs = require('fs');
const sm = require('service-metadata');
const hm = require('header-metadata');
fs.readAsJSON(`local:///json/service-certs/${sm.processorName}.json`, readFileCallback);
function readFileCallback(error, data) {
if (error) {
session.reject(error.errorMessage);
return;
}
session.input.readAsBuffer(readInputCallback.bind(this, data));
}
function readInputCallback(data, error, plaintext) {
if (error) {
session.reject(error.errorMessage);
return;
}
const jwsObj = jose.parse(plaintext);
let certKey;
if (data.config['http-header']) {
certKey = hm.current.get(data.config['http-header']);
} else if (data.config['jws-header']) {
certKey = jwsObj.getProtected(data.config['http-header']);
} else if (data.config['context-variable']) {
const contextParts = data.config['context-variable'].substring(6).split('/').slice(1);
const name = contextParts.shift();
const variable = contextParts.join('/');