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 24 additions and 9 deletions
Showing only changes of commit ec25b6e4f1 - Show all commits

View File

@ -159,7 +159,7 @@ pub struct NewToken {
pub description: String, pub description: String,
pub rights: TokenRights, pub rights: TokenRights,
pub ip_restriction: Option<ipnetwork::IpNetwork>, pub ip_restriction: Option<ipnetwork::IpNetwork>,
pub delete_after_inactivity: Option<u64>, pub max_inactivity: Option<u64>,
} }
impl NewToken { impl NewToken {
@ -185,7 +185,7 @@ impl NewToken {
return Some(err); return Some(err);
} }
if let Some(t) = self.delete_after_inactivity { if let Some(t) = self.max_inactivity {
if t < 3600 { if t < 3600 {
return Some("API tokens shall be valid for at least 1 hour!"); return Some("API tokens shall be valid for at least 1 hour!");
} }
@ -209,7 +209,7 @@ pub async fn create(t: &NewToken) -> anyhow::Result<(Token, TokenPrivKey)> {
rights: t.rights.clone(), rights: t.rights.clone(),
last_used: time(), last_used: time(),
ip_restriction: t.ip_restriction, ip_restriction: t.ip_restriction,
max_inactivity: t.delete_after_inactivity, max_inactivity: t.max_inactivity,
}; };
token.save()?; token.save()?;

View File

@ -12,11 +12,11 @@ import { useSnackbar } from "../../hooks/providers/SnackbarProvider";
import { AsyncWidget } from "../AsyncWidget"; import { AsyncWidget } from "../AsyncWidget";
import { TabsWidget } from "../TabsWidget"; import { TabsWidget } from "../TabsWidget";
import { EditSection } from "../forms/EditSection"; import { EditSection } from "../forms/EditSection";
import { IPInput, IPInputWithMask } from "../forms/IPInput"; import { IPInputWithMask } from "../forms/IPInput";
import { ResAutostartInput } from "../forms/ResAutostartInput";
import { SelectInput } from "../forms/SelectInput";
import { TextInput } from "../forms/TextInput";
import { RadioGroupInput } from "../forms/RadioGroupInput"; import { RadioGroupInput } from "../forms/RadioGroupInput";
import { TextInput } from "../forms/TextInput";
const SECS_PER_DAY = 3600 * 24;
export enum TokenWidgetStatus { export enum TokenWidgetStatus {
Create, Create,
@ -149,7 +149,7 @@ function NetworkDetailsTabGeneral(p: DetailsInnerProps): React.ReactElement {
</EditSection> </EditSection>
<EditSection title="General settings"> <EditSection title="General settings">
{(p.status === TokenWidgetStatus.Create || p.token.ip_restriction) && ( {p.status === TokenWidgetStatus.Create && (
<RadioGroupInput <RadioGroupInput
{...p} {...p}
editable={p.status === TokenWidgetStatus.Create} editable={p.status === TokenWidgetStatus.Create}
@ -161,6 +161,7 @@ function NetworkDetailsTabGeneral(p: DetailsInnerProps): React.ReactElement {
onValueChange={(v) => { onValueChange={(v) => {
setIpVersion(Number(v) as any); setIpVersion(Number(v) as any);
}} }}
label="Token IP restriction version"
/> />
)} )}
<IPInputWithMask <IPInputWithMask
@ -175,7 +176,21 @@ function NetworkDetailsTabGeneral(p: DetailsInnerProps): React.ReactElement {
}} }}
/> />
{/* TODO : remaining */} <TextInput
editable={p.status === TokenWidgetStatus.Create}
label="Max inactivity of tokens (days)"
type="number"
value={
p.token.max_inactivity
? Math.floor(p.token.max_inactivity / SECS_PER_DAY).toString()
: ""
}
onValueChange={(v) => {
const secs = Number(v ?? "0") * SECS_PER_DAY;
p.token.max_inactivity = secs === 0 ? undefined : secs;
p.onChange?.();
}}
/>
</EditSection> </EditSection>
</Grid> </Grid>
); );