k2hr3-api 1.0.42 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. package/config/k2hr3-init.sh.templ +4 -4
  2. package/dist/.gitkeep +0 -0
  3. package/dist/src/app.js +262 -0
  4. package/{bin → dist/src/bin}/run.sh +1 -1
  5. package/dist/src/bin/watcher.js +113 -0
  6. package/dist/src/bin/www.js +217 -0
  7. package/dist/src/lib/basicipcheck.js +392 -0
  8. package/dist/src/lib/cacerts.js +106 -0
  9. package/dist/src/lib/dbglogging.js +190 -0
  10. package/dist/src/lib/dummyuserapi.js +719 -0
  11. package/dist/src/lib/ipwatch.js +354 -0
  12. package/dist/src/lib/k2hr3acrutil.js +532 -0
  13. package/dist/src/lib/k2hr3apiutil.js +1444 -0
  14. package/dist/src/lib/k2hr3cliutil.js +183 -0
  15. package/dist/src/lib/k2hr3config.js +832 -0
  16. package/dist/src/lib/k2hr3cryptutil.js +258 -0
  17. package/dist/src/lib/k2hr3dkc.js +12121 -0
  18. package/dist/src/lib/k2hr3extdata.js +198 -0
  19. package/dist/src/lib/k2hr3keys.js +207 -0
  20. package/dist/src/lib/k2hr3resutil.js +111 -0
  21. package/dist/src/lib/k2hr3template.js +6546 -0
  22. package/dist/src/lib/k2hr3tokens.js +2643 -0
  23. package/dist/src/lib/k2hr3userdata.js +296 -0
  24. package/dist/src/lib/k8soidc.js +1000 -0
  25. package/dist/src/lib/openstackapiv2.js +695 -0
  26. package/dist/src/lib/openstackapiv3.js +932 -0
  27. package/dist/src/lib/openstackep.js +667 -0
  28. package/{tests/auto_common.js → dist/src/lib/types.js} +4 -38
  29. package/dist/src/routes/acr.js +704 -0
  30. package/dist/src/routes/debugVerify.js +294 -0
  31. package/dist/src/routes/extdata.js +219 -0
  32. package/dist/src/routes/list.js +264 -0
  33. package/dist/src/routes/policy.js +840 -0
  34. package/dist/src/routes/resource.js +1489 -0
  35. package/dist/src/routes/role.js +2627 -0
  36. package/dist/src/routes/service.js +908 -0
  37. package/dist/src/routes/tenant.js +1141 -0
  38. package/dist/src/routes/userTokens.js +482 -0
  39. package/dist/src/routes/userdata.js +212 -0
  40. package/dist/src/routes/version.js +103 -0
  41. package/package.json +152 -121
  42. package/ChangeLog +0 -378
  43. package/app.js +0 -292
  44. package/bin/watcher +0 -122
  45. package/bin/www +0 -180
  46. package/eslint.config.mjs +0 -68
  47. package/lib/basicipcheck.js +0 -376
  48. package/lib/cacerts.js +0 -71
  49. package/lib/dbglogging.js +0 -151
  50. package/lib/dummyuserapi.js +0 -766
  51. package/lib/ipwatch.js +0 -379
  52. package/lib/k2hr3acrutil.js +0 -516
  53. package/lib/k2hr3apiutil.js +0 -1494
  54. package/lib/k2hr3cliutil.js +0 -191
  55. package/lib/k2hr3config.js +0 -826
  56. package/lib/k2hr3cryptutil.js +0 -254
  57. package/lib/k2hr3dkc.js +0 -12632
  58. package/lib/k2hr3extdata.js +0 -198
  59. package/lib/k2hr3keys.js +0 -234
  60. package/lib/k2hr3resutil.js +0 -100
  61. package/lib/k2hr3template.js +0 -6925
  62. package/lib/k2hr3tokens.js +0 -2799
  63. package/lib/k2hr3userdata.js +0 -312
  64. package/lib/k8soidc.js +0 -1012
  65. package/lib/openstackapiv2.js +0 -764
  66. package/lib/openstackapiv3.js +0 -1032
  67. package/lib/openstackep.js +0 -553
  68. package/routes/acr.js +0 -738
  69. package/routes/debugVerify.js +0 -263
  70. package/routes/extdata.js +0 -232
  71. package/routes/list.js +0 -270
  72. package/routes/policy.js +0 -869
  73. package/routes/resource.js +0 -1441
  74. package/routes/role.js +0 -2664
  75. package/routes/service.js +0 -894
  76. package/routes/tenant.js +0 -1095
  77. package/routes/userTokens.js +0 -511
  78. package/routes/userdata.js +0 -218
  79. package/routes/version.js +0 -108
  80. package/templ/Dockerfile.templ +0 -71
  81. package/tests/auto_acr.js +0 -1101
  82. package/tests/auto_acr_spec.js +0 -79
  83. package/tests/auto_all_spec.js +0 -142
  84. package/tests/auto_control_subprocess.sh +0 -243
  85. package/tests/auto_extdata.js +0 -220
  86. package/tests/auto_extdata_spec.js +0 -79
  87. package/tests/auto_init_config_json.sh +0 -275
  88. package/tests/auto_k2hdkc_server.ini +0 -109
  89. package/tests/auto_k2hdkc_slave.ini +0 -83
  90. package/tests/auto_list.js +0 -439
  91. package/tests/auto_list_spec.js +0 -79
  92. package/tests/auto_policy.js +0 -1579
  93. package/tests/auto_policy_spec.js +0 -79
  94. package/tests/auto_resource.js +0 -10956
  95. package/tests/auto_resource_spec.js +0 -79
  96. package/tests/auto_role.js +0 -6150
  97. package/tests/auto_role_spec.js +0 -79
  98. package/tests/auto_service.js +0 -770
  99. package/tests/auto_service_spec.js +0 -79
  100. package/tests/auto_subprocesses.js +0 -114
  101. package/tests/auto_template.sh +0 -126
  102. package/tests/auto_tenant.js +0 -1100
  103. package/tests/auto_tenant_spec.js +0 -79
  104. package/tests/auto_token_util.js +0 -219
  105. package/tests/auto_userdata.js +0 -292
  106. package/tests/auto_userdata_spec.js +0 -79
  107. package/tests/auto_usertokens.js +0 -565
  108. package/tests/auto_usertokens_spec.js +0 -79
  109. package/tests/auto_version.js +0 -127
  110. package/tests/auto_version_spec.js +0 -79
  111. package/tests/auto_watcher.js +0 -157
  112. package/tests/auto_watcher_spec.js +0 -79
  113. package/tests/k2hdkc_test.data +0 -986
  114. package/tests/k2hdkc_test_load.sh +0 -255
  115. package/tests/k2hr3template_test.js +0 -187
  116. package/tests/k2hr3template_test.sh +0 -339
  117. package/tests/k2hr3template_test_async.js +0 -216
  118. package/tests/k2hr3template_test_template.result +0 -7117
  119. package/tests/k2hr3template_test_template.txt +0 -3608
  120. package/tests/k2hr3template_test_vars.js +0 -194
  121. package/tests/manual_acr_delete.js +0 -143
  122. package/tests/manual_acr_get.js +0 -297
  123. package/tests/manual_acr_postput.js +0 -215
  124. package/tests/manual_allusertenant_get.js +0 -113
  125. package/tests/manual_extdata_get.js +0 -191
  126. package/tests/manual_k2hr3keys_get.js +0 -84
  127. package/tests/manual_list_gethead.js +0 -230
  128. package/tests/manual_policy_delete.js +0 -132
  129. package/tests/manual_policy_gethead.js +0 -275
  130. package/tests/manual_policy_postput.js +0 -297
  131. package/tests/manual_resource_delete.js +0 -433
  132. package/tests/manual_resource_gethead.js +0 -423
  133. package/tests/manual_resource_postput.js +0 -487
  134. package/tests/manual_role_delete.js +0 -404
  135. package/tests/manual_role_gethead.js +0 -547
  136. package/tests/manual_role_postput.js +0 -544
  137. package/tests/manual_service_delete.js +0 -153
  138. package/tests/manual_service_gethead.js +0 -178
  139. package/tests/manual_service_postput.js +0 -348
  140. package/tests/manual_tenant_delete.js +0 -186
  141. package/tests/manual_tenant_gethead.js +0 -268
  142. package/tests/manual_tenant_postput.js +0 -293
  143. package/tests/manual_test.sh +0 -352
  144. package/tests/manual_userdata_get.js +0 -173
  145. package/tests/manual_usertoken_gethead.js +0 -136
  146. package/tests/manual_usertoken_postput.js +0 -310
  147. package/tests/manual_version_get.js +0 -127
  148. package/tests/run_local_test_k2hdkc.sh +0 -174
  149. package/tests/test.sh +0 -333
@@ -0,0 +1,532 @@
1
+ "use strict";
2
+ /*
3
+ * K2HR3 REST API
4
+ *
5
+ * Copyright 2017 Yahoo Japan Corporation.
6
+ *
7
+ * K2HR3 is K2hdkc based Resource and Roles and policy Rules, gathers
8
+ * common management information for the cloud.
9
+ * K2HR3 can dynamically manage information as "who", "what", "operate".
10
+ * These are stored as roles, resources, policies in K2hdkc, and the
11
+ * client system can dynamically read and modify these information.
12
+ *
13
+ * For the full copyright and license information, please view
14
+ * the license file that was distributed with this source code.
15
+ *
16
+ * AUTHOR: Takeshi Nakatani
17
+ * CREATE: Fri Oct 27 2017
18
+ * REVISION:
19
+ *
20
+ */
21
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
22
+ if (k2 === undefined) k2 = k;
23
+ var desc = Object.getOwnPropertyDescriptor(m, k);
24
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
25
+ desc = { enumerable: true, get: function() { return m[k]; } };
26
+ }
27
+ Object.defineProperty(o, k2, desc);
28
+ }) : (function(o, m, k, k2) {
29
+ if (k2 === undefined) k2 = k;
30
+ o[k2] = m[k];
31
+ }));
32
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
33
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
34
+ }) : function(o, v) {
35
+ o["default"] = v;
36
+ });
37
+ var __importStar = (this && this.__importStar) || (function () {
38
+ var ownKeys = function(o) {
39
+ ownKeys = Object.getOwnPropertyNames || function (o) {
40
+ var ar = [];
41
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
42
+ return ar;
43
+ };
44
+ return ownKeys(o);
45
+ };
46
+ return function (mod) {
47
+ if (mod && mod.__esModule) return mod;
48
+ var result = {};
49
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
50
+ __setModuleDefault(result, mod);
51
+ return result;
52
+ };
53
+ })();
54
+ var __importDefault = (this && this.__importDefault) || function (mod) {
55
+ return (mod && mod.__esModule) ? mod : { "default": mod };
56
+ };
57
+ Object.defineProperty(exports, "__esModule", { value: true });
58
+ exports.k2hr3acrutil = void 0;
59
+ const https = __importStar(require("https"));
60
+ const http = __importStar(require("http"));
61
+ const k2hr3tokens_1 = __importDefault(require("./k2hr3tokens"));
62
+ const k2hr3apiutil_1 = __importDefault(require("./k2hr3apiutil"));
63
+ const dbglogging_1 = __importDefault(require("./dbglogging"));
64
+ const cacerts_1 = require("./cacerts");
65
+ //
66
+ // Type chekcer
67
+ //
68
+ const rawIsResTypeACRVerifyResponseKeys = (val) => {
69
+ if (!k2hr3apiutil_1.default.isPlainObject(val)) {
70
+ return false;
71
+ }
72
+ for (const [, value] of Object.entries(val)) {
73
+ if (null !== value && !k2hr3apiutil_1.default.isString(value)) {
74
+ return false;
75
+ }
76
+ }
77
+ return true;
78
+ };
79
+ const rawIsDkcTypeACRVerifyOneResponseBodyData = (val) => {
80
+ if (!k2hr3apiutil_1.default.isPlainObject(val)) {
81
+ return false;
82
+ }
83
+ const _obj = val;
84
+ const _isName = (key) => k2hr3apiutil_1.default.isString(_obj[key]);
85
+ const _isExpire = (key) => 'undefined' === typeof _obj[key] || k2hr3apiutil_1.default.isSafeNumber(_obj[key]);
86
+ const _isType = (key) => null === _obj[key] || k2hr3apiutil_1.default.isString(_obj[key]);
87
+ const _isData = (key) => 'undefined' === typeof _obj[key] || k2hr3apiutil_1.default.isValTypeAll(_obj[key]);
88
+ const _isKeys = (key) => 'undefined' === typeof _obj[key] || rawIsResTypeACRVerifyResponseKeys(_obj[key]);
89
+ return (_isName('name') &&
90
+ _isExpire('expire') &&
91
+ _isType('data') &&
92
+ _isData('type') &&
93
+ _isKeys('keys'));
94
+ };
95
+ //---------------------------------------------------------
96
+ // Common send verify request
97
+ //---------------------------------------------------------
98
+ //
99
+ // Send and Get response from verify URL
100
+ //
101
+ // Inputs
102
+ // scopedtoken : scoped user token
103
+ // urlobj : url object built by apiutil.parseUrl()
104
+ // callback(error, response) : callback function received response data
105
+ //
106
+ // Response : following formatted array
107
+ //
108
+ // Ex.) Response format from verify url
109
+ //
110
+ // response body = [ : undefined/null or resource array(if one element, allows only it not array)
111
+ // dkcTypeACRVerifyOneResponseBodyData = {
112
+ // name : resource name which is key name(path) for resource
113
+ // expire : undefined/null or integer
114
+ // type : resource data type(string or object), if date is null or '', this value must be string.
115
+ // data : resource data which must be string or object or null/undefined.
116
+ // keys = { : resource has keys(associative array), or null/undefined.
117
+ // 'foo': bar, : any value is allowed
118
+ // ... :
119
+ // } :
120
+ // },
121
+ // ...
122
+ // ]
123
+ //
124
+ const rawACRSendVerifyEx = (scopedtoken, urlobj, callback) => {
125
+ if (!k2hr3apiutil_1.default.isFunction(callback)) {
126
+ dbglogging_1.default.elog('callback parameter is wrong : callback=' + JSON.stringify(callback));
127
+ return;
128
+ }
129
+ const _callback = callback;
130
+ if (!k2hr3apiutil_1.default.isSafeString(scopedtoken) || !k2hr3apiutil_1.default.isSafeEntity(urlobj)) {
131
+ const error = new Error('some parameters are wrong : scopedtoken=' + JSON.stringify(scopedtoken) + ', urlobj=' + JSON.stringify(urlobj));
132
+ dbglogging_1.default.elog(error.message);
133
+ _callback(error, null);
134
+ return;
135
+ }
136
+ const headers = {
137
+ 'Content-Type': 'application/json',
138
+ 'Content-Length': 0,
139
+ 'X-Auth-Token': scopedtoken ?? undefined // [NOTE] this token string does not have 'U=' prefix.
140
+ };
141
+ const caCert = (urlobj && urlobj.https) ? (0, cacerts_1.ca)() : null;
142
+ const options = {
143
+ 'host': k2hr3apiutil_1.default.getSafeString(urlobj.host),
144
+ 'port': urlobj.port,
145
+ 'path': k2hr3apiutil_1.default.getSafeString(urlobj.path),
146
+ 'method': 'GET',
147
+ 'headers': headers,
148
+ 'ca': caCert ?? undefined
149
+ };
150
+ const agent = (urlobj && urlobj.https) ? https : http;
151
+ dbglogging_1.default.dlog('request options = ' + JSON.stringify({ ...options, ca: options.ca ? '[Buffer]' : undefined }));
152
+ dbglogging_1.default.dlog('request headers = ' + JSON.stringify(headers));
153
+ // send request
154
+ const req = agent.request(options, (res) => {
155
+ let _body = '';
156
+ const _status = res.statusCode;
157
+ const _headers = res.headers;
158
+ dbglogging_1.default.dlog('verify response status: ' + _status);
159
+ dbglogging_1.default.dlog('verify response header: ' + JSON.stringify(_headers));
160
+ res.setEncoding('utf8');
161
+ res.on('data', (chunk) => {
162
+ //r3logger.dlog('verify response chunk: ' + chunk);
163
+ _body += chunk;
164
+ });
165
+ res.on('end', () => {
166
+ let _error = null;
167
+ let resobj = [];
168
+ if (300 <= (_status ?? 500)) {
169
+ _error = new Error('got error response for verify request by status=' + String(_status ?? 0));
170
+ dbglogging_1.default.elog(_error.message);
171
+ _callback(_error, null);
172
+ return;
173
+ }
174
+ //r3logger.dlog('response body: ' + _body);
175
+ // check response body
176
+ if (!k2hr3apiutil_1.default.isSafeString(_body)) {
177
+ _error = new Error('verify resource is not json string : response(json) =' + JSON.stringify(_body));
178
+ }
179
+ else {
180
+ // check response string(json)
181
+ if (k2hr3apiutil_1.default.checkSimpleJSON(_body)) {
182
+ const tmp_resobj = JSON.parse(_body);
183
+ if (k2hr3apiutil_1.default.isArray(tmp_resobj)) {
184
+ if (tmp_resobj.every((element) => rawIsDkcTypeACRVerifyOneResponseBodyData(element))) {
185
+ resobj = tmp_resobj;
186
+ }
187
+ }
188
+ }
189
+ else if (k2hr3apiutil_1.default.isArray(_body)) {
190
+ if (_body.every((element) => rawIsDkcTypeACRVerifyOneResponseBodyData(element))) {
191
+ resobj = _body;
192
+ }
193
+ }
194
+ else if (rawIsDkcTypeACRVerifyOneResponseBodyData(_body)) { // This format is not officially permitted (maybe it should be removed)
195
+ resobj = [_body];
196
+ }
197
+ if (!k2hr3apiutil_1.default.isArray(resobj)) {
198
+ _error = new Error('verify resource is not array : response(object) =' + JSON.stringify(resobj));
199
+ }
200
+ else {
201
+ // check each object in array
202
+ const namemap = {};
203
+ for (let cnt = 0; cnt < resobj.length; ++cnt) {
204
+ // check name field
205
+ if (!k2hr3apiutil_1.default.isSafeString(resobj[cnt].name)) {
206
+ _error = new Error('empty resource name in verify response is found : response pos=' + cnt);
207
+ break;
208
+ }
209
+ if (k2hr3apiutil_1.default.isSafeEntity(namemap[resobj[cnt].name])) {
210
+ _error = new Error('same resource name in verify response is found : response pos=' + cnt + ', name=' + JSON.stringify(resobj[cnt].name));
211
+ break;
212
+ }
213
+ namemap[resobj[cnt].name] = true; // set any value.
214
+ // check expire field
215
+ if (!k2hr3apiutil_1.default.isSafeEntity(resobj[cnt].expire)) {
216
+ resobj[cnt].expire = -1; // set no expire
217
+ }
218
+ else if (!k2hr3apiutil_1.default.isSafeNumber(resobj[cnt].expire)) {
219
+ _error = new Error('wrong expire value in verify response is found : response pos=' + cnt + ', expire=' + JSON.stringify(resobj[cnt].expire));
220
+ break;
221
+ }
222
+ }
223
+ }
224
+ }
225
+ if (null !== _error) {
226
+ dbglogging_1.default.elog(_error.message);
227
+ _callback(_error, null);
228
+ return;
229
+ }
230
+ // return response
231
+ _callback(null, resobj);
232
+ return;
233
+ });
234
+ });
235
+ req.on('error', (exception) => {
236
+ dbglogging_1.default.elog('problem with verify request: ' + exception.message);
237
+ _callback(exception, null);
238
+ return;
239
+ });
240
+ req.end();
241
+ };
242
+ //---------------------------------------------------------
243
+ // Send verify request
244
+ //---------------------------------------------------------
245
+ //
246
+ // Send and Get response from verify URL
247
+ //
248
+ // Inputs
249
+ // user : user name
250
+ // passwd : pass phrase for user
251
+ // tenant : tenant name for scoped
252
+ // verifyurl : verify url(allow not url string, it means static value formatted JSON)
253
+ // callback(error, response) : callback function received response data
254
+ //
255
+ // Response : see rawACRSendVerifyEx()
256
+ //
257
+ const rawACRSendVerifyByUser = (user, passwd, tenant, verifyurl, callback) => {
258
+ if (!k2hr3apiutil_1.default.isFunction(callback)) {
259
+ dbglogging_1.default.elog('callback parameter is wrong : callback=' + JSON.stringify(callback));
260
+ return;
261
+ }
262
+ const _callback = callback;
263
+ if (!k2hr3apiutil_1.default.isSafeString(verifyurl)) {
264
+ const error = new Error('parameter is wrong : verifyurl=' + JSON.stringify(verifyurl));
265
+ dbglogging_1.default.elog(error.message);
266
+ _callback(error, null);
267
+ return;
268
+ }
269
+ if (k2hr3apiutil_1.default.checkSimpleJSON(verifyurl)) {
270
+ // verifyurl is formatted by JSON, it means static value.
271
+ _callback(null, JSON.parse(verifyurl));
272
+ return;
273
+ }
274
+ const urlobj = k2hr3apiutil_1.default.parseUrl(verifyurl); // parse verify url string to url obejct.
275
+ if (null === urlobj) {
276
+ const error = new Error('parameter is wrong : verifyurl=' + JSON.stringify(verifyurl));
277
+ dbglogging_1.default.elog(error.message);
278
+ _callback(error, null);
279
+ return;
280
+ }
281
+ if (!k2hr3apiutil_1.default.isSafeString(user) || !k2hr3apiutil_1.default.isSafeString(tenant)) {
282
+ const error = new Error('some parameters are wrong : user=' + JSON.stringify(user) + ', tenant=' + JSON.stringify(tenant));
283
+ dbglogging_1.default.elog(error.message);
284
+ _callback(error, null);
285
+ return;
286
+ }
287
+ const _user = user;
288
+ const _tenant = tenant;
289
+ const _passwd = passwd;
290
+ // get scoped token directly.
291
+ k2hr3tokens_1.default.getUserToken(_user, _passwd, _tenant, (err, token) => {
292
+ if (null !== err) {
293
+ const error = new Error('could not get scoped user token for user=' + _user + ', tenant=' + _tenant + ' by ' + k2hr3apiutil_1.default.getSafeString(err.message));
294
+ dbglogging_1.default.elog(error.message);
295
+ _callback(error, null);
296
+ }
297
+ dbglogging_1.default.dlog('get user token jsonres = ' + JSON.stringify(token));
298
+ // call verify url
299
+ rawACRSendVerifyEx(token, urlobj, _callback);
300
+ });
301
+ };
302
+ //---------------------------------------------------------
303
+ // Send verify request
304
+ //---------------------------------------------------------
305
+ //
306
+ // Send and Get response from verify URL
307
+ //
308
+ // Inputs
309
+ // unscopedtoken : unscoped user token
310
+ // user : user name
311
+ // tenant : tenant name for scoped
312
+ // verifyurl : verify url(allow not url string, it means static value formatted JSON)
313
+ // callback(error, response) : callback function received response data
314
+ //
315
+ // Response : see rawACRSendVerifyEx()
316
+ //
317
+ const rawACRSendVerifyByUnscopedToken = (unscopedtoken, user, tenant, verifyurl, callback) => {
318
+ if (!k2hr3apiutil_1.default.isFunction(callback)) {
319
+ dbglogging_1.default.elog('callback parameter is wrong : callback=' + JSON.stringify(callback));
320
+ return;
321
+ }
322
+ const _callback = callback;
323
+ if (!k2hr3apiutil_1.default.isSafeString(verifyurl)) {
324
+ const error = new Error('parameter is wrong : verifyurl=' + JSON.stringify(verifyurl));
325
+ dbglogging_1.default.elog(error.message);
326
+ _callback(error, null);
327
+ return;
328
+ }
329
+ if (k2hr3apiutil_1.default.checkSimpleJSON(verifyurl)) {
330
+ // verifyurl is formatted by JSON, it means static value.
331
+ _callback(null, JSON.parse(verifyurl));
332
+ return;
333
+ }
334
+ const urlobj = k2hr3apiutil_1.default.parseUrl(verifyurl);
335
+ if (null === urlobj) {
336
+ const error = new Error('parameter is wrong : verifyurl=' + JSON.stringify(verifyurl));
337
+ dbglogging_1.default.elog(error.message);
338
+ _callback(error, null);
339
+ return;
340
+ }
341
+ if (!k2hr3apiutil_1.default.isSafeString(unscopedtoken) || !k2hr3apiutil_1.default.isSafeString(user) || !k2hr3apiutil_1.default.isSafeString(tenant)) {
342
+ const error = new Error('some parameters are wrong : unscopedtoken=' + JSON.stringify(unscopedtoken) + ', user=' + JSON.stringify(user) + ', tenant=' + JSON.stringify(tenant));
343
+ dbglogging_1.default.elog(error.message);
344
+ _callback(error, null);
345
+ return;
346
+ }
347
+ const _unscopedtoken = unscopedtoken;
348
+ const _user = user;
349
+ const _tenant = tenant;
350
+ // get scoped token from unscoped token
351
+ k2hr3tokens_1.default.getScopedUserToken(_unscopedtoken, _user, _tenant, (err, token) => {
352
+ if (null !== err) {
353
+ const error = new Error('could not get scoped user token for user=' + _user + ', tenant=' + _tenant + ' by ' + k2hr3apiutil_1.default.getSafeString(err.message));
354
+ dbglogging_1.default.elog(error.message);
355
+ _callback(error, null);
356
+ }
357
+ dbglogging_1.default.dlog('get user token jsonres = ' + JSON.stringify(token));
358
+ // call verify url
359
+ rawACRSendVerifyEx(token, urlobj, _callback);
360
+ });
361
+ };
362
+ //---------------------------------------------------------
363
+ // Send verify request
364
+ //---------------------------------------------------------
365
+ //
366
+ // Send and Get response from verify URL
367
+ //
368
+ // Inputs
369
+ // scopedtoken : scoped user token
370
+ // verifyurl : verify url(allow not url string, it means static value formatted JSON)
371
+ // callback(error, response) : callback function received response data
372
+ //
373
+ // Response : see rawACRSendVerifyEx()
374
+ //
375
+ const rawACRSendVerifyByScopedToken = (scopedtoken, verifyurl, callback) => {
376
+ if (!k2hr3apiutil_1.default.isFunction(callback)) {
377
+ dbglogging_1.default.elog('callback parameter is wrong : callback=' + JSON.stringify(callback));
378
+ return;
379
+ }
380
+ const _callback = callback;
381
+ if (!k2hr3apiutil_1.default.isSafeString(verifyurl)) {
382
+ const error = new Error('parameter is wrong : verifyurl=' + JSON.stringify(verifyurl));
383
+ dbglogging_1.default.elog(error.message);
384
+ _callback(error, null);
385
+ return;
386
+ }
387
+ if (k2hr3apiutil_1.default.checkSimpleJSON(verifyurl)) {
388
+ // verifyurl is formatted by JSON, it means static value.
389
+ _callback(null, JSON.parse(verifyurl));
390
+ return;
391
+ }
392
+ const urlobj = k2hr3apiutil_1.default.parseUrl(verifyurl);
393
+ if (null === urlobj) {
394
+ const error = new Error('parameter is wrong : verifyurl=' + JSON.stringify(verifyurl));
395
+ dbglogging_1.default.elog(error.message);
396
+ _callback(error, null);
397
+ return;
398
+ }
399
+ if (!k2hr3apiutil_1.default.isSafeString(scopedtoken)) {
400
+ const error = new Error('parameter is wrong : scopedtoken=' + JSON.stringify(scopedtoken));
401
+ dbglogging_1.default.elog(error.message);
402
+ _callback(error, null);
403
+ return;
404
+ }
405
+ const _scopedtoken = scopedtoken;
406
+ // parse token to get user/tenant
407
+ const _token_res = k2hr3tokens_1.default.checkUserToken(_scopedtoken);
408
+ if (null === _token_res ||
409
+ !k2hr3apiutil_1.default.isSafeString(_token_res.user) ||
410
+ !k2hr3apiutil_1.default.isSafeString(_token_res.tenant) ||
411
+ !k2hr3apiutil_1.default.isSafeEntity(_token_res.scoped) ||
412
+ !k2hr3apiutil_1.default.isBoolean(_token_res.scoped) ||
413
+ true !== _token_res.scoped) {
414
+ const error = new Error('parameter scopedtoken(' + _scopedtoken + ' -> ' + JSON.stringify(_token_res) + ') is something wrong,');
415
+ dbglogging_1.default.elog(error.message);
416
+ _callback(error, null);
417
+ return;
418
+ }
419
+ // get scoped token from unscoped token
420
+ //
421
+ // [NOTE]
422
+ // getScopedUserToken is allowed scoped token when is has as same as tenant name specified.
423
+ //
424
+ k2hr3tokens_1.default.getScopedUserToken(_scopedtoken, _token_res.user, _token_res.tenant, (err, token) => {
425
+ if (null !== err) {
426
+ const error = new Error('could not get scoped user token for user=' + _token_res.user + ', tenant=' + _token_res.tenant + ' by ' + k2hr3apiutil_1.default.getSafeString(err.message));
427
+ dbglogging_1.default.elog(error.message);
428
+ _callback(error, null);
429
+ }
430
+ dbglogging_1.default.dlog('get user token jsonres = ' + JSON.stringify(token));
431
+ // call verify url
432
+ rawACRSendVerifyEx(token, urlobj, _callback);
433
+ });
434
+ };
435
+ //---------------------------------------------------------
436
+ // Send verify request for all pattern
437
+ //---------------------------------------------------------
438
+ //
439
+ // Send and Get response from verify URL
440
+ //
441
+ // Inputs
442
+ // unscopedtoken : unscoped user token
443
+ // user : user name
444
+ // passwd : pass phrase for user
445
+ // token : (un)scoped user token
446
+ // tenant : tenant name for scoped
447
+ // verifyurl : verify url(allow not url string, it means static value formatted JSON)
448
+ // callback(error, response) : callback function received response data
449
+ //
450
+ // Response : see rawACRSendVerifyEx()
451
+ //
452
+ // [NOTE]
453
+ // This function automatically decides raw functions by parameters.
454
+ //
455
+ // 1) no token
456
+ // Both user and passwd parameters must be specified, and token must be null(undefined).
457
+ // 2) unscoped token
458
+ // Both user and token parameters must be specified, and passwd must be null(undefined).
459
+ // The token parameter should be "unscoped" user token.
460
+ // 3) scoped token
461
+ // token parameter must be specified, and both user and passwd must be null(undefined).
462
+ // The token parameter should be "scoped" user token.
463
+ //
464
+ const rawACRSendVerify = (token, user, passwd, tenant, verifyurl, callback) => {
465
+ if (!k2hr3apiutil_1.default.isFunction(callback)) {
466
+ dbglogging_1.default.elog('callback parameter is wrong : callback=' + JSON.stringify(callback));
467
+ return;
468
+ }
469
+ const _callback = callback;
470
+ let error = null;
471
+ // check pattern
472
+ if (k2hr3apiutil_1.default.isSafeString(user)) {
473
+ if (k2hr3apiutil_1.default.isSafeString(token)) {
474
+ if (k2hr3apiutil_1.default.isSafeEntity(passwd)) {
475
+ error = new Error('all parameters user(' + JSON.stringify(user) + ') and token(' + JSON.stringify(token) + ') and passwd(xxxxx) are specified.');
476
+ }
477
+ else {
478
+ // case : user + unscoped token
479
+ rawACRSendVerifyByUser(user, passwd, tenant, verifyurl, _callback);
480
+ }
481
+ }
482
+ else {
483
+ token = null;
484
+ if (k2hr3apiutil_1.default.isSafeEntity(passwd)) {
485
+ // case : user + passwd
486
+ }
487
+ else {
488
+ // case : user + passwd(null)
489
+ //
490
+ // [NOTE]
491
+ // This case is not error pattern, because empty passwd is allowed by some authorizing system.
492
+ }
493
+ rawACRSendVerifyByUnscopedToken(token, user, tenant, verifyurl, _callback);
494
+ }
495
+ }
496
+ else {
497
+ if (k2hr3apiutil_1.default.isSafeString(token)) {
498
+ if (k2hr3apiutil_1.default.isSafeEntity(passwd)) {
499
+ error = new Error('user parameter is empty, but passwd(xxxx) is specified.');
500
+ }
501
+ else {
502
+ // case : scoped token
503
+ rawACRSendVerifyByScopedToken(token, verifyurl, _callback);
504
+ }
505
+ }
506
+ else {
507
+ error = new Error('user parameter is empty, but scoped token is not specified.');
508
+ }
509
+ }
510
+ if (null !== error) {
511
+ dbglogging_1.default.elog(error.message);
512
+ _callback(error, null);
513
+ }
514
+ };
515
+ //---------------------------------------------------------
516
+ // Export types
517
+ //---------------------------------------------------------
518
+ exports.k2hr3acrutil = {
519
+ getACRVerifyByUser: rawACRSendVerifyByUser,
520
+ getACRVerifyByUnscopedToken: rawACRSendVerifyByUnscopedToken,
521
+ getACRVerifyByScopedToken: rawACRSendVerifyByScopedToken,
522
+ getACRSendVerify: rawACRSendVerify
523
+ };
524
+ exports.default = exports.k2hr3acrutil;
525
+ /*
526
+ * Local variables:
527
+ * tab-width: 4
528
+ * c-basic-offset: 4
529
+ * End:
530
+ * vim600: noexpandtab sw=4 ts=4 fdm=marker
531
+ * vim<600: noexpandtab sw=4 ts=4
532
+ */