From 39d7c4227bf6acf7c73e9721e7f81858c4dfe682 Mon Sep 17 00:00:00 2001 From: Seb Julliand Date: Tue, 26 Sep 2023 16:12:00 +0200 Subject: [PATCH 1/5] Completly cleanup & fix Login settings --- src/extension.ts | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 217567ac1..c84f2bda0 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -113,18 +113,31 @@ export async function activate(context: ExtensionContext): Promise ]); }); - await fixPrivateKeys(); + await fixLoginSettings(); return { instance, customUI: () => new CustomUI(), deployTools: DeployTools, evfeventParser: parseErrors, tools: Tools }; } -async function fixPrivateKeys(){ +async function fixLoginSettings(){ const connections = (GlobalConfiguration.get(`connections`) || []); let update = false; for(const connection of connections){ - if('privateKey' in connection && connection[`privateKey`]){ - connection.privateKeyPath = connection[`privateKey`] as string; - connection[`privateKey`] = undefined; + if('privateKey' in connection){ + const privateKey = connection.privateKey as string; + if(privateKey){ + connection.privateKeyPath = privateKey; + } + connection.privateKey = undefined; + update = true; + } + + if(!connection.privateKeyPath || !connection.privateKeyPath.trim()) { + connection.privateKeyPath = undefined; + update = true; + } + + if(`buttons` in connection) { + connection.buttons = undefined; update = true; } } From bd68be23e37b28685b9629efdbab50b9def8f63a Mon Sep 17 00:00:00 2001 From: Seb Julliand Date: Tue, 26 Sep 2023 16:13:05 +0200 Subject: [PATCH 2/5] Added comments --- src/extension.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/extension.ts b/src/extension.ts index c84f2bda0..b3d20f2b7 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -122,6 +122,7 @@ async function fixLoginSettings(){ const connections = (GlobalConfiguration.get(`connections`) || []); let update = false; for(const connection of connections){ + //privateKey was used to hold privateKeyPath if('privateKey' in connection){ const privateKey = connection.privateKey as string; if(privateKey){ @@ -131,11 +132,13 @@ async function fixLoginSettings(){ update = true; } + //An empty privateKeyPath will crash the connection if(!connection.privateKeyPath || !connection.privateKeyPath.trim()) { connection.privateKeyPath = undefined; update = true; } + //buttons were added by the login settings page if(`buttons` in connection) { connection.buttons = undefined; update = true; From 736e7c0232bf4a854986b2663b10d6919489fd30 Mon Sep 17 00:00:00 2001 From: Seb Julliand Date: Tue, 26 Sep 2023 16:49:42 +0200 Subject: [PATCH 3/5] Fixed login data assignments - Remove unused fields - Ensure privateKeyPath is undefined and never '' when unused --- src/extension.ts | 2 +- src/webviews/login/index.ts | 21 ++++++++++++++------- src/webviews/settings/index.ts | 30 +++++++++++++++++++----------- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index b3d20f2b7..59fb90447 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -133,7 +133,7 @@ async function fixLoginSettings(){ } //An empty privateKeyPath will crash the connection - if(!connection.privateKeyPath || !connection.privateKeyPath.trim()) { + if(!connection.privateKeyPath?.trim()) { connection.privateKeyPath = undefined; update = true; } diff --git a/src/webviews/login/index.ts b/src/webviews/login/index.ts index b6253de70..20bf920ac 100644 --- a/src/webviews/login/index.ts +++ b/src/webviews/login/index.ts @@ -5,6 +5,11 @@ import IBMi from "../../api/IBMi"; import { disconnect, instance } from "../../instantiate"; import { ConnectionData } from '../../typings'; +type NewLoginSettings = ConnectionData & { + savePassword: boolean + buttons: 'saveExit' | 'connect' +} + export class Login { /** @@ -20,10 +25,10 @@ export class Login { const existingConnections = GlobalConfiguration.get(`connections`) || []; const page = await new CustomUI() - .addInput(`name`, `Connection Name`, undefined, {minlength: 1}) - .addInput(`host`, `Host or IP Address`, undefined, {minlength: 1}) + .addInput(`name`, `Connection Name`, undefined, { minlength: 1 }) + .addInput(`host`, `Host or IP Address`, undefined, { minlength: 1 }) .addInput(`port`, `Port (SSH)`, ``, { default: `22`, minlength: 1, maxlength: 5, regexTest: `^\\d+$` }) - .addInput(`username`, `Username`, undefined, {minlength: 1, maxlength: 10}) + .addInput(`username`, `Username`, undefined, { minlength: 1, maxlength: 10 }) .addParagraph(`Only provide either the password or a private key - not both.`) .addPassword(`password`, `Password`) .addCheckbox(`savePassword`, `Save Password`) @@ -32,7 +37,7 @@ export class Login { { id: `connect`, label: `Connect`, requiresValidation: true }, { id: `saveExit`, label: `Save & Exit` } ) - .loadPage(`IBM i Login`); + .loadPage(`IBM i Login`); if (page && page.data) { const data = page.data; @@ -46,7 +51,7 @@ export class Login { if (existingConnection) { vscode.window.showErrorMessage(`Connection with name ${data.name} already exists.`); } else { - let newConnection = (!existingConnections.some(item => item.name === data.name)); + const newConnection = (!existingConnections.some(item => item.name === data.name)); if (newConnection) { // New connection! existingConnections.push({ @@ -54,10 +59,12 @@ export class Login { host: data.host, port: data.port, username: data.username, - privateKeyPath: data.privateKeyPath + privateKeyPath: data.privateKeyPath ? data.privateKeyPath : undefined }); - if (data.savePassword) context.secrets.store(`${data.name}_password`, `${data.password}`); + if (data.savePassword) { + context.secrets.store(`${data.name}_password`, `${data.password}`); + } await GlobalConfiguration.set(`connections`, existingConnections); vscode.commands.executeCommand(`code-for-ibmi.refreshConnections`); diff --git a/src/webviews/settings/index.ts b/src/webviews/settings/index.ts index 74eb0c10f..74c3adbf2 100644 --- a/src/webviews/settings/index.ts +++ b/src/webviews/settings/index.ts @@ -18,6 +18,10 @@ const TERMINAL_TYPES = [ { key: `IBM-5292-2`, text: `IBM-5292-2 (24x80 color)` }, ]; +type LoginSettings = ConnectionData & { + buttons?: 'submitButton' +} + export class SettingsUI { static init(context: vscode.ExtensionContext) { @@ -230,29 +234,33 @@ export class SettingsUI { .addPassword(`password`, `Password`, `Only provide a password if you want to update an existing one or set a new one.`) .addFile(`privateKeyPath`, `Private Key${connection.privateKeyPath ? ` (current: ${connection.privateKeyPath})` : ``}`, `Only provide a private key if you want to update from the existing one or set one. OpenSSH, RFC4716, or PPK formats are supported.`) .addButtons({ id: `submitButton`, label: `Save`, requiresValidation: true }) - .loadPage(`Login Settings: ${name}`); + .loadPage(`Login Settings: ${name}`); if (page && page.data) { page.panel.dispose(); const data = page.data; - data.port = Number(data.port); - if (data.privateKeyPath === ``) data.privateKeyPath = connection.privateKeyPath; + if (!data.privateKeyPath?.trim()) { + if (connection.privateKeyPath?.trim()) { + data.privateKeyPath = connection.privateKeyPath; + } + else { + delete data.privateKeyPath; + } + } if (data.password && !data.privateKeyPath) { context.secrets.delete(`${name}_password`); context.secrets.store(`${name}_password`, `${data.password}`); - data.privateKeyPath = ``; - }; + delete data.privateKeyPath; + } + //Fix values before assigning the data + data.port = Number(data.port); delete data.password; + delete data.buttons; - connection = { - ...connection, - ...data - }; - - connections[connectionIdx] = connection; + connections[connectionIdx] = Object.assign(connection, data); await GlobalConfiguration.set(`connections`, connections); } } From 4d4423edfa3bcff3021c85e4e0e0f9c0e263cf03 Mon Sep 17 00:00:00 2001 From: Seb Julliand Date: Tue, 26 Sep 2023 16:54:08 +0200 Subject: [PATCH 4/5] privateKeyPath = '' if connecting from new connection page --- src/webviews/login/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/webviews/login/index.ts b/src/webviews/login/index.ts index 20bf920ac..5d16a582b 100644 --- a/src/webviews/login/index.ts +++ b/src/webviews/login/index.ts @@ -44,7 +44,7 @@ export class Login { page.panel.dispose(); data.port = Number(data.port); - + data.privateKeyPath = data.privateKeyPath?.trim() ? data.privateKeyPath : undefined; if (data.name) { const existingConnection = existingConnections.find(item => item.name === data.name); @@ -59,7 +59,7 @@ export class Login { host: data.host, port: data.port, username: data.username, - privateKeyPath: data.privateKeyPath ? data.privateKeyPath : undefined + privateKeyPath: data.privateKeyPath }); if (data.savePassword) { From 9f6efd7e355d4ec58a2958afc8523fb0c3b44a5d Mon Sep 17 00:00:00 2001 From: Seb Julliand Date: Tue, 26 Sep 2023 19:43:01 +0200 Subject: [PATCH 5/5] Fixed types generation --- src/extension.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 59fb90447..cb1ad02e5 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -124,11 +124,11 @@ async function fixLoginSettings(){ for(const connection of connections){ //privateKey was used to hold privateKeyPath if('privateKey' in connection){ - const privateKey = connection.privateKey as string; + const privateKey = connection["privateKey"] as string; if(privateKey){ connection.privateKeyPath = privateKey; } - connection.privateKey = undefined; + delete connection["privateKey"]; update = true; } @@ -140,7 +140,7 @@ async function fixLoginSettings(){ //buttons were added by the login settings page if(`buttons` in connection) { - connection.buttons = undefined; + delete connection["buttons"]; update = true; } }