basant307/AI_Governance_Project
048
1"use strict";2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {3 if (k2 === undefined) k2 = k;4 var desc = Object.getOwnPropertyDescriptor(m, k);5 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {6 desc = { enumerable: true, get: function() { return m[k]; } };7 }8 Object.defineProperty(o, k2, desc);9}) : (function(o, m, k, k2) {10 if (k2 === undefined) k2 = k;11 o[k2] = m[k];12}));13var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {14 Object.defineProperty(o, "default", { enumerable: true, value: v });15}) : function(o, v) {16 o["default"] = v;17});18var __importStar = (this && this.__importStar) || (function () {19 var ownKeys = function(o) {20 ownKeys = Object.getOwnPropertyNames || function (o) {21 var ar = [];22 for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;23 return ar;24 };25 return ownKeys(o);26 };27 return function (mod) {28 if (mod && mod.__esModule) return mod;29 var result = {};30 if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);31 __setModuleDefault(result, mod);32 return result;33 };34})();35Object.defineProperty(exports, "__esModule", { value: true });36exports.KeytarStore = exports.FileStore = void 0;37exports.verifyPat = verifyPat;38exports.getPublisher = getPublisher;39exports.loginPublisher = loginPublisher;40exports.logoutPublisher = logoutPublisher;41exports.deletePublisher = deletePublisher;42exports.listPublishers = listPublishers;43const fs = __importStar(require("fs"));44const path = __importStar(require("path"));45const os_1 = require("os");46const util_1 = require("./util");47const validation_1 = require("./validation");48const package_1 = require("./package");49const publish_1 = require("./publish");50;51class FileStore {52 static async open(path = FileStore.DefaultPath) {53 try {54 const rawStore = await fs.promises.readFile(path, 'utf8');55 return new FileStore(path, JSON.parse(rawStore).publishers);56 }57 catch (err) {58 if (err.code === 'ENOENT') {59 return new FileStore(path, []);60 }61 else if (/SyntaxError/.test(err)) {62 throw new Error(`Error parsing file store: ${path}`);63 }64 throw err;65 }66 }67 get size() {68 return this.publishers.length;69 }70 constructor(path, publishers) {71 this.path = path;72 this.publishers = publishers;73 }74 async save() {75 await fs.promises.writeFile(this.path, JSON.stringify({ publishers: this.publishers }), { mode: '0600' });76 }77 async deleteStore() {78 try {79 await fs.promises.unlink(this.path);80 }81 catch {82 // noop83 }84 }85 get(name) {86 return this.publishers.filter(p => p.name === name)[0];87 }88 async add(publisher) {89 this.publishers = [...this.publishers.filter(p => p.name !== publisher.name), publisher];90 await this.save();91 }92 async delete(name) {93 this.publishers = this.publishers.filter(p => p.name !== name);94 await this.save();95 }96 [Symbol.iterator]() {97 return this.publishers[Symbol.iterator]();98 }99}100exports.FileStore = FileStore;101FileStore.DefaultPath = path.join((0, os_1.homedir)(), '.vsce');102class KeytarStore {103 static async open(serviceName = 'vscode-vsce') {104 const keytar = await import('keytar').then(module => module.default);105 const creds = await keytar.findCredentials(serviceName);106 return new KeytarStore(keytar, serviceName, creds.map(({ account, password }) => ({ name: account, pat: password })));107 }108 get size() {109 return this.publishers.length;110 }111 constructor(keytar, serviceName, publishers) {112 this.keytar = keytar;113 this.serviceName = serviceName;114 this.publishers = publishers;115 }116 get(name) {117 return this.publishers.filter(p => p.name === name)[0];118 }119 async add(publisher) {120 this.publishers = [...this.publishers.filter(p => p.name !== publisher.name), publisher];121 await this.keytar.setPassword(this.serviceName, publisher.name, publisher.pat);122 }123 async delete(name) {124 this.publishers = this.publishers.filter(p => p.name !== name);125 await this.keytar.deletePassword(this.serviceName, name);126 }127 [Symbol.iterator]() {128 return this.publishers[Symbol.iterator]();129 }130}131exports.KeytarStore = KeytarStore;132async function verifyPat(options) {133 const publisherName = options.publisherName ?? (0, validation_1.validatePublisher)((await (0, package_1.readManifest)()).publisher);134 const pat = await (0, publish_1.getPAT)(publisherName, options);135 try {136 // If the caller of the `getRoleAssignments` API has any of the roles137 // (Creator, Owner, Contributor, Reader) on the publisher, we get a 200,138 // otherwise we get a 403.139 const api = await (0, util_1.getSecurityRolesAPI)(pat);140 await api.getRoleAssignments('gallery.publisher', publisherName);141 }142 catch (error) {143 if (error instanceof Error) {144 throw new Error('The Personal Access Token verification has failed. Additional information:\n\n' + error.message);145 }146 throw new Error('The Personal Access Token verification has failed.' + error);147 }148 console.log(`The Personal Access Token verification succeeded for the publisher '${publisherName}'.`);149}150async function requestPAT(publisherName) {151 console.log(`${(0, util_1.getMarketplaceUrl)()}/manage/publishers/`);152 const pat = await (0, util_1.read)(`Personal Access Token for publisher '${publisherName}':`, { silent: true, replace: '*' });153 await verifyPat({ publisherName, pat });154 return pat;155}156async function openDefaultStore() {157 if (/^file$/i.test(process.env['VSCE_STORE'] ?? '')) {158 return await FileStore.open();159 }160 let keytarStore;161 try {162 keytarStore = await KeytarStore.open();163 }164 catch (err) {165 const store = await FileStore.open();166 util_1.log.warn(`Failed to open credential store. Falling back to storing secrets clear-text in: ${store.path}`);167 return store;168 }169 const fileStore = await FileStore.open();170 // migrate from file store171 if (fileStore.size) {172 for (const publisher of fileStore) {173 await keytarStore.add(publisher);174 }175 await fileStore.deleteStore();176 util_1.log.info(`Migrated ${fileStore.size} publishers to system credential manager. Deleted local store '${fileStore.path}'.`);177 }178 return keytarStore;179}180async function getPublisher(publisherName) {181 (0, validation_1.validatePublisher)(publisherName);182 const store = await openDefaultStore();183 let publisher = store.get(publisherName);184 if (publisher) {185 return publisher;186 }187 const pat = await requestPAT(publisherName);188 publisher = { name: publisherName, pat };189 await store.add(publisher);190 return publisher;191}192async function loginPublisher(publisherName) {193 (0, validation_1.validatePublisher)(publisherName);194 const store = await openDefaultStore();195 let publisher = store.get(publisherName);196 if (publisher) {197 console.log(`Publisher '${publisherName}' is already known`);198 const answer = await (0, util_1.read)('Do you want to overwrite its PAT? [y/N] ');199 if (!/^y$/i.test(answer)) {200 throw new Error('Aborted');201 }202 }203 const pat = await requestPAT(publisherName);204 publisher = { name: publisherName, pat };205 await store.add(publisher);206 return publisher;207}208async function logoutPublisher(publisherName) {209 (0, validation_1.validatePublisher)(publisherName);210 const store = await openDefaultStore();211 const publisher = store.get(publisherName);212 if (!publisher) {213 throw new Error(`Unknown publisher '${publisherName}'`);214 }215 await store.delete(publisherName);216}217async function deletePublisher(publisherName) {218 const publisher = await getPublisher(publisherName);219 const answer = await (0, util_1.read)(`This will FOREVER delete '${publisherName}'! Are you sure? [y/N] `);220 if (!/^y$/i.test(answer)) {221 throw new Error('Aborted');222 }223 const api = await (0, util_1.getGalleryAPI)(publisher.pat);224 await api.deletePublisher(publisherName);225 const store = await openDefaultStore();226 await store.delete(publisherName);227 util_1.log.done(`Deleted publisher '${publisherName}'.`);228}229async function listPublishers() {230 const store = await openDefaultStore();231 for (const publisher of store) {232 console.log(publisher.name);233 }234}235//# sourceMappingURL=store.js.map