Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/connectors/in-memory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ISecret, ISecretsConnector } from '../token';
import { ISecret, ISecretsConnector, ISecretsList } from '../token';

/**
* An in memory password connector to store passwords during the session.
Expand All @@ -19,9 +19,7 @@ export class InMemoryConnector implements ISecretsConnector {
this._secrets.delete(id);
}

async list(
query?: string | undefined
): Promise<{ ids: string[]; values: ISecret[] }> {
async list(query?: string | undefined): Promise<ISecretsList> {
const ids: string[] = [];
const values: ISecret[] = [];
this._secrets.forEach((value, key) => {
Expand Down
18 changes: 10 additions & 8 deletions src/connectors/local-storage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ISecret, ISecretsConnector } from '../token';
import { ISecret, ISecretsConnector, ISecretsList } from '../token';

/**
* Example connector that save the secrets to the local storage.
Expand Down Expand Up @@ -36,13 +36,15 @@ passwords are stored as plain text in the local storage of the browser'
localStorage.setItem(this.storage, JSON.stringify(secrets));
}

async list(
query?: string | undefined
): Promise<{ ids: string[]; values: ISecret[] }> {
async list(query?: string | undefined): Promise<ISecretsList> {
const secrets = JSON.parse(localStorage.getItem(this.storage) ?? '{}');
return {
ids: Object.keys(secrets).filter(key => secrets[key].namespace === query),
values: []
};
const initialValue: ISecretsList = { ids: [], values: [] };
return Object.keys(secrets)
.filter(key => secrets[key].namespace === query)
.reduce((acc, cur) => {
acc.ids.push(cur);
acc.values.push(secrets[cur]);
return acc;
}, initialValue);
}
}
13 changes: 9 additions & 4 deletions src/manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { ISecret, ISecretsConnector, ISecretsManager } from './token';
import {
ISecret,
ISecretsConnector,
ISecretsList,
ISecretsManager
} from './token';

export namespace SecretsManager {
export interface IOptions {
Expand All @@ -22,8 +27,8 @@ export class SecretsManager implements ISecretsManager {
this._connector.remove(id);
}

async list(namespace: string): Promise<string[]> {
return (await this._connector.list(namespace)).ids;
async list(namespace: string): Promise<ISecretsList> {
return await this._connector.list(namespace);
}

private _onchange = (e: Event): void => {
Expand Down Expand Up @@ -82,7 +87,7 @@ export class SecretsManager implements ISecretsManager {

async detachAll(namespace: string): Promise<void> {
const attachedIds = await this.list(namespace);
attachedIds.forEach(id => {
attachedIds.ids.forEach(id => {
this.detach(namespace, id);
});
}
Expand Down
6 changes: 5 additions & 1 deletion src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export interface ISecret {
}

export interface ISecretsConnector extends IDataConnector<ISecret> {}
export interface ISecretsList<T = ISecret> {
ids: string[];
values: T[];
}

export const ISecretsConnector = new Token<ISecretsConnector>(
'jupyter-secret-manager:connector',
Expand All @@ -18,7 +22,7 @@ export interface ISecretsManager {
get(id: string): Promise<ISecret | undefined>;
set(id: string, secret: ISecret): Promise<void>;
remove(id: string): Promise<void>;
list(namespace: string): Promise<string[]>;
list(namespace: string): Promise<ISecretsList>;
attach(
namespace: string,
id: string,
Expand Down
Loading