Add capabilities checking to TOTPView.
This commit is contained in:
parent
6fc11dfbe4
commit
19c366b3a5
|
@ -8,9 +8,7 @@ export type CapabilitiesType = {
|
||||||
|
|
||||||
export async function getCapabilitiesPath(path: string | string[]): Promise<CapabilitiesType> {
|
export async function getCapabilitiesPath(path: string | string[]): Promise<CapabilitiesType> {
|
||||||
if (!Array.isArray(path)) {
|
if (!Array.isArray(path)) {
|
||||||
path = [removeDoubleSlash(path)];
|
path = [path];
|
||||||
} else {
|
|
||||||
path = path.map((s) => removeDoubleSlash(s));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const request = new Request(appendAPIURL("/v1/sys/capabilities-self"), {
|
const request = new Request(appendAPIURL("/v1/sys/capabilities-self"), {
|
||||||
|
|
|
@ -4,16 +4,16 @@ import { DoesNotExistError } from "../../../types/internalErrors";
|
||||||
import { Grid, GridSizes } from "../../../elements/Grid";
|
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 { PageRouter } from "z-pagerouter";
|
|
||||||
import { PageState } from "../../../state/PageState";
|
|
||||||
import { SecretTitleElement } from "../SecretTitleElement";
|
import { SecretTitleElement } from "../SecretTitleElement";
|
||||||
|
import { getCapabilitiesPath } 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 { setErrorText } from "../../../pageUtils";
|
import { setErrorText } from "../../../pageUtils";
|
||||||
import i18next from "i18next";
|
import i18next from "i18next";
|
||||||
|
|
||||||
export class RefreshingTOTPGridItem extends Component<
|
export class RefreshingTOTPGridItem extends Component<
|
||||||
{ baseMount: string; totpKey: string; router: PageRouter },
|
{ baseMount: string; totpKey: string; page: Page; canDelete: boolean },
|
||||||
{ totpValue: string }
|
{ totpValue: string }
|
||||||
> {
|
> {
|
||||||
constructor() {
|
constructor() {
|
||||||
|
@ -46,16 +46,18 @@ export class RefreshingTOTPGridItem extends Component<
|
||||||
<CopyableInputBox text={this.state.totpValue} copyable />
|
<CopyableInputBox text={this.state.totpValue} copyable />
|
||||||
<div>
|
<div>
|
||||||
<MarginInline>
|
<MarginInline>
|
||||||
<button
|
{this.props.canDelete && (
|
||||||
class="uk-button uk-button-danger"
|
<button
|
||||||
onClick={async () => {
|
class="uk-button uk-button-danger"
|
||||||
const state = this.props.router.state as PageState;
|
onClick={async () => {
|
||||||
state.secretItem = this.props.totpKey;
|
const page = this.props.page;
|
||||||
await this.props.router.changePage("TOTP_DELETE");
|
page.state.secretItem = this.props.totpKey;
|
||||||
}}
|
await page.router.changePage("TOTP_DELETE");
|
||||||
>
|
}}
|
||||||
{i18next.t("totp_view_delete_btn")}
|
>
|
||||||
</button>
|
{i18next.t("totp_view_delete_btn")}
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
</MarginInline>
|
</MarginInline>
|
||||||
</div>
|
</div>
|
||||||
</Grid>
|
</Grid>
|
||||||
|
@ -94,13 +96,23 @@ export class TOTPViewPage extends Page {
|
||||||
await (async () => {
|
await (async () => {
|
||||||
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) => {
|
||||||
<RefreshingTOTPGridItem
|
const caps = (
|
||||||
baseMount={this.state.baseMount}
|
await getCapabilitiesPath(
|
||||||
totpKey={key}
|
removeDoubleSlash(this.state.baseMount + "code/" + key),
|
||||||
router={this.router}
|
)
|
||||||
/>
|
).capabilities;
|
||||||
)),
|
if (caps.includes("read")) {
|
||||||
|
return (
|
||||||
|
<RefreshingTOTPGridItem
|
||||||
|
baseMount={this.state.baseMount}
|
||||||
|
totpKey={key}
|
||||||
|
page={this}
|
||||||
|
canDelete={caps.includes("delete")}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
return elem;
|
return elem;
|
||||||
} catch (e: unknown) {
|
} catch (e: unknown) {
|
||||||
|
|
Loading…
Reference in a new issue