web - move selfhost pieces out of workbench

This commit is contained in:
Benjamin Pasero
2019-09-10 11:06:49 +02:00
parent 0030e6ee92
commit d0481dca92
8 changed files with 343 additions and 212 deletions

View File

@@ -25,7 +25,7 @@ export class BrowserCredentialsService implements ICredentialsService {
if (environmentService.options && environmentService.options.credentialsProvider) {
this.credentialsProvider = environmentService.options.credentialsProvider;
} else {
this.credentialsProvider = new LocalStorageCredentialsProvider();
this.credentialsProvider = new InMemoryCredentialsProvider();
}
}
@@ -56,80 +56,44 @@ interface ICredential {
password: string;
}
class LocalStorageCredentialsProvider implements ICredentialsProvider {
class InMemoryCredentialsProvider implements ICredentialsProvider {
static readonly CREDENTIALS_OPENED_KEY = 'credentials.provider';
private _credentials: ICredential[] | undefined;
private get credentials(): ICredential[] {
if (!this._credentials) {
try {
const serializedCredentials = window.localStorage.getItem(LocalStorageCredentialsProvider.CREDENTIALS_OPENED_KEY);
if (serializedCredentials) {
this._credentials = JSON.parse(serializedCredentials);
}
} catch (error) {
// ignore
}
if (!Array.isArray(this._credentials)) {
this._credentials = [];
}
}
return this._credentials;
}
private save(): void {
window.localStorage.setItem(LocalStorageCredentialsProvider.CREDENTIALS_OPENED_KEY, JSON.stringify(this.credentials));
}
private credentials: ICredential[] = [];
async getPassword(service: string, account: string): Promise<string | null> {
return this.doGetPassword(service, account);
}
const credential = this.doFindPassword(service, account);
private async doGetPassword(service: string, account?: string): Promise<string | null> {
for (const credential of this.credentials) {
if (credential.service === service) {
if (typeof account !== 'string' || account === credential.account) {
return credential.password;
}
}
}
return null;
return credential ? credential.password : null;
}
async setPassword(service: string, account: string, password: string): Promise<void> {
this.deletePassword(service, account);
this.credentials.push({ service, account, password });
this.save();
}
async deletePassword(service: string, account: string): Promise<boolean> {
let found = false;
this._credentials = this.credentials.filter(credential => {
if (credential.service === service && credential.account === account) {
found = true;
return false;
}
return true;
});
if (found) {
this.save();
const credential = this.doFindPassword(service, account);
if (credential) {
this.credentials = this.credentials.splice(this.credentials.indexOf(credential), 1);
}
return found;
return !!credential;
}
async findPassword(service: string): Promise<string | null> {
return this.doGetPassword(service);
const credential = this.doFindPassword(service);
return credential ? credential.password : null;
}
private doFindPassword(service: string, account?: string): ICredential | null {
for (const credential of this.credentials) {
if (credential.service === service && (typeof account !== 'string' || credential.account === account)) {
return credential;
}
}
return null;
}
async findCredentials(service: string): Promise<Array<{ account: string, password: string }>> {