added component to set lifespan of token (#955)

This commit is contained in:
Erik Jan de Wit 2021-08-10 13:49:08 +02:00 committed by GitHub
parent 45bd2fd385
commit b0154b39a3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 101 additions and 26 deletions

View file

@ -14,6 +14,7 @@ import {
import { FormAccess } from "../../components/form-access/FormAccess"; import { FormAccess } from "../../components/form-access/FormAccess";
import { HelpItem } from "../../components/help-enabler/HelpItem"; import { HelpItem } from "../../components/help-enabler/HelpItem";
import { TimeSelector } from "../../components/time-selector/TimeSelector"; import { TimeSelector } from "../../components/time-selector/TimeSelector";
import { TokenLifespan } from "./TokenLifespan";
type AdvancedSettingsProps = { type AdvancedSettingsProps = {
control: Control<Record<string, any>>; control: Control<Record<string, any>>;
@ -60,32 +61,13 @@ export const AdvancedSettings = ({
)} )}
{protocol === "openid-connect" && ( {protocol === "openid-connect" && (
<> <>
<FormGroup <TokenLifespan
label={t("accessTokenLifespan")} id="accessTokenLifespan"
fieldId="accessTokenLifespan" name="attributes.access-token-lifespan"
labelIcon={ defaultValue=""
<HelpItem units={["minutes", "days", "hours"]}
helpText="clients-help:accessTokenLifespan" control={control}
forLabel={t("accessTokenLifespan")} />
forID={t(`common:helpLabel`, {
label: t("accessTokenLifespan"),
})}
/>
}
>
<Controller
name="attributes.access-token-lifespan"
defaultValue=""
control={control}
render={({ onChange, value }) => (
<TimeSelector
units={["minutes", "days", "hours"]}
value={value}
onChange={onChange}
/>
)}
/>
</FormGroup>
<FormGroup <FormGroup
label={t("oAuthMutual")} label={t("oAuthMutual")}

View file

@ -0,0 +1,89 @@
import React, { useState } from "react";
import { Control, Controller, FieldValues } from "react-hook-form";
import { useTranslation } from "react-i18next";
import {
FormGroup,
Select,
SelectOption,
SelectVariant,
Split,
SplitItem,
} from "@patternfly/react-core";
import {
TimeSelector,
Unit,
} from "../../components/time-selector/TimeSelector";
import { HelpItem } from "../../components/help-enabler/HelpItem";
type TokenLifespanProps = {
id: string;
name: string;
defaultValue: string;
control: Control<FieldValues>;
units?: Unit[];
};
const never = "tokenLifespan.never";
const expires = "tokenLifespan.expires";
export const TokenLifespan = ({
id,
name,
defaultValue,
control,
units,
}: TokenLifespanProps) => {
const { t } = useTranslation("clients");
const [open, setOpen] = useState(false);
return (
<FormGroup
label={t(id)}
fieldId={id}
labelIcon={
<HelpItem
helpText={`clients-help:${id}`}
forLabel={t(id)}
forID={t(`common:helpLabel`, {
label: t(id),
})}
/>
}
>
<Controller
name={name}
defaultValue={defaultValue}
control={control}
render={({ onChange, value }) => (
<Split hasGutter>
<SplitItem>
<Select
variant={SelectVariant.single}
onToggle={(isExpanded) => setOpen(isExpanded)}
isOpen={open}
onSelect={(_, value) => {
onChange(value);
setOpen(false);
}}
selections={[
typeof value === "number" && value !== -1
? t(expires)
: t(never),
]}
>
<SelectOption value={-1}>{t(never)}</SelectOption>
<SelectOption value={60}>{t(expires)}</SelectOption>
</Select>
</SplitItem>
<SplitItem>
{typeof value === "number" && value !== -1 && (
<TimeSelector units={units} value={value} onChange={onChange} />
)}
</SplitItem>
</Split>
)}
/>
</FormGroup>
);
};

View file

@ -245,5 +245,9 @@ export default {
importFile: "Import file", importFile: "Import file",
importSuccess: "New certificate imported", importSuccess: "New certificate imported",
importError: "Could not import certificate {{error}}", importError: "Could not import certificate {{error}}",
tokenLifespan: {
expires: "Expires in",
never: "Never expires",
},
}, },
}; };