Skip to content

Commit

Permalink
Merge pull request #1551 from halcyon-tech/fix/connectionSettingsCleanup
Browse files Browse the repository at this point in the history
Fix and cleanup login settings
  • Loading branch information
sebjulliand committed Sep 26, 2023
2 parents 7e1d381 + 9f6efd7 commit 77eef4c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 23 deletions.
26 changes: 21 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,34 @@ export async function activate(context: ExtensionContext): Promise<CodeForIBMi>
]);
});

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<ConnectionData[]>(`connections`) || []);
let update = false;
for(const connection of connections){
if('privateKey' in connection && connection[`privateKey`]){
connection.privateKeyPath = connection[`privateKey`] as string;
connection[`privateKey`] = undefined;
//privateKey was used to hold privateKeyPath
if('privateKey' in connection){
const privateKey = connection["privateKey"] as string;
if(privateKey){
connection.privateKeyPath = privateKey;
}
delete connection["privateKey"];
update = true;
}

//An empty privateKeyPath will crash the connection
if(!connection.privateKeyPath?.trim()) {
connection.privateKeyPath = undefined;
update = true;
}

//buttons were added by the login settings page
if(`buttons` in connection) {
delete connection["buttons"];
update = true;
}
}
Expand Down
21 changes: 14 additions & 7 deletions src/webviews/login/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

/**
Expand All @@ -20,10 +25,10 @@ export class Login {
const existingConnections = GlobalConfiguration.get<ConnectionData[]>(`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`)
Expand All @@ -32,21 +37,21 @@ export class Login {
{ id: `connect`, label: `Connect`, requiresValidation: true },
{ id: `saveExit`, label: `Save & Exit` }
)
.loadPage<any>(`IBM i Login`);
.loadPage<NewLoginSettings>(`IBM i Login`);

if (page && page.data) {
const data = page.data;
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);

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({
Expand All @@ -57,7 +62,9 @@ export class Login {
privateKeyPath: data.privateKeyPath
});

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`);
Expand Down
30 changes: 19 additions & 11 deletions src/webviews/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down Expand Up @@ -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<any>(`Login Settings: ${name}`);
.loadPage<LoginSettings>(`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);
}
}
Expand Down

0 comments on commit 77eef4c

Please sign in to comment.