1
0
Fork 0

rename state to settings

This commit is contained in:
ChaotiCryptidz 2022-01-07 14:26:21 +00:00
parent 12eb745d2c
commit 1d037f0f19
19 changed files with 78 additions and 89 deletions

View file

@ -1,14 +1,14 @@
import { BaseAPIResponse } from "./types/api";
import { DoesNotExistError } from "../types/internalErrors";
import { pageState } from "../globalPageState";
import { settings } from "../globalSettings";
export function getHeaders(): Record<string, string> {
return {
"X-Vault-Token": pageState.token,
"X-Vault-Token": settings.token,
};
}
export const appendAPIURL = (url: string): string => pageState.apiURL + url;
export const appendAPIURL = (url: string): string => settings.apiURL + url;
export async function checkResponse(resp: Response): Promise<void> {
if (resp.ok) return;

View file

@ -1,2 +0,0 @@
import { PageState } from "./state/PageState";
export const pageState = new PageState();

2
src/globalSettings.ts Normal file
View file

@ -0,0 +1,2 @@
import { Settings } from "./settings/Settings";
export const settings = new Settings();

View file

@ -27,13 +27,13 @@ import { getSealStatus } from "./api/sys/getSealStatus";
//import { pageList } from "./allPages";
import { render } from "preact";
import { NavBar } from "./ui/elements/NavBar";
import { pageState } from "./globalPageState";
import { settings } from "./globalSettings";
import { playground } from "./playground";
import i18next from "i18next";
import { Main } from "./pages";
async function onLoad(): Promise<void> {
document.documentElement.dir = pageState.pageDirection;
document.documentElement.dir = settings.pageDirection;
render(
<>
@ -74,7 +74,7 @@ document.addEventListener(
// @ts-expect-error
console.log("Build Data:", BUILD_STRING);
await i18next.init({
lng: pageState.language,
lng: settings.language,
fallbackLng: "en",
debug: true,
// @ts-ignore
@ -87,7 +87,7 @@ document.addEventListener(
},
format: function (value: unknown, format, _): string {
if (format === "until_date" && value instanceof Date)
return formatDistance(new Date(), new Date(value), pageState.language);
return formatDistance(new Date(), new Date(value), settings.language);
return value as string;
},
},

View file

@ -1,4 +1,4 @@
import { PageState } from "./state/PageState";
import { Settings } from "./settings/Settings";
import { getSealStatus } from "./api/sys/getSealStatus";
import { lookupSelf } from "./api/sys/lookupSelf";
import { route } from "preact-router";
@ -6,13 +6,13 @@ import ClipboardJS from "clipboard";
import UIkit from "uikit";
import i18next from "i18next";
async function prePageChecksReal(state: PageState) {
if (state.language.length == 0) {
async function prePageChecksReal(settings: Settings) {
if (settings.language.length == 0) {
route("/set_language", true);
throw new Error("Language Not Set");
}
if (!state.apiURL) {
if (!settings.apiURL) {
route("/set_vault_url", true);
throw new Error("Vault URL Not Set");
}
@ -31,9 +31,9 @@ async function prePageChecksReal(state: PageState) {
}
}
export async function prePageChecks(state: PageState): Promise<boolean> {
export async function prePageChecks(settings: Settings): Promise<boolean> {
try {
await prePageChecksReal(state);
await prePageChecksReal(settings);
} catch (e) {
console.log("OHNO", e);
return false;

View file

@ -1,5 +1,5 @@
import Router from "preact-router";
import { pageState } from "./globalPageState";
import { settings } from "./globalSettings";
import { DeleteSecretsEngine } from "./ui/pages/Secrets/DeleteSecretsEngine";
import { Home } from "./ui/pages/Home";
@ -44,54 +44,54 @@ import { UserPassUserEdit } from "./ui/pages/Access/Auth/userpass/UserPassUserEd
export const Main = () => (
<Router>
<Home path="/" state={pageState} />
<Me path="/me" state={pageState} />
<Login path="/login" state={pageState} />
<Home path="/" settings={settings} />
<Me path="/me" settings={settings} />
<Login path="/login" settings={settings} />
<PasswordGenerator path="/pw_gen" />
<SetVaultURL path="/set_vault_url" state={pageState} />
<Unseal path="/unseal" state={pageState} />
<SetLanguage path="/set_language" state={pageState} />
<SetVaultURL path="/set_vault_url" settings={settings} />
<Unseal path="/unseal" settings={settings} />
<SetLanguage path="/set_language" settings={settings} />
<Secrets path="/secrets" state={pageState} />
<DeleteSecretsEngine path="/secrets/delete_engine/:mount" state={pageState} />
<Secrets path="/secrets" settings={settings} />
<DeleteSecretsEngine path="/secrets/delete_engine/:mount" settings={settings} />
<NewSecretsEngine path="/secrets/new_secrets_engine" />
<NewKVEngine path="/secrets/new_secrets_engine/kv" />
<NewTOTPEngine path="/secrets/new_secrets_engine/totp" />
<NewTransitEngine path="/secrets/new_secrets_engine/trasit" />
<KeyValueNew path="/secrets/kv/new/:baseMount/:secretPath*?" state={pageState} />
<KeyValueList path="/secrets/kv/list/:baseMount/:secretPath*?" state={pageState} />
<KeyValueView path="/secrets/kv/view/:item/:baseMount/:secretPath*?" state={pageState} />
<KeyValueEdit path="/secrets/kv/edit/:item/:baseMount/:secretPath*?" state={pageState} />
<KeyValueDelete path="/secrets/kv/delete/:item/:baseMount/:secretPath*?" state={pageState} />
<KeyValueNew path="/secrets/kv/new/:baseMount/:secretPath*?" settings={settings} />
<KeyValueList path="/secrets/kv/list/:baseMount/:secretPath*?" settings={settings} />
<KeyValueView path="/secrets/kv/view/:item/:baseMount/:secretPath*?" settings={settings} />
<KeyValueEdit path="/secrets/kv/edit/:item/:baseMount/:secretPath*?" settings={settings} />
<KeyValueDelete path="/secrets/kv/delete/:item/:baseMount/:secretPath*?" settings={settings} />
<TOTPList path="/secrets/totp/list/:baseMount" state={pageState} />
<TOTPNew path="/secrets/totp/new/:baseMount" state={pageState} />
<TOTPDelete path="/secrets/totp/delete/:baseMount/:item" state={pageState} />
<TOTPList path="/secrets/totp/list/:baseMount" settings={settings} />
<TOTPNew path="/secrets/totp/new/:baseMount" settings={settings} />
<TOTPDelete path="/secrets/totp/delete/:baseMount/:item" settings={settings} />
<TransitNew path="/secrets/transit/new/:baseMount" state={pageState} />
<TransitList path="/secrets/transit/list/:baseMount" state={pageState} />
<TransitView path="/secrets/transit/view/:baseMount/:secretItem" state={pageState} />
<TransitEncrypt path="/secrets/transit/encrypt/:baseMount/:secretItem" state={pageState} />
<TransitDecrypt path="/secrets/transit/decrypt/:baseMount/:secretItem" state={pageState} />
<TransitRewrap path="/secrets/transit/rewrap/:baseMount/:secretItem" state={pageState} />
<TransitNew path="/secrets/transit/new/:baseMount" settings={settings} />
<TransitList path="/secrets/transit/list/:baseMount" settings={settings} />
<TransitView path="/secrets/transit/view/:baseMount/:secretItem" settings={settings} />
<TransitEncrypt path="/secrets/transit/encrypt/:baseMount/:secretItem" settings={settings} />
<TransitDecrypt path="/secrets/transit/decrypt/:baseMount/:secretItem" settings={settings} />
<TransitRewrap path="/secrets/transit/rewrap/:baseMount/:secretItem" settings={settings} />
<PoliciesHome path="/policies" state={pageState} />
<PolicyNew path="/policies/new" state={pageState} />
<PolicyView path="/policies/view/:policyName" state={pageState} />
<PolicyEdit path="/policies/edit/:policyName" state={pageState} />
<PolicyDelete path="/policies/delete/:policyName" state={pageState} />
<PoliciesHome path="/policies" settings={settings} />
<PolicyNew path="/policies/new" settings={settings} />
<PolicyView path="/policies/view/:policyName" settings={settings} />
<PolicyEdit path="/policies/edit/:policyName" settings={settings} />
<PolicyDelete path="/policies/delete/:policyName" settings={settings} />
<AccessHomePage path="/access" state={pageState} />
<AuthHome path="/access/auth" state={pageState} />
<AuthViewConfig path="/access/auth/view/:baseMount" state={pageState} />
<AccessHomePage path="/access" settings={settings} />
<AuthHome path="/access/auth" settings={settings} />
<AuthViewConfig path="/access/auth/view/:baseMount" settings={settings} />
<UserPassUsersList path="/access/auth/userpass/list/:baseMount" state={pageState} />
<UserPassUserNew path="/access/auth/userpass/new/:baseMount" state={pageState} />
<UserPassUserView path="/access/auth/userpass/view/:baseMount/:user" state={pageState} />
<UserPassUserEdit path="/access/auth/userpass/edit/:baseMount/:user" state={pageState} />
<UserPassUserDelete path="/access/auth/userpass/delete/:baseMount/:user" state={pageState} />
<UserPassUsersList path="/access/auth/userpass/list/:baseMount" settings={settings} />
<UserPassUserNew path="/access/auth/userpass/new/:baseMount" settings={settings} />
<UserPassUserView path="/access/auth/userpass/view/:baseMount/:user" settings={settings} />
<UserPassUserEdit path="/access/auth/userpass/edit/:baseMount/:user" settings={settings} />
<UserPassUserDelete path="/access/auth/userpass/delete/:baseMount/:user" settings={settings} />
<div default>
<p>PAGE NOT YET IMPLEMENTED</p>

View file

@ -1,6 +1,6 @@
import { StorageType } from "./storage/StorageType";
export class PageState {
export class Settings {
constructor() {
this.storage = localStorage;
}

View file

@ -1,6 +1,6 @@
import { PageState } from "../state/PageState";
import { Settings } from "../settings/Settings";
export type DefaultPageProps = {
state: PageState;
settings: Settings;
matches?: { [key: string]: string };
};

View file

@ -9,7 +9,7 @@ import { PageTitle } from "../../elements/PageTitle";
export class AccessHomePage extends Component<DefaultPageProps> {
async componentDidMount() {
if (!(await prePageChecks(this.props.state))) return;
if (!(await prePageChecks(this.props.settings))) return;
}
render() {

View file

@ -1,7 +1,6 @@
import { Component, JSX, render } from "preact";
import { Grid, GridSizes } from "../elements/Grid";
import { Margin } from "../elements/Margin";
import { PageState } from "../../state/PageState";
import { PageTitle } from "../elements/PageTitle";
import { Tile } from "../elements/Tile";
import { TokenInfo } from "../../api/types/token";
@ -10,10 +9,7 @@ import { lookupSelf } from "../../api/sys/lookupSelf";
import { prePageChecks, setErrorText } from "../../pageUtils";
import { route } from "preact-router";
import i18next from "i18next";
type HomeProps = {
state: PageState;
};
import { DefaultPageProps } from "../../types/DefaultPageProps";
type HomeState = {
selfTokenInfo: TokenInfo;
@ -21,9 +17,9 @@ type HomeState = {
policiesCaps: string[];
};
export class Home extends Component<HomeProps, HomeState> {
export class Home extends Component<DefaultPageProps, HomeState> {
async componentDidMount() {
if (!(await prePageChecks(this.props.state))) return;
if (!(await prePageChecks(this.props.settings))) return;
let selfTokenInfo: TokenInfo;
try {
@ -32,7 +28,7 @@ export class Home extends Component<HomeProps, HomeState> {
const error = e as Error;
setErrorText(error.message);
if (error.message == "permission denied") {
this.props.state.token = "";
this.props.settings.token = "";
route("/login", true);
return;
}
@ -53,11 +49,11 @@ export class Home extends Component<HomeProps, HomeState> {
return (
this.state.selfTokenInfo && (
<>
<PageTitle title={this.name} />
<PageTitle title={i18next.t("home_page_title")} />
<div>
<ul id="textList" class="uk-nav">
<li>
<span>{i18next.t("home_vaulturl_text", { text: this.props.state.apiURL })}</span>
<span>{i18next.t("home_vaulturl_text", { text: this.props.settings.apiURL })}</span>
</li>
<li>
<a href="/pw_gen">{i18next.t("home_password_generator_btn")}</a>
@ -105,8 +101,4 @@ export class Home extends Component<HomeProps, HomeState> {
)
);
}
get name(): string {
return i18next.t("home_page_title");
}
}

View file

@ -3,7 +3,6 @@ import { DefaultPageProps } from "../../types/DefaultPageProps";
import { Form } from "../elements/Form";
import { Margin } from "../elements/Margin";
import { MarginInline } from "../elements/MarginInline";
import { PageState } from "../../state/PageState";
import { PageTitle } from "../elements/PageTitle";
import { lookupSelf } from "../../api/sys/lookupSelf";
import { route } from "preact-router";
@ -11,7 +10,7 @@ import { setErrorText } from "../../pageUtils";
import { usernameLogin } from "../../api/auth/usernameLogin";
import i18next from "i18next";
export class TokenLoginForm extends Component<{ state: PageState }, unknown> {
export class TokenLoginForm extends Component<DefaultPageProps> {
constructor() {
super();
}
@ -40,7 +39,7 @@ export class TokenLoginForm extends Component<{ state: PageState }, unknown> {
async onSubmit(data: FormData): Promise<void> {
const token = data.get("token");
this.props.state.token = token as string;
this.props.settings.token = token as string;
try {
await lookupSelf();
@ -57,7 +56,7 @@ export class TokenLoginForm extends Component<{ state: PageState }, unknown> {
}
}
export class UsernameLoginForm extends Component<{ state: PageState }, unknown> {
export class UsernameLoginForm extends Component<DefaultPageProps> {
constructor() {
super();
}
@ -100,7 +99,7 @@ export class UsernameLoginForm extends Component<{ state: PageState }, unknown>
data.get("username") as string,
data.get("password") as string,
);
this.props.state.token = res;
this.props.settings.token = res;
route("/");
} catch (e: unknown) {
const error = e as Error;
@ -128,10 +127,10 @@ export class Login extends Component<DefaultPageProps> {
<p id="errorText" class="uk-text-danger" />
<ul class="uk-switcher uk-margin switcher-container">
<li>
<TokenLoginForm state={this.props.state} />
<TokenLoginForm settings={this.props.settings} />
</li>
<li>
<UsernameLoginForm state={this.props.state} />
<UsernameLoginForm settings={this.props.settings} />
</li>
</ul>
</div>

View file

@ -66,7 +66,7 @@ export class Me extends Component<DefaultPageProps, MeState> {
<li>
<a
onClick={async () => {
this.props.state.token = "";
this.props.settings.token = "";
route("/");
}}
>
@ -74,7 +74,7 @@ export class Me extends Component<DefaultPageProps, MeState> {
</a>
</li>
<li>
<CopyLink text={i18next.t("me_copy_token_btn")} data={this.props.state.token} />
<CopyLink text={i18next.t("me_copy_token_btn")} data={this.props.settings.token} />
</li>
<li>
<a

View file

@ -10,7 +10,7 @@ import { policyNewURL, policyViewURL } from "../pageLinks";
export class PoliciesHome extends Component<DefaultPageProps, { policies: string[] }> {
async componentDidMount() {
if (!(await prePageChecks(this.props.state))) return;
if (!(await prePageChecks(this.props.settings))) return;
let policies = await getPolicies();
policies = policies.sort();

View file

@ -14,7 +14,7 @@ export class PolicyView extends Component<
{ policy: string; policyName: string }
> {
async componentDidMount() {
if (!(await prePageChecks(this.props.state))) return;
if (!(await prePageChecks(this.props.settings))) return;
console.log(this.props);
const policyName = this.props.matches["policyName"];
const policy = await getPolicy(policyName);

View file

@ -10,7 +10,7 @@ import { route } from "preact-router";
import { setErrorText } from "../../../../pageUtils";
import i18next from "i18next";
export type KVKeysListProps = DefaultPageProps & {
export type KVKeysListProps = {
baseMount: string;
secretPath: string[];
};
@ -201,7 +201,7 @@ export class KeyValueList extends Component<DefaultPageProps, KeyValueListState>
)}
</p>
{this.state.mountType == "cubbyhole" && <p>{i18next.t("kv_view_cubbyhole_text")}</p>}
<KVKeysList baseMount={baseMount} secretPath={secretPath} state={this.props.state} />
<KVKeysList baseMount={baseMount} secretPath={secretPath} />
</>
);
}

View file

@ -1,7 +1,6 @@
import { Component, JSX, render } from "preact";
import { DefaultPageProps } from "../../../types/DefaultPageProps";
import { MountType, getMounts } from "../../../api/sys/getMounts";
import { PageState } from "../../../state/PageState";
import { PageTitle } from "../../elements/PageTitle";
import { getCapsPath } from "../../../api/sys/getCapabilities";
import { prePageChecks } from "../../../pageUtils";
@ -20,7 +19,6 @@ export function isSupportedMount(mount: MountType): boolean {
}
export type MountLinkProps = {
state: PageState;
mount: MountType;
baseMount: string;
};
@ -61,7 +59,7 @@ type SecretsState = {
export class Secrets extends Component<DefaultPageProps, SecretsState> {
async componentDidMount() {
if (!(await prePageChecks(this.props.state))) return;
if (!(await prePageChecks(this.props.settings))) return;
const mountsCapabilities = await getCapsPath("/sys/mounts");
const mounts = await getMounts();
@ -102,7 +100,7 @@ export class Secrets extends Component<DefaultPageProps, SecretsState> {
console.log(baseMount, mount);
if (isSupportedMount(mount)) {
return (
<MountLink state={this.props.state} mount={mount} baseMount={baseMount} />
<MountLink mount={mount} baseMount={baseMount} />
);
}
})}

View file

@ -44,10 +44,10 @@ export class SetLanguage extends Component<DefaultPageProps> {
async onSubmit(data: FormData): Promise<void> {
const language = data.get("language") as string;
this.props.state.language = language;
this.props.settings.language = language;
const t = await i18next.changeLanguage(language);
this.props.state.pageDirection = t("language_direction");
this.props.settings.pageDirection = t("language_direction");
// TODO: make navbar somethingy
//reloadNavBar(this.router);
route("/");

View file

@ -33,7 +33,7 @@ export class SetVaultURL extends Component<DefaultPageProps> {
async onSubmit(data: FormData): Promise<void> {
// TODO: check if vault is actually working here.
this.props.state.apiURL = data.get("vaultURL") as string;
this.props.settings.apiURL = data.get("vaultURL") as string;
route("/");
}