@gzhangx/googleapi 0.0.76 → 0.0.78
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.
- package/lib/google/drive.d.ts +15 -15
- package/lib/google/drive.js +54 -45
- package/lib/google/drive.js.map +1 -1
- package/lib/google/googleApiServiceAccount.d.ts +129 -132
- package/lib/google/googleApiServiceAccount.js +411 -551
- package/lib/google/googleApiServiceAccount.js.map +1 -1
- package/lib/googleApi.d.ts +58 -58
- package/lib/googleApi.js +325 -459
- package/lib/googleApi.js.map +1 -1
- package/lib/httpRequest.d.ts +24 -25
- package/lib/httpRequest.js +138 -184
- package/lib/httpRequest.js.map +1 -1
- package/lib/index.d.ts +24 -24
- package/lib/index.js +62 -55
- package/lib/index.js.map +1 -1
- package/lib/jwt.d.ts +11 -11
- package/lib/jwt.js +45 -46
- package/lib/jwt.js.map +1 -1
- package/lib/msGraph/msExcell.d.ts +47 -47
- package/lib/msGraph/msExcell.js +69 -157
- package/lib/msGraph/msExcell.js.map +1 -1
- package/lib/msGraph/msauth.d.ts +91 -91
- package/lib/msGraph/msauth.js +309 -533
- package/lib/msGraph/msauth.js.map +1 -1
- package/lib/msGraph/msdir.d.ts +124 -125
- package/lib/msGraph/msdir.js +171 -263
- package/lib/msGraph/msdir.js.map +1 -1
- package/lib/msGraph/types.d.ts +4 -4
- package/lib/msGraph/types.js +2 -2
- package/lib/tests/gtest.d.ts +1 -1
- package/lib/tests/gtest.js +170 -234
- package/lib/tests/gtest.js.map +1 -1
- package/lib/tests/mstest.d.ts +1 -1
- package/lib/tests/mstest.js +184 -269
- package/lib/tests/mstest.js.map +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/util.d.ts +9 -9
- package/lib/util.js +95 -96
- package/lib/util.js.map +1 -1
- package/package.json +3 -3
package/lib/msGraph/msauth.js
CHANGED
|
@@ -1,534 +1,310 @@
|
|
|
1
|
-
"use strict";
|
|
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
|
-
function
|
|
248
|
-
return
|
|
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
|
-
function
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
return
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
return
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
switch (_b.label) {
|
|
311
|
-
case 0:
|
|
312
|
-
now = Math.round(new Date().getTime() / 1000);
|
|
313
|
-
cacheKey = "".concat(opt.tenantId, "-").concat(opt.client_id);
|
|
314
|
-
if (!!connCaccheInfo.cache) return [3 /*break*/, 3];
|
|
315
|
-
if (!opt.loadTokenCache) return [3 /*break*/, 2];
|
|
316
|
-
_a = connCaccheInfo;
|
|
317
|
-
return [4 /*yield*/, opt.loadTokenCache()];
|
|
318
|
-
case 1:
|
|
319
|
-
_a.cache = _b.sent();
|
|
320
|
-
return [3 /*break*/, 3];
|
|
321
|
-
case 2:
|
|
322
|
-
connCaccheInfo.cache = {};
|
|
323
|
-
_b.label = 3;
|
|
324
|
-
case 3:
|
|
325
|
-
optTokenInfo = connCaccheInfo.cache[cacheKey];
|
|
326
|
-
if (opt.debugLogger)
|
|
327
|
-
opt.debugLogger("debugrm getMsGraphConn now=".concat(now, " exp=").concat(optTokenInfo === null || optTokenInfo === void 0 ? void 0 : optTokenInfo.expires_on));
|
|
328
|
-
if (!(!optTokenInfo || optTokenInfo.expires_on < now)) return [3 /*break*/, 7];
|
|
329
|
-
getAccessToken = getAuth(opt).getAccessToken;
|
|
330
|
-
opt.logger('getting new token');
|
|
331
|
-
return [4 /*yield*/, getAccessToken()];
|
|
332
|
-
case 4:
|
|
333
|
-
tok = _b.sent();
|
|
334
|
-
console.log('------------------------>', tok);
|
|
335
|
-
retToken = {
|
|
336
|
-
access_token: tok.access_token,
|
|
337
|
-
expires_on: tok.expires_in + now,
|
|
338
|
-
};
|
|
339
|
-
connCaccheInfo.cache[cacheKey] = retToken;
|
|
340
|
-
if (!opt.saveTokenCache) return [3 /*break*/, 6];
|
|
341
|
-
opt.logger('saving token cache');
|
|
342
|
-
return [4 /*yield*/, opt.saveTokenCache(connCaccheInfo.cache)];
|
|
343
|
-
case 5:
|
|
344
|
-
_b.sent();
|
|
345
|
-
_b.label = 6;
|
|
346
|
-
case 6: return [2 /*return*/, retToken];
|
|
347
|
-
case 7: return [2 /*return*/, optTokenInfo];
|
|
348
|
-
}
|
|
349
|
-
});
|
|
350
|
-
});
|
|
351
|
-
}
|
|
352
|
-
function getHeaders() {
|
|
353
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
354
|
-
var tok;
|
|
355
|
-
return __generator(this, function (_a) {
|
|
356
|
-
switch (_a.label) {
|
|
357
|
-
case 0: return [4 /*yield*/, getToken()];
|
|
358
|
-
case 1:
|
|
359
|
-
tok = _a.sent();
|
|
360
|
-
return [2 /*return*/, {
|
|
361
|
-
headers: {
|
|
362
|
-
"Authorization": "Bearer ".concat(tok.access_token)
|
|
363
|
-
},
|
|
364
|
-
//maxContentLength: Infinity,
|
|
365
|
-
//maxBodyLength: Infinity,
|
|
366
|
-
}];
|
|
367
|
-
}
|
|
368
|
-
});
|
|
369
|
-
});
|
|
370
|
-
}
|
|
371
|
-
function parseResp(opts, r) {
|
|
372
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
373
|
-
return __generator(this, function (_a) {
|
|
374
|
-
//if (r.statusCode === 302) {
|
|
375
|
-
// if (r.headers.location) {
|
|
376
|
-
// return await doHttpGet(r.headers.location, opts).then(async r => await parseResp(opts, r)).catch(errProc(new GGraphError(r.headers.location)));
|
|
377
|
-
// }
|
|
378
|
-
//}
|
|
379
|
-
return [2 /*return*/, r.data];
|
|
380
|
-
});
|
|
381
|
-
});
|
|
382
|
-
}
|
|
383
|
-
function errProc(errTrace) {
|
|
384
|
-
return function (err) {
|
|
385
|
-
var message = axiosErrorProcessing(err);
|
|
386
|
-
opt.logger("error on ".concat(errTrace.requestUrl, ": ").concat(message));
|
|
387
|
-
errTrace.message = err.message;
|
|
388
|
-
throw errTrace;
|
|
389
|
-
};
|
|
390
|
-
}
|
|
391
|
-
function doGet(urlPostFix, fmt) {
|
|
392
|
-
if (fmt === void 0) { fmt = function (x) { return x; }; }
|
|
393
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
394
|
-
var uri, headers, opts;
|
|
395
|
-
var _this = this;
|
|
396
|
-
return __generator(this, function (_a) {
|
|
397
|
-
switch (_a.label) {
|
|
398
|
-
case 0:
|
|
399
|
-
uri = getMsGraphBaseUrl(urlPostFix);
|
|
400
|
-
opt.logger("GET ".concat(uri));
|
|
401
|
-
return [4 /*yield*/, getHeaders()];
|
|
402
|
-
case 1:
|
|
403
|
-
headers = _a.sent();
|
|
404
|
-
opts = fmt(__assign(__assign({}, headers), { followRedirect: true }));
|
|
405
|
-
return [4 /*yield*/, doHttpGet(uri, opts)
|
|
406
|
-
.then(function (r) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
407
|
-
switch (_a.label) {
|
|
408
|
-
case 0: return [4 /*yield*/, parseResp(opts, r)];
|
|
409
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
410
|
-
}
|
|
411
|
-
}); }); }).catch(errProc(new GGraphError(uri)))];
|
|
412
|
-
case 2: return [2 /*return*/, _a.sent()];
|
|
413
|
-
}
|
|
414
|
-
});
|
|
415
|
-
});
|
|
416
|
-
}
|
|
417
|
-
function doPost(urlPostFix, data) {
|
|
418
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
419
|
-
var uri, opts;
|
|
420
|
-
var _this = this;
|
|
421
|
-
return __generator(this, function (_a) {
|
|
422
|
-
switch (_a.label) {
|
|
423
|
-
case 0:
|
|
424
|
-
uri = getMsGraphBaseUrl(urlPostFix);
|
|
425
|
-
opt.logger("POST ".concat(uri));
|
|
426
|
-
return [4 /*yield*/, getHeaders()];
|
|
427
|
-
case 1:
|
|
428
|
-
opts = _a.sent();
|
|
429
|
-
return [2 /*return*/, doHttpPost(uri, data, opts).then(function (r) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
430
|
-
switch (_a.label) {
|
|
431
|
-
case 0: return [4 /*yield*/, parseResp(opts, r)];
|
|
432
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
433
|
-
}
|
|
434
|
-
}); }); }).catch(errProc(new GGraphError(uri)))];
|
|
435
|
-
}
|
|
436
|
-
});
|
|
437
|
-
});
|
|
438
|
-
}
|
|
439
|
-
function doPut(urlPostFix, data) {
|
|
440
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
441
|
-
var uri, opts;
|
|
442
|
-
var _this = this;
|
|
443
|
-
return __generator(this, function (_a) {
|
|
444
|
-
switch (_a.label) {
|
|
445
|
-
case 0:
|
|
446
|
-
uri = getMsGraphBaseUrl(urlPostFix);
|
|
447
|
-
opt.logger("PUT ".concat(uri));
|
|
448
|
-
return [4 /*yield*/, getHeaders()];
|
|
449
|
-
case 1:
|
|
450
|
-
opts = _a.sent();
|
|
451
|
-
return [2 /*return*/, doHttpPost(uri, data, opts).then(function (r) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
452
|
-
switch (_a.label) {
|
|
453
|
-
case 0: return [4 /*yield*/, parseResp(opts, r)];
|
|
454
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
455
|
-
}
|
|
456
|
-
}); }); }).catch(errProc(new GGraphError(uri)))];
|
|
457
|
-
}
|
|
458
|
-
});
|
|
459
|
-
});
|
|
460
|
-
}
|
|
461
|
-
function doPatch(urlPostFix, data) {
|
|
462
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
463
|
-
var uri, opts;
|
|
464
|
-
var _this = this;
|
|
465
|
-
return __generator(this, function (_a) {
|
|
466
|
-
switch (_a.label) {
|
|
467
|
-
case 0:
|
|
468
|
-
uri = getMsGraphBaseUrl(urlPostFix);
|
|
469
|
-
opt.logger("PATCH ".concat(uri));
|
|
470
|
-
return [4 /*yield*/, getHeaders()];
|
|
471
|
-
case 1:
|
|
472
|
-
opts = _a.sent();
|
|
473
|
-
return [2 /*return*/, doHttpPatch(uri, data, opts).then(function (r) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
474
|
-
switch (_a.label) {
|
|
475
|
-
case 0: return [4 /*yield*/, parseResp(opts, r)];
|
|
476
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
477
|
-
}
|
|
478
|
-
}); }); }).catch(errProc(new GGraphError(uri)))];
|
|
479
|
-
}
|
|
480
|
-
});
|
|
481
|
-
});
|
|
482
|
-
}
|
|
483
|
-
function doDelete(urlPostFix) {
|
|
484
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
485
|
-
var uri, opts;
|
|
486
|
-
var _this = this;
|
|
487
|
-
return __generator(this, function (_a) {
|
|
488
|
-
switch (_a.label) {
|
|
489
|
-
case 0:
|
|
490
|
-
uri = getMsGraphBaseUrl(urlPostFix);
|
|
491
|
-
opt.logger("PATCH ".concat(uri));
|
|
492
|
-
return [4 /*yield*/, getHeaders()];
|
|
493
|
-
case 1:
|
|
494
|
-
opts = _a.sent();
|
|
495
|
-
return [2 /*return*/, doHttpDelete(uri, opts).then(function (r) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
496
|
-
switch (_a.label) {
|
|
497
|
-
case 0: return [4 /*yield*/, parseResp(opts, r)];
|
|
498
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
499
|
-
}
|
|
500
|
-
}); }); }).catch(errProc(new GGraphError(uri)))];
|
|
501
|
-
}
|
|
502
|
-
});
|
|
503
|
-
});
|
|
504
|
-
}
|
|
505
|
-
//const getMsGraphBaseUrl = (urlPostFix: string) => `${ROOT_URL}/users('${opt.tenantClientInfo.userId}')/${urlPostFix}`;
|
|
506
|
-
function getSharedItemInfo(sharedUrl) {
|
|
507
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
508
|
-
return __generator(this, function (_a) {
|
|
509
|
-
return [2 /*return*/, doGet("shares/".concat(encodeSharedUrl(sharedUrl), "/driveItem")).then(function (r) {
|
|
510
|
-
return r;
|
|
511
|
-
})];
|
|
512
|
-
});
|
|
513
|
-
});
|
|
514
|
-
}
|
|
515
|
-
var getMsGraphBaseUrl, ROOT_URL;
|
|
516
|
-
return __generator(this, function (_a) {
|
|
517
|
-
getMsGraphBaseUrl = function (urlPostFix) { return "".concat(ROOT_URL, "/").concat(urlPostFix); };
|
|
518
|
-
ROOT_URL = 'https://graph.microsoft.com/v1.0';
|
|
519
|
-
return [2 /*return*/, {
|
|
520
|
-
getMsGraphBaseUrl: getMsGraphBaseUrl,
|
|
521
|
-
getHeaders: getHeaders,
|
|
522
|
-
parseResp: parseResp,
|
|
523
|
-
doGet: doGet,
|
|
524
|
-
doPost: doPost,
|
|
525
|
-
doPut: doPut,
|
|
526
|
-
doPatch: doPatch,
|
|
527
|
-
doDelete: doDelete,
|
|
528
|
-
getSharedItemInfo: getSharedItemInfo,
|
|
529
|
-
}];
|
|
530
|
-
});
|
|
531
|
-
});
|
|
532
|
-
}
|
|
533
|
-
exports.getMsGraphConn = getMsGraphConn;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GGraphError = void 0;
|
|
4
|
+
exports.delay = delay;
|
|
5
|
+
exports.encodeSharedUrl = encodeSharedUrl;
|
|
6
|
+
exports.getAuth = getAuth;
|
|
7
|
+
exports.axiosErrorProcessing = axiosErrorProcessing;
|
|
8
|
+
exports.getMsGraphConn = getMsGraphConn;
|
|
9
|
+
//import Axios, { GMRequestConfig } from "axios";
|
|
10
|
+
const httpRequest_1 = require("../httpRequest");
|
|
11
|
+
//import * as promise from 'bluebird';
|
|
12
|
+
const util_1 = require("../util");
|
|
13
|
+
function doHttpGet(url, opts) {
|
|
14
|
+
return (0, httpRequest_1.doHttpRequest)({
|
|
15
|
+
method: 'GET',
|
|
16
|
+
...opts,
|
|
17
|
+
url,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
function doHttpDelete(url, opts) {
|
|
21
|
+
return (0, httpRequest_1.doHttpRequest)({
|
|
22
|
+
method: 'DELETE',
|
|
23
|
+
...opts,
|
|
24
|
+
url,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
function doHttpPost(url, data, opts) {
|
|
28
|
+
return (0, httpRequest_1.doHttpRequest)({
|
|
29
|
+
method: 'POST',
|
|
30
|
+
...opts,
|
|
31
|
+
url,
|
|
32
|
+
data,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
function doHttpPatch(url, data, opts) {
|
|
36
|
+
return (0, httpRequest_1.doHttpRequest)({
|
|
37
|
+
method: 'PATCH',
|
|
38
|
+
...opts,
|
|
39
|
+
url,
|
|
40
|
+
data,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
async function delay(ms) {
|
|
44
|
+
return new Promise(resolve => {
|
|
45
|
+
setTimeout(() => {
|
|
46
|
+
resolve(null);
|
|
47
|
+
}, ms);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
class GGraphError extends Error {
|
|
51
|
+
requestUrl;
|
|
52
|
+
constructor(requestUrl) {
|
|
53
|
+
super('');
|
|
54
|
+
this.requestUrl = requestUrl;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.GGraphError = GGraphError;
|
|
58
|
+
function encodeSharedUrl(sharingUrl) {
|
|
59
|
+
//see https://docs.microsoft.com/en-us/graph/api/shares-get?view=graph-rest-1.0&irgwc=1&OCID=AID2200057_aff_7593_1243925&tduid=(ir__ksd0kmgl9ckf6nyskg6fwnqce32xt3umkhw9f9gn00)(7593)(1243925)(je6NUbpObpQ-XTpQa0NuXTfWX1VU38TMYg)()&irclickid=_ksd0kmgl9ckf6nyskg6fwnqce32xt3umkhw9f9gn00&tabs=http#encoding-sharing-urls&ranMID=24542&ranEAID=je6NUbpObpQ&ranSiteID=je6NUbpObpQ-XTpQa0NuXTfWX1VU38TMYg&epi=je6NUbpObpQ-XTpQa0NuXTfWX1VU38TMYg
|
|
60
|
+
const base64Value = Buffer.from(sharingUrl).toString('base64');
|
|
61
|
+
//string encodedUrl = "u!" + base64Value .TrimEnd('=').Replace('/', '_').Replace('+', '-');
|
|
62
|
+
const encodedUrl = base64Value.replace(/=/g, '').replace(/\//g, '_').replace(/\+/g, '-');
|
|
63
|
+
const resUrl = `u!${encodedUrl}`;
|
|
64
|
+
return resUrl;
|
|
65
|
+
}
|
|
66
|
+
function getAuth(opt) {
|
|
67
|
+
const tenantId = opt.tenantId;
|
|
68
|
+
const client_id = opt.client_id;
|
|
69
|
+
if (!tenantId)
|
|
70
|
+
throw {
|
|
71
|
+
message: 'tenantId required'
|
|
72
|
+
};
|
|
73
|
+
if (!client_id)
|
|
74
|
+
throw {
|
|
75
|
+
message: 'client_id required'
|
|
76
|
+
};
|
|
77
|
+
//const promptUser = opt.promptUser || ((msg: string, info: ICodeWaitInfo)=>console.log(msg,info));
|
|
78
|
+
//const saveToken = opt.saveToken;
|
|
79
|
+
const baseQueryUrl = `https://login.microsoftonline.com/${tenantId}/oauth2/v2.0`;
|
|
80
|
+
const queryCodeurl = `${baseQueryUrl}/token`;
|
|
81
|
+
async function doPost(url, data) {
|
|
82
|
+
const dataStr = (0, util_1.getFormData)(data);
|
|
83
|
+
return await (0, httpRequest_1.doHttpRequest)({ url, data: dataStr,
|
|
84
|
+
headers: {
|
|
85
|
+
'Content-Type': 'application/x-www-form-urlencoded'
|
|
86
|
+
},
|
|
87
|
+
method: 'POST'
|
|
88
|
+
}).then(r => {
|
|
89
|
+
return r.data;
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
const scope = opt.scope || 'offline_access Mail.Read openid profile User.Read email Files.ReadWrite.All Files.ReadWrite Files.Read Files.Read.All Files.Read.Selected Files.ReadWrite.AppFolder Files.ReadWrite.Selected';
|
|
93
|
+
async function getRefreshTokenPart1GetCodeWaitInfo() {
|
|
94
|
+
const codeWaitInfo = await doPost(`${baseQueryUrl}/devicecode`, {
|
|
95
|
+
scope,
|
|
96
|
+
client_id,
|
|
97
|
+
});
|
|
98
|
+
return codeWaitInfo;
|
|
99
|
+
}
|
|
100
|
+
async function getRefreshTokenPartFinish(deviceCode, saveToken, pollTime, maxPollTime) {
|
|
101
|
+
if (!maxPollTime)
|
|
102
|
+
maxPollTime = 1000 * 120;
|
|
103
|
+
if (!pollTime)
|
|
104
|
+
pollTime = 1000;
|
|
105
|
+
let totalWait = 0;
|
|
106
|
+
while (totalWait < maxPollTime) {
|
|
107
|
+
try {
|
|
108
|
+
const rrOrError = await doPost(queryCodeurl, {
|
|
109
|
+
grant_type: 'urn:ietf:params:oauth:grant-type:device_code',
|
|
110
|
+
//resource: 'https://graph.microsoft.com',
|
|
111
|
+
scope,
|
|
112
|
+
code: deviceCode,
|
|
113
|
+
client_id
|
|
114
|
+
});
|
|
115
|
+
opt.logger('getRefreshTokenPartFinish got result ', rrOrError);
|
|
116
|
+
if (rrOrError.error === 'authorization_pending') { //this no longer works with axios
|
|
117
|
+
opt.logger(`Waiting for deviceCode ${totalWait}/${maxPollTime}`, deviceCode);
|
|
118
|
+
//await promise.Promise.delay(opt.pollTime || 1000);
|
|
119
|
+
totalWait += pollTime;
|
|
120
|
+
await delay(pollTime);
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
const rr = rrOrError;
|
|
124
|
+
///console.log(rr);
|
|
125
|
+
//const { access_token, refresh_token } = rr;
|
|
126
|
+
//fs.writeFileSync('credentials.json', JSON.stringify(rr, null, 2));
|
|
127
|
+
opt.logger('saving token for deviceCode', deviceCode);
|
|
128
|
+
await saveToken(rr);
|
|
129
|
+
return rr;
|
|
130
|
+
}
|
|
131
|
+
catch (err) {
|
|
132
|
+
const errData = (0, util_1.get)(err, 'response.data');
|
|
133
|
+
if (errData && errData.error === 'authorization_pending') {
|
|
134
|
+
opt.logger(`Waiting for deviceCode(err) ${totalWait}/${maxPollTime}`, deviceCode);
|
|
135
|
+
await delay(pollTime);
|
|
136
|
+
totalWait += pollTime;
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
opt.logger('getRefreshTokenPartFinish: other error happened', errData || err);
|
|
140
|
+
throw err;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
async function getRefreshToken(saveToken, promptUser) {
|
|
145
|
+
const codeWaitInfo = await getRefreshTokenPart1GetCodeWaitInfo();
|
|
146
|
+
//const user_code = codeWaitInfo.user_code; // presented to the user
|
|
147
|
+
const deviceCode = codeWaitInfo.device_code; // internal code to identify the user
|
|
148
|
+
//const url = codeWaitInfo.verification_url; // URL the user needs to visit & paste in the code
|
|
149
|
+
const message = codeWaitInfo.message; //send user code to url
|
|
150
|
+
await promptUser(message, codeWaitInfo);
|
|
151
|
+
return await getRefreshTokenPartFinish(deviceCode, saveToken);
|
|
152
|
+
}
|
|
153
|
+
async function getAccessToken() {
|
|
154
|
+
const { refresh_token } = opt;
|
|
155
|
+
const form = {
|
|
156
|
+
scope,
|
|
157
|
+
refresh_token,
|
|
158
|
+
grant_type: 'refresh_token',
|
|
159
|
+
client_id
|
|
160
|
+
};
|
|
161
|
+
const ac = await doPost(queryCodeurl, form);
|
|
162
|
+
return ac;
|
|
163
|
+
}
|
|
164
|
+
//getAuth({tenantId, client_id, promptUser, saveToken, loadToken})
|
|
165
|
+
return {
|
|
166
|
+
getRefreshToken,
|
|
167
|
+
getAccessToken,
|
|
168
|
+
refreshTokenSeperated: {
|
|
169
|
+
getRefreshTokenPart1GetCodeWaitInfo,
|
|
170
|
+
getRefreshTokenPartFinish,
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
function axiosErrorProcessing(err) {
|
|
175
|
+
function doSteps(obj, initialMsg, steps) {
|
|
176
|
+
const msg = steps.reduce((acc, step) => {
|
|
177
|
+
const cur = (0, util_1.get)(acc.obj, step);
|
|
178
|
+
if (typeof cur === 'string') {
|
|
179
|
+
if (acc.msg)
|
|
180
|
+
acc.msg = `${acc.msg} ${cur}`;
|
|
181
|
+
else
|
|
182
|
+
acc.msg = cur;
|
|
183
|
+
}
|
|
184
|
+
return acc;
|
|
185
|
+
}, {
|
|
186
|
+
obj,
|
|
187
|
+
msg: initialMsg
|
|
188
|
+
});
|
|
189
|
+
return msg.msg;
|
|
190
|
+
}
|
|
191
|
+
const steps = ['response.data.message', 'response.data.error.message'];
|
|
192
|
+
const msg = doSteps(err, err.message, steps);
|
|
193
|
+
return msg;
|
|
194
|
+
}
|
|
195
|
+
const connCaccheInfo = {
|
|
196
|
+
cache: null,
|
|
197
|
+
};
|
|
198
|
+
async function getMsGraphConn(opt) {
|
|
199
|
+
async function getToken() {
|
|
200
|
+
const now = Math.round(new Date().getTime() / 1000);
|
|
201
|
+
const cacheKey = `${opt.tenantId}-${opt.client_id}`;
|
|
202
|
+
if (!connCaccheInfo.cache) {
|
|
203
|
+
if (opt.loadTokenCache)
|
|
204
|
+
connCaccheInfo.cache = await opt.loadTokenCache();
|
|
205
|
+
else
|
|
206
|
+
connCaccheInfo.cache = {};
|
|
207
|
+
}
|
|
208
|
+
const optTokenInfo = connCaccheInfo.cache[cacheKey];
|
|
209
|
+
if (opt.debugLogger)
|
|
210
|
+
opt.debugLogger(`debugrm getMsGraphConn now=${now} exp=${optTokenInfo?.expires_on}`);
|
|
211
|
+
if (!optTokenInfo || optTokenInfo.expires_on < now) {
|
|
212
|
+
const { getAccessToken } = getAuth(opt);
|
|
213
|
+
opt.logger('getting new token');
|
|
214
|
+
const tok = await getAccessToken();
|
|
215
|
+
console.log('------------------------>', tok);
|
|
216
|
+
const retToken = {
|
|
217
|
+
access_token: tok.access_token,
|
|
218
|
+
expires_on: tok.expires_in + now,
|
|
219
|
+
};
|
|
220
|
+
connCaccheInfo.cache[cacheKey] = retToken;
|
|
221
|
+
if (opt.saveTokenCache) {
|
|
222
|
+
opt.logger('saving token cache');
|
|
223
|
+
await opt.saveTokenCache(connCaccheInfo.cache);
|
|
224
|
+
}
|
|
225
|
+
return retToken;
|
|
226
|
+
}
|
|
227
|
+
return optTokenInfo;
|
|
228
|
+
}
|
|
229
|
+
async function getHeaders() {
|
|
230
|
+
const tok = await getToken();
|
|
231
|
+
return {
|
|
232
|
+
headers: {
|
|
233
|
+
"Authorization": `Bearer ${tok.access_token}`
|
|
234
|
+
},
|
|
235
|
+
//maxContentLength: Infinity,
|
|
236
|
+
//maxBodyLength: Infinity,
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
async function parseResp(opts, r) {
|
|
240
|
+
//if (r.statusCode === 302) {
|
|
241
|
+
// if (r.headers.location) {
|
|
242
|
+
// return await doHttpGet(r.headers.location, opts).then(async r => await parseResp(opts, r)).catch(errProc(new GGraphError(r.headers.location)));
|
|
243
|
+
// }
|
|
244
|
+
//}
|
|
245
|
+
return r.data;
|
|
246
|
+
}
|
|
247
|
+
function errProc(errTrace) {
|
|
248
|
+
return err => {
|
|
249
|
+
const message = axiosErrorProcessing(err);
|
|
250
|
+
opt.logger(`error on ${errTrace.requestUrl}: ${message}`);
|
|
251
|
+
errTrace.message = err.message;
|
|
252
|
+
throw errTrace;
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
const getMsGraphBaseUrl = (urlPostFix) => `${ROOT_URL}/${urlPostFix}`;
|
|
256
|
+
async function doGet(urlPostFix, fmt = x => x) {
|
|
257
|
+
const uri = getMsGraphBaseUrl(urlPostFix);
|
|
258
|
+
opt.logger(`GET ${uri}`);
|
|
259
|
+
const headers = await getHeaders();
|
|
260
|
+
const opts = fmt({
|
|
261
|
+
...headers,
|
|
262
|
+
followRedirect: true,
|
|
263
|
+
});
|
|
264
|
+
return await doHttpGet(uri, opts)
|
|
265
|
+
.then(async (r) => await parseResp(opts, r)).catch(errProc(new GGraphError(uri)));
|
|
266
|
+
}
|
|
267
|
+
async function doPost(urlPostFix, data) {
|
|
268
|
+
const uri = getMsGraphBaseUrl(urlPostFix);
|
|
269
|
+
opt.logger(`POST ${uri}`);
|
|
270
|
+
const opts = await getHeaders();
|
|
271
|
+
return doHttpPost(uri, data, opts).then(async (r) => await parseResp(opts, r)).catch(errProc(new GGraphError(uri)));
|
|
272
|
+
}
|
|
273
|
+
async function doPut(urlPostFix, data) {
|
|
274
|
+
const uri = getMsGraphBaseUrl(urlPostFix);
|
|
275
|
+
opt.logger(`PUT ${uri}`);
|
|
276
|
+
const opts = await getHeaders();
|
|
277
|
+
return doHttpPost(uri, data, opts).then(async (r) => await parseResp(opts, r)).catch(errProc(new GGraphError(uri)));
|
|
278
|
+
}
|
|
279
|
+
async function doPatch(urlPostFix, data) {
|
|
280
|
+
const uri = getMsGraphBaseUrl(urlPostFix);
|
|
281
|
+
opt.logger(`PATCH ${uri}`);
|
|
282
|
+
const opts = await getHeaders();
|
|
283
|
+
return doHttpPatch(uri, data, opts).then(async (r) => await parseResp(opts, r)).catch(errProc(new GGraphError(uri)));
|
|
284
|
+
}
|
|
285
|
+
async function doDelete(urlPostFix) {
|
|
286
|
+
const uri = getMsGraphBaseUrl(urlPostFix);
|
|
287
|
+
opt.logger(`PATCH ${uri}`);
|
|
288
|
+
const opts = await getHeaders();
|
|
289
|
+
return doHttpDelete(uri, opts).then(async (r) => await parseResp(opts, r)).catch(errProc(new GGraphError(uri)));
|
|
290
|
+
}
|
|
291
|
+
const ROOT_URL = 'https://graph.microsoft.com/v1.0';
|
|
292
|
+
//const getMsGraphBaseUrl = (urlPostFix: string) => `${ROOT_URL}/users('${opt.tenantClientInfo.userId}')/${urlPostFix}`;
|
|
293
|
+
async function getSharedItemInfo(sharedUrl) {
|
|
294
|
+
return doGet(`shares/${encodeSharedUrl(sharedUrl)}/driveItem`).then((r) => {
|
|
295
|
+
return r;
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
return {
|
|
299
|
+
getMsGraphBaseUrl,
|
|
300
|
+
getHeaders,
|
|
301
|
+
parseResp,
|
|
302
|
+
doGet,
|
|
303
|
+
doPost,
|
|
304
|
+
doPut,
|
|
305
|
+
doPatch,
|
|
306
|
+
doDelete,
|
|
307
|
+
getSharedItemInfo,
|
|
308
|
+
};
|
|
309
|
+
}
|
|
534
310
|
//# sourceMappingURL=msauth.js.map
|