|
| 1 | +import { Button, FormHelperText, Grid, Typography } from '@mui/material'; |
| 2 | +import React, { FC, useState } from 'react'; |
| 3 | +import TextArea from './TextArea'; |
| 4 | + |
| 5 | +export type ManualClientCertSelectionProps = { |
| 6 | + onChangeCert: (cert: string) => void; |
| 7 | + onChangeKey: (key: string) => void; |
| 8 | +}; |
| 9 | + |
| 10 | +const ManualClientCertSelection: FC<ManualClientCertSelectionProps> = ({ |
| 11 | + onChangeCert, |
| 12 | + onChangeKey, |
| 13 | +}) => { |
| 14 | + const [certText, setCertText] = useState(''); |
| 15 | + const [keyText, setKeyText] = useState(''); |
| 16 | + |
| 17 | + const handleCertText = (evt: React.ChangeEvent<HTMLInputElement>) => { |
| 18 | + setCertText(evt.target.value); |
| 19 | + onChangeCert(evt.target.value); |
| 20 | + }; |
| 21 | + |
| 22 | + const handleKeyText = (evt: React.ChangeEvent<HTMLInputElement>) => { |
| 23 | + setKeyText(evt.target.value); |
| 24 | + onChangeKey(evt.target.value); |
| 25 | + }; |
| 26 | + |
| 27 | + const handleCertFile = (evt: React.ChangeEvent<HTMLInputElement>) => { |
| 28 | + const file = evt.target.files?.[0]; |
| 29 | + if (file) { |
| 30 | + const reader = new FileReader(); |
| 31 | + reader.onload = (e) => { |
| 32 | + setCertText(e?.target?.result as string); |
| 33 | + onChangeCert(e?.target?.result as string); |
| 34 | + }; |
| 35 | + reader.readAsText(file); |
| 36 | + } |
| 37 | + }; |
| 38 | + |
| 39 | + const handleKeyFile = (evt: React.ChangeEvent<HTMLInputElement>) => { |
| 40 | + const file = evt.target.files?.[0]; |
| 41 | + if (file) { |
| 42 | + const reader = new FileReader(); |
| 43 | + reader.onload = (e) => { |
| 44 | + setKeyText(e?.target?.result as string); |
| 45 | + onChangeKey(e?.target?.result as string); |
| 46 | + }; |
| 47 | + reader.readAsText(file); |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + return ( |
| 52 | + <> |
| 53 | + <Grid item xs={12}> |
| 54 | + <label htmlFor="cert-file"> |
| 55 | + <input |
| 56 | + style={{ display: 'none' }} |
| 57 | + id="cert-file" |
| 58 | + type="file" |
| 59 | + onChange={handleCertFile} |
| 60 | + /> |
| 61 | + <Button variant="contained" color="primary" component="span"> |
| 62 | + Client Certificate from File |
| 63 | + </Button> |
| 64 | + </label> |
| 65 | + </Grid> |
| 66 | + <Grid item xs={12}> |
| 67 | + <Typography variant="body2">Client Certificate Text</Typography> |
| 68 | + <TextArea |
| 69 | + fullWidth |
| 70 | + variant="filled" |
| 71 | + value={certText} |
| 72 | + multiline |
| 73 | + required={!!keyText} |
| 74 | + rows={5} |
| 75 | + placeholder="e.g. copy/paste the cert in PEM format" |
| 76 | + onChange={handleCertText} |
| 77 | + spellCheck={false} |
| 78 | + /> |
| 79 | + <FormHelperText sx={{ pl: 2 }}> |
| 80 | + Add a Client Certificate with the File Selector or Copy/Paste to the |
| 81 | + Text Area. Key is required if the Certificate is present. |
| 82 | + </FormHelperText> |
| 83 | + </Grid> |
| 84 | + <Grid item xs={12}> |
| 85 | + <label htmlFor="key-file"> |
| 86 | + <input |
| 87 | + style={{ display: 'none' }} |
| 88 | + id="key-file" |
| 89 | + type="file" |
| 90 | + onChange={handleKeyFile} |
| 91 | + /> |
| 92 | + <Button variant="contained" color="primary" component="span"> |
| 93 | + Client Certificate Key from File |
| 94 | + </Button> |
| 95 | + </label> |
| 96 | + </Grid> |
| 97 | + <Grid item xs={12}> |
| 98 | + <Typography variant="body2">Client Certificate Key Text</Typography> |
| 99 | + <TextArea |
| 100 | + fullWidth |
| 101 | + variant="filled" |
| 102 | + value={keyText} |
| 103 | + required={!!certText} |
| 104 | + multiline |
| 105 | + rows={5} |
| 106 | + placeholder="e.g. copy/paste the key in PEM format" |
| 107 | + onChange={handleKeyText} |
| 108 | + /> |
| 109 | + <FormHelperText sx={{ pl: 2 }}> |
| 110 | + Add a Client Certificate Key with the File Selector or Copy/Paste to |
| 111 | + the Text Area. Certificate is required if the Key is present. |
| 112 | + </FormHelperText> |
| 113 | + </Grid> |
| 114 | + </> |
| 115 | + ); |
| 116 | +}; |
| 117 | + |
| 118 | +export default ManualClientCertSelection; |
0 commit comments