-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ssl with certificate (#168)
- Loading branch information
Showing
8 changed files
with
239 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
src/renderer/components/ConnectionListTree/ConnectionConfigEditor/CertificatePicker.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; | ||
import styles from './certificate.module.scss'; | ||
import { | ||
faCheckCircle, | ||
faCircleMinus, | ||
faCircleXmark, | ||
faFolder, | ||
} from '@fortawesome/free-solid-svg-icons'; | ||
import { useCallback } from 'react'; | ||
import { ConnectionSslOption } from 'drivers/base/SQLLikeConnection'; | ||
|
||
interface PickerButtonProps { | ||
label: string; | ||
onChange: (v?: string) => void; | ||
value?: string; | ||
} | ||
|
||
interface CertificatePickerProps { | ||
value?: ConnectionSslOption; | ||
onChange: (v: ConnectionSslOption | undefined) => void; | ||
} | ||
|
||
function CertificatePickerButton({ | ||
label, | ||
onChange, | ||
value, | ||
}: PickerButtonProps) { | ||
const onClick = useCallback(() => { | ||
window.electron.showOpenDialog({}).then((e) => { | ||
if (!e.canceled) { | ||
if (e.filePaths.length === 1) { | ||
window.electron | ||
.readFile(e.filePaths[0]) | ||
.then((content) => onChange(new TextDecoder().decode(content))); | ||
} | ||
} | ||
}); | ||
}, [onChange]); | ||
|
||
const className = [styles.button, value ? styles.provided : undefined] | ||
.filter(Boolean) | ||
.join(' '); | ||
|
||
return ( | ||
<div className={className}> | ||
<div onClick={onClick}> | ||
<FontAwesomeIcon | ||
icon={value ? faCheckCircle : faCircleMinus} | ||
style={{ opacity: value ? 1 : 0.4, marginRight: 5 }} | ||
/> | ||
|
||
<span>{label}</span> | ||
</div> | ||
{value && ( | ||
<div | ||
className={`${styles.icon} ${styles.close}`} | ||
onClick={() => { | ||
onChange(undefined); | ||
}} | ||
> | ||
<FontAwesomeIcon | ||
icon={faCircleXmark} | ||
style={{ opacity: 1, marginRight: 5 }} | ||
/> | ||
</div> | ||
)} | ||
{!value && ( | ||
<div className={styles.icon} onClick={onClick}> | ||
<FontAwesomeIcon | ||
icon={faFolder} | ||
style={{ opacity: 0.4, marginRight: 5 }} | ||
/> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
} | ||
|
||
export default function CertificatePicker({ | ||
value, | ||
onChange, | ||
}: CertificatePickerProps) { | ||
const onUpdate = useCallback( | ||
(property: 'ca' | 'cert' | 'key', newValue?: string) => { | ||
let tmp: ConnectionSslOption | undefined; | ||
if (!value) tmp = { [property]: newValue }; | ||
else if (value === true) { | ||
tmp = { [property]: newValue }; | ||
} else { | ||
tmp = { ...value, [property]: newValue }; | ||
} | ||
|
||
// Remove all the undefined property | ||
if (typeof tmp === 'object') { | ||
for (const tmpKey of Object.keys(tmp)) { | ||
const key = tmpKey as keyof ConnectionSslOption; | ||
if (!tmp[key]) { | ||
delete tmp[key]; | ||
} | ||
} | ||
|
||
if (Object.entries(tmp).length === 0) { | ||
onChange(value === true ? true : undefined); | ||
} else onChange(tmp); | ||
} else { | ||
onChange(undefined); | ||
} | ||
}, | ||
[value, onChange], | ||
); | ||
|
||
const ca = typeof value === 'object' ? value.ca : undefined; | ||
const cert = typeof value === 'object' ? value.cert : undefined; | ||
const key = typeof value === 'object' ? value.key : undefined; | ||
const ssl = !!value; | ||
|
||
return ( | ||
<div className={styles.container}> | ||
<h3> | ||
<label> | ||
<input | ||
type="checkbox" | ||
checked={ssl} | ||
onClick={() => { | ||
if (!value) onChange(true); | ||
if (value === true) onChange(undefined); | ||
}} | ||
/> | ||
<span> Over SSL</span> | ||
</label> | ||
</h3> | ||
<div className={styles.buttonGroup}> | ||
<CertificatePickerButton | ||
label="CA Certificate" | ||
value={ca} | ||
onChange={(e) => onUpdate('ca', e)} | ||
/> | ||
<CertificatePickerButton | ||
label="Certificate" | ||
value={cert} | ||
onChange={(e) => onUpdate('cert', e)} | ||
/> | ||
<CertificatePickerButton | ||
label="Key" | ||
value={key} | ||
onChange={(e) => onUpdate('key', e)} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/renderer/components/ConnectionListTree/ConnectionConfigEditor/certificate.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
.container { | ||
h3 { | ||
font-size: 1rem; | ||
border-bottom: 1px solid var(--color-border); | ||
padding-bottom: 5px; | ||
margin-bottom: 10px; | ||
} | ||
|
||
border-bottom: 1px solid var(--color-border); | ||
padding-bottom: 10px; | ||
} | ||
|
||
.buttonGroup { | ||
display: flex; | ||
gap: 10px; | ||
} | ||
|
||
.button { | ||
border-radius: 4px; | ||
overflow: hidden; | ||
cursor: pointer; | ||
display: flex; | ||
|
||
> div { | ||
background: var(--color-input-border); | ||
padding: 10px; | ||
|
||
&:hover { | ||
opacity: 0.8; | ||
} | ||
} | ||
} | ||
|
||
.icon { | ||
border-left: 1px solid var(--color-surface); | ||
} | ||
|
||
.button.provided { | ||
> div { | ||
background: #27ae60; | ||
color: #fff; | ||
} | ||
} | ||
|
||
.close { | ||
background: #e74c3c !important; | ||
color: #fff !important; | ||
} |
2 changes: 1 addition & 1 deletion
2
src/renderer/components/ConnectionListTree/ConnectionConfigEditor/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters