diff --git a/src/connectors/in-memory.ts b/src/connectors/in-memory.ts new file mode 100644 index 0000000..8041887 --- /dev/null +++ b/src/connectors/in-memory.ts @@ -0,0 +1,37 @@ +import { ISecret, ISecretsConnector } from '../token'; + +/** + * An in memory password connector to store passwords during the session. + * Refreshing the page clear the passwords. + * + * This is the default implementation of ISecretsConnector. + */ +export class InMemoryConnector implements ISecretsConnector { + async fetch(id: string): Promise { + return this._secrets.get(id); + } + + async save(id: string, value: ISecret): Promise { + this._secrets.set(id, value); + } + + async remove(id: string): Promise { + this._secrets.delete(id); + } + + async list( + query?: string | undefined + ): Promise<{ ids: string[]; values: ISecret[] }> { + const ids: string[] = []; + const values: ISecret[] = []; + this._secrets.forEach((value, key) => { + if (value.namespace === query) { + ids.push(key); + values.push(value); + } + }); + return { ids, values }; + } + + private _secrets = new Map(); +} diff --git a/src/connectors/index.ts b/src/connectors/index.ts new file mode 100644 index 0000000..7407626 --- /dev/null +++ b/src/connectors/index.ts @@ -0,0 +1,2 @@ +export * from './in-memory'; +export * from './local-storage'; diff --git a/src/index.ts b/src/index.ts index 841d213..d643c3f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,12 +4,27 @@ import { } from '@jupyterlab/application'; import { SecretsManager } from './manager'; import { ISecretsConnector, ISecretsManager } from './token'; +import { InMemoryConnector } from './connectors'; /** - * Initialization data for the jupyter-secrets-manager extension. + * A basic secret connector extension, that should be disabled to provide a new + * connector. */ -const plugin: JupyterFrontEndPlugin = { - id: 'jupyter-secrets-manager:plugin', +const inMemoryConnector: JupyterFrontEndPlugin = { + id: 'jupyter-secrets-manager:connector', + description: 'A JupyterLab extension to manage secrets.', + autoStart: true, + provides: ISecretsConnector, + activate: (app: JupyterFrontEnd): ISecretsConnector => { + return new InMemoryConnector(); + } +}; + +/** + * The secret manager extension. + */ +const manager: JupyterFrontEndPlugin = { + id: 'jupyter-secrets-manager:manager', description: 'A JupyterLab extension to manage secrets.', autoStart: true, provides: ISecretsManager, @@ -23,6 +38,6 @@ const plugin: JupyterFrontEndPlugin = { } }; -export * from './connectors/local-storage'; +export * from './connectors'; export * from './token'; -export default plugin; +export default [inMemoryConnector, manager];