Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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, ISecretsConnectorList } 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<ISecretsConnectorList> {
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, ISecretsConnectorList } 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<ISecretsConnectorList> {
const secrets = JSON.parse(localStorage.getItem(this.storage) ?? '{}');
return {
ids: Object.keys(secrets).filter(key => secrets[key].namespace === query),
values: []
};
const initialValue: ISecretsConnectorList = { 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,
ISecretsConnectorList,
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<ISecretsConnectorList> {
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 ISecretsConnectorList<T = ISecret> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if this should be named ISecretsList instead?

With ISecretsConnectorList, on may think this is a connector for interacting with lists of secrets at once?

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<ISecretsConnectorList>;
attach(
namespace: string,
id: string,
Expand Down
Loading