refactor credentialsmainservice and ensure the right one is used (#141100)

This commit is contained in:
Tyler James Leonhardt
2022-01-20 13:22:03 -08:00
committed by GitHub
parent c82dc2245b
commit 33040b9696
11 changed files with 77 additions and 51 deletions

View File

@@ -4,7 +4,6 @@
*--------------------------------------------------------------------------------------------*/
import { Disposable } from 'vs/base/common/lifecycle';
import { IProductService } from 'vs/platform/product/common/productService';
import { extHostNamedCustomer } from 'vs/workbench/api/common/extHostCustomers';
import { ICredentialsService } from 'vs/platform/credentials/common/credentials';
import { IEncryptionService } from 'vs/workbench/services/encryption/common/encryptionService';
@@ -14,27 +13,28 @@ import { ExtHostContext, ExtHostSecretStateShape, IExtHostContext, MainContext,
export class MainThreadSecretState extends Disposable implements MainThreadSecretStateShape {
private readonly _proxy: ExtHostSecretStateShape;
private secretStoragePrefix = this.credentialsService.getSecretStoragePrefix();
constructor(
extHostContext: IExtHostContext,
@ICredentialsService private readonly credentialsService: ICredentialsService,
@IEncryptionService private readonly encryptionService: IEncryptionService,
@IProductService private readonly productService: IProductService
) {
super();
this._proxy = extHostContext.getProxy(ExtHostContext.ExtHostSecretState);
this._register(this.credentialsService.onDidChangePassword(e => {
const extensionId = e.service.substring(this.productService.urlProtocol.length);
this._register(this.credentialsService.onDidChangePassword(async e => {
const extensionId = e.service.substring((await this.secretStoragePrefix).length);
this._proxy.$onDidChangePassword({ extensionId, key: e.account });
}));
}
private getFullKey(extensionId: string): string {
return `${this.productService.urlProtocol}${extensionId}`;
private async getFullKey(extensionId: string): Promise<string> {
return `${await this.secretStoragePrefix}${extensionId}`;
}
async $getPassword(extensionId: string, key: string): Promise<string | undefined> {
const fullKey = this.getFullKey(extensionId);
const fullKey = await this.getFullKey(extensionId);
const password = await this.credentialsService.getPassword(fullKey, key);
const decrypted = password && await this.encryptionService.decrypt(password);
@@ -53,7 +53,7 @@ export class MainThreadSecretState extends Disposable implements MainThreadSecre
}
async $setPassword(extensionId: string, key: string, value: string): Promise<void> {
const fullKey = this.getFullKey(extensionId);
const fullKey = await this.getFullKey(extensionId);
const toEncrypt = JSON.stringify({
extensionId,
content: value
@@ -64,7 +64,7 @@ export class MainThreadSecretState extends Disposable implements MainThreadSecre
async $deletePassword(extensionId: string, key: string): Promise<void> {
try {
const fullKey = this.getFullKey(extensionId);
const fullKey = await this.getFullKey(extensionId);
await this.credentialsService.deletePassword(fullKey, key);
} catch (_) {
throw new Error('Cannot delete password');