1
0
Fork 0

tidy up some code and look at some TODOs

This commit is contained in:
ChaotiCryptidz 2022-01-19 12:43:25 +00:00
parent a03a2655d0
commit cbc52b7e63
6 changed files with 42 additions and 32 deletions

View file

@ -13,6 +13,7 @@ import yaml from "js-yaml";
// todo: put this in settings // todo: put this in settings
const defaultIndent = 2; const defaultIndent = 2;
const defaultSyntax = "json";
function parseData(data: string, syntax = "json"): Record<string, unknown> { function parseData(data: string, syntax = "json"): Record<string, unknown> {
if (syntax == "json") { if (syntax == "json") {
@ -74,10 +75,9 @@ type KVEditState =
export class KVEditor extends Component<KVEditProps, KVEditState> { export class KVEditor extends Component<KVEditProps, KVEditState> {
constructor() { constructor() {
super(); super();
// TODO: add a global syntax option to settings for default
this.state = { this.state = {
dataLoaded: false, dataLoaded: false,
syntax: "json", syntax: defaultSyntax,
}; };
} }

View file

@ -25,11 +25,10 @@ function SecretsList(baseMount: string, secretPath: string[], secrets: string[])
<a <a
onClick={async () => { onClick={async () => {
if (secret.endsWith("/")) { if (secret.endsWith("/")) {
// TODO: fix this mess ?
route( route(
kvListURL( kvListURL(
baseMount, baseMount,
[...secretPath, secret.replace("/", "")].filter((e) => e.length > 0), [...secretPath, secret.replace("/", "")]
), ),
); );
} else { } else {
@ -145,7 +144,7 @@ type KeyValueListState = {
export class KeyValueList extends Component<DefaultPageProps, KeyValueListState> { export class KeyValueList extends Component<DefaultPageProps, KeyValueListState> {
async componentDidMount() { async componentDidMount() {
const baseMount = this.props.matches["baseMount"]; 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 mountsPath = "/sys/mounts/" + baseMount;
const currentPath = baseMount + secretPath.join(); const currentPath = baseMount + secretPath.join();

View file

@ -51,8 +51,13 @@ export class KeyValueNew extends Component<DefaultPageProps> {
): Promise<void> { ): Promise<void> {
const path = formData.get("path") as string; const path = formData.get("path") as string;
// TODO: check only do this on kv v1 let keyData: Record<string, unknown> = {};
const keyData = { key: "value" };
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 { try {
await this.props.api.createOrUpdateSecret(baseMount, secretPath, path, keyData); await this.props.api.createOrUpdateSecret(baseMount, secretPath, path, keyData);

View file

@ -37,8 +37,10 @@ export function SecretTitleElement(props: SecretTitleElementProps): JSX.Element
</a> </a>
{...secretPath.map((secretPath, index, secretPaths) => { {...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; if (secretPath.length < 1) return;
return ( return (
<a <a
test-data="secrets-title-secretPath" test-data="secrets-title-secretPath"

View file

@ -68,9 +68,14 @@ export class RefreshingTOTPGridItem extends Component<TOTPGridItemProps, { totpV
} }
} }
type TOTPItem = {
totpKey: string;
canDelete: boolean;
}
type TOTPListState = { type TOTPListState = {
capabilities?: CapabilitiesType; capabilities?: CapabilitiesType;
totpItems: Partial<TOTPGridItemProps>[]; totpItems: TOTPItem[];
}; };
export class TOTPList extends Component<DefaultPageProps, TOTPListState> { export class TOTPList extends Component<DefaultPageProps, TOTPListState> {
@ -88,22 +93,24 @@ export class TOTPList extends Component<DefaultPageProps, TOTPListState> {
const mountsPath = "/sys/mounts/" + baseMount; const mountsPath = "/sys/mounts/" + baseMount;
const caps = await api.getCapabilitiesPath([mountsPath, baseMount]); const caps = await api.getCapabilitiesPath([mountsPath, baseMount]);
let totpItems: Partial<TOTPGridItemProps>[] = []; let totpItems: TOTPItem[] = [];
// TODO: tidy this up i guess
try { try {
totpItems = await Promise.all( const totpKeys = await api.getTOTPKeys(baseMount);
Array.from(await api.getTOTPKeys(baseMount)).map(async (key) => {
const totpCaps = await api.getCapsPath(removeDoubleSlash(baseMount + "/code/" + key)); let totpKeyPermissions = await Promise.all(Array.from(totpKeys.map(async (key) => {
if (totpCaps.includes("read")) { const totpCaps = await api.getCapsPath(removeDoubleSlash(baseMount + "/code/" + key));
return { return {key: key, caps: totpCaps};
baseMount: baseMount, })));
totpKey: key,
canDelete: totpCaps.includes("delete"), 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) { } catch (e: unknown) {
const error = e as Error; const error = e as Error;
if (error != DoesNotExistError) { if (error != DoesNotExistError) {
@ -160,11 +167,9 @@ export class TOTPList extends Component<DefaultPageProps, TOTPListState> {
return this.state.totpItems.map((totpItem) => { return this.state.totpItems.map((totpItem) => {
return ( return (
<RefreshingTOTPGridItem <RefreshingTOTPGridItem
settings={this.props.settings} {...this.props}
api={this.props.api} baseMount={baseMount}
baseMount={totpItem.baseMount} {...totpItem}
totpKey={totpItem.totpKey}
canDelete={totpItem.canDelete}
/> />
); );
}); });

View file

@ -10,6 +10,7 @@ import { Margin } from "../elements/Margin";
import { MarginInline } from "../elements/MarginInline"; import { MarginInline } from "../elements/MarginInline";
import { PageTitle } from "../elements/PageTitle"; import { PageTitle } from "../elements/PageTitle";
import i18next from "i18next"; import i18next from "i18next";
import { route } from "preact-router";
const languageIDs = Object.getOwnPropertyNames(translations); const languageIDs = Object.getOwnPropertyNames(translations);
@ -46,9 +47,7 @@ export class SetLanguage extends Component<DefaultPageProps> {
const t = await i18next.changeLanguage(language); const t = await i18next.changeLanguage(language);
this.props.settings.pageDirection = t("language_direction"); this.props.settings.pageDirection = t("language_direction");
// TODO: make navbar somethingy route("/");
//reloadNavBar(this.router); window.location.reload();
//route("/");
window.location.pathname = "/";
} }
} }