Debian Codegraph @

install/freeipa-webui/src/pages/CertificateMapping/CertificateMappingMatch.tsx

raw
1 import React from "react";
2 // PatternFly
3 import {
4 Button,
5 Flex,
6 FlexItem,
7 Form,
8 FormGroup,
9 Sidebar,
10 SidebarContent,
11 SidebarPanel,
12 TextArea,
13 TextInput,
14 } from "@patternfly/react-core";
15 import { Th, Tr } from "@patternfly/react-table";
16 // Data types
17 import { Certificate } from "src/utils/datatypes/globalDataTypes";
18 // Redux
19 import { useAppDispatch } from "src/store/hooks";
20 // Hooks
21 import { addAlert } from "src/store/Global/alerts-slice";
22 import useUpdateRoute from "src/hooks/useUpdateRoute";
23 // Icons
24 import { OutlinedQuestionCircleIcon } from "@patternfly/react-icons";
25 // Utils
26 import { removeCertificateDelimiters } from "src/utils/utils";
27 // RPC
28 import { useMatchCertificateMutation } from "src/services/rpcCertMapping";
29 import { ErrorResult } from "src/services/rpc";
30 // Components
31 import TitleLayout from "src/components/layouts/TitleLayout";
32 import HelpTextWithIconLayout from "src/components/layouts/HelpTextWithIconLayout";
33 import PageWithGrayBorderLayout from "src/components/layouts/PageWithGrayBorderLayout";
34 import TableLayout from "src/components/layouts/TableLayout";
35
36 interface TableEntry {
37 userLogin: string;
38 domain: string;
39 }
40
41 const CertificateMappingMatch = () => {
42 const dispatch = useAppDispatch();
43
44 // Update current route data to Redux and highlight the current page in the Nav bar
45 const { browserTitle } = useUpdateRoute({
46 pathname: "cert-id-mapping-match",
47 });
48
49 // Set the page title to be shown in the browser tab
50 React.useEffect(() => {
51 document.title = browserTitle;
52 }, [browserTitle]);
53
54 // API calls
55 const [matchCertificate] = useMatchCertificateMutation();
56
57 // States
58 const [certificateText, setCertificateText] = React.useState<string>("");
59 const [issuedBy, setIssuedBy] = React.useState<string>("");
60 const [issuedTo, setIssuedTo] = React.useState<string>("");
61 const [serialNumber, setSerialNumber] = React.useState<string>("");
62 const [serialNumberHex, setSerialNumberHex] = React.useState<string>("");
63 const [validFrom, setValidFrom] = React.useState<string>("");
64 const [validTo, setValidTo] = React.useState<string>("");
65 const [fingerprintSha1, setFingerprintSha1] = React.useState<string>("");
66 const [fingerprintSha256, setFingerprintSha256] = React.useState<string>("");
67 // Table data
68 const [tableElements, setTableElements] = React.useState<TableEntry[]>([]);
69
70 // Extract subject values
71 const extractSubjectValues = (subject: string) => {
72 // Split the subject string into an array of strings
73 const subjectArray = subject.split(",");
74 // Find the "CN" and "O" values in the subject array
75 const cnValueWithIndicator = subjectArray.find((item) =>
76 item.trim().startsWith("CN=")
77 );
78 const oValueWithIndicator = subjectArray.find((item) =>
79 item.trim().startsWith("O=")
80 );
81 // Extract the values from the strings
82 const cnValue = cnValueWithIndicator
83 ? cnValueWithIndicator.split("=")[1].trim()
84 : "";
85 const oValue = oValueWithIndicator
86 ? oValueWithIndicator.split("=")[1].trim()
87 : "";
88
89 return { cnValue, oValue };
90 };
91
92 // On match certificate
93 const onMatchCertificate = () => {
94 const parsedCert = removeCertificateDelimiters(certificateText);
95
96 matchCertificate(parsedCert).then((response) => {
97 if ("data" in response && response.data?.result) {
98 if (response.data.result.results[0].error) {
99 const errorMessage = response.data.result.results[0].error as string;
100 dispatch(
101 addAlert({
102 name: "match-certificate-error-1",
103 title: errorMessage,
104 variant: "danger",
105 })
106 );
107 } else {
108 const certData = response.data.result.results[1]
109 .result[0] as Certificate;
110 // Set certificate data
111 setIssuedBy(certData.issuer);
112 setIssuedTo(certData.subject);
113 setSerialNumber(certData.serial_number);
114 setSerialNumberHex(certData.serial_number_hex);
115 setValidFrom(certData.valid_not_before);
116 setValidTo(certData.valid_not_after);
117 setFingerprintSha1(certData.sha1_fingerprint);
118 setFingerprintSha256(certData.sha256_fingerprint);
119 // Set table data
120 const matchedUsers: TableEntry[] = [];
121 const subject = certData.subject;
122 const { cnValue, oValue } = extractSubjectValues(subject);
123 // Add the values to the matchedUsers array
124 if (cnValue && oValue) {
125 matchedUsers.push({
126 userLogin: cnValue,
127 domain: oValue,
128 });
129 }
130 setTableElements(matchedUsers);
131
132 // Set alert: success
133 dispatch(
134 addAlert({
135 name: "match-certificate-success",
136 title: response.data.result.results[0].summary,
137 variant: "success",
138 })
139 );
140 }
141 } else if ("data" in response && response.data?.error) {
142 // Set alert: error
143 const errorMessage = response.data.error as unknown as ErrorResult;
144 dispatch(
145 addAlert({
146 name: "match-certificate-error",
147 title: errorMessage.message,
148 variant: "danger",
149 })
150 );
151 }
152 });
153 };
154
155 // On clear certificate data
156 const onClearCertificate = () => {
157 setCertificateText("");
158 setIssuedBy("");
159 setIssuedTo("");
160 setSerialNumber("");
161 setSerialNumberHex("");
162 setValidFrom("");
163 setValidTo("");
164 setFingerprintSha1("");
165 setFingerprintSha256("");
166 setTableElements([]);
167 };
168
169 // Toolbar
170 const toolbarFields = [
171 {
172 key: 0,
173 element: (
174 <Button
175 data-cy="certificate-identity-mapping-match-button-match"
176 onClick={onMatchCertificate}
177 >
178 Match
179 </Button>
180 ),
181 },
182 {
183 key: 1,
184 element: (
185 <Button
186 data-cy="certificate-identity-mapping-match-button-clear"
187 variant="secondary"
188 onClick={onClearCertificate}
189 >
190 Clear
191 </Button>
192 ),
193 },
194 ];
195
196 // Table
197 const header = (
198 <Tr key="header" id="table-header">
199 <Th modifier="wrap" key="user_login">
200 User login
201 </Th>
202 <Th modifier="wrap" key="domain">
203 Domain
204 </Th>
205 </Tr>
206 );
207
208 const body = tableElements.map((element, rowIndex) => {
209 return (
210 <Tr key={rowIndex} id={`table-row-${rowIndex}`}>
211 <td>{element.userLogin}</td>
212 <td>{element.domain}</td>
213 </Tr>
214 );
215 });
216
217 // Return component
218 return (
219 <PageWithGrayBorderLayout
220 id="certificate-mapping-match"
221 pageTitle="Certificate Mapping Match"
222 toolbarItems={toolbarFields}
223 >
224 <>
225 <Sidebar isPanelRight>
226 <SidebarPanel variant="sticky" className="pf-v6-u-pl-md">
227 <HelpTextWithIconLayout
228 textContent="Help"
229 icon={
230 <OutlinedQuestionCircleIcon className="pf-v6-u-primary-color-100" />
231 }
232 />
233 </SidebarPanel>
234 <SidebarContent>
235 <Flex direction={{ default: "column", lg: "row" }}>
236 <FlexItem flex={{ default: "flex_1" }}>
237 <TitleLayout
238 key={0}
239 headingLevel="h1"
240 id="certificate-for-match"
241 text="Certificate for match"
242 className="pf-v6-u-mt-0 pf-v6-u-ml-0 pf-v6-u-mb-md"
243 />
244 <Form className="pf-v6-u-mb-lg">
245 <FormGroup label="Certificate" fieldId="cert_textarea">
246 <TextArea
247 data-cy="certificate-identity-mapping-match-textbox-certificate"
248 name="cert_textarea"
249 aria-label="Certificate"
250 value={certificateText}
251 onChange={(_event, value) => setCertificateText(value)}
252 resizeOrientation="vertical"
253 rows={15}
254 />
255 </FormGroup>
256 </Form>
257 </FlexItem>
258 <FlexItem flex={{ default: "flex_1" }}>
259 <TitleLayout
260 key={1}
261 headingLevel="h1"
262 id="certificate-data"
263 text="Certificate data"
264 className="pf-v6-u-mt-lg pf-v6-u-mb-md"
265 />
266 <Form className="pf-v6-u-mb-lg">
267 <FormGroup label="Issued by" fieldId="issuer">
268 <TextInput
269 data-cy="certificate-identity-mapping-match-textbox-issuer"
270 name="issuer"
271 aria-label="Issued by text input"
272 type="text"
273 value={issuedBy}
274 isDisabled
275 />
276 </FormGroup>
277 <FormGroup label="Issued to" fieldId="subject">
278 <TextInput
279 data-cy="certificate-identity-mapping-match-textbox-subject"
280 name="subject"
281 aria-label="Issued to text input"
282 type="text"
283 value={issuedTo}
284 isDisabled
285 />
286 </FormGroup>
287 <FormGroup label="Serial number" fieldId="serial_number">
288 <TextInput
289 data-cy="certificate-identity-mapping-match-textbox-serial-number"
290 name="serial_number"
291 aria-label="Serial number text input"
292 type="text"
293 value={serialNumber}
294 isDisabled
295 />
296 </FormGroup>
297 <FormGroup
298 label="Serial number (hex)"
299 fieldId="serial_number_hex"
300 >
301 <TextInput
302 data-cy="certificate-identity-mapping-match-textbox-serial-number-hex"
303 name="serial_number_hex"
304 aria-label="Serial number in hexadecimal text input"
305 type="text"
306 value={serialNumberHex}
307 isDisabled
308 />
309 </FormGroup>
310 <FormGroup label="Valid from" fieldId="valid_not_before">
311 <TextInput
312 data-cy="certificate-identity-mapping-match-textbox-valid-from"
313 name="valid_not_before"
314 aria-label="Valid from text input"
315 type="text"
316 value={validFrom}
317 isDisabled
318 />
319 </FormGroup>
320 <FormGroup label="Valid to" fieldId="valid_not_after">
321 <TextInput
322 data-cy="certificate-identity-mapping-match-textbox-valid-to"
323 name="valid_not_after"
324 aria-label="Valid to text input"
325 type="text"
326 value={validTo}
327 isDisabled
328 />
329 </FormGroup>
330 <FormGroup
331 label="SHA1 Fingerprint"
332 fieldId="sha1_fingerprint"
333 >
334 <TextArea
335 data-cy="certificate-identity-mapping-match-textbox-sha1-fingerprint"
336 name="sha1_fingerprint"
337 aria-label="SHA1 Fingerprint text input"
338 value={fingerprintSha1}
339 resizeOrientation="vertical"
340 rows={2}
341 isDisabled
342 />
343 </FormGroup>
344 <FormGroup
345 label="SHA256 Fingerprint"
346 fieldId="sha256_fingerprint"
347 >
348 <TextArea
349 data-cy="certificate-identity-mapping-match-textbox-sha256-fingerprint"
350 name="sha256_fingerprint"
351 aria-label="SHA256 Fingerprint text input"
352 value={fingerprintSha256}
353 resizeOrientation="vertical"
354 rows={2}
355 isDisabled
356 autoResize={false}
357 />
358 </FormGroup>
359 </Form>
360 </FlexItem>
361 </Flex>
362 <TitleLayout
363 key={2}
364 headingLevel="h1"
365 id="matched-users"
366 text="Matched users"
367 className="pf-v6-u-mt-lg pf-v6-u-mb-md"
368 />
369 <TableLayout
370 ariaLabel={"Matched users"}
371 variant={"compact"}
372 hasBorders={true}
373 classes={"pf-v6-u-mt-md"}
374 tableId={"matched-users-table"}
375 isStickyHeader={true}
376 tableHeader={header}
377 tableBody={body}
378 />
379 </SidebarContent>
380 </Sidebar>
381 </>
382 </PageWithGrayBorderLayout>
383 );
384 };
385
386 export default CertificateMappingMatch;