|
1 | 1 | import * as vscode from "vscode"; |
2 | 2 | import { getServerNames } from "../api/getServerNames"; |
3 | 3 | import { credentialCache } from "../api/getServerSpec"; |
| 4 | +import { ServerManagerAuthenticationProvider } from "../authenticationProvider"; |
4 | 5 | import { extensionId } from "../extension"; |
5 | 6 | import { Keychain } from "../keychain"; |
6 | 7 | import { ServerTreeItem } from "../ui/serverManagerView"; |
@@ -60,6 +61,57 @@ export async function clearPassword(treeItem?: ServerTreeItem): Promise<string> |
60 | 61 | return reply; |
61 | 62 | } |
62 | 63 |
|
| 64 | +export async function migratePasswords(secretStorage: vscode.SecretStorage): Promise<void> { |
| 65 | + const credentials = await Keychain.findCredentials(); |
| 66 | + console.log(credentials); |
| 67 | + if (credentials.length === 0) { |
| 68 | + vscode.window.showInformationMessage('No legacy passwords found'); |
| 69 | + } else { |
| 70 | + |
| 71 | + // Collect only those for which server definition exists with a username |
| 72 | + // and no credentials yet stored in our SecretStorage |
| 73 | + const migratableCredentials = (await Promise.all( |
| 74 | + credentials.map(async (item) => { |
| 75 | + const serverName = item.account; |
| 76 | + const username: string | undefined = vscode.workspace.getConfiguration("intersystems.servers." + serverName).get("username"); |
| 77 | + if (!username) { |
| 78 | + return undefined; |
| 79 | + } |
| 80 | + if (username === "" || username === "UnknownUser") { |
| 81 | + return undefined; |
| 82 | + } |
| 83 | + const sessionId = ServerManagerAuthenticationProvider.sessionId(serverName, username); |
| 84 | + const credentialKey = ServerManagerAuthenticationProvider.credentialKey(sessionId); |
| 85 | + return (await secretStorage.get(credentialKey) ? {...item, username} : undefined); |
| 86 | + }) |
| 87 | + )) |
| 88 | + .filter((item) => item); |
| 89 | + if (migratableCredentials.length === 0) { |
| 90 | + vscode.window.showInformationMessage('No legacy passwords found for servers whose definitions specify a username'); |
| 91 | + } else { |
| 92 | + const disqualified = credentials.length - migratableCredentials.length; |
| 93 | + const detail = disqualified > 0 ? `${disqualified} other ${disqualified > 1 ? "passwords" : "password"} ignored because associated server is no longer defined, or has no username set, or already has a password in the new keystore.` : ""; |
| 94 | + const message = `Migrate ${migratableCredentials.length} legacy stored ${migratableCredentials.length > 1 ? "passwords" : "password"}?`; |
| 95 | + switch (await vscode.window.showInformationMessage(message, {modal: true, detail}, "Yes", "No")) { |
| 96 | + case undefined: |
| 97 | + return; |
| 98 | + |
| 99 | + case "Yes": |
| 100 | + vscode.window.showInformationMessage('TODO migration'); |
| 101 | + break; |
| 102 | + |
| 103 | + default: |
| 104 | + break; |
| 105 | + } |
| 106 | + } |
| 107 | + const detail = "Do this to tidy up your keystore once you have migrated passwords and will not be reverting to an earlier Server Manager."; |
| 108 | + if (await vscode.window.showInformationMessage(`Delete all legacy stored passwords?`, {modal: true, detail}, "Yes", "No") === "Yes") { |
| 109 | + vscode.window.showInformationMessage('TODO deletion'); |
| 110 | + } |
| 111 | +} |
| 112 | + return; |
| 113 | +} |
| 114 | + |
63 | 115 | async function commonPickServer(options?: vscode.QuickPickOptions): Promise<string | undefined> { |
64 | 116 | // Deliberately uses its own API to illustrate how other extensions would |
65 | 117 | const serverManagerExtension = vscode.extensions.getExtension(extensionId); |
|
0 commit comments