Skip to content

Commit fe0ea8e

Browse files
committed
Store more information about authorized extensions
Namely: display name, date of authorization, date of last access Signed-off-by: Seb Julliand <[email protected]>
1 parent bb90491 commit fe0ea8e

File tree

3 files changed

+37
-22
lines changed

3 files changed

+37
-22
lines changed

src/api/Storage.ts

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const LAST_PROFILE_KEY = `currentProfile`;
55
const SOURCE_LIST_KEY = `sourceList`;
66
const DEPLOYMENT_KEY = `deployment`;
77
const DEBUG_KEY = `debug`;
8-
const SERVER_SETTINGS_CACHE_KEY = (name : string) => `serverSettingsCache_${name}`;
8+
const SERVER_SETTINGS_CACHE_KEY = (name: string) => `serverSettingsCache_${name}`;
99
const PREVIOUS_SEARCH_TERMS_KEY = `prevSearchTerms`;
1010
const RECENTLY_OPENED_FILES_KEY = `recentlyOpenedFiles`;
1111
const AUTHORISED_EXTENSIONS_KEY = `authorisedExtensions`
@@ -14,6 +14,13 @@ export type PathContent = Record<string, string[]>;
1414
export type DeploymentPath = Record<string, string>;
1515
export type DebugCommands = Record<string, string>;
1616

17+
type AuthorisedExtension = {
18+
id: string
19+
displayName: string
20+
since: number
21+
lastAccess: number
22+
}
23+
1724
abstract class Storage {
1825
protected readonly globalState;
1926

@@ -181,7 +188,7 @@ export class ConnectionStorage extends Storage {
181188
return this.set(DEBUG_KEY, existingCommands);
182189
}
183190

184-
getWorkspaceDeployPath(workspaceFolder : vscode.WorkspaceFolder){
191+
getWorkspaceDeployPath(workspaceFolder: vscode.WorkspaceFolder) {
185192
const deployDirs = this.get<DeploymentPath>(DEPLOYMENT_KEY) || {};
186193
return deployDirs[workspaceFolder.uri.fsPath].toLowerCase();
187194
}
@@ -198,26 +205,37 @@ export class ConnectionStorage extends Storage {
198205
await this.set(RECENTLY_OPENED_FILES_KEY, undefined);
199206
}
200207

201-
async addAuthorizedExtension(extension: string) {
202-
const extensions = this.get<string[]>(AUTHORISED_EXTENSIONS_KEY) || [];
203-
if (!extensions.includes(extension)) {
204-
extensions.push(extension);
208+
async addAuthorisedExtension(extension: vscode.Extension<any>) {
209+
const extensions = this.getAuthorisedExtensions();
210+
if (!this.isExtensionAuthorised(extension)) {
211+
extensions.push({
212+
id: extension.id,
213+
displayName: extension.packageJSON.displayName,
214+
since: new Date().getTime(),
215+
lastAccess: new Date().getTime()
216+
});
205217
await this.set(AUTHORISED_EXTENSIONS_KEY, extensions);
206218
}
207219
}
208220

209-
isExtensionAuthorised(extension: string) {
210-
const extensions = this.get<string[]>(AUTHORISED_EXTENSIONS_KEY) || [];
211-
return extensions.includes(extension);
221+
isExtensionAuthorised(extension: vscode.Extension<any>) {
222+
const authorisedExtension = this.getAuthorisedExtensions().find(authorisedExtension => authorisedExtension.id === extension.id);
223+
if (authorisedExtension) {
224+
authorisedExtension.lastAccess = new Date().getTime();
225+
}
226+
return authorisedExtension !== undefined;
227+
}
228+
229+
getAuthorisedExtensions(): AuthorisedExtension[] {
230+
return this.get<AuthorisedExtension[]>(AUTHORISED_EXTENSIONS_KEY) || [];
212231
}
213232

214-
getAuthorizedExtensions(): string[] {
215-
return this.get<string[]>(AUTHORISED_EXTENSIONS_KEY) || [];
233+
removeAllAuthorisedExtension() {
234+
this.removeAuthorisedExtension(this.getAuthorisedExtensions());
216235
}
217236

218-
removeAuthorizedExtension(extensions: string[]) {
219-
const authorizedExtensions = this.get<string[]>(AUTHORISED_EXTENSIONS_KEY) || [];
220-
const newExtensions = authorizedExtensions.filter(ext => !extensions.includes(ext));
237+
removeAuthorisedExtension(extensions: AuthorisedExtension[]) {
238+
const newExtensions = this.getAuthorisedExtensions().filter(ext => !extensions.includes(ext));
221239
return this.set(AUTHORISED_EXTENSIONS_KEY, newExtensions);
222240
}
223241
}

src/instantiate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ export async function loadAllofExtension(context: vscode.ExtensionContext) {
642642
const storedPassword = await context.secrets.get(connectionKey);
643643

644644
if (storedPassword) {
645-
let isAuthed = storage.isExtensionAuthorised(extensionId);
645+
let isAuthed = storage.isExtensionAuthorised(extension);
646646

647647
if (!isAuthed) {
648648
const detail = `The ${displayName} extension is requesting access to your password for this connection. ${reason ? `\n\nReason: ${reason}` : `The extension did not provide a reason for password access.`}`;
@@ -669,7 +669,7 @@ export async function loadAllofExtension(context: vscode.ExtensionContext) {
669669

670670
switch (result) {
671671
case `Allow`:
672-
await storage.addAuthorizedExtension(extensionId);
672+
await storage.addAuthorisedExtension(extension);
673673
isAuthed = true;
674674
done = true;
675675
break;

src/webviews/settings/index.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class SettingsUI {
3232
vscode.commands.registerCommand(`code-for-ibmi.showAdditionalSettings`, async (server?: Server, tab?: string) => {
3333
const connectionSettings = GlobalConfiguration.get<ConnectionConfiguration.Parameters[]>(`connectionSettings`);
3434
const connection = instance.getConnection();
35-
const passwordAuthorisedExtensions: string[] = instance.getStorage()?.getAuthorizedExtensions() || [];
35+
const passwordAuthorisedExtensions = instance.getStorage()?.getAuthorisedExtensions() || [];
3636

3737
let config: ConnectionConfiguration.Parameters;
3838

@@ -211,7 +211,7 @@ export class SettingsUI {
211211

212212
passwordAuthTab
213213
.addParagraph(`The following extensions are authorized to use the password for this connection.`)
214-
.addParagraph(`<ul>${passwordAuthorisedExtensions.map(ext => `<li>✅ <code>${ext}</code></li>`).join(``)}</ul>`)
214+
.addParagraph(`<ul>${passwordAuthorisedExtensions.map(authExtension => `<li>✅ <code>${authExtension.displayName || authExtension.id}</code> - since ${new Date(authExtension.since).toDateString()} - last access on ${new Date(authExtension.lastAccess).toDateString()}</li>`).join(``)}</ul>`)
215215
.addButtons({ id: `clearAllowedExts`, label: `Clear list` })
216216

217217
tabs.push({ label: `Extension Auth`, fields: passwordAuthTab.fields });
@@ -238,10 +238,7 @@ export class SettingsUI {
238238
break;
239239

240240
case `clearAllowedExts`:
241-
const storage = instance.getStorage();
242-
if (storage) {
243-
storage.removeAuthorizedExtension(storage.getAuthorizedExtensions());
244-
}
241+
instance.getStorage()?.removeAllAuthorisedExtension();
245242
break;
246243

247244
default:

0 commit comments

Comments
 (0)