From c379b810a4476a14a6cac9a76614c3e5e91f30ec Mon Sep 17 00:00:00 2001 From: Kitteh Date: Sun, 30 May 2021 10:21:02 +0100 Subject: [PATCH] Make some code easier to read with capabilities. --- src/api/sys/getCapabilities.ts | 4 ++++ src/pages/Me.tsx | 4 ++-- src/pages/Secrets/KeyValue/KeyValueView.tsx | 5 ++--- src/pages/Secrets/SecretsHome.tsx | 4 ++-- src/pages/Secrets/TOTP/TOTPView.tsx | 12 +++++------- src/pages/Secrets/Transit/TransitView.tsx | 4 ++-- 6 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/api/sys/getCapabilities.ts b/src/api/sys/getCapabilities.ts index 356a362..0c30ee4 100644 --- a/src/api/sys/getCapabilities.ts +++ b/src/api/sys/getCapabilities.ts @@ -28,6 +28,10 @@ export async function getCapabilitiesPath(path: string | string[]): Promise { + return (await getCapabilitiesPath(path)).capabilities; +} + export async function getCapabilities( baseMount: string, secretPath: string[], diff --git a/src/pages/Me.tsx b/src/pages/Me.tsx index b72dd4d..432a6e9 100644 --- a/src/pages/Me.tsx +++ b/src/pages/Me.tsx @@ -1,7 +1,7 @@ import { Component, JSX, createRef, render } from "preact"; import { Page } from "../types/Page"; import { addClipboardNotifications, prePageChecks, setErrorText } from "../pageUtils"; -import { getCapabilitiesPath } from "../api/sys/getCapabilities"; +import { getCapsPath } from "../api/sys/getCapabilities"; import { renewSelf } from "../api/sys/renewSelf"; import { sealVault } from "../api/sys/sealVault"; import ClipboardJS from "clipboard"; @@ -34,7 +34,7 @@ export class MePage extends Page { let canSealVault = false; try { - const caps = (await getCapabilitiesPath("sys/seal")).capabilities; + const caps = await getCapsPath("sys/seal"); canSealVault = caps.includes("sudo") && caps.includes("update"); } catch (e) { canSealVault = false; diff --git a/src/pages/Secrets/KeyValue/KeyValueView.tsx b/src/pages/Secrets/KeyValue/KeyValueView.tsx index 7315fc1..59aa49e 100644 --- a/src/pages/Secrets/KeyValue/KeyValueView.tsx +++ b/src/pages/Secrets/KeyValue/KeyValueView.tsx @@ -2,7 +2,7 @@ import { Component, JSX, render } from "preact"; import { DoesNotExistError } from "../../../types/internalErrors"; import { Page } from "../../../types/Page"; import { SecretTitleElement } from "../SecretTitleElement"; -import { getCapabilitiesPath } from "../../../api/sys/getCapabilities"; +import { getCapsPath } from "../../../api/sys/getCapabilities"; import { getSecrets } from "../../../api/kv/getSecrets"; import { setErrorText } from "../../../pageUtils"; import i18next from "i18next"; @@ -92,7 +92,6 @@ export class KVKeysList extends Component { { const page = this.props.page; - console.log(secret, page.state.secretPath, page.state.baseMount); if (secret.endsWith("/")) { page.state.pushSecretPath(secret); @@ -125,7 +124,7 @@ export class KeyValueViewPage extends Page { } } async render(): Promise { - const caps = (await getCapabilitiesPath("/sys/mounts/" + this.state.baseMount)).capabilities; + const caps = await getCapsPath("/sys/mounts/" + this.state.baseMount); render( <> diff --git a/src/pages/Secrets/SecretsHome.tsx b/src/pages/Secrets/SecretsHome.tsx index cac9d12..bbf9806 100644 --- a/src/pages/Secrets/SecretsHome.tsx +++ b/src/pages/Secrets/SecretsHome.tsx @@ -1,7 +1,7 @@ import { JSX, render } from "preact"; import { MountType, getMounts } from "../../api/sys/getMounts"; import { Page } from "../../types/Page"; -import { getCapabilitiesPath } from "../../api/sys/getCapabilities"; +import { getCapsPath } from "../../api/sys/getCapabilities"; import { prePageChecks } from "../../pageUtils"; import { sortedObjectMap } from "../../utils"; import i18next from "i18next"; @@ -73,7 +73,7 @@ export class SecretsHomePage extends Page { this.state.secretItem = ""; this.state.secretVersion = null; - const mountsCapabilities = (await getCapabilitiesPath("/sys/mounts")).capabilities; + const mountsCapabilities = await getCapsPath("/sys/mounts"); const mounts = await getMounts(); // sort it by secretPath so it's in alphabetical order consistantly. const mountsMap = sortedObjectMap(mounts); diff --git a/src/pages/Secrets/TOTP/TOTPView.tsx b/src/pages/Secrets/TOTP/TOTPView.tsx index 6d7c996..1a4dc25 100644 --- a/src/pages/Secrets/TOTP/TOTPView.tsx +++ b/src/pages/Secrets/TOTP/TOTPView.tsx @@ -5,7 +5,7 @@ import { Grid, GridSizes } from "../../../elements/Grid"; import { MarginInline } from "../../../elements/MarginInline"; import { Page } from "../../../types/Page"; import { SecretTitleElement } from "../SecretTitleElement"; -import { getCapabilitiesPath } from "../../../api/sys/getCapabilities"; +import { getCapsPath } from "../../../api/sys/getCapabilities"; import { getTOTPCode } from "../../../api/totp/getTOTPCode"; import { getTOTPKeys } from "../../../api/totp/getTOTPKeys"; import { removeDoubleSlash } from "../../../utils"; @@ -80,7 +80,7 @@ export class TOTPViewPage extends Page { async render(): Promise { this.state.secretItem = ""; - const caps = (await getCapabilitiesPath("/sys/mounts/" + this.state.baseMount)).capabilities; + const caps = await getCapsPath("/sys/mounts/" + this.state.baseMount); render(
@@ -114,11 +114,9 @@ export class TOTPViewPage extends Page { try { const elem = await Promise.all( Array.from(await getTOTPKeys(this.state.baseMount)).map(async (key) => { - const caps = ( - await getCapabilitiesPath( - removeDoubleSlash(this.state.baseMount + "code/" + key), - ) - ).capabilities; + const caps = await getCapsPath( + removeDoubleSlash(this.state.baseMount + "code/" + key), + ); if (caps.includes("read")) { return ( { this.state.secretItem = ""; - const caps = (await getCapabilitiesPath("/sys/mounts/" + this.state.baseMount)).capabilities; + const caps = await getCapsPath("/sys/mounts/" + this.state.baseMount); render( <>