1
0
Fork 0

Make some code easier to read with capabilities.

This commit is contained in:
Kitteh 2021-05-30 10:21:02 +01:00
parent 5b0b0496be
commit c379b810a4
6 changed files with 17 additions and 16 deletions

View file

@ -28,6 +28,10 @@ export async function getCapabilitiesPath(path: string | string[]): Promise<Capa
return data; return data;
} }
export async function getCapsPath(path: string | string[]): Promise<string[]> {
return (await getCapabilitiesPath(path)).capabilities;
}
export async function getCapabilities( export async function getCapabilities(
baseMount: string, baseMount: string,
secretPath: string[], secretPath: string[],

View file

@ -1,7 +1,7 @@
import { Component, JSX, createRef, render } from "preact"; import { Component, JSX, createRef, render } from "preact";
import { Page } from "../types/Page"; import { Page } from "../types/Page";
import { addClipboardNotifications, prePageChecks, setErrorText } from "../pageUtils"; 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 { renewSelf } from "../api/sys/renewSelf";
import { sealVault } from "../api/sys/sealVault"; import { sealVault } from "../api/sys/sealVault";
import ClipboardJS from "clipboard"; import ClipboardJS from "clipboard";
@ -34,7 +34,7 @@ export class MePage extends Page {
let canSealVault = false; let canSealVault = false;
try { try {
const caps = (await getCapabilitiesPath("sys/seal")).capabilities; const caps = await getCapsPath("sys/seal");
canSealVault = caps.includes("sudo") && caps.includes("update"); canSealVault = caps.includes("sudo") && caps.includes("update");
} catch (e) { } catch (e) {
canSealVault = false; canSealVault = false;

View file

@ -2,7 +2,7 @@ import { Component, JSX, render } from "preact";
import { DoesNotExistError } from "../../../types/internalErrors"; import { DoesNotExistError } from "../../../types/internalErrors";
import { Page } from "../../../types/Page"; import { Page } from "../../../types/Page";
import { SecretTitleElement } from "../SecretTitleElement"; import { SecretTitleElement } from "../SecretTitleElement";
import { getCapabilitiesPath } from "../../../api/sys/getCapabilities"; import { getCapsPath } from "../../../api/sys/getCapabilities";
import { getSecrets } from "../../../api/kv/getSecrets"; import { getSecrets } from "../../../api/kv/getSecrets";
import { setErrorText } from "../../../pageUtils"; import { setErrorText } from "../../../pageUtils";
import i18next from "i18next"; import i18next from "i18next";
@ -92,7 +92,6 @@ export class KVKeysList extends Component<KVKeysListProps, KVKeysListState> {
<a <a
onClick={async () => { onClick={async () => {
const page = this.props.page; const page = this.props.page;
console.log(secret, page.state.secretPath, page.state.baseMount);
if (secret.endsWith("/")) { if (secret.endsWith("/")) {
page.state.pushSecretPath(secret); page.state.pushSecretPath(secret);
@ -125,7 +124,7 @@ export class KeyValueViewPage extends Page {
} }
} }
async render(): Promise<void> { async render(): Promise<void> {
const caps = (await getCapabilitiesPath("/sys/mounts/" + this.state.baseMount)).capabilities; const caps = await getCapsPath("/sys/mounts/" + this.state.baseMount);
render( render(
<> <>

View file

@ -1,7 +1,7 @@
import { JSX, render } from "preact"; import { JSX, render } from "preact";
import { MountType, getMounts } from "../../api/sys/getMounts"; import { MountType, getMounts } from "../../api/sys/getMounts";
import { Page } from "../../types/Page"; import { Page } from "../../types/Page";
import { getCapabilitiesPath } from "../../api/sys/getCapabilities"; import { getCapsPath } from "../../api/sys/getCapabilities";
import { prePageChecks } from "../../pageUtils"; import { prePageChecks } from "../../pageUtils";
import { sortedObjectMap } from "../../utils"; import { sortedObjectMap } from "../../utils";
import i18next from "i18next"; import i18next from "i18next";
@ -73,7 +73,7 @@ export class SecretsHomePage extends Page {
this.state.secretItem = ""; this.state.secretItem = "";
this.state.secretVersion = null; this.state.secretVersion = null;
const mountsCapabilities = (await getCapabilitiesPath("/sys/mounts")).capabilities; const mountsCapabilities = await getCapsPath("/sys/mounts");
const mounts = await getMounts(); const mounts = await getMounts();
// sort it by secretPath so it's in alphabetical order consistantly. // sort it by secretPath so it's in alphabetical order consistantly.
const mountsMap = sortedObjectMap(mounts); const mountsMap = sortedObjectMap(mounts);

View file

@ -5,7 +5,7 @@ import { Grid, GridSizes } from "../../../elements/Grid";
import { MarginInline } from "../../../elements/MarginInline"; import { MarginInline } from "../../../elements/MarginInline";
import { Page } from "../../../types/Page"; import { Page } from "../../../types/Page";
import { SecretTitleElement } from "../SecretTitleElement"; import { SecretTitleElement } from "../SecretTitleElement";
import { getCapabilitiesPath } from "../../../api/sys/getCapabilities"; import { getCapsPath } from "../../../api/sys/getCapabilities";
import { getTOTPCode } from "../../../api/totp/getTOTPCode"; import { getTOTPCode } from "../../../api/totp/getTOTPCode";
import { getTOTPKeys } from "../../../api/totp/getTOTPKeys"; import { getTOTPKeys } from "../../../api/totp/getTOTPKeys";
import { removeDoubleSlash } from "../../../utils"; import { removeDoubleSlash } from "../../../utils";
@ -80,7 +80,7 @@ export class TOTPViewPage extends Page {
async render(): Promise<void> { async render(): Promise<void> {
this.state.secretItem = ""; this.state.secretItem = "";
const caps = (await getCapabilitiesPath("/sys/mounts/" + this.state.baseMount)).capabilities; const caps = await getCapsPath("/sys/mounts/" + this.state.baseMount);
render( render(
<div> <div>
@ -114,11 +114,9 @@ export class TOTPViewPage extends Page {
try { try {
const elem = await Promise.all( const elem = await Promise.all(
Array.from(await getTOTPKeys(this.state.baseMount)).map(async (key) => { Array.from(await getTOTPKeys(this.state.baseMount)).map(async (key) => {
const caps = ( const caps = await getCapsPath(
await getCapabilitiesPath( removeDoubleSlash(this.state.baseMount + "code/" + key),
removeDoubleSlash(this.state.baseMount + "code/" + key), );
)
).capabilities;
if (caps.includes("read")) { if (caps.includes("read")) {
return ( return (
<RefreshingTOTPGridItem <RefreshingTOTPGridItem

View file

@ -1,7 +1,7 @@
import { Component, JSX, render } from "preact"; import { Component, JSX, render } from "preact";
import { Page } from "../../../types/Page"; import { Page } from "../../../types/Page";
import { SecretTitleElement } from "../SecretTitleElement"; import { SecretTitleElement } from "../SecretTitleElement";
import { getCapabilitiesPath } from "../../../api/sys/getCapabilities"; import { getCapsPath } from "../../../api/sys/getCapabilities";
import { getTransitKeys } from "../../../api/transit/getTransitKeys"; import { getTransitKeys } from "../../../api/transit/getTransitKeys";
import i18next from "i18next"; import i18next from "i18next";
@ -81,7 +81,7 @@ export class TransitViewPage extends Page {
async render(): Promise<void> { async render(): Promise<void> {
this.state.secretItem = ""; this.state.secretItem = "";
const caps = (await getCapabilitiesPath("/sys/mounts/" + this.state.baseMount)).capabilities; const caps = await getCapsPath("/sys/mounts/" + this.state.baseMount);
render( render(
<> <>