2022-08-03 12:12:07 +00:00
|
|
|
import { useState } from "react";
|
2022-02-09 22:37:31 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import {
|
|
|
|
FormGroup,
|
|
|
|
PageSection,
|
|
|
|
ActionGroup,
|
|
|
|
Button,
|
|
|
|
AlertVariant,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
|
|
|
|
import { FormAccess } from "../../components/form-access/FormAccess";
|
|
|
|
import { HelpItem } from "../../components/help-enabler/HelpItem";
|
|
|
|
import { useAdminClient, useFetch } from "../../context/auth/AdminClient";
|
|
|
|
import { useParams } from "react-router-dom";
|
|
|
|
import FileSaver from "file-saver";
|
|
|
|
import { prettyPrintJSON } from "../../util";
|
|
|
|
import { useAlerts } from "../../components/alert/Alerts";
|
|
|
|
import type { ClientParams } from "../routes/Client";
|
|
|
|
import type ResourceServerRepresentation from "@keycloak/keycloak-admin-client/lib/defs/resourceServerRepresentation";
|
2022-03-25 11:19:15 +00:00
|
|
|
import { KeycloakSpinner } from "../../components/keycloak-spinner/KeycloakSpinner";
|
2022-05-11 09:46:14 +00:00
|
|
|
import { KeycloakTextArea } from "../../components/keycloak-text-area/KeycloakTextArea";
|
2022-03-25 11:19:15 +00:00
|
|
|
|
|
|
|
import "./authorization-details.css";
|
2022-02-09 22:37:31 +00:00
|
|
|
|
|
|
|
export const AuthorizationExport = () => {
|
|
|
|
const { t } = useTranslation("clients");
|
2022-07-14 13:02:28 +00:00
|
|
|
const { adminClient } = useAdminClient();
|
2022-02-09 22:37:31 +00:00
|
|
|
const { clientId } = useParams<ClientParams>();
|
|
|
|
const { addAlert, addError } = useAlerts();
|
|
|
|
|
|
|
|
const [code, setCode] = useState<string>();
|
|
|
|
const [authorizationDetails, setAuthorizationDetails] =
|
|
|
|
useState<ResourceServerRepresentation>();
|
|
|
|
|
|
|
|
useFetch(
|
|
|
|
() =>
|
|
|
|
adminClient.clients.exportResource({
|
|
|
|
id: clientId,
|
|
|
|
}),
|
|
|
|
|
|
|
|
(authDetails) => {
|
|
|
|
setCode(JSON.stringify(authDetails, null, 2));
|
|
|
|
setAuthorizationDetails(authDetails);
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
const exportAuthDetails = () => {
|
|
|
|
try {
|
|
|
|
FileSaver.saveAs(
|
|
|
|
new Blob([prettyPrintJSON(authorizationDetails)], {
|
|
|
|
type: "application/json",
|
|
|
|
}),
|
|
|
|
"test-authz-config.json"
|
|
|
|
);
|
|
|
|
addAlert(t("exportAuthDetailsSuccess"), AlertVariant.success);
|
|
|
|
} catch (error) {
|
|
|
|
addError("exportAuthDetailsError", error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-25 11:19:15 +00:00
|
|
|
if (!code) {
|
|
|
|
return <KeycloakSpinner />;
|
|
|
|
}
|
|
|
|
|
2022-02-09 22:37:31 +00:00
|
|
|
return (
|
|
|
|
<PageSection>
|
2022-05-30 09:23:24 +00:00
|
|
|
<FormAccess isHorizontal role="view-realm" className="pf-u-mt-lg">
|
2022-02-09 22:37:31 +00:00
|
|
|
<FormGroup
|
|
|
|
label={t("authDetails")}
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText="clients-help:authDetails"
|
|
|
|
fieldLabelId="clients:authDetails"
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
fieldId="client"
|
|
|
|
>
|
2022-05-11 09:46:14 +00:00
|
|
|
<KeycloakTextArea
|
2022-02-09 22:37:31 +00:00
|
|
|
id="authorizationDetails"
|
|
|
|
readOnly
|
|
|
|
resizeOrientation="vertical"
|
|
|
|
value={code}
|
|
|
|
aria-label={t("authDetails")}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<ActionGroup>
|
|
|
|
<Button
|
|
|
|
data-testid="authorization-export-download"
|
|
|
|
onClick={() => exportAuthDetails()}
|
|
|
|
>
|
|
|
|
{t("common:download")}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
data-testid="authorization-export-copy"
|
|
|
|
variant="secondary"
|
|
|
|
onClick={async () => {
|
|
|
|
try {
|
|
|
|
await navigator.clipboard.writeText(code!);
|
|
|
|
addAlert(t("copied"), AlertVariant.success);
|
|
|
|
} catch (error) {
|
|
|
|
addError(t("copyError"), error);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t("copy")}
|
|
|
|
</Button>
|
|
|
|
</ActionGroup>
|
|
|
|
</FormAccess>
|
|
|
|
</PageSection>
|
|
|
|
);
|
|
|
|
};
|