UNPKG

25.4 kBJavaScriptView Raw
1"use strict";
2// Copyright (c) Microsoft. All rights reserved.
3// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
5 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
6 return new (P || (P = Promise))(function (resolve, reject) {
7 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
8 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
9 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
10 step((generator = generator.apply(thisArg, _arguments || [])).next());
11 });
12};
13Object.defineProperty(exports, "__esModule", { value: true });
14exports.WebApi = exports.getHandlerFromToken = exports.getPersonalAccessTokenHandler = exports.getBearerHandler = exports.getNtlmHandler = exports.getBasicHandler = void 0;
15const alertm = require("./AlertApi");
16const buildm = require("./BuildApi");
17const corem = require("./CoreApi");
18const dashboardm = require("./DashboardApi");
19const extmgmtm = require("./ExtensionManagementApi");
20const featuremgmtm = require("./FeatureManagementApi");
21const filecontainerm = require("./FileContainerApi");
22const gallerym = require("./GalleryApi");
23const gitm = require("./GitApi");
24const locationsm = require("./LocationsApi");
25const managementm = require("./ManagementApi");
26const notificationm = require("./NotificationApi");
27const policym = require("./PolicyApi");
28const profilem = require("./ProfileApi");
29const projectm = require("./ProjectAnalysisApi");
30const releasem = require("./ReleaseApi");
31const securityrolesm = require("./SecurityRolesApi");
32const taskagentm = require("./TaskAgentApi");
33const taskm = require("./TaskApi");
34const testm = require("./TestApi");
35const testplanm = require("./TestPlanApi");
36const testresultsm = require("./TestResultsApi");
37const tfvcm = require("./TfvcApi");
38const wikim = require("./WikiApi");
39const workm = require("./WorkApi");
40const pipelinesm = require("./PipelinesApi");
41const cixm = require("./CIXApi");
42const workitemtrackingm = require("./WorkItemTrackingApi");
43const workitemtrackingprocessm = require("./WorkItemTrackingProcessApi");
44const workitemtrackingprocessdefinitionm = require("./WorkItemTrackingProcessDefinitionsApi");
45const basicm = require("./handlers/basiccreds");
46const bearm = require("./handlers/bearertoken");
47const ntlmm = require("./handlers/ntlm");
48const patm = require("./handlers/personalaccesstoken");
49const rm = require("typed-rest-client/RestClient");
50const vsom = require("./VsoClient");
51const crypto = require("crypto");
52const fs = require("fs");
53const os = require("os");
54const url = require("url");
55const path = require("path");
56const isBrowser = typeof window !== 'undefined';
57/**
58 * Methods to return handler objects (see handlers folder)
59 */
60function getBasicHandler(username, password, allowCrossOriginAuthentication) {
61 return new basicm.BasicCredentialHandler(username, password, allowCrossOriginAuthentication);
62}
63exports.getBasicHandler = getBasicHandler;
64function getNtlmHandler(username, password, workstation, domain) {
65 return new ntlmm.NtlmCredentialHandler(username, password, workstation, domain);
66}
67exports.getNtlmHandler = getNtlmHandler;
68function getBearerHandler(token, allowCrossOriginAuthentication) {
69 return new bearm.BearerCredentialHandler(token, allowCrossOriginAuthentication);
70}
71exports.getBearerHandler = getBearerHandler;
72function getPersonalAccessTokenHandler(token, allowCrossOriginAuthentication) {
73 return new patm.PersonalAccessTokenCredentialHandler(token, allowCrossOriginAuthentication);
74}
75exports.getPersonalAccessTokenHandler = getPersonalAccessTokenHandler;
76function getHandlerFromToken(token, allowCrossOriginAuthentication) {
77 if (token.length === 52) {
78 return getPersonalAccessTokenHandler(token, allowCrossOriginAuthentication);
79 }
80 else {
81 return getBearerHandler(token, allowCrossOriginAuthentication);
82 }
83}
84exports.getHandlerFromToken = getHandlerFromToken;
85;
86// ---------------------------------------------------------------------------
87// Factory to return client apis
88// When new APIs are added, a method must be added here to instantiate the API
89//----------------------------------------------------------------------------
90class WebApi {
91 /*
92 * Factory to return client apis and handlers
93 * @param defaultUrl default server url to use when creating new apis from factory methods
94 * @param authHandler default authentication credentials to use when creating new apis from factory methods
95 */
96 constructor(defaultUrl, authHandler, options, requestSettings) {
97 /**
98 * Determines if the domain is exluded for proxy via the no_proxy env var
99 * @param url: the server url
100 */
101 this.isNoProxyHost = function (_url) {
102 if (!process.env.no_proxy) {
103 return false;
104 }
105 const noProxyDomains = (process.env.no_proxy || '')
106 .split(',')
107 .map(v => v.toLowerCase());
108 const serverUrl = url.parse(_url).host.toLowerCase();
109 // return true if the no_proxy includes the host
110 return noProxyDomains.indexOf(serverUrl) !== -1;
111 };
112 this.serverUrl = defaultUrl;
113 this.authHandler = authHandler;
114 this.options = options || {};
115 if (!this.isNoProxyHost(this.serverUrl)) {
116 // try to get proxy setting from environment variable set by VSTS-Task-Lib if there is no proxy setting in the options
117 if (!this.options.proxy || !this.options.proxy.proxyUrl) {
118 if (global['_vsts_task_lib_proxy']) {
119 let proxyFromEnv = {
120 proxyUrl: global['_vsts_task_lib_proxy_url'],
121 proxyUsername: global['_vsts_task_lib_proxy_username'],
122 proxyPassword: this._readTaskLibSecrets(global['_vsts_task_lib_proxy_password']),
123 proxyBypassHosts: JSON.parse(global['_vsts_task_lib_proxy_bypass'] || "[]"),
124 };
125 this.options.proxy = proxyFromEnv;
126 }
127 }
128 }
129 // try get cert setting from environment variable set by VSTS-Task-Lib if there is no cert setting in the options
130 if (!this.options.cert) {
131 if (global['_vsts_task_lib_cert']) {
132 let certFromEnv = {
133 caFile: global['_vsts_task_lib_cert_ca'],
134 certFile: global['_vsts_task_lib_cert_clientcert'],
135 keyFile: global['_vsts_task_lib_cert_key'],
136 passphrase: this._readTaskLibSecrets(global['_vsts_task_lib_cert_passphrase']),
137 };
138 this.options.cert = certFromEnv;
139 }
140 }
141 // try get ignore SSL error setting from environment variable set by VSTS-Task-Lib if there is no ignore SSL error setting in the options
142 if (!this.options.ignoreSslError) {
143 this.options.ignoreSslError = !!global['_vsts_task_lib_skip_cert_validation'];
144 }
145 let userAgent;
146 const nodeApiName = 'azure-devops-node-api';
147 if (isBrowser) {
148 if (requestSettings) {
149 userAgent = `${requestSettings.productName}/${requestSettings.productVersion} (${nodeApiName}; ${window.navigator.userAgent})`;
150 }
151 else {
152 userAgent = `${nodeApiName} (${window.navigator.userAgent})`;
153 }
154 }
155 else {
156 let nodeApiVersion = 'unknown';
157 const packageJsonPath = path.resolve(__dirname, 'package.json');
158 if (fs.existsSync(packageJsonPath)) {
159 nodeApiVersion = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')).version;
160 }
161 const osName = os.platform();
162 const osVersion = os.release();
163 if (requestSettings) {
164 userAgent = `${requestSettings.productName}/${requestSettings.productVersion} (${nodeApiName} ${nodeApiVersion}; ${osName} ${osVersion})`;
165 }
166 else {
167 userAgent = `${nodeApiName}/${nodeApiVersion} (${osName} ${osVersion})`;
168 }
169 }
170 this.rest = new rm.RestClient(userAgent, null, [this.authHandler], this.options);
171 this.vsoClient = new vsom.VsoClient(defaultUrl, this.rest);
172 }
173 /**
174 * Convenience factory to create with a bearer token.
175 * @param defaultServerUrl default server url to use when creating new apis from factory methods
176 * @param defaultAuthHandler default authentication credentials to use when creating new apis from factory methods
177 */
178 static createWithBearerToken(defaultUrl, token, options) {
179 let bearerHandler = getBearerHandler(token);
180 return new this(defaultUrl, bearerHandler, options);
181 }
182 connect() {
183 return __awaiter(this, void 0, void 0, function* () {
184 return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
185 try {
186 let res;
187 res = yield this.rest.get(this.vsoClient.resolveUrl('/_apis/connectionData'));
188 resolve(res.result);
189 }
190 catch (err) {
191 reject(err);
192 }
193 }));
194 });
195 }
196 /**
197 * Each factory method can take a serverUrl and a list of handlers
198 * if these aren't provided, the default url and auth handler given to the constructor for this class will be used
199 */
200 getAlertApi(serverUrl, handlers) {
201 return __awaiter(this, void 0, void 0, function* () {
202 serverUrl = yield this._getResourceAreaUrl(serverUrl || this.serverUrl, "0f2ca920-f269-4545-b1f4-5b4173aa784e");
203 handlers = handlers || [this.authHandler];
204 return new alertm.AlertApi(serverUrl, handlers, this.options);
205 });
206 }
207 getBuildApi(serverUrl, handlers) {
208 return __awaiter(this, void 0, void 0, function* () {
209 serverUrl = yield this._getResourceAreaUrl(serverUrl || this.serverUrl, buildm.BuildApi.RESOURCE_AREA_ID);
210 handlers = handlers || [this.authHandler];
211 return new buildm.BuildApi(serverUrl, handlers, this.options);
212 });
213 }
214 getCoreApi(serverUrl, handlers) {
215 return __awaiter(this, void 0, void 0, function* () {
216 // TODO: Load RESOURCE_AREA_ID correctly.
217 serverUrl = yield this._getResourceAreaUrl(serverUrl || this.serverUrl, "79134c72-4a58-4b42-976c-04e7115f32bf");
218 handlers = handlers || [this.authHandler];
219 return new corem.CoreApi(serverUrl, handlers, this.options);
220 });
221 }
222 getDashboardApi(serverUrl, handlers) {
223 return __awaiter(this, void 0, void 0, function* () {
224 // TODO: Load RESOURCE_AREA_ID correctly.
225 serverUrl = yield this._getResourceAreaUrl(serverUrl || this.serverUrl, "31c84e0a-3ece-48fd-a29d-100849af99ba");
226 handlers = handlers || [this.authHandler];
227 return new dashboardm.DashboardApi(serverUrl, handlers, this.options);
228 });
229 }
230 getExtensionManagementApi(serverUrl, handlers) {
231 return __awaiter(this, void 0, void 0, function* () {
232 // TODO: Load RESOURCE_AREA_ID correctly.
233 serverUrl = yield this._getResourceAreaUrl(serverUrl || this.serverUrl, "6c2b0933-3600-42ae-bf8b-93d4f7e83594");
234 handlers = handlers || [this.authHandler];
235 return new extmgmtm.ExtensionManagementApi(serverUrl, handlers, this.options);
236 });
237 }
238 getFeatureManagementApi(serverUrl, handlers) {
239 return __awaiter(this, void 0, void 0, function* () {
240 // TODO: Load RESOURCE_AREA_ID correctly.
241 serverUrl = yield this._getResourceAreaUrl(serverUrl || this.serverUrl, "");
242 handlers = handlers || [this.authHandler];
243 return new featuremgmtm.FeatureManagementApi(serverUrl, handlers, this.options);
244 });
245 }
246 getFileContainerApi(serverUrl, handlers) {
247 return __awaiter(this, void 0, void 0, function* () {
248 // TODO: Load RESOURCE_AREA_ID correctly.
249 serverUrl = yield this._getResourceAreaUrl(serverUrl || this.serverUrl, "");
250 handlers = handlers || [this.authHandler];
251 return new filecontainerm.FileContainerApi(serverUrl, handlers, this.options);
252 });
253 }
254 getGalleryApi(serverUrl, handlers) {
255 return __awaiter(this, void 0, void 0, function* () {
256 serverUrl = yield this._getResourceAreaUrl(serverUrl || this.serverUrl, gallerym.GalleryApi.RESOURCE_AREA_ID);
257 handlers = handlers || [this.authHandler];
258 return new gallerym.GalleryApi(serverUrl, handlers, this.options);
259 });
260 }
261 getGitApi(serverUrl, handlers) {
262 return __awaiter(this, void 0, void 0, function* () {
263 serverUrl = yield this._getResourceAreaUrl(serverUrl || this.serverUrl, gitm.GitApi.RESOURCE_AREA_ID);
264 handlers = handlers || [this.authHandler];
265 return new gitm.GitApi(serverUrl, handlers, this.options);
266 });
267 }
268 // TODO: Don't call resource area here? Will cause infinite loop?
269 getLocationsApi(serverUrl, handlers) {
270 return __awaiter(this, void 0, void 0, function* () {
271 let optionsClone = Object.assign({}, this.options);
272 optionsClone.allowRetries = true;
273 optionsClone.maxRetries = 5;
274 serverUrl = (yield serverUrl) || this.serverUrl;
275 handlers = handlers || [this.authHandler];
276 return new locationsm.LocationsApi(serverUrl, handlers, optionsClone);
277 });
278 }
279 getManagementApi(serverUrl, handlers) {
280 return __awaiter(this, void 0, void 0, function* () {
281 serverUrl = yield this._getResourceAreaUrl(serverUrl || this.serverUrl, "f101720c-9790-45a6-9fb3-494a09fddeeb");
282 handlers = handlers || [this.authHandler];
283 return new managementm.ManagementApi(serverUrl, handlers, this.options);
284 });
285 }
286 getNotificationApi(serverUrl, handlers) {
287 return __awaiter(this, void 0, void 0, function* () {
288 // TODO: Load RESOURCE_AREA_ID correctly.
289 serverUrl = yield this._getResourceAreaUrl(serverUrl || this.serverUrl, "");
290 handlers = handlers || [this.authHandler];
291 return new notificationm.NotificationApi(serverUrl, handlers, this.options);
292 });
293 }
294 getPolicyApi(serverUrl, handlers) {
295 return __awaiter(this, void 0, void 0, function* () {
296 // TODO: Load RESOURCE_AREA_ID correctly.
297 serverUrl = yield this._getResourceAreaUrl(serverUrl || this.serverUrl, "fb13a388-40dd-4a04-b530-013a739c72ef");
298 handlers = handlers || [this.authHandler];
299 return new policym.PolicyApi(serverUrl, handlers, this.options);
300 });
301 }
302 getProfileApi(serverUrl, handlers) {
303 return __awaiter(this, void 0, void 0, function* () {
304 // TODO: Load RESOURCE_AREA_ID correctly.
305 serverUrl = yield this._getResourceAreaUrl(serverUrl || this.serverUrl, "8ccfef3d-2b87-4e99-8ccb-66e343d2daa8");
306 handlers = handlers || [this.authHandler];
307 return new profilem.ProfileApi(serverUrl, handlers, this.options);
308 });
309 }
310 getProjectAnalysisApi(serverUrl, handlers) {
311 return __awaiter(this, void 0, void 0, function* () {
312 // TODO: Load RESOURCE_AREA_ID correctly.
313 serverUrl = yield this._getResourceAreaUrl(serverUrl || this.serverUrl, "7658fa33-b1bf-4580-990f-fac5896773d3");
314 handlers = handlers || [this.authHandler];
315 return new projectm.ProjectAnalysisApi(serverUrl, handlers, this.options);
316 });
317 }
318 getSecurityRolesApi(serverUrl, handlers) {
319 return __awaiter(this, void 0, void 0, function* () {
320 // TODO: Load RESOURCE_AREA_ID correctly.
321 serverUrl = yield this._getResourceAreaUrl(serverUrl || this.serverUrl, "");
322 handlers = handlers || [this.authHandler];
323 return new securityrolesm.SecurityRolesApi(serverUrl, handlers, this.options);
324 });
325 }
326 getReleaseApi(serverUrl, handlers) {
327 return __awaiter(this, void 0, void 0, function* () {
328 // TODO: Load RESOURCE_AREA_ID correctly.
329 serverUrl = yield this._getResourceAreaUrl(serverUrl || this.serverUrl, "efc2f575-36ef-48e9-b672-0c6fb4a48ac5");
330 handlers = handlers || [this.authHandler];
331 return new releasem.ReleaseApi(serverUrl, handlers, this.options);
332 });
333 }
334 getTaskApi(serverUrl, handlers) {
335 return __awaiter(this, void 0, void 0, function* () {
336 // TODO: Load RESOURCE_AREA_ID correctly.
337 serverUrl = yield this._getResourceAreaUrl(serverUrl || this.serverUrl, "");
338 handlers = handlers || [this.authHandler];
339 return new taskm.TaskApi(serverUrl, handlers, this.options);
340 });
341 }
342 getTaskAgentApi(serverUrl, handlers) {
343 return __awaiter(this, void 0, void 0, function* () {
344 // TODO: Load RESOURCE_AREA_ID correctly.
345 serverUrl = yield this._getResourceAreaUrl(serverUrl || this.serverUrl, "a85b8835-c1a1-4aac-ae97-1c3d0ba72dbd");
346 handlers = handlers || [this.authHandler];
347 return new taskagentm.TaskAgentApi(serverUrl, handlers, this.options);
348 });
349 }
350 getTestApi(serverUrl, handlers) {
351 return __awaiter(this, void 0, void 0, function* () {
352 // TODO: Load RESOURCE_AREA_ID correctly.
353 serverUrl = yield this._getResourceAreaUrl(serverUrl || this.serverUrl, "c2aa639c-3ccc-4740-b3b6-ce2a1e1d984e");
354 handlers = handlers || [this.authHandler];
355 return new testm.TestApi(serverUrl, handlers, this.options);
356 });
357 }
358 getTestPlanApi(serverUrl, handlers) {
359 return __awaiter(this, void 0, void 0, function* () {
360 // TODO: Load RESOURCE_AREA_ID correctly.
361 serverUrl = yield this._getResourceAreaUrl(serverUrl || this.serverUrl, "e4c27205-9d23-4c98-b958-d798bc3f9cd4");
362 handlers = handlers || [this.authHandler];
363 return new testplanm.TestPlanApi(serverUrl, handlers, this.options);
364 });
365 }
366 getTestResultsApi(serverUrl, handlers) {
367 return __awaiter(this, void 0, void 0, function* () {
368 // TODO: Load RESOURCE_AREA_ID correctly.
369 serverUrl = yield this._getResourceAreaUrl(serverUrl || this.serverUrl, "c83eaf52-edf3-4034-ae11-17d38f25404c");
370 handlers = handlers || [this.authHandler];
371 return new testresultsm.TestResultsApi(serverUrl, handlers, this.options);
372 });
373 }
374 getTfvcApi(serverUrl, handlers) {
375 return __awaiter(this, void 0, void 0, function* () {
376 // TODO: Load RESOURCE_AREA_ID correctly.
377 serverUrl = yield this._getResourceAreaUrl(serverUrl || this.serverUrl, "8aa40520-446d-40e6-89f6-9c9f9ce44c48");
378 handlers = handlers || [this.authHandler];
379 return new tfvcm.TfvcApi(serverUrl, handlers, this.options);
380 });
381 }
382 getWikiApi(serverUrl, handlers) {
383 return __awaiter(this, void 0, void 0, function* () {
384 // TODO: Load RESOURCE_AREA_ID correctly.
385 serverUrl = yield this._getResourceAreaUrl(serverUrl || this.serverUrl, "bf7d82a0-8aa5-4613-94ef-6172a5ea01f3");
386 handlers = handlers || [this.authHandler];
387 return new wikim.WikiApi(serverUrl, handlers, this.options);
388 });
389 }
390 getWorkApi(serverUrl, handlers) {
391 return __awaiter(this, void 0, void 0, function* () {
392 // TODO: Load RESOURCE_AREA_ID correctly.
393 serverUrl = yield this._getResourceAreaUrl(serverUrl || this.serverUrl, "1d4f49f9-02b9-4e26-b826-2cdb6195f2a9");
394 handlers = handlers || [this.authHandler];
395 return new workm.WorkApi(serverUrl, handlers, this.options);
396 });
397 }
398 getWorkItemTrackingApi(serverUrl, handlers) {
399 return __awaiter(this, void 0, void 0, function* () {
400 serverUrl = yield this._getResourceAreaUrl(serverUrl || this.serverUrl, workitemtrackingm.WorkItemTrackingApi.RESOURCE_AREA_ID);
401 handlers = handlers || [this.authHandler];
402 return new workitemtrackingm.WorkItemTrackingApi(serverUrl, handlers, this.options);
403 });
404 }
405 getWorkItemTrackingProcessApi(serverUrl, handlers) {
406 return __awaiter(this, void 0, void 0, function* () {
407 // TODO: Load RESOURCE_AREA_ID correctly.
408 serverUrl = yield this._getResourceAreaUrl(serverUrl || this.serverUrl, "5264459e-e5e0-4bd8-b118-0985e68a4ec5");
409 handlers = handlers || [this.authHandler];
410 return new workitemtrackingprocessm.WorkItemTrackingProcessApi(serverUrl, handlers, this.options);
411 });
412 }
413 getWorkItemTrackingProcessDefinitionApi(serverUrl, handlers) {
414 return __awaiter(this, void 0, void 0, function* () {
415 // TODO: Load RESOURCE_AREA_ID correctly.
416 serverUrl = yield this._getResourceAreaUrl(serverUrl || this.serverUrl, "5264459e-e5e0-4bd8-b118-0985e68a4ec5");
417 handlers = handlers || [this.authHandler];
418 return new workitemtrackingprocessdefinitionm.WorkItemTrackingProcessDefinitionsApi(serverUrl, handlers, this.options);
419 });
420 }
421 getPipelinesApi(serverUrl, handlers) {
422 return __awaiter(this, void 0, void 0, function* () {
423 // TODO: Load RESOURCE_AREA_ID correctly.
424 serverUrl = yield this._getResourceAreaUrl(serverUrl || this.serverUrl, "5264459e-e5e0-4bd8-b118-0985e68a4ec5");
425 handlers = handlers || [this.authHandler];
426 return new pipelinesm.PipelinesApi(serverUrl, handlers, this.options);
427 });
428 }
429 getCixApi(serverUrl, handlers) {
430 return __awaiter(this, void 0, void 0, function* () {
431 // TODO: Load RESOURCE_AREA_ID correctly.
432 serverUrl = yield this._getResourceAreaUrl(serverUrl || this.serverUrl, "5264459e-e5e0-4bd8-b118-0985e68a4ec5");
433 handlers = handlers || [this.authHandler];
434 return new cixm.CixApi(serverUrl, handlers, this.options);
435 });
436 }
437 _getResourceAreaUrl(serverUrl, resourceId) {
438 return __awaiter(this, void 0, void 0, function* () {
439 if (!resourceId) {
440 return serverUrl;
441 }
442 // This must be of type any, see comment just below.
443 const resourceAreas = yield this._getResourceAreas();
444 if (resourceAreas === undefined) {
445 throw new Error((`Failed to retrieve resource areas ' + 'from server: ${serverUrl}`));
446 }
447 // The response type differs based on whether or not there are resource areas. When we are on prem we get:
448 // {"count":0,"value":null} and when we are on VSTS we get an array of resource areas.
449 // Due to this strangeness the type of resourceAreas needs to be any and we need to check .count
450 // When going against vsts count will be undefined. On prem it will be 0
451 if (!resourceAreas || resourceAreas.length === 0 || resourceAreas.count === 0) {
452 // For on prem environments we get an empty list
453 return serverUrl;
454 }
455 for (var resourceArea of resourceAreas) {
456 if (resourceArea.id.toLowerCase() === resourceId.toLowerCase()) {
457 return resourceArea.locationUrl;
458 }
459 }
460 throw new Error((`Could not find information for resource area ${resourceId} ' + 'from server: ${serverUrl}`));
461 });
462 }
463 _getResourceAreas() {
464 return __awaiter(this, void 0, void 0, function* () {
465 if (!this._resourceAreas) {
466 const locationClient = yield this.getLocationsApi();
467 this._resourceAreas = yield locationClient.getResourceAreas();
468 }
469 return this._resourceAreas;
470 });
471 }
472 _readTaskLibSecrets(lookupKey) {
473 if (isBrowser) {
474 throw new Error("Browsers can't securely keep secrets");
475 }
476 // the lookupKey should has following format
477 // base64encoded<keyFilePath>:base64encoded<encryptedContent>
478 if (lookupKey && lookupKey.indexOf(':') > 0) {
479 let lookupInfo = lookupKey.split(':', 2);
480 // file contains encryption key
481 let keyFile = new Buffer(lookupInfo[0], 'base64').toString('utf8');
482 let encryptKey = new Buffer(fs.readFileSync(keyFile, 'utf8'), 'base64');
483 let encryptedContent = new Buffer(lookupInfo[1], 'base64').toString('utf8');
484 let decipher = crypto.createDecipher("aes-256-ctr", encryptKey);
485 let decryptedContent = decipher.update(encryptedContent, 'hex', 'utf8');
486 decryptedContent += decipher.final('utf8');
487 return decryptedContent;
488 }
489 }
490}
491exports.WebApi = WebApi;