UNPKG

4.14 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2021 Red Hat, Inc. and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16// *****************************************************************************
17var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
18 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
19 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
20 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
21 return c > 3 && r && Object.defineProperty(target, key, r), r;
22};
23var KeytarServiceImpl_1;
24Object.defineProperty(exports, "__esModule", { value: true });
25exports.KeytarServiceImpl = void 0;
26const inversify_1 = require("inversify");
27const common_1 = require("../common");
28const keytar = require("keytar");
29let KeytarServiceImpl = KeytarServiceImpl_1 = class KeytarServiceImpl {
30 async setPassword(service, account, password) {
31 if (common_1.isWindows && password.length > KeytarServiceImpl_1.MAX_PASSWORD_LENGTH) {
32 let index = 0;
33 let chunk = 0;
34 let hasNextChunk = true;
35 while (hasNextChunk) {
36 const passwordChunk = password.substring(index, index + KeytarServiceImpl_1.PASSWORD_CHUNK_SIZE);
37 index += KeytarServiceImpl_1.PASSWORD_CHUNK_SIZE;
38 hasNextChunk = password.length - index > 0;
39 const content = {
40 content: passwordChunk,
41 hasNextChunk: hasNextChunk
42 };
43 await keytar.setPassword(service, chunk ? `${account}-${chunk}` : account, JSON.stringify(content));
44 chunk++;
45 }
46 }
47 else {
48 await keytar.setPassword(service, account, password);
49 }
50 }
51 deletePassword(service, account) {
52 return keytar.deletePassword(service, account);
53 }
54 async getPassword(service, account) {
55 const password = await keytar.getPassword(service, account);
56 if (password) {
57 try {
58 let { content, hasNextChunk } = JSON.parse(password);
59 if (!content || !hasNextChunk) {
60 return password;
61 }
62 let index = 1;
63 while (hasNextChunk) {
64 const nextChunk = await keytar.getPassword(service, `${account}-${index++}`);
65 const result = JSON.parse(nextChunk);
66 content += result.content;
67 hasNextChunk = result.hasNextChunk;
68 }
69 return content;
70 }
71 catch (_a) {
72 return password;
73 }
74 }
75 }
76 async findPassword(service) {
77 const password = await keytar.findPassword(service);
78 if (password) {
79 return password;
80 }
81 }
82 async findCredentials(service) {
83 return keytar.findCredentials(service);
84 }
85};
86KeytarServiceImpl.MAX_PASSWORD_LENGTH = 2500;
87KeytarServiceImpl.PASSWORD_CHUNK_SIZE = KeytarServiceImpl_1.MAX_PASSWORD_LENGTH - 100;
88KeytarServiceImpl = KeytarServiceImpl_1 = __decorate([
89 (0, inversify_1.injectable)()
90], KeytarServiceImpl);
91exports.KeytarServiceImpl = KeytarServiceImpl;
92//# sourceMappingURL=keytar-server.js.map
\No newline at end of file