mirror of
https://github.com/microsoft/vscode.git
synced 2026-09-29 18:09:01 +01:00
extensions - fully provide keytar API
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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 {
|
||||
//
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
@@ -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);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
@@ -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 {
|
||||
@@ -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);
|
||||
|
||||
@@ -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 }>>;
|
||||
}
|
||||
|
||||
@@ -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>;
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user