Skip to content

Commit

Permalink
fix layer breaker with credentials service, #74997
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Jun 7, 2019
1 parent 7b321a1 commit 5f91328
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 24 deletions.
16 changes: 16 additions & 0 deletions src/vs/platform/credentials/common/credentials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { createDecorator } from 'vs/platform/instantiation/common/instantiation';

export const ICredentialsService = createDecorator<ICredentialsService>('ICredentialsService');

export interface ICredentialsService {
_serviceBrand: any;
getPassword(service: string, account: string): Promise<string | null>;
setPassword(service: string, account: string, password: string): Promise<void>;
deletePassword(service: string, account: string): Promise<boolean>;
findPassword(service: string): Promise<string | null>;
}
41 changes: 41 additions & 0 deletions src/vs/platform/credentials/node/credentialsService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { ICredentialsService } from 'vs/platform/credentials/common/credentials';
import { IdleValue } from 'vs/base/common/async';

type KeytarModule = {
getPassword(service: string, account: string): Promise<string | null>;
setPassword(service: string, account: string, password: string): Promise<void>;
deletePassword(service: string, account: string): Promise<boolean>;
findPassword(service: string): Promise<string | null>;
};

export class KeytarCredentialsService implements ICredentialsService {

_serviceBrand: any;

private readonly _keytar = new IdleValue<Promise<KeytarModule>>(() => import('keytar'));

async getPassword(service: string, account: string): Promise<string | null> {
const keytar = await this._keytar.getValue();
return keytar.getPassword(service, account);
}

async setPassword(service: string, account: string, password: string): Promise<void> {
const keytar = await this._keytar.getValue();
return keytar.setPassword(service, account, password);
}

async deletePassword(service: string, account: string): Promise<boolean> {
const keytar = await this._keytar.getValue();
return keytar.deletePassword(service, account);
}

async findPassword(service: string): Promise<string | null> {
const keytar = await this._keytar.getValue();
return keytar.findPassword(service);
}
}
38 changes: 14 additions & 24 deletions src/vs/workbench/api/browser/mainThreadKeytar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,48 @@

import { extHostNamedCustomer } from 'vs/workbench/api/common/extHostCustomers';
import { MainContext, MainThreadKeytarShape, IExtHostContext } from 'vs/workbench/api/common/extHost.protocol';

interface IKeytarModule {
getPassword(service: string, account: string): Promise<string | null>;
setPassword(service: string, account: string, password: string): Promise<void>;
deletePassword(service: string, account: string): Promise<boolean>;
findPassword(service: string): Promise<string | null>;
}
import { ICredentialsService } from 'vs/platform/credentials/common/credentials';
import { optional } from 'vs/platform/instantiation/common/instantiation';

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

private _keytar: Promise<IKeytarModule | null>;
private readonly _credentialsService?: ICredentialsService;

constructor(
extHostContext: IExtHostContext
_extHostContext: IExtHostContext,
@optional(ICredentialsService) credentialsService: ICredentialsService,
) {
// tslint:disable-next-line:import-patterns
this._keytar = import('keytar')
.catch(e => null);
this._credentialsService = credentialsService;
}

dispose(): void {
//
}

async $getPassword(service: string, account: string): Promise<string | null> {
const keytar = await this._keytar;
if (keytar) {
return keytar.getPassword(service, account);
if (this._credentialsService) {
return this._credentialsService.getPassword(service, account);
}
return null;
}

async $setPassword(service: string, account: string, password: string): Promise<void> {
const keytar = await this._keytar;
if (keytar) {
return keytar.setPassword(service, account, password);
if (this._credentialsService) {
return this._credentialsService.setPassword(service, account, password);
}
}

async $deletePassword(service: string, account: string): Promise<boolean> {
const keytar = await this._keytar;
if (keytar) {
return keytar.deletePassword(service, account);
if (this._credentialsService) {
return this._credentialsService.deletePassword(service, account);
}
return false;
}

async $findPassword(service: string): Promise<string | null> {
const keytar = await this._keytar;
if (keytar) {
return keytar.findPassword(service);
if (this._credentialsService) {
return this._credentialsService.findPassword(service);
}
return null;
}
Expand Down
3 changes: 3 additions & 0 deletions src/vs/workbench/workbench.main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ registerSingleton(IMenubarService, MenubarService);
registerSingleton(IURLService, RelayURLService);
registerSingleton(ITunnelService, TunnelService, true);
registerSingleton(IConfigurationResolverService, ConfigurationResolverService, true);
registerSingleton(ICredentialsService, KeytarCredentialsService, true);

//#endregion

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

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

//#endregion

0 comments on commit 5f91328

Please sign in to comment.