RSAGeneratedModal.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import React, {useState} from "../_snowpack/pkg/react.js";
  2. import {
  3. AlertVariant,
  4. Button,
  5. ButtonVariant,
  6. Form,
  7. FormGroup,
  8. Modal,
  9. ModalVariant,
  10. Select,
  11. SelectOption,
  12. SelectVariant,
  13. Switch,
  14. TextInput
  15. } from "../_snowpack/pkg/@patternfly/react-core.js";
  16. import {useTranslation} from "../_snowpack/pkg/react-i18next.js";
  17. import {Controller, useForm} from "../_snowpack/pkg/react-hook-form.js";
  18. import {useAdminClient} from "../context/auth/AdminClient.js";
  19. import {useAlerts} from "../components/alert/Alerts.js";
  20. import {HelpItem} from "../components/help-enabler/HelpItem.js";
  21. import {useServerInfo} from "../context/server-info/ServerInfoProvider.js";
  22. import {KEY_PROVIDER_TYPE} from "../util.js";
  23. export const RSAGeneratedModal = ({
  24. providerType,
  25. handleModalToggle,
  26. open,
  27. refresh
  28. }) => {
  29. const {t} = useTranslation("realm-settings");
  30. const serverInfo = useServerInfo();
  31. const adminClient = useAdminClient();
  32. const {addAlert, addError} = useAlerts();
  33. const {handleSubmit, control} = useForm({});
  34. const [isKeySizeDropdownOpen, setIsKeySizeDropdownOpen] = useState(false);
  35. const [isRSAalgDropdownOpen, setIsRSAalgDropdownOpen] = useState(false);
  36. const allComponentTypes = serverInfo.componentTypes?.[KEY_PROVIDER_TYPE] ?? [];
  37. const save = async (component) => {
  38. try {
  39. await adminClient.components.create({
  40. ...component,
  41. parentId: component.parentId,
  42. providerId: providerType,
  43. providerType: KEY_PROVIDER_TYPE,
  44. config: {priority: ["0"]}
  45. });
  46. handleModalToggle();
  47. addAlert(t("saveProviderSuccess"), AlertVariant.success);
  48. refresh();
  49. } catch (error) {
  50. addError("realm-settings:saveProviderError", error);
  51. }
  52. };
  53. return /* @__PURE__ */ React.createElement(Modal, {
  54. className: "add-provider-modal",
  55. variant: ModalVariant.medium,
  56. title: t("addProvider"),
  57. isOpen: open,
  58. onClose: handleModalToggle,
  59. actions: [
  60. /* @__PURE__ */ React.createElement(Button, {
  61. "data-testid": "add-provider-button",
  62. key: "confirm",
  63. variant: "primary",
  64. type: "submit",
  65. form: "add-provider"
  66. }, t("common:Add")),
  67. /* @__PURE__ */ React.createElement(Button, {
  68. id: "modal-cancel",
  69. key: "cancel",
  70. variant: ButtonVariant.link,
  71. onClick: () => {
  72. handleModalToggle();
  73. }
  74. }, t("common:cancel"))
  75. ]
  76. }, /* @__PURE__ */ React.createElement(Form, {
  77. isHorizontal: true,
  78. id: "add-provider",
  79. className: "pf-u-mt-lg",
  80. onSubmit: handleSubmit(save)
  81. }, /* @__PURE__ */ React.createElement(FormGroup, {
  82. label: t("consoleDisplayName"),
  83. fieldId: "kc-console-display-name",
  84. labelIcon: /* @__PURE__ */ React.createElement(HelpItem, {
  85. helpText: "realm-settings-help:displayName",
  86. fieldLabelId: "realm-settings:loginTheme"
  87. })
  88. }, /* @__PURE__ */ React.createElement(Controller, {
  89. name: "name",
  90. control,
  91. defaultValue: providerType,
  92. render: ({onChange}) => /* @__PURE__ */ React.createElement(TextInput, {
  93. "aria-label": t("consoleDisplayName"),
  94. defaultValue: providerType,
  95. onChange: (value) => {
  96. onChange(value);
  97. },
  98. "data-testid": "display-name-input"
  99. })
  100. })), /* @__PURE__ */ React.createElement(FormGroup, {
  101. label: t("common:enabled"),
  102. fieldId: "kc-enabled",
  103. labelIcon: /* @__PURE__ */ React.createElement(HelpItem, {
  104. helpText: "realm-settings:realm-settings-help:enabled",
  105. fieldLabelId: "enabled"
  106. })
  107. }, /* @__PURE__ */ React.createElement(Controller, {
  108. name: "config.enabled",
  109. control,
  110. defaultValue: ["true"],
  111. render: ({onChange, value}) => /* @__PURE__ */ React.createElement(Switch, {
  112. id: "kc-enabled",
  113. label: t("common:on"),
  114. labelOff: t("common:off"),
  115. isChecked: value[0] === "true",
  116. "data-testid": value[0] === "true" ? "internationalization-enabled" : "internationalization-disabled",
  117. onChange: (value2) => {
  118. onChange([value2 + ""]);
  119. }
  120. })
  121. })), /* @__PURE__ */ React.createElement(FormGroup, {
  122. label: t("active"),
  123. fieldId: "kc-active",
  124. labelIcon: /* @__PURE__ */ React.createElement(HelpItem, {
  125. helpText: "realm-settings-help:active",
  126. fieldLabelId: "realm-settings:active"
  127. })
  128. }, /* @__PURE__ */ React.createElement(Controller, {
  129. name: "config.active",
  130. control,
  131. defaultValue: ["true"],
  132. render: ({onChange, value}) => /* @__PURE__ */ React.createElement(Switch, {
  133. id: "kc-active",
  134. label: t("common:on"),
  135. labelOff: t("common:off"),
  136. isChecked: value[0] === "true",
  137. "data-testid": value[0] === "true" ? "internationalization-enabled" : "internationalization-disabled",
  138. onChange: (value2) => {
  139. onChange([value2 + ""]);
  140. }
  141. })
  142. })), providerType === "rsa-generated" && /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(FormGroup, {
  143. label: t("algorithm"),
  144. fieldId: "kc-algorithm",
  145. labelIcon: /* @__PURE__ */ React.createElement(HelpItem, {
  146. helpText: "realm-settings-help:algorithm",
  147. fieldLabelId: "realm-settings:algorithm"
  148. })
  149. }, /* @__PURE__ */ React.createElement(Controller, {
  150. name: "config.algorithm",
  151. control,
  152. defaultValue: ["RS256"],
  153. render: ({onChange, value}) => /* @__PURE__ */ React.createElement(Select, {
  154. toggleId: "kc-rsa-algorithm",
  155. onToggle: () => setIsRSAalgDropdownOpen(!isRSAalgDropdownOpen),
  156. onSelect: (_, value2) => {
  157. onChange([value2 + ""]);
  158. setIsRSAalgDropdownOpen(false);
  159. },
  160. selections: [value + ""],
  161. variant: SelectVariant.single,
  162. "aria-label": t("algorithm"),
  163. isOpen: isRSAalgDropdownOpen,
  164. "data-testid": "select-rsa-algorithm"
  165. }, allComponentTypes[5].properties[3].options.map((p, idx) => /* @__PURE__ */ React.createElement(SelectOption, {
  166. selected: p === value,
  167. key: `rsa-algorithm-${idx}`,
  168. value: p
  169. })))
  170. })), /* @__PURE__ */ React.createElement(FormGroup, {
  171. label: t("AESKeySize"),
  172. fieldId: "kc-aes-keysize",
  173. labelIcon: /* @__PURE__ */ React.createElement(HelpItem, {
  174. helpText: "realm-settings-help:AESKeySize",
  175. fieldLabelId: "realm-settings:AESKeySize"
  176. })
  177. }, /* @__PURE__ */ React.createElement(Controller, {
  178. name: "config.secretSize",
  179. control,
  180. defaultValue: ["2048"],
  181. render: ({onChange, value}) => /* @__PURE__ */ React.createElement(Select, {
  182. toggleId: "kc-rsa-keysize",
  183. onToggle: () => setIsKeySizeDropdownOpen(!isKeySizeDropdownOpen),
  184. onSelect: (_, value2) => {
  185. onChange([value2 + ""]);
  186. setIsKeySizeDropdownOpen(false);
  187. },
  188. selections: [value + ""],
  189. isOpen: isKeySizeDropdownOpen,
  190. variant: SelectVariant.single,
  191. "aria-label": t("keySize"),
  192. "data-testid": "select-secret-size"
  193. }, allComponentTypes[5].properties[4].options.map((item, idx) => /* @__PURE__ */ React.createElement(SelectOption, {
  194. selected: item === value,
  195. key: `rsa-generated-key-size-${idx}`,
  196. value: item
  197. })))
  198. })))));
  199. };