From cbc52b7e630806566377848b517dfa0152833b0f Mon Sep 17 00:00:00 2001 From: ChaotiCryptidz Date: Wed, 19 Jan 2022 12:43:25 +0000 Subject: [PATCH] tidy up some code and look at some TODOs --- .../pages/Secrets/KeyValue/KeyValueEdit.tsx | 4 +- .../pages/Secrets/KeyValue/KeyValueList.tsx | 5 +-- src/ui/pages/Secrets/KeyValue/KeyValueNew.tsx | 9 +++- src/ui/pages/Secrets/SecretTitleElement.tsx | 4 +- src/ui/pages/Secrets/TOTP/TOTPList.tsx | 45 ++++++++++--------- src/ui/pages/SetLanguage.tsx | 7 ++- 6 files changed, 42 insertions(+), 32 deletions(-) diff --git a/src/ui/pages/Secrets/KeyValue/KeyValueEdit.tsx b/src/ui/pages/Secrets/KeyValue/KeyValueEdit.tsx index ec3ad6c..52a8a5e 100644 --- a/src/ui/pages/Secrets/KeyValue/KeyValueEdit.tsx +++ b/src/ui/pages/Secrets/KeyValue/KeyValueEdit.tsx @@ -13,6 +13,7 @@ import yaml from "js-yaml"; // todo: put this in settings const defaultIndent = 2; +const defaultSyntax = "json"; function parseData(data: string, syntax = "json"): Record { if (syntax == "json") { @@ -74,10 +75,9 @@ type KVEditState = export class KVEditor extends Component { constructor() { super(); - // TODO: add a global syntax option to settings for default this.state = { dataLoaded: false, - syntax: "json", + syntax: defaultSyntax, }; } diff --git a/src/ui/pages/Secrets/KeyValue/KeyValueList.tsx b/src/ui/pages/Secrets/KeyValue/KeyValueList.tsx index 3a10e11..0f5116d 100644 --- a/src/ui/pages/Secrets/KeyValue/KeyValueList.tsx +++ b/src/ui/pages/Secrets/KeyValue/KeyValueList.tsx @@ -25,11 +25,10 @@ function SecretsList(baseMount: string, secretPath: string[], secrets: string[]) { if (secret.endsWith("/")) { - // TODO: fix this mess ? route( kvListURL( baseMount, - [...secretPath, secret.replace("/", "")].filter((e) => e.length > 0), + [...secretPath, secret.replace("/", "")] ), ); } else { @@ -145,7 +144,7 @@ type KeyValueListState = { export class KeyValueList extends Component { async componentDidMount() { const baseMount = this.props.matches["baseMount"]; - const secretPath = this.props.matches["secretPath"].split("/"); + const secretPath = this.props.matches["secretPath"].split("/").filter((e) => e.length > 0); const mountsPath = "/sys/mounts/" + baseMount; const currentPath = baseMount + secretPath.join(); diff --git a/src/ui/pages/Secrets/KeyValue/KeyValueNew.tsx b/src/ui/pages/Secrets/KeyValue/KeyValueNew.tsx index 7bbfd27..dbd1858 100644 --- a/src/ui/pages/Secrets/KeyValue/KeyValueNew.tsx +++ b/src/ui/pages/Secrets/KeyValue/KeyValueNew.tsx @@ -51,8 +51,13 @@ export class KeyValueNew extends Component { ): Promise { const path = formData.get("path") as string; - // TODO: check only do this on kv v1 - const keyData = { key: "value" }; + let keyData: Record = {}; + + const mountInfo = await this.props.api.getMount(baseMount); + if (mountInfo.options.version == "1") { + // Can't have a empty secret on KV V1 + keyData = { "placeholder_on_kv1": "placeholder_on_kv1" }; + } try { await this.props.api.createOrUpdateSecret(baseMount, secretPath, path, keyData); diff --git a/src/ui/pages/Secrets/SecretTitleElement.tsx b/src/ui/pages/Secrets/SecretTitleElement.tsx index b740deb..bf84b74 100644 --- a/src/ui/pages/Secrets/SecretTitleElement.tsx +++ b/src/ui/pages/Secrets/SecretTitleElement.tsx @@ -37,8 +37,10 @@ export function SecretTitleElement(props: SecretTitleElementProps): JSX.Element {...secretPath.map((secretPath, index, secretPaths) => { - // TODO: find where a '' is returned so dont need this + // Sometimes we pass a extra / in urls + // just ignore this. if (secretPath.length < 1) return; + return ( []; + totpItems: TOTPItem[]; }; export class TOTPList extends Component { @@ -88,22 +93,24 @@ export class TOTPList extends Component { const mountsPath = "/sys/mounts/" + baseMount; const caps = await api.getCapabilitiesPath([mountsPath, baseMount]); - let totpItems: Partial[] = []; + let totpItems: TOTPItem[] = []; - // TODO: tidy this up i guess try { - totpItems = await Promise.all( - Array.from(await api.getTOTPKeys(baseMount)).map(async (key) => { - const totpCaps = await api.getCapsPath(removeDoubleSlash(baseMount + "/code/" + key)); - if (totpCaps.includes("read")) { - return { - baseMount: baseMount, - totpKey: key, - canDelete: totpCaps.includes("delete"), - }; - } - }), - ); + const totpKeys = await api.getTOTPKeys(baseMount); + + let totpKeyPermissions = await Promise.all(Array.from(totpKeys.map(async (key) => { + const totpCaps = await api.getCapsPath(removeDoubleSlash(baseMount + "/code/" + key)); + return {key: key, caps: totpCaps}; + }))); + + totpItems = Array.from(totpKeyPermissions.map((keyData) => { + // Filter out all non-readable totp keys. + if (!keyData.caps.includes("read")) return; + return { + totpKey: keyData.key, + canDelete: keyData.caps.includes("delete"), + }; + })); } catch (e: unknown) { const error = e as Error; if (error != DoesNotExistError) { @@ -160,11 +167,9 @@ export class TOTPList extends Component { return this.state.totpItems.map((totpItem) => { return ( ); }); diff --git a/src/ui/pages/SetLanguage.tsx b/src/ui/pages/SetLanguage.tsx index 8f7692a..a831249 100644 --- a/src/ui/pages/SetLanguage.tsx +++ b/src/ui/pages/SetLanguage.tsx @@ -10,6 +10,7 @@ import { Margin } from "../elements/Margin"; import { MarginInline } from "../elements/MarginInline"; import { PageTitle } from "../elements/PageTitle"; import i18next from "i18next"; +import { route } from "preact-router"; const languageIDs = Object.getOwnPropertyNames(translations); @@ -46,9 +47,7 @@ export class SetLanguage extends Component { const t = await i18next.changeLanguage(language); this.props.settings.pageDirection = t("language_direction"); - // TODO: make navbar somethingy - //reloadNavBar(this.router); - //route("/"); - window.location.pathname = "/"; + route("/"); + window.location.reload(); } }