Skip to content

Commit 107dffd

Browse files
authored
[UI]: Hide user tokens by default (#4464)
Tokens throughout the UI are now hidden until you click "Show token". This keeps them from leaking when you share or record your screen. The copy buttons still copy the real token.
1 parent b08facc commit 107dffd

17 files changed

Lines changed: 291 additions & 90 deletions

File tree

‎frontend/src/App/Login/LoginByTokenForm/index.tsx‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import React from 'react';
1+
import React, { useState } from 'react';
22
import { useForm } from 'react-hook-form';
33
import { useTranslation } from 'react-i18next';
44
import { useNavigate } from 'react-router-dom';
55

6-
import { Button, FormInput, SpaceBetween } from 'components';
6+
import { Button, FormInput, SpaceBetween, TokenVisibilityButton } from 'components';
77

88
import { useAppDispatch } from 'hooks';
99
import { useCheckAuthTokenMutation } from 'services/user';
@@ -14,7 +14,9 @@ type FormValues = Pick<IUserWithCreds['creds'], 'token'>;
1414

1515
export const LoginByTokenForm: React.FC = () => {
1616
const { t } = useTranslation();
17-
const { handleSubmit, control, setError } = useForm<FormValues>();
17+
const { handleSubmit, control, setError, watch } = useForm<FormValues>();
18+
const token = watch('token');
19+
const [isTokenVisible, setIsTokenVisible] = useState(false);
1820
const dispatch = useAppDispatch();
1921
const navigate = useNavigate();
2022

@@ -45,6 +47,14 @@ export const LoginByTokenForm: React.FC = () => {
4547
constraintText="Use your personal access token"
4648
control={control}
4749
name="token"
50+
type={isTokenVisible ? 'text' : 'password'}
51+
secondaryControl={
52+
<TokenVisibilityButton
53+
disabled={isLoading || !token}
54+
visible={isTokenVisible}
55+
onChange={setIsTokenVisible}
56+
/>
57+
}
4858
disabled={isLoading}
4959
rules={{ required: t('validation.required') }}
5060
autoComplete="off"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React from 'react';
2+
import { useTranslation } from 'react-i18next';
3+
4+
import { Button, IProps as ButtonProps } from '../Button';
5+
6+
export interface Props {
7+
visible: boolean;
8+
onChange: (visible: boolean) => void;
9+
variant?: ButtonProps['variant'];
10+
disabled?: boolean;
11+
}
12+
13+
export const TokenVisibilityButton: React.FC<Props> = ({ visible, onChange, variant = 'normal', disabled }) => {
14+
const { t } = useTranslation();
15+
16+
return (
17+
<Button formAction="none" variant={variant} disabled={disabled} wrapText={false} onClick={() => onChange(!visible)}>
18+
{visible ? t('common.hide_token') : t('common.show_token')}
19+
</Button>
20+
);
21+
};

‎frontend/src/components/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,5 @@ export { Button } from './Button';
104104
export type { IProps as ButtonProps } from './Button';
105105
export { Code } from './Code';
106106
export { Hotspot } from './Hotspot';
107+
export { TokenVisibilityButton } from './TokenVisibilityButton';
107108
export type { IProps as TabsProps } from './Tabs';

‎frontend/src/libs/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ export const copyToClipboard = (copyText: string, success?: () => void, failed?:
4141
navigator.clipboard.writeText(copyText).then(success, failed);
4242
};
4343

44+
export const TOKEN_MASK = '********';
45+
4446
export const MINUTE = 60000;
4547

4648
export const getDateAgoSting = (time: number): string => {

‎frontend/src/locale/en.json‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@
5555
"quickstart": "Quickstart",
5656
"ask_ai": "Ask AI",
5757
"new": "New",
58-
"full_view": "Full view"
58+
"full_view": "Full view",
59+
"show_token": "Show token",
60+
"hide_token": "Hide token"
5961
},
6062

6163
"auth": {

‎frontend/src/pages/Models/Details/index.tsx‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ import {
2323
NavigateLink,
2424
SpaceBetween,
2525
Tabs,
26+
TokenVisibilityButton,
2627
} from 'components';
2728

2829
import { useAppSelector, useBreadcrumbs, useNotifications } from 'hooks';
29-
import { copyToClipboard, riseRouterException } from 'libs';
30+
import { copyToClipboard, riseRouterException, TOKEN_MASK } from 'libs';
3031
import { getExtendedModelFromRun } from 'libs/run';
3132
import { ROUTES } from 'routes';
3233
import { useGetRunQuery } from 'services/run';
@@ -60,6 +61,7 @@ export const ModelDetails: React.FC = () => {
6061
const [messages, setMessages] = useState<Message[]>([]);
6162
const [viewCodeVisible, setViewCodeVisible] = useState<boolean>(false);
6263
const [codeTab, setCodeTab] = useState<CodeTab>(CodeTab.Python);
64+
const [isTokenVisible, setIsTokenVisible] = useState<boolean>(false);
6365
const [loading, setLoading] = useState<boolean>(false);
6466
const params = useParams();
6567
const paramProjectName = params.projectName ?? '';
@@ -90,6 +92,11 @@ export const ModelDetails: React.FC = () => {
9092
},
9193
]);
9294

95+
const closeViewCode = () => {
96+
setViewCodeVisible(false);
97+
setIsTokenVisible(false);
98+
};
99+
93100
const scrollChatToBottom = () => {
94101
if (!chatList.current) return;
95102

@@ -284,16 +291,18 @@ export const ModelDetails: React.FC = () => {
284291
}
285292
};
286293

287-
const pythonCode = getPythonModelCode({ model: modelData, token });
294+
const displayedToken = isTokenVisible ? token : TOKEN_MASK;
295+
296+
const pythonCode = getPythonModelCode({ model: modelData, token: displayedToken });
288297

289-
const curlCode = getCurlModelCode({ model: modelData, token });
298+
const curlCode = getCurlModelCode({ model: modelData, token: displayedToken });
290299

291300
const onCopyCode = () => {
292301
switch (codeTab) {
293302
case CodeTab.Python:
294-
return copyToClipboard(pythonCode);
303+
return copyToClipboard(getPythonModelCode({ model: modelData, token }));
295304
case CodeTab.Curl:
296-
return copyToClipboard(curlCode);
305+
return copyToClipboard(getCurlModelCode({ model: modelData, token }));
297306
}
298307
};
299308

@@ -425,19 +434,22 @@ export const ModelDetails: React.FC = () => {
425434
size="large"
426435
footer={
427436
<Box float="right">
428-
<Button variant="normal" onClick={() => setViewCodeVisible(false)}>
437+
<Button variant="normal" onClick={closeViewCode}>
429438
{t('common.close')}
430439
</Button>
431440
</Box>
432441
}
433-
onDismiss={() => setViewCodeVisible(false)}
442+
onDismiss={closeViewCode}
434443
>
435444
<SpaceBetween size="m" direction="vertical">
436445
<Box>{t('models.details.view_code_description')}</Box>
437446

438447
<div className={css.viewCodeControls}>
439448
<div className={css.copyButton}>
440-
<Button iconName="copy" onClick={onCopyCode}></Button>
449+
<SpaceBetween direction="horizontal" size="xs">
450+
<TokenVisibilityButton visible={isTokenVisible} onChange={setIsTokenVisible} />
451+
<Button iconName="copy" onClick={onCopyCode}></Button>
452+
</SpaceBetween>
441453
</div>
442454

443455
<Tabs

‎frontend/src/pages/Project/Details/Settings/index.tsx‎

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
SelectCSD,
2525
SpaceBetween,
2626
StatusIndicator,
27+
TokenVisibilityButton,
2728
} from 'components';
2829
import { HotspotIds } from 'layouts/AppLayout/TutorialPanel/constants';
2930

@@ -72,7 +73,9 @@ export const ProjectSettings: React.FC = () => {
7273
const location = useLocation();
7374
const paramProjectName = params.projectName ?? '';
7475
const [isExpandedCliSection, setIsExpandedCliSection] = React.useState(false);
75-
const [configCliCommand, copyCliCommand] = useConfigProjectCliCommand({ projectName: paramProjectName });
76+
const [configCliCommand, copyCliCommand, isTokenVisible, setIsTokenVisible] = useConfigProjectCliCommand({
77+
projectName: paramProjectName,
78+
});
7679

7780
const { isAvailableDeletingPermission, isProjectManager, isProjectAdmin, isAvailableProjectManaging } =
7881
useCheckAvailableProjectPermission();
@@ -366,20 +369,26 @@ export const ProjectSettings: React.FC = () => {
366369
<Code className={styles.code}>{configCliCommand}</Code>
367370

368371
<div className={styles.copy}>
369-
<Popover
370-
dismissButton={false}
371-
position="top"
372-
size="small"
373-
triggerType="custom"
374-
content={<StatusIndicator type="success">{t('common.copied')}</StatusIndicator>}
375-
>
376-
<Button
377-
formAction="none"
378-
iconName="copy"
379-
variant="normal"
380-
onClick={copyCliCommand}
381-
/>
382-
</Popover>
372+
<SpaceBetween direction="horizontal" size="xs">
373+
<TokenVisibilityButton visible={isTokenVisible} onChange={setIsTokenVisible} />
374+
375+
<Popover
376+
dismissButton={false}
377+
position="top"
378+
size="small"
379+
triggerType="custom"
380+
content={
381+
<StatusIndicator type="success">{t('common.copied')}</StatusIndicator>
382+
}
383+
>
384+
<Button
385+
formAction="none"
386+
iconName="copy"
387+
variant="normal"
388+
onClick={copyCliCommand}
389+
/>
390+
</Popover>
391+
</SpaceBetween>
383392
</div>
384393
</Hotspot>
385394
</div>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/** @jest-environment node */
2+
import React from 'react';
3+
import { act, create, ReactTestRenderer } from 'react-test-renderer';
4+
5+
import { copyToClipboard, TOKEN_MASK } from 'libs';
6+
7+
import { useConfigProjectCliCommand } from './useConfigProjectCliComand';
8+
9+
jest.mock('libs', () => ({ ...jest.requireActual('libs'), copyToClipboard: jest.fn() }));
10+
jest.mock('App/slice', () => ({ selectAuthToken: jest.fn() }));
11+
jest.mock('hooks', () => ({ useAppSelector: () => 'secret-token' }));
12+
13+
let result: ReturnType<typeof useConfigProjectCliCommand>;
14+
let renderer: ReactTestRenderer;
15+
16+
const Harness = () => {
17+
result = useConfigProjectCliCommand({ projectName: 'main' });
18+
return null;
19+
};
20+
21+
beforeAll(() => {
22+
Object.defineProperty(globalThis, 'location', { value: { origin: 'https://dstack.example' }, configurable: true });
23+
});
24+
25+
beforeEach(() => {
26+
jest.mocked(copyToClipboard).mockReset();
27+
act(() => {
28+
renderer = create(<Harness />);
29+
});
30+
});
31+
32+
afterEach(() => act(() => renderer.unmount()));
33+
34+
describe('useConfigProjectCliCommand', () => {
35+
test('hides the token by default', () => {
36+
const [cliCommand] = result;
37+
38+
expect(cliCommand).toContain(`--token ${TOKEN_MASK}`);
39+
expect(cliCommand).not.toContain('secret-token');
40+
});
41+
42+
test('shows the token when requested', () => {
43+
act(() => result[3](true));
44+
45+
const [cliCommand, , isTokenVisible] = result;
46+
expect(isTokenVisible).toBe(true);
47+
expect(cliCommand).toContain('--token secret-token');
48+
});
49+
50+
test('copies the command with the real token while it is hidden', () => {
51+
const [, copyCliCommand] = result;
52+
53+
copyCliCommand();
54+
55+
expect(copyToClipboard).toHaveBeenCalledWith(expect.stringContaining('--token secret-token'));
56+
});
57+
});
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { useState } from 'react';
2+
13
import { useAppSelector } from 'hooks';
2-
import { copyToClipboard } from 'libs';
4+
import { copyToClipboard, TOKEN_MASK } from 'libs';
35

46
import { selectAuthToken } from 'App/slice';
57

@@ -8,12 +10,16 @@ type Args = {
810
};
911
export const useConfigProjectCliCommand = ({ projectName }: Args) => {
1012
const currentUserToken = useAppSelector(selectAuthToken);
13+
const [isTokenVisible, setIsTokenVisible] = useState(false);
14+
15+
const getCliCommand = (token?: string) =>
16+
`dstack project add \\\n --url ${location.origin} \\\n --name ${projectName} \\\n --token ${token}`;
1117

12-
const cliCommand = `dstack project add \\\n --url ${location.origin} \\\n --name ${projectName} \\\n --token ${currentUserToken}`;
18+
const cliCommand = getCliCommand(isTokenVisible ? currentUserToken : TOKEN_MASK);
1319

1420
const copyCliCommand = () => {
15-
copyToClipboard(cliCommand);
21+
copyToClipboard(getCliCommand(currentUserToken));
1622
};
1723

18-
return [cliCommand, copyCliCommand] as const;
24+
return [cliCommand, copyCliCommand, isTokenVisible, setIsTokenVisible] as const;
1925
};

‎frontend/src/pages/Runs/Details/RunDetails/ConnectToRunWithDevEnvConfiguration/index.tsx‎

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
import React, { FC } from 'react';
22
import { useTranslation } from 'react-i18next';
33

4-
import { Alert, Box, Button, Code, ExpandableSection, Popover, SpaceBetween, StatusIndicator, Tabs, Wizard } from 'components';
4+
import {
5+
Alert,
6+
Box,
7+
Button,
8+
Code,
9+
ExpandableSection,
10+
Popover,
11+
SpaceBetween,
12+
StatusIndicator,
13+
Tabs,
14+
TokenVisibilityButton,
15+
Wizard,
16+
} from 'components';
517

618
import { copyToClipboard } from 'libs';
719

@@ -50,7 +62,9 @@ export const ConnectToRunWithDevEnvConfiguration: FC<{ run: IRun }> = ({ run })
5062
const openInIDEUrl = run.jobs[0]?.job_connection_info?.attached_ide_url ?? undefined;
5163
const ideDisplayName = hasIDE ? getIDEDisplayName(configuration.ide!) : undefined;
5264

53-
const [configCliCommand, copyCliCommand] = useConfigProjectCliCommand({ projectName: run.project_name });
65+
const [configCliCommand, copyCliCommand, isTokenVisible, setIsTokenVisible] = useConfigProjectCliCommand({
66+
projectName: run.project_name,
67+
});
5468

5569
return (
5670
<ExpandableSection
@@ -187,24 +201,31 @@ export const ConnectToRunWithDevEnvConfiguration: FC<{ run: IRun }> = ({ run })
187201
<Code className={styles.code}>{configCliCommand}</Code>
188202

189203
<div className={styles.copy}>
190-
<Popover
191-
dismissButton={false}
192-
position="top"
193-
size="small"
194-
triggerType="custom"
195-
content={
196-
<StatusIndicator type="success">
197-
{t('common.copied')}
198-
</StatusIndicator>
199-
}
200-
>
201-
<Button
202-
formAction="none"
203-
iconName="copy"
204-
variant="normal"
205-
onClick={copyCliCommand}
204+
<SpaceBetween direction="horizontal" size="xs">
205+
<TokenVisibilityButton
206+
visible={isTokenVisible}
207+
onChange={setIsTokenVisible}
206208
/>
207-
</Popover>
209+
210+
<Popover
211+
dismissButton={false}
212+
position="top"
213+
size="small"
214+
triggerType="custom"
215+
content={
216+
<StatusIndicator type="success">
217+
{t('common.copied')}
218+
</StatusIndicator>
219+
}
220+
>
221+
<Button
222+
formAction="none"
223+
iconName="copy"
224+
variant="normal"
225+
onClick={copyCliCommand}
226+
/>
227+
</Popover>
228+
</SpaceBetween>
208229
</div>
209230
</div>
210231
</SpaceBetween>

0 commit comments

Comments
 (0)