/* eslint-disable */ // @ts-nocheck import type ClientScopeRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientScopeRepresentation"; import type ProtocolMapperRepresentation from "@keycloak/keycloak-admin-client/lib/defs/protocolMapperRepresentation"; import type RoleRepresentation from "@keycloak/keycloak-admin-client/lib/defs/roleRepresentation"; import type { ProtocolMapperTypeRepresentation } from "@keycloak/keycloak-admin-client/lib/defs/serverInfoRepesentation"; import { HelpItem, KeycloakDataTable, KeycloakSelect, SelectVariant, useFetch, useHelp, } from "../../../shared/keycloak-ui-shared"; import { ClipboardCopy, Form, FormGroup, Grid, GridItem, PageSection, SelectOption, Split, SplitItem, Tab, TabContent, Tabs, TabTitleText, Text, TextContent, } from "../../../shared/@patternfly/react-core"; import { QuestionCircleIcon } from "../../../shared/@patternfly/react-icons"; import { useEffect, useRef, useState } from "react"; import { FormProvider, useForm } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { useAdminClient } from "../../admin-client"; import { ClientSelect } from "../../components/client/ClientSelect"; import { UserSelect } from "../../components/users/UserSelect"; import { useAccess } from "../../context/access/Access"; import { useRealm } from "../../context/realm-context/RealmContext"; import { useServerInfo } from "../../context/server-info/ServerInfoProvider"; import { prettyPrintJSON } from "../../util"; import { GeneratedCodeTab } from "./GeneratedCodeTab"; import "./evaluate.css"; export type EvaluateScopesProps = { clientId: string; protocol: string; }; const ProtocolMappers = ({ protocolMappers, }: { protocolMappers: ProtocolMapperRepresentation[]; }) => { const [key, setKey] = useState(0); useEffect(() => { setKey(key + 1); }, [protocolMappers]); return ( Promise.resolve(protocolMappers)} ariaLabelKey="effectiveProtocolMappers" searchPlaceholderKey="searchForProtocol" data-testid="effective-protocol-mappers" columns={[ { name: "mapperName", displayKey: "name", }, { name: "containerName", displayKey: "parentClientScope", }, { name: "type.category", displayKey: "category", }, { name: "type.priority", displayKey: "priority", }, ]} /> ); }; const EffectiveRoles = ({ effectiveRoles, }: { effectiveRoles: RoleRepresentation[]; }) => { const [key, setKey] = useState(0); useEffect(() => { setKey(key + 1); }, [effectiveRoles]); return ( Promise.resolve(effectiveRoles)} ariaLabelKey="effectiveRoleScopeMappings" searchPlaceholderKey="searchForRole" data-testid="effective-role-scope-mappings" columns={[ { name: "name", displayKey: "role", }, { name: "containerId", displayKey: "origin", }, ]} /> ); }; export const EvaluateScopes = ({ clientId, protocol }: EvaluateScopesProps) => { const { adminClient } = useAdminClient(); const openidPrefix = "openid"; const { t } = useTranslation(); const { enabled } = useHelp(); const { realm } = useRealm(); const mapperTypes = useServerInfo().protocolMapperTypes![protocol]; const supportsScopeSelection = (proto: string) => { return proto !== "saml"; }; const isOpenIdConnectProtocol = () => { return protocol === "openid-connect"; }; const isSamlProtocol = () => { return protocol === "saml"; }; const [selectableScopes, setSelectableScopes] = useState< ClientScopeRepresentation[] >([]); const [isScopeOpen, setIsScopeOpen] = useState(false); const [selected, setSelected] = useState( isOpenIdConnectProtocol() ? [openidPrefix] : [], ); const [activeTab, setActiveTab] = useState(0); const [key, setKey] = useState(""); const refresh = () => setKey(`${new Date().getTime()}`); const [effectiveRoles, setEffectiveRoles] = useState( [], ); const [protocolMappers, setProtocolMappers] = useState< ProtocolMapperRepresentation[] >([]); const [accessToken, setAccessToken] = useState(""); const [userInfo, setUserInfo] = useState(""); const [idToken, setIdToken] = useState(""); const [samlResponse, setSamlResponse] = useState(""); const tabContent1 = useRef(null); const tabContent2 = useRef(null); const tabContent3 = useRef(null); const tabContent4 = useRef(null); const tabContent5 = useRef(null); const tabContent6 = useRef(null); const form = useForm(); const { watch } = form; const selectedAudience: string[] = watch("targetAudience"); const { hasAccess } = useAccess(); const hasViewUsers = hasAccess("view-users"); useFetch( () => adminClient.clients.listOptionalClientScopes({ id: clientId }), (optionalClientScopes) => setSelectableScopes(optionalClientScopes), [], ); useFetch( async () => { const scope = selected.join(" "); const effectiveRoles = await adminClient.clients.evaluatePermission({ id: clientId, roleContainer: realm, scope, type: "granted", }); const mapperList = (await adminClient.clients.evaluateListProtocolMapper({ id: clientId, scope, })) as ({ type: ProtocolMapperTypeRepresentation; } & ProtocolMapperRepresentation)[]; return { mapperList, effectiveRoles, }; }, ({ mapperList, effectiveRoles }) => { setEffectiveRoles(effectiveRoles); mapperList.map((mapper) => { mapper.type = mapperTypes.find( (type) => type.id === mapper.protocolMapper, )!; }); setProtocolMappers(mapperList); refresh(); }, [selected], ); useFetch( async () => { const scope = selected.join(" "); const user = form.getValues("user"); if (user.length === 0) { return []; } const audience = selectedAudience.join(" "); if (isOpenIdConnectProtocol()) { return await Promise.all([ adminClient.clients.evaluateGenerateAccessToken({ id: clientId, userId: user[0], scope, audience, }), adminClient.clients.evaluateGenerateUserInfo({ id: clientId, userId: user[0], scope, }), adminClient.clients.evaluateGenerateIdToken({ id: clientId, userId: user[0], scope, }), ]); } if (isSamlProtocol()) { return await Promise.all([ adminClient.clients.evaluateGenerateSamlResponse({ id: clientId, userId: user[0], scope, }), ]); } return await Promise.all([]); }, (responses) => { if (isOpenIdConnectProtocol()) { const [generatedAccessToken, generatedUserInfo, generatedIdToken] = responses; setAccessToken(prettyPrintJSON(generatedAccessToken)); setUserInfo(prettyPrintJSON(generatedUserInfo)); setIdToken(prettyPrintJSON(generatedIdToken)); } else if (isSamlProtocol()) { const [generatedSamlResponse] = responses; setSamlResponse(generatedSamlResponse as any); } }, [form.getValues("user"), selected, selectedAudience], ); return ( <> {enabled && ( {t("evaluateExplain")} )}
{supportsScopeSelection(protocol) && ( } > setIsScopeOpen(isOpen)} isOpen={isScopeOpen} selections={selected} onSelect={(value) => { const option = value as string; if (selected.includes(option)) { if (option !== openidPrefix) { setSelected( selected.filter((item) => item !== option), ); } } else { setSelected([...selected, option]); } }} aria-labelledby={t("scopeParameter")} placeholderText={t("scopeParameterPlaceholder")} > {selectableScopes.map((option, index) => ( {option.name} ))} {selected.join(" ")} )} {hasViewUsers && ( )}
{isOpenIdConnectProtocol() && ( )} {isOpenIdConnectProtocol() && ( )} {isOpenIdConnectProtocol() && ( )} {isSamlProtocol() && ( )} setActiveTab(key as number)} > {t("effectiveProtocolMappers")}{" "} } tabContentRef={tabContent1} /> {t("effectiveRoleScopeMappings")}{" "} } tabContentRef={tabContent2} > {isOpenIdConnectProtocol() && ( {t("generatedAccessToken")}{" "} } tabContentRef={tabContent3} /> )} {isOpenIdConnectProtocol() && ( {t("generatedIdToken")}{" "} } tabContentRef={tabContent4} /> )} {isOpenIdConnectProtocol() && ( {t("generatedUserInfo")}{" "} } tabContentRef={tabContent5} /> )} {isSamlProtocol() && ( {t("generatedSamlResponse")}{" "} } tabContentRef={tabContent6} /> )} ); };