mirror of
https://github.com/pterodactyl/panel.git
synced 2026-11-30 11:25:52 -05:00
mask database password when viewing credentials
This commit is contained in:
@@ -15,6 +15,7 @@ import Label from '@/components/elements/Label';
|
||||
import { TextInput } from '@/components/form/controls';
|
||||
import Button from '@/components/elements/Button';
|
||||
import CopyOnClick from '@/components/elements/CopyOnClick';
|
||||
import SecretInput from '@/components/elements/SecretInput';
|
||||
import { DeleteAction, RowActionButton, RowActions } from '@/components/elements/table/RowActions';
|
||||
import { relationshipAttributes } from '@/api/relationships';
|
||||
|
||||
@@ -154,9 +155,7 @@ function ServerDatabaseConnectionDialog({ serverId, database, open, onClose }: S
|
||||
</div>
|
||||
<div className={'mt-6'}>
|
||||
<Label>Password</Label>
|
||||
<CopyOnClick text={password} showInNotification={false}>
|
||||
<TextInput type={'text'} readOnly value={password} />
|
||||
</CopyOnClick>
|
||||
<SecretInput value={password} label={'Password'} />
|
||||
</div>
|
||||
<div className={'mt-6'}>
|
||||
<Label>JDBC Connection String</Label>
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
/** @vitest-environment jsdom */
|
||||
|
||||
import { act, cleanup, fireEvent, render, screen } from '@testing-library/react';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import SecretInput from './SecretInput';
|
||||
|
||||
const clipboard = vi.hoisted(() => ({ copy: vi.fn<(text: string) => Promise<boolean>>() }));
|
||||
|
||||
vi.mock('@/lib/clipboard', () => ({ default: clipboard.copy }));
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
document.getElementById('modal-portal')?.remove();
|
||||
});
|
||||
beforeEach(() => {
|
||||
const portal = document.createElement('div');
|
||||
portal.id = 'modal-portal';
|
||||
document.body.append(portal);
|
||||
clipboard.copy.mockReset();
|
||||
clipboard.copy.mockResolvedValue(true);
|
||||
});
|
||||
|
||||
describe('SecretInput', () => {
|
||||
it('masks the value until revealed', () => {
|
||||
const { container } = render(<SecretInput value={'hunter2'} label={'Password'} />);
|
||||
const input = container.querySelector('input')!;
|
||||
|
||||
expect(input).toHaveAttribute('type', 'password');
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Show password' }));
|
||||
expect(input).toHaveAttribute('type', 'text');
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Hide password' }));
|
||||
expect(input).toHaveAttribute('type', 'password');
|
||||
});
|
||||
|
||||
it('copies the value without revealing it', async () => {
|
||||
const { container } = render(<SecretInput value={'hunter2'} label={'Password'} />);
|
||||
|
||||
await act(async () => fireEvent.click(screen.getByRole('button', { name: 'Copy password' })));
|
||||
|
||||
expect(clipboard.copy).toHaveBeenCalledTimes(1);
|
||||
expect(clipboard.copy).toHaveBeenCalledWith('hunter2');
|
||||
expect(container.querySelector('input')).toHaveAttribute('type', 'password');
|
||||
expect(screen.getByRole('status')).toHaveTextContent('Copied text to clipboard.');
|
||||
});
|
||||
|
||||
it('disables both actions when there is no value', () => {
|
||||
render(<SecretInput value={undefined} label={'Password'} />);
|
||||
|
||||
expect(screen.getByRole('button', { name: 'Show password' })).toBeDisabled();
|
||||
expect(screen.getByRole('button', { name: 'Copy password' })).toBeDisabled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,67 @@
|
||||
import { useState } from 'react';
|
||||
import { Copy, Eye, EyeOff } from 'lucide-react';
|
||||
import Button from '@/components/elements/Button';
|
||||
import CopyOnClick from '@/components/elements/CopyOnClick';
|
||||
import Icon from '@/components/elements/Icon';
|
||||
import Tooltip from '@/components/elements/tooltip/Tooltip';
|
||||
import { TextInput } from '@/components/form/controls';
|
||||
|
||||
interface SecretInputProps {
|
||||
id?: string;
|
||||
value: string | null | undefined;
|
||||
label: string;
|
||||
}
|
||||
|
||||
const SecretInput = ({ id, value, label }: SecretInputProps) => {
|
||||
const [visible, setVisible] = useState(false);
|
||||
const toggleLabel = `${visible ? 'Hide' : 'Show'} ${label.toLowerCase()}`;
|
||||
const copyLabel = `Copy ${label.toLowerCase()}`;
|
||||
|
||||
return (
|
||||
<div className={'relative'}>
|
||||
<TextInput
|
||||
id={id}
|
||||
type={visible ? 'text' : 'password'}
|
||||
readOnly
|
||||
autoComplete={'off'}
|
||||
spellCheck={false}
|
||||
value={value ?? ''}
|
||||
className={'pr-20 font-mono'}
|
||||
/>
|
||||
<div className={'absolute inset-y-0 right-2 flex items-center gap-1'}>
|
||||
<Tooltip content={toggleLabel}>
|
||||
<span className={'inline-flex'}>
|
||||
<Button.Text
|
||||
type={'button'}
|
||||
size={'xsmall'}
|
||||
isSecondary
|
||||
aria-label={toggleLabel}
|
||||
aria-pressed={visible}
|
||||
disabled={!value}
|
||||
onClick={() => setVisible((current) => !current)}
|
||||
>
|
||||
<Icon icon={visible ? EyeOff : Eye} className={'h-3.5 w-3.5'} />
|
||||
</Button.Text>
|
||||
</span>
|
||||
</Tooltip>
|
||||
<Tooltip content={copyLabel}>
|
||||
<span className={'inline-flex'}>
|
||||
<CopyOnClick text={value} showInNotification={false}>
|
||||
<Button.Text
|
||||
type={'button'}
|
||||
size={'xsmall'}
|
||||
isSecondary
|
||||
aria-label={copyLabel}
|
||||
disabled={!value}
|
||||
>
|
||||
<Icon icon={Copy} className={'h-3.5 w-3.5'} />
|
||||
</Button.Text>
|
||||
</CopyOnClick>
|
||||
</span>
|
||||
</Tooltip>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default SecretInput;
|
||||
@@ -11,6 +11,7 @@ import Button from '@/components/elements/Button';
|
||||
import Can from '@/components/elements/Can';
|
||||
import CopyOnClick from '@/components/elements/CopyOnClick';
|
||||
import Label from '@/components/elements/Label';
|
||||
import SecretInput from '@/components/elements/SecretInput';
|
||||
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
||||
import DataTableColumnHeader from '@/components/elements/table/DataTableColumnHeader';
|
||||
import { actionsColumn, DeleteAction, RowActionButton, RowActions } from '@/components/elements/table/RowActions';
|
||||
@@ -136,9 +137,7 @@ function DatabaseConnectionDialog({
|
||||
<Can action={'database.view_password'}>
|
||||
<div className={'mt-6'}>
|
||||
<Label>Password</Label>
|
||||
<CopyOnClick text={password} showInNotification={false}>
|
||||
<TextInput type={'text'} readOnly value={password} />
|
||||
</CopyOnClick>
|
||||
<SecretInput value={password} label={'Password'} />
|
||||
</div>
|
||||
</Can>
|
||||
<div className={'mt-6'}>
|
||||
|
||||
Reference in New Issue
Block a user