Skip to content

Commit 5f91328

Browse files
committed
fix layer breaker with credentials service, microsoft#74997
1 parent 7b321a1 commit 5f91328

4 files changed

Lines changed: 74 additions & 24 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
7+
8+
export const ICredentialsService = createDecorator<ICredentialsService>('ICredentialsService');
9+
10+
export interface ICredentialsService {
11+
_serviceBrand: any;
12+
getPassword(service: string, account: string): Promise<string | null>;
13+
setPassword(service: string, account: string, password: string): Promise<void>;
14+
deletePassword(service: string, account: string): Promise<boolean>;
15+
findPassword(service: string): Promise<string | null>;
16+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import { ICredentialsService } from 'vs/platform/credentials/common/credentials';
7+
import { IdleValue } from 'vs/base/common/async';
8+
9+
type KeytarModule = {
10+
getPassword(service: string, account: string): Promise<string | null>;
11+
setPassword(service: string, account: string, password: string): Promise<void>;
12+
deletePassword(service: string, account: string): Promise<boolean>;
13+
findPassword(service: string): Promise<string | null>;
14+
};
15+
16+
export class KeytarCredentialsService implements ICredentialsService {
17+
18+
_serviceBrand: any;
19+
20+
private readonly _keytar = new IdleValue<Promise<KeytarModule>>(() => import('keytar'));
21+
22+
async getPassword(service: string, account: string): Promise<string | null> {
23+
const keytar = await this._keytar.getValue();
24+
return keytar.getPassword(service, account);
25+
}
26+
27+
async setPassword(service: string, account: string, password: string): Promise<void> {
28+
const keytar = await this._keytar.getValue();
29+
return keytar.setPassword(service, account, password);
30+
}
31+
32+
async deletePassword(service: string, account: string): Promise<boolean> {
33+
const keytar = await this._keytar.getValue();
34+
return keytar.deletePassword(service, account);
35+
}
36+
37+
async findPassword(service: string): Promise<string | null> {
38+
const keytar = await this._keytar.getValue();
39+
return keytar.findPassword(service);
40+
}
41+
}

src/vs/workbench/api/browser/mainThreadKeytar.ts

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,58 +5,48 @@
55

66
import { extHostNamedCustomer } from 'vs/workbench/api/common/extHostCustomers';
77
import { MainContext, MainThreadKeytarShape, IExtHostContext } from 'vs/workbench/api/common/extHost.protocol';
8-
9-
interface IKeytarModule {
10-
getPassword(service: string, account: string): Promise<string | null>;
11-
setPassword(service: string, account: string, password: string): Promise<void>;
12-
deletePassword(service: string, account: string): Promise<boolean>;
13-
findPassword(service: string): Promise<string | null>;
14-
}
8+
import { ICredentialsService } from 'vs/platform/credentials/common/credentials';
9+
import { optional } from 'vs/platform/instantiation/common/instantiation';
1510

1611
@extHostNamedCustomer(MainContext.MainThreadKeytar)
1712
export class MainThreadKeytar implements MainThreadKeytarShape {
1813

19-
private _keytar: Promise<IKeytarModule | null>;
14+
private readonly _credentialsService?: ICredentialsService;
2015

2116
constructor(
22-
extHostContext: IExtHostContext
17+
_extHostContext: IExtHostContext,
18+
@optional(ICredentialsService) credentialsService: ICredentialsService,
2319
) {
24-
// tslint:disable-next-line:import-patterns
25-
this._keytar = import('keytar')
26-
.catch(e => null);
20+
this._credentialsService = credentialsService;
2721
}
2822

2923
dispose(): void {
3024
//
3125
}
3226

3327
async $getPassword(service: string, account: string): Promise<string | null> {
34-
const keytar = await this._keytar;
35-
if (keytar) {
36-
return keytar.getPassword(service, account);
28+
if (this._credentialsService) {
29+
return this._credentialsService.getPassword(service, account);
3730
}
3831
return null;
3932
}
4033

4134
async $setPassword(service: string, account: string, password: string): Promise<void> {
42-
const keytar = await this._keytar;
43-
if (keytar) {
44-
return keytar.setPassword(service, account, password);
35+
if (this._credentialsService) {
36+
return this._credentialsService.setPassword(service, account, password);
4537
}
4638
}
4739

4840
async $deletePassword(service: string, account: string): Promise<boolean> {
49-
const keytar = await this._keytar;
50-
if (keytar) {
51-
return keytar.deletePassword(service, account);
41+
if (this._credentialsService) {
42+
return this._credentialsService.deletePassword(service, account);
5243
}
5344
return false;
5445
}
5546

5647
async $findPassword(service: string): Promise<string | null> {
57-
const keytar = await this._keytar;
58-
if (keytar) {
59-
return keytar.findPassword(service);
48+
if (this._credentialsService) {
49+
return this._credentialsService.findPassword(service);
6050
}
6151
return null;
6252
}

src/vs/workbench/workbench.main.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ registerSingleton(IMenubarService, MenubarService);
162162
registerSingleton(IURLService, RelayURLService);
163163
registerSingleton(ITunnelService, TunnelService, true);
164164
registerSingleton(IConfigurationResolverService, ConfigurationResolverService, true);
165+
registerSingleton(ICredentialsService, KeytarCredentialsService, true);
165166

166167
//#endregion
167168

@@ -326,5 +327,7 @@ import 'vs/workbench/contrib/experiments/electron-browser/experiments.contributi
326327

327328
// Issues
328329
import 'vs/workbench/contrib/issue/electron-browser/issue.contribution';
330+
import { ICredentialsService } from 'vs/platform/credentials/common/credentials';
331+
import { KeytarCredentialsService } from 'vs/platform/credentials/node/credentialsService';
329332

330333
//#endregion

0 commit comments

Comments
 (0)