|
| 1 | +import { ISecret, ISecretsConnector } from '../token'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Example connector that save the secrets to the local storage. |
| 5 | + * |
| 6 | + * WARNING: It should not be used in production, since the passwords are stored as plain |
| 7 | + * text in the local storage of the browser. |
| 8 | + */ |
| 9 | +export class LocalStorageConnector implements ISecretsConnector { |
| 10 | + storage = 'jupyter-secrets:secrets'; |
| 11 | + |
| 12 | + constructor() { |
| 13 | + console.warn(` |
| 14 | +The secret connector used currently should not be used in production, since the |
| 15 | +passwords are stored as plain text in the local storage of the browser' |
| 16 | + `); |
| 17 | + } |
| 18 | + async fetch(id: string): Promise<ISecret | undefined> { |
| 19 | + const secrets = JSON.parse(localStorage.getItem(this.storage) ?? '{}'); |
| 20 | + if (!secrets || !secrets[id]) { |
| 21 | + return; |
| 22 | + } |
| 23 | + return secrets[id]; |
| 24 | + } |
| 25 | + |
| 26 | + async save(id: string, value: ISecret): Promise<any> { |
| 27 | + const secrets = JSON.parse(localStorage.getItem(this.storage) ?? '{}'); |
| 28 | + secrets[id] = value; |
| 29 | + localStorage.setItem(this.storage, JSON.stringify(secrets)); |
| 30 | + } |
| 31 | + |
| 32 | + async remove(id: string): Promise<any> { |
| 33 | + const secrets = JSON.parse(localStorage.getItem(this.storage) ?? '{}'); |
| 34 | + delete secrets[id]; |
| 35 | + localStorage.setItem(this.storage, JSON.stringify(secrets)); |
| 36 | + } |
| 37 | + |
| 38 | + async list( |
| 39 | + query?: string | undefined |
| 40 | + ): Promise<{ ids: string[]; values: ISecret[] }> { |
| 41 | + const secrets = JSON.parse(localStorage.getItem(this.storage) ?? '{}'); |
| 42 | + return { |
| 43 | + ids: Object.keys(secrets).filter(key => secrets[key].namespace === query), |
| 44 | + values: [] |
| 45 | + }; |
| 46 | + } |
| 47 | +} |
0 commit comments