Add API tokens support #9

Merged
pierre merged 40 commits from api into master 2024-04-23 17:04:45 +00:00
2 changed files with 21 additions and 7 deletions
Showing only changes of commit 592466bcc8 - Show all commits

View File

@ -1,3 +1,4 @@
import { time } from "../utils/DateUtils";
import { APIClient } from "./ApiClient"; import { APIClient } from "./ApiClient";
export type RightVerb = "POST" | "GET" | "PUT" | "DELETE" | "PATCH"; export type RightVerb = "POST" | "GET" | "PUT" | "DELETE" | "PATCH";
@ -23,6 +24,11 @@ export function APITokenURL(t: APIToken, edit: boolean = false): string {
return `/token/${t.id}${edit ? "/edit" : ""}`; return `/token/${t.id}${edit ? "/edit" : ""}`;
} }
export function ExpiredAPIToken(t: APIToken): boolean {
if (!t.max_inactivity) return false;
return t.last_used + t.max_inactivity < time();
}
export interface APITokenPrivateKey { export interface APITokenPrivateKey {
alg: string; alg: string;
priv: string; priv: string;

View File

@ -1,6 +1,4 @@
import React from "react"; import VisibilityIcon from "@mui/icons-material/Visibility";
import { APIToken, APITokenURL, TokensApi } from "../api/TokensApi";
import { AsyncWidget } from "../widgets/AsyncWidget";
import { import {
Button, Button,
IconButton, IconButton,
@ -12,11 +10,18 @@ import {
TableHead, TableHead,
TableRow, TableRow,
} from "@mui/material"; } from "@mui/material";
import { RouterLink } from "../widgets/RouterLink"; import React from "react";
import { VirtWebRouteContainer } from "../widgets/VirtWebRouteContainer";
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
import VisibilityIcon from "@mui/icons-material/Visibility"; import {
APIToken,
APITokenURL,
ExpiredAPIToken,
TokensApi,
} from "../api/TokensApi";
import { AsyncWidget } from "../widgets/AsyncWidget";
import { RouterLink } from "../widgets/RouterLink";
import { TimeWidget, timeDiff } from "../widgets/TimeWidget"; import { TimeWidget, timeDiff } from "../widgets/TimeWidget";
import { VirtWebRouteContainer } from "../widgets/VirtWebRouteContainer";
export function TokensListRoute(): React.ReactElement { export function TokensListRoute(): React.ReactElement {
const [list, setList] = React.useState<APIToken[] | undefined>(); const [list, setList] = React.useState<APIToken[] | undefined>();
@ -74,8 +79,11 @@ export function TokensListRouteInner(p: {
key={t.id} key={t.id}
hover hover
onDoubleClick={() => navigate(APITokenURL(t))} onDoubleClick={() => navigate(APITokenURL(t))}
style={{ backgroundColor: ExpiredAPIToken(t) ? "red" : "" }}
> >
<TableCell>{t.name}</TableCell> <TableCell>
{t.name} {ExpiredAPIToken(t) && <i>(Expired)</i>}
</TableCell>
<TableCell>{t.description}</TableCell> <TableCell>{t.description}</TableCell>
<TableCell> <TableCell>
<TimeWidget time={t.created} /> <TimeWidget time={t.created} />