diff --git a/frontend/src/App/Login/LoginByTokenForm/index.tsx b/frontend/src/App/Login/LoginByTokenForm/index.tsx index f4ad61a471..b863a6c6c9 100644 --- a/frontend/src/App/Login/LoginByTokenForm/index.tsx +++ b/frontend/src/App/Login/LoginByTokenForm/index.tsx @@ -1,9 +1,9 @@ -import React from 'react'; +import React, { useState } from 'react'; import { useForm } from 'react-hook-form'; import { useTranslation } from 'react-i18next'; import { useNavigate } from 'react-router-dom'; -import { Button, FormInput, SpaceBetween } from 'components'; +import { Button, FormInput, SpaceBetween, TokenVisibilityButton } from 'components'; import { useAppDispatch } from 'hooks'; import { useCheckAuthTokenMutation } from 'services/user'; @@ -14,7 +14,9 @@ type FormValues = Pick; export const LoginByTokenForm: React.FC = () => { const { t } = useTranslation(); - const { handleSubmit, control, setError } = useForm(); + const { handleSubmit, control, setError, watch } = useForm(); + const token = watch('token'); + const [isTokenVisible, setIsTokenVisible] = useState(false); const dispatch = useAppDispatch(); const navigate = useNavigate(); @@ -45,6 +47,14 @@ export const LoginByTokenForm: React.FC = () => { constraintText="Use your personal access token" control={control} name="token" + type={isTokenVisible ? 'text' : 'password'} + secondaryControl={ + + } disabled={isLoading} rules={{ required: t('validation.required') }} autoComplete="off" diff --git a/frontend/src/components/TokenVisibilityButton/index.tsx b/frontend/src/components/TokenVisibilityButton/index.tsx new file mode 100644 index 0000000000..4f017f770e --- /dev/null +++ b/frontend/src/components/TokenVisibilityButton/index.tsx @@ -0,0 +1,21 @@ +import React from 'react'; +import { useTranslation } from 'react-i18next'; + +import { Button, IProps as ButtonProps } from '../Button'; + +export interface Props { + visible: boolean; + onChange: (visible: boolean) => void; + variant?: ButtonProps['variant']; + disabled?: boolean; +} + +export const TokenVisibilityButton: React.FC = ({ visible, onChange, variant = 'normal', disabled }) => { + const { t } = useTranslation(); + + return ( + + ); +}; diff --git a/frontend/src/components/index.ts b/frontend/src/components/index.ts index 26c37a9e0a..288f810065 100644 --- a/frontend/src/components/index.ts +++ b/frontend/src/components/index.ts @@ -104,4 +104,5 @@ export { Button } from './Button'; export type { IProps as ButtonProps } from './Button'; export { Code } from './Code'; export { Hotspot } from './Hotspot'; +export { TokenVisibilityButton } from './TokenVisibilityButton'; export type { IProps as TabsProps } from './Tabs'; diff --git a/frontend/src/libs/index.ts b/frontend/src/libs/index.ts index 4d5123787c..42249ed305 100644 --- a/frontend/src/libs/index.ts +++ b/frontend/src/libs/index.ts @@ -41,6 +41,8 @@ export const copyToClipboard = (copyText: string, success?: () => void, failed?: navigator.clipboard.writeText(copyText).then(success, failed); }; +export const TOKEN_MASK = '********'; + export const MINUTE = 60000; export const getDateAgoSting = (time: number): string => { diff --git a/frontend/src/locale/en.json b/frontend/src/locale/en.json index 7168d12221..78f95503ea 100644 --- a/frontend/src/locale/en.json +++ b/frontend/src/locale/en.json @@ -55,7 +55,9 @@ "quickstart": "Quickstart", "ask_ai": "Ask AI", "new": "New", - "full_view": "Full view" + "full_view": "Full view", + "show_token": "Show token", + "hide_token": "Hide token" }, "auth": { diff --git a/frontend/src/pages/Models/Details/index.tsx b/frontend/src/pages/Models/Details/index.tsx index ad9906d5b1..cad0d56c51 100644 --- a/frontend/src/pages/Models/Details/index.tsx +++ b/frontend/src/pages/Models/Details/index.tsx @@ -23,10 +23,11 @@ import { NavigateLink, SpaceBetween, Tabs, + TokenVisibilityButton, } from 'components'; import { useAppSelector, useBreadcrumbs, useNotifications } from 'hooks'; -import { copyToClipboard, riseRouterException } from 'libs'; +import { copyToClipboard, riseRouterException, TOKEN_MASK } from 'libs'; import { getExtendedModelFromRun } from 'libs/run'; import { ROUTES } from 'routes'; import { useGetRunQuery } from 'services/run'; @@ -60,6 +61,7 @@ export const ModelDetails: React.FC = () => { const [messages, setMessages] = useState([]); const [viewCodeVisible, setViewCodeVisible] = useState(false); const [codeTab, setCodeTab] = useState(CodeTab.Python); + const [isTokenVisible, setIsTokenVisible] = useState(false); const [loading, setLoading] = useState(false); const params = useParams(); const paramProjectName = params.projectName ?? ''; @@ -90,6 +92,11 @@ export const ModelDetails: React.FC = () => { }, ]); + const closeViewCode = () => { + setViewCodeVisible(false); + setIsTokenVisible(false); + }; + const scrollChatToBottom = () => { if (!chatList.current) return; @@ -284,16 +291,18 @@ export const ModelDetails: React.FC = () => { } }; - const pythonCode = getPythonModelCode({ model: modelData, token }); + const displayedToken = isTokenVisible ? token : TOKEN_MASK; + + const pythonCode = getPythonModelCode({ model: modelData, token: displayedToken }); - const curlCode = getCurlModelCode({ model: modelData, token }); + const curlCode = getCurlModelCode({ model: modelData, token: displayedToken }); const onCopyCode = () => { switch (codeTab) { case CodeTab.Python: - return copyToClipboard(pythonCode); + return copyToClipboard(getPythonModelCode({ model: modelData, token })); case CodeTab.Curl: - return copyToClipboard(curlCode); + return copyToClipboard(getCurlModelCode({ model: modelData, token })); } }; @@ -425,19 +434,22 @@ export const ModelDetails: React.FC = () => { size="large" footer={ - } - onDismiss={() => setViewCodeVisible(false)} + onDismiss={closeViewCode} > {t('models.details.view_code_description')}
- + + + +
{ const location = useLocation(); const paramProjectName = params.projectName ?? ''; const [isExpandedCliSection, setIsExpandedCliSection] = React.useState(false); - const [configCliCommand, copyCliCommand] = useConfigProjectCliCommand({ projectName: paramProjectName }); + const [configCliCommand, copyCliCommand, isTokenVisible, setIsTokenVisible] = useConfigProjectCliCommand({ + projectName: paramProjectName, + }); const { isAvailableDeletingPermission, isProjectManager, isProjectAdmin, isAvailableProjectManaging } = useCheckAvailableProjectPermission(); @@ -366,20 +369,26 @@ export const ProjectSettings: React.FC = () => { {configCliCommand}
- {t('common.copied')}} - > -
diff --git a/frontend/src/pages/Project/hooks/useConfigProjectCliComand.test.tsx b/frontend/src/pages/Project/hooks/useConfigProjectCliComand.test.tsx new file mode 100644 index 0000000000..dd1c83e547 --- /dev/null +++ b/frontend/src/pages/Project/hooks/useConfigProjectCliComand.test.tsx @@ -0,0 +1,57 @@ +/** @jest-environment node */ +import React from 'react'; +import { act, create, ReactTestRenderer } from 'react-test-renderer'; + +import { copyToClipboard, TOKEN_MASK } from 'libs'; + +import { useConfigProjectCliCommand } from './useConfigProjectCliComand'; + +jest.mock('libs', () => ({ ...jest.requireActual('libs'), copyToClipboard: jest.fn() })); +jest.mock('App/slice', () => ({ selectAuthToken: jest.fn() })); +jest.mock('hooks', () => ({ useAppSelector: () => 'secret-token' })); + +let result: ReturnType; +let renderer: ReactTestRenderer; + +const Harness = () => { + result = useConfigProjectCliCommand({ projectName: 'main' }); + return null; +}; + +beforeAll(() => { + Object.defineProperty(globalThis, 'location', { value: { origin: 'https://dstack.example' }, configurable: true }); +}); + +beforeEach(() => { + jest.mocked(copyToClipboard).mockReset(); + act(() => { + renderer = create(); + }); +}); + +afterEach(() => act(() => renderer.unmount())); + +describe('useConfigProjectCliCommand', () => { + test('hides the token by default', () => { + const [cliCommand] = result; + + expect(cliCommand).toContain(`--token ${TOKEN_MASK}`); + expect(cliCommand).not.toContain('secret-token'); + }); + + test('shows the token when requested', () => { + act(() => result[3](true)); + + const [cliCommand, , isTokenVisible] = result; + expect(isTokenVisible).toBe(true); + expect(cliCommand).toContain('--token secret-token'); + }); + + test('copies the command with the real token while it is hidden', () => { + const [, copyCliCommand] = result; + + copyCliCommand(); + + expect(copyToClipboard).toHaveBeenCalledWith(expect.stringContaining('--token secret-token')); + }); +}); diff --git a/frontend/src/pages/Project/hooks/useConfigProjectCliComand.ts b/frontend/src/pages/Project/hooks/useConfigProjectCliComand.ts index d6bacb4408..27dbb2a523 100644 --- a/frontend/src/pages/Project/hooks/useConfigProjectCliComand.ts +++ b/frontend/src/pages/Project/hooks/useConfigProjectCliComand.ts @@ -1,5 +1,7 @@ +import { useState } from 'react'; + import { useAppSelector } from 'hooks'; -import { copyToClipboard } from 'libs'; +import { copyToClipboard, TOKEN_MASK } from 'libs'; import { selectAuthToken } from 'App/slice'; @@ -8,12 +10,16 @@ type Args = { }; export const useConfigProjectCliCommand = ({ projectName }: Args) => { const currentUserToken = useAppSelector(selectAuthToken); + const [isTokenVisible, setIsTokenVisible] = useState(false); + + const getCliCommand = (token?: string) => + `dstack project add \\\n --url ${location.origin} \\\n --name ${projectName} \\\n --token ${token}`; - const cliCommand = `dstack project add \\\n --url ${location.origin} \\\n --name ${projectName} \\\n --token ${currentUserToken}`; + const cliCommand = getCliCommand(isTokenVisible ? currentUserToken : TOKEN_MASK); const copyCliCommand = () => { - copyToClipboard(cliCommand); + copyToClipboard(getCliCommand(currentUserToken)); }; - return [cliCommand, copyCliCommand] as const; + return [cliCommand, copyCliCommand, isTokenVisible, setIsTokenVisible] as const; }; diff --git a/frontend/src/pages/Runs/Details/RunDetails/ConnectToRunWithDevEnvConfiguration/index.tsx b/frontend/src/pages/Runs/Details/RunDetails/ConnectToRunWithDevEnvConfiguration/index.tsx index 9e03c6b6c2..295e86f813 100644 --- a/frontend/src/pages/Runs/Details/RunDetails/ConnectToRunWithDevEnvConfiguration/index.tsx +++ b/frontend/src/pages/Runs/Details/RunDetails/ConnectToRunWithDevEnvConfiguration/index.tsx @@ -1,7 +1,19 @@ import React, { FC } from 'react'; import { useTranslation } from 'react-i18next'; -import { Alert, Box, Button, Code, ExpandableSection, Popover, SpaceBetween, StatusIndicator, Tabs, Wizard } from 'components'; +import { + Alert, + Box, + Button, + Code, + ExpandableSection, + Popover, + SpaceBetween, + StatusIndicator, + Tabs, + TokenVisibilityButton, + Wizard, +} from 'components'; import { copyToClipboard } from 'libs'; @@ -50,7 +62,9 @@ export const ConnectToRunWithDevEnvConfiguration: FC<{ run: IRun }> = ({ run }) const openInIDEUrl = run.jobs[0]?.job_connection_info?.attached_ide_url ?? undefined; const ideDisplayName = hasIDE ? getIDEDisplayName(configuration.ide!) : undefined; - const [configCliCommand, copyCliCommand] = useConfigProjectCliCommand({ projectName: run.project_name }); + const [configCliCommand, copyCliCommand, isTokenVisible, setIsTokenVisible] = useConfigProjectCliCommand({ + projectName: run.project_name, + }); return ( = ({ run }) {configCliCommand}
- - {t('common.copied')} - - } - > -
diff --git a/frontend/src/pages/Runs/Details/RunDetails/ConnectToTaskRun/index.tsx b/frontend/src/pages/Runs/Details/RunDetails/ConnectToTaskRun/index.tsx index 976dfb5dde..d7a2f04502 100644 --- a/frontend/src/pages/Runs/Details/RunDetails/ConnectToTaskRun/index.tsx +++ b/frontend/src/pages/Runs/Details/RunDetails/ConnectToTaskRun/index.tsx @@ -12,6 +12,7 @@ import { SpaceBetween, StatusIndicator, Tabs, + TokenVisibilityButton, Wizard, } from 'components'; @@ -35,7 +36,9 @@ export const ConnectToTaskRun: FC<{ run: IRun }> = ({ run }) => { const mappedAppSpecs = appSpecs.filter((spec) => getMappedPort(spec) != null); const [activeStepIndex, setActiveStepIndex] = React.useState(0); - const [configCliCommand, copyCliCommand] = useConfigProjectCliCommand({ projectName: run.project_name }); + const [configCliCommand, copyCliCommand, isTokenVisible, setIsTokenVisible] = useConfigProjectCliCommand({ + projectName: run.project_name, + }); return ( = ({ run }) => { {configCliCommand}
- - {t('common.copied')} - - } - > -
diff --git a/frontend/src/pages/Runs/LaunchOptions/index.tsx b/frontend/src/pages/Runs/LaunchOptions/index.tsx index 24af78f1ef..2462214c42 100644 --- a/frontend/src/pages/Runs/LaunchOptions/index.tsx +++ b/frontend/src/pages/Runs/LaunchOptions/index.tsx @@ -18,6 +18,7 @@ import { SpaceBetween, Spinner, StatusIndicator, + TokenVisibilityButton, } from 'components'; import { QUICK_START_URL } from 'consts'; @@ -202,7 +203,7 @@ const CliPanel: FC<{ projectName: string }> = ({ projectName }) => { const { t } = useTranslation(); const [searchParams] = useSearchParams(); const [openHelpPanel] = useHelpPanel(); - const [configCliCommand] = useConfigProjectCliCommand({ projectName }); + const [configCliCommand, copyCliCommand, isTokenVisible, setIsTokenVisible] = useConfigProjectCliCommand({ projectName }); return ( = ({ projectName }) => { - + } + /> ): number | un return height; }; -const CopyableCode: FC<{ command: string }> = ({ command }) => { +const CopyableCode: FC<{ command: string; onCopy?: () => void; actions?: React.ReactNode }> = ({ + command, + onCopy = () => copyToClipboard(command), + actions, +}) => { const { t } = useTranslation(); return ( @@ -394,15 +403,19 @@ const CopyableCode: FC<{ command: string }> = ({ command }) => { {command}
- {t('common.copied')}} - > -
); diff --git a/frontend/src/pages/User/Details/Settings/index.tsx b/frontend/src/pages/User/Details/Settings/index.tsx index 2f1c1c6638..0ef6726dfa 100644 --- a/frontend/src/pages/User/Details/Settings/index.tsx +++ b/frontend/src/pages/User/Details/Settings/index.tsx @@ -1,12 +1,24 @@ -import React from 'react'; +import React, { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useNavigate, useParams } from 'react-router-dom'; -import { Box, Button, ColumnLayout, Container, Header, Link, Loader, Popover, SpaceBetween, StatusIndicator } from 'components'; +import { + Box, + Button, + ColumnLayout, + Container, + Header, + Link, + Loader, + Popover, + SpaceBetween, + StatusIndicator, + TokenVisibilityButton, +} from 'components'; import { PermissionGuard } from 'components/PermissionGuard'; import { useAppSelector, useBreadcrumbs, usePermissionGuard } from 'hooks'; -import { copyToClipboard } from 'libs'; +import { copyToClipboard, TOKEN_MASK } from 'libs'; import { ROUTES } from 'routes'; import { useDeleteUsersMutation, useGetUserQuery } from 'services/user'; import { GlobalUserRole } from 'types'; @@ -26,6 +38,7 @@ export const Settings: React.FC = () => { const { isLoading, data } = useGetUserQuery({ name: paramUserName }, { skip: !params.userName }); const [, { isLoading: isDeleting }] = useDeleteUsersMutation(); + const [isTokenVisible, setIsTokenVisible] = useState(false); const [isAvailableDelete] = usePermissionGuard({ allowedGlobalRoles: [GlobalUserRole.ADMIN] }); @@ -71,7 +84,7 @@ export const Settings: React.FC = () => { {isLoading && } {data && ( - + {/*
*/} {/* {t('users.user_name')}*/} @@ -104,7 +117,13 @@ export const Settings: React.FC = () => {
diff --git a/frontend/src/pages/User/Details/Settings/styles.module.scss b/frontend/src/pages/User/Details/Settings/styles.module.scss index af62ebfb58..2273c0f7b4 100644 --- a/frontend/src/pages/User/Details/Settings/styles.module.scss +++ b/frontend/src/pages/User/Details/Settings/styles.module.scss @@ -1,11 +1,16 @@ .token { display: flex; align-items: center; - height: 20px; gap: 12px; button { padding: 0 !important; border: none !important; + flex-shrink: 0; } } + +.tokenValue { + min-width: 0; + overflow-wrap: anywhere; +} diff --git a/frontend/src/pages/User/Details/index.tsx b/frontend/src/pages/User/Details/index.tsx index aaaca99f5f..126bdc19b7 100644 --- a/frontend/src/pages/User/Details/index.tsx +++ b/frontend/src/pages/User/Details/index.tsx @@ -98,7 +98,8 @@ export const UserDetails: React.FC = () => { > - + {/* Remount tabs on user change so per-user state (e.g. token visibility) doesn't leak */} + { {data && ( = ({ }) => { const { t } = useTranslation(); const isEditing = !!initialValues; + const [isTokenVisible, setIsTokenVisible] = useState(false); const { handleSubmit, control } = useForm({ // eslint-disable-next-line @typescript-eslint/ban-ts-comment @@ -170,7 +172,16 @@ export const UserForm: React.FC = ({ - {initialValues.creds.token} + {isTokenVisible ? initialValues.creds.token : TOKEN_MASK} + + + +