Skip to content

Commit

Permalink
extensions - fully provide keytar API
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Aug 28, 2019
1 parent b05051e commit fa7eba4
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/vs/base/test/node/keytar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ suite('Keytar', () => {
const name = `VSCode Test ${Math.floor(Math.random() * 1e9)}`;
try {
await keytar.setPassword(name, 'foo', 'bar');
assert.equal(await keytar.findPassword(name), 'bar');
assert.equal((await keytar.findCredentials(name)).length, 1);
assert.equal(await keytar.getPassword(name, 'foo'), 'bar');
await keytar.deletePassword(name, 'foo');
assert.equal(await keytar.getPassword(name, 'foo'), undefined);
Expand Down
4 changes: 4 additions & 0 deletions src/vs/workbench/api/browser/mainThreadKeytar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export class MainThreadKeytar implements MainThreadKeytarShape {
return this._credentialsService.findPassword(service);
}

async $findCredentials(service: string): Promise<Array<{ account: string, password: string }>> {
return this._credentialsService.findCredentials(service);
}

dispose(): void {
//
}
Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/api/common/extHost.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ export interface MainThreadKeytarShape extends IDisposable {
$setPassword(service: string, account: string, password: string): Promise<void>;
$deletePassword(service: string, account: string): Promise<boolean>;
$findPassword(service: string): Promise<string | null>;
$findCredentials(service: string): Promise<Array<{ account: string, password: string }>>;
}

export interface IRegExpDto {
Expand Down
4 changes: 4 additions & 0 deletions src/vs/workbench/api/common/extHostRequireInterceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ interface IKeytarModule {
setPassword(service: string, account: string, password: string): Promise<void>;
deletePassword(service: string, account: string): Promise<boolean>;
findPassword(service: string): Promise<string | null>;
findCredentials(service: string): Promise<Array<{ account: string, password: string }>>;
}

class KeytarNodeModuleFactory implements INodeModuleFactory {
Expand Down Expand Up @@ -169,6 +170,9 @@ class KeytarNodeModuleFactory implements INodeModuleFactory {
},
findPassword: (service: string): Promise<string | null> => {
return mainThreadKeytar.$findPassword(service);
},
findCredentials(service: string): Promise<Array<{ account: string, password: string }>> {
return mainThreadKeytar.$findCredentials(service);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface ICredentialsProvider {
setPassword(service: string, account: string, password: string): Promise<void>;
deletePassword(service: string, account: string): Promise<boolean>;
findPassword(service: string): Promise<string | null>;
findCredentials(service: string): Promise<Array<{ account: string, password: string }>>;
}

export class BrowserCredentialsService implements ICredentialsService {
Expand All @@ -29,21 +30,25 @@ export class BrowserCredentialsService implements ICredentialsService {
}
}

async getPassword(service: string, account: string): Promise<string | null> {
getPassword(service: string, account: string): Promise<string | null> {
return this.credentialsProvider.getPassword(service, account);
}

async setPassword(service: string, account: string, password: string): Promise<void> {
setPassword(service: string, account: string, password: string): Promise<void> {
return this.credentialsProvider.setPassword(service, account, password);
}

async deletePassword(service: string, account: string): Promise<boolean> {
deletePassword(service: string, account: string): Promise<boolean> {
return this.credentialsProvider.deletePassword(service, account);
}

async findPassword(service: string): Promise<string | null> {
findPassword(service: string): Promise<string | null> {
return this.credentialsProvider.findPassword(service);
}

findCredentials(service: string): Promise<Array<{ account: string, password: string }>> {
return this.credentialsProvider.findCredentials(service);
}
}

interface ICredential {
Expand Down Expand Up @@ -127,6 +132,12 @@ class LocalStorageCredentialsProvider implements ICredentialsProvider {
async findPassword(service: string): Promise<string | null> {
return this.doGetPassword(service);
}

async findCredentials(service: string): Promise<Array<{ account: string, password: string }>> {
return this.credentials
.filter(credential => credential.service === service)
.map(({ account, password }) => ({ account, password }));
}
}

registerSingleton(ICredentialsService, BrowserCredentialsService, true);
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export interface ICredentialsService {
setPassword(service: string, account: string, password: string): Promise<void>;
deletePassword(service: string, account: string): Promise<boolean>;
findPassword(service: string): Promise<string | null>;
findCredentials(service: string): Promise<Array<{ account: string, password: string }>>;
}
13 changes: 6 additions & 7 deletions src/vs/workbench/services/credentials/node/credentialsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ import { IdleValue } from 'vs/base/common/async';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { ServiceIdentifier } from 'vs/platform/instantiation/common/instantiation';

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>;
};

type KeytarModule = typeof import('keytar');
export class KeytarCredentialsService implements ICredentialsService {

_serviceBrand!: ServiceIdentifier<any>;
Expand All @@ -40,6 +34,11 @@ export class KeytarCredentialsService implements ICredentialsService {
const keytar = await this._keytar.getValue();
return keytar.findPassword(service);
}

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

registerSingleton(ICredentialsService, KeytarCredentialsService, true);

0 comments on commit fa7eba4

Please sign in to comment.