mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-26 19:31:12 +01:00
[WIP] Encryption for secure values (#183)
This commit is contained in:
@@ -1,15 +1,14 @@
|
||||
import { createWorkspaceModel } from '@yaakapp-internal/models';
|
||||
import { useAtomValue } from 'jotai';
|
||||
import { useMemo } from 'react';
|
||||
import { createFolder } from '../commands/commands';
|
||||
import type { DropdownItem } from '../components/core/Dropdown';
|
||||
import { Icon } from '../components/core/Icon';
|
||||
import { createRequestAndNavigate } from '../lib/createRequestAndNavigate';
|
||||
import { generateId } from '../lib/generateId';
|
||||
import { jotaiStore } from '../lib/jotai';
|
||||
import { BODY_TYPE_GRAPHQL } from '../lib/model_util';
|
||||
import { activeRequestAtom } from './useActiveRequest';
|
||||
import { activeWorkspaceIdAtom } from './useActiveWorkspace';
|
||||
import { useCreateHttpRequest } from './useCreateHttpRequest';
|
||||
|
||||
export function useCreateDropdownItems({
|
||||
hideFolder,
|
||||
@@ -20,7 +19,6 @@ export function useCreateDropdownItems({
|
||||
hideIcons?: boolean;
|
||||
folderId?: string | null | 'active-folder';
|
||||
} = {}): DropdownItem[] {
|
||||
const { mutate: createHttpRequest } = useCreateHttpRequest();
|
||||
const workspaceId = useAtomValue(activeWorkspaceIdAtom);
|
||||
|
||||
const items = useMemo((): DropdownItem[] => {
|
||||
@@ -33,15 +31,15 @@ export function useCreateDropdownItems({
|
||||
{
|
||||
label: 'HTTP',
|
||||
leftSlot: hideIcons ? undefined : <Icon icon="plus" />,
|
||||
onSelect: () => {
|
||||
createHttpRequest({ folderId });
|
||||
},
|
||||
onSelect: () => createRequestAndNavigate({ model: 'http_request', workspaceId, folderId }),
|
||||
},
|
||||
{
|
||||
label: 'GraphQL',
|
||||
leftSlot: hideIcons ? undefined : <Icon icon="plus" />,
|
||||
onSelect: () =>
|
||||
createHttpRequest({
|
||||
createRequestAndNavigate({
|
||||
model: 'http_request',
|
||||
workspaceId,
|
||||
folderId,
|
||||
bodyType: BODY_TYPE_GRAPHQL,
|
||||
method: 'POST',
|
||||
@@ -51,12 +49,13 @@ export function useCreateDropdownItems({
|
||||
{
|
||||
label: 'gRPC',
|
||||
leftSlot: hideIcons ? undefined : <Icon icon="plus" />,
|
||||
onSelect: () => createWorkspaceModel({ model: 'grpc_request', workspaceId, folderId }),
|
||||
onSelect: () => createRequestAndNavigate({ model: 'grpc_request', workspaceId, folderId }),
|
||||
},
|
||||
{
|
||||
label: 'WebSocket',
|
||||
leftSlot: hideIcons ? undefined : <Icon icon="plus" />,
|
||||
onSelect: () => createWorkspaceModel({ model: 'websocket_request', workspaceId, folderId }),
|
||||
onSelect: () =>
|
||||
createRequestAndNavigate({ model: 'websocket_request', workspaceId, folderId }),
|
||||
},
|
||||
...((hideFolder
|
||||
? []
|
||||
@@ -69,7 +68,7 @@ export function useCreateDropdownItems({
|
||||
},
|
||||
]) as DropdownItem[]),
|
||||
];
|
||||
}, [createHttpRequest, folderIdOption, hideFolder, hideIcons, workspaceId]);
|
||||
}, [folderIdOption, hideFolder, hideIcons, workspaceId]);
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
import type { GrpcRequest } from '@yaakapp-internal/models';
|
||||
import { createWorkspaceModel } from '@yaakapp-internal/models';
|
||||
import { jotaiStore } from '../lib/jotai';
|
||||
import { router } from '../lib/router';
|
||||
import { activeRequestAtom } from './useActiveRequest';
|
||||
import { activeWorkspaceIdAtom } from './useActiveWorkspace';
|
||||
import { useFastMutation } from './useFastMutation';
|
||||
|
||||
export function useCreateGrpcRequest() {
|
||||
return useFastMutation<
|
||||
string,
|
||||
unknown,
|
||||
Partial<Pick<GrpcRequest, 'name' | 'sortPriority' | 'folderId'>>
|
||||
>({
|
||||
mutationKey: ['create_grpc_request'],
|
||||
mutationFn: async (patch) => {
|
||||
const workspaceId = jotaiStore.get(activeWorkspaceIdAtom);
|
||||
if (workspaceId === null) {
|
||||
throw new Error("Cannot create grpc request when there's no active workspace");
|
||||
}
|
||||
const activeRequest = jotaiStore.get(activeRequestAtom);
|
||||
if (patch.sortPriority === undefined) {
|
||||
if (activeRequest != null) {
|
||||
// Place above currently active request
|
||||
patch.sortPriority = activeRequest.sortPriority + 0.0001;
|
||||
} else {
|
||||
// Place at the very top
|
||||
patch.sortPriority = -Date.now();
|
||||
}
|
||||
}
|
||||
patch.folderId = patch.folderId || activeRequest?.folderId;
|
||||
return createWorkspaceModel({ model: 'grpc_request', workspaceId, ...patch });
|
||||
},
|
||||
onSuccess: async (requestId) => {
|
||||
const workspaceId = jotaiStore.get(activeWorkspaceIdAtom);
|
||||
if (workspaceId == null) return;
|
||||
|
||||
await router.navigate({
|
||||
to: '/workspaces/$workspaceId',
|
||||
params: { workspaceId },
|
||||
search: (prev) => ({ ...prev, request_id: requestId }),
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
import type { HttpRequest } from '@yaakapp-internal/models';
|
||||
import { createWorkspaceModel } from '@yaakapp-internal/models';
|
||||
import { jotaiStore } from '../lib/jotai';
|
||||
import { router } from '../lib/router';
|
||||
import { activeRequestAtom } from './useActiveRequest';
|
||||
import { activeWorkspaceIdAtom } from './useActiveWorkspace';
|
||||
import { useFastMutation } from './useFastMutation';
|
||||
|
||||
export function useCreateHttpRequest() {
|
||||
return useFastMutation<string, unknown, Partial<HttpRequest>>({
|
||||
mutationKey: ['create_http_request'],
|
||||
mutationFn: async (patch = {}) => {
|
||||
const workspaceId = jotaiStore.get(activeWorkspaceIdAtom);
|
||||
if (workspaceId == null) {
|
||||
throw new Error("Cannot create request when there's no active workspace");
|
||||
}
|
||||
|
||||
const activeRequest = jotaiStore.get(activeRequestAtom);
|
||||
if (patch.sortPriority === undefined) {
|
||||
if (activeRequest != null) {
|
||||
// Place above currently active request
|
||||
patch.sortPriority = activeRequest.sortPriority - 0.0001;
|
||||
} else {
|
||||
// Place at the very top
|
||||
patch.sortPriority = -Date.now();
|
||||
}
|
||||
}
|
||||
patch.folderId = patch.folderId || activeRequest?.folderId;
|
||||
return createWorkspaceModel({ model: 'http_request', workspaceId, ...patch });
|
||||
},
|
||||
onSuccess: async (requestId) => {
|
||||
const workspaceId = jotaiStore.get(activeWorkspaceIdAtom);
|
||||
if (workspaceId == null) return;
|
||||
await router.navigate({
|
||||
to: '/workspaces/$workspaceId',
|
||||
params: { workspaceId },
|
||||
search: (prev) => ({ ...prev, request_id: requestId }),
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -1,10 +1,4 @@
|
||||
import type {
|
||||
Environment,
|
||||
Folder,
|
||||
GrpcRequest,
|
||||
HttpRequest,
|
||||
Workspace,
|
||||
} from '@yaakapp-internal/models';
|
||||
import type { BatchUpsertResult } from '@yaakapp-internal/models';
|
||||
import { Button } from '../components/core/Button';
|
||||
import { FormattedError } from '../components/core/FormattedError';
|
||||
import { VStack } from '../components/core/Stacks';
|
||||
@@ -21,13 +15,7 @@ import { useFastMutation } from './useFastMutation';
|
||||
export function useImportData() {
|
||||
const importData = async (filePath: string): Promise<boolean> => {
|
||||
const activeWorkspace = jotaiStore.get(activeWorkspaceAtom);
|
||||
const imported: {
|
||||
workspaces: Workspace[];
|
||||
environments: Environment[];
|
||||
folders: Folder[];
|
||||
httpRequests: HttpRequest[];
|
||||
grpcRequests: GrpcRequest[];
|
||||
} = await invokeCmd('cmd_import_data', {
|
||||
const imported = await invokeCmd<BatchUpsertResult>('cmd_import_data', {
|
||||
filePath,
|
||||
workspaceId: activeWorkspace?.id,
|
||||
});
|
||||
@@ -40,15 +28,25 @@ export function useImportData() {
|
||||
size: 'sm',
|
||||
hideX: true,
|
||||
render: ({ hide }) => {
|
||||
const { workspaces, environments, folders, httpRequests, grpcRequests } = imported;
|
||||
return (
|
||||
<VStack space={3} className="pb-4">
|
||||
<ul className="list-disc pl-6">
|
||||
<li>{pluralizeCount('Workspace', workspaces.length)}</li>
|
||||
<li>{pluralizeCount('Environment', environments.length)}</li>
|
||||
<li>{pluralizeCount('Folder', folders.length)}</li>
|
||||
<li>{pluralizeCount('HTTP Request', httpRequests.length)}</li>
|
||||
<li>{pluralizeCount('GRPC Request', grpcRequests.length)}</li>
|
||||
<li>{pluralizeCount('Workspace', imported.workspaces.length)}</li>
|
||||
{imported.environments.length > 0 && (
|
||||
<li>{pluralizeCount('Environment', imported.environments.length)}</li>
|
||||
)}
|
||||
{imported.folders.length > 0 && (
|
||||
<li>{pluralizeCount('Folder', imported.folders.length)}</li>
|
||||
)}
|
||||
{imported.httpRequests.length > 0 && (
|
||||
<li>{pluralizeCount('HTTP Request', imported.httpRequests.length)}</li>
|
||||
)}
|
||||
{imported.grpcRequests.length > 0 && (
|
||||
<li>{pluralizeCount('GRPC Request', imported.grpcRequests.length)}</li>
|
||||
)}
|
||||
{imported.websocketRequests.length > 0 && (
|
||||
<li>{pluralizeCount('Websocket Request', imported.websocketRequests.length)}</li>
|
||||
)}
|
||||
</ul>
|
||||
<div>
|
||||
<Button className="ml-auto" onClick={hide} color="primary">
|
||||
|
||||
7
src-web/hooks/useIsEncryptionEnabled.ts
Normal file
7
src-web/hooks/useIsEncryptionEnabled.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import {useAtomValue} from "jotai/index";
|
||||
import {activeWorkspaceMetaAtom} from "./useActiveWorkspace";
|
||||
|
||||
export function useIsEncryptionEnabled() {
|
||||
const workspaceMeta = useAtomValue(activeWorkspaceMetaAtom);
|
||||
return workspaceMeta?.encryptionKey != null;
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import type { Tokens } from '@yaakapp-internal/templates';
|
||||
import { invokeCmd } from '../lib/tauri';
|
||||
|
||||
export function useParseTemplate(template: string) {
|
||||
return useQuery<Tokens>({
|
||||
queryKey: ['parse_template', template],
|
||||
queryFn: () => parseTemplate(template),
|
||||
});
|
||||
}
|
||||
|
||||
export async function parseTemplate(template: string): Promise<Tokens> {
|
||||
return invokeCmd('cmd_parse_template', { template });
|
||||
}
|
||||
8
src-web/hooks/useRandomKey.ts
Normal file
8
src-web/hooks/useRandomKey.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { useCallback, useState } from 'react';
|
||||
import { generateId } from '../lib/generateId';
|
||||
|
||||
export function useRandomKey() {
|
||||
const [value, setValue] = useState<string>(generateId());
|
||||
const regenerate = useCallback(() => setValue(generateId()), []);
|
||||
return [value, regenerate] as const;
|
||||
}
|
||||
@@ -26,3 +26,15 @@ export async function renderTemplate({
|
||||
}): Promise<string> {
|
||||
return invokeCmd('cmd_render_template', { template, workspaceId, environmentId });
|
||||
}
|
||||
|
||||
export async function decryptTemplate({
|
||||
template,
|
||||
workspaceId,
|
||||
environmentId,
|
||||
}: {
|
||||
template: string;
|
||||
workspaceId: string;
|
||||
environmentId: string | null;
|
||||
}): Promise<string> {
|
||||
return invokeCmd('cmd_decrypt_template', { template, workspaceId, environmentId });
|
||||
}
|
||||
|
||||
@@ -1,31 +1,22 @@
|
||||
import { emit } from '@tauri-apps/api/event';
|
||||
import { getCurrentWebviewWindow } from '@tauri-apps/api/webviewWindow';
|
||||
import { setWindowTitle } from '@yaakapp-internal/mac-window';
|
||||
import { useAtomValue } from 'jotai';
|
||||
import { useEffect } from 'react';
|
||||
import { jotaiStore } from '../lib/jotai';
|
||||
import { resolvedModelName } from '../lib/resolvedModelName';
|
||||
import { useActiveEnvironment } from './useActiveEnvironment';
|
||||
import { activeRequestAtom } from './useActiveRequest';
|
||||
import { activeWorkspaceAtom } from './useActiveWorkspace';
|
||||
import { useAppInfo } from './useAppInfo';
|
||||
import { useOsInfo } from './useOsInfo';
|
||||
import { appInfo } from './useAppInfo';
|
||||
|
||||
export function useSyncWorkspaceRequestTitle() {
|
||||
const activeWorkspace = useAtomValue(activeWorkspaceAtom);
|
||||
const activeEnvironment = useActiveEnvironment();
|
||||
const osInfo = useOsInfo();
|
||||
const appInfo = useAppInfo();
|
||||
const activeRequest = useAtomValue(activeRequestAtom);
|
||||
|
||||
useEffect(() => {
|
||||
if (osInfo.osType == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
let newTitle = activeWorkspace ? activeWorkspace.name : 'Yaak';
|
||||
if (activeEnvironment) {
|
||||
newTitle += ` [${activeEnvironment.name}]`;
|
||||
}
|
||||
const activeRequest = jotaiStore.get(activeRequestAtom);
|
||||
if (activeRequest) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
newTitle += ` › ${resolvedModelName(activeRequest)}`;
|
||||
@@ -35,12 +26,6 @@ export function useSyncWorkspaceRequestTitle() {
|
||||
newTitle = `[DEV] ${newTitle}`;
|
||||
}
|
||||
|
||||
// TODO: This resets the stoplight position so we can't use it on macOS yet. So we send
|
||||
// a custom command instead
|
||||
if (osInfo.osType !== 'macos') {
|
||||
getCurrentWebviewWindow().setTitle(newTitle).catch(console.error);
|
||||
} else {
|
||||
emit('yaak_title_changed', newTitle).catch(console.error);
|
||||
}
|
||||
}, [activeEnvironment, activeWorkspace, appInfo.isDev, osInfo.osType]);
|
||||
setWindowTitle(newTitle);
|
||||
}, [activeEnvironment, activeRequest, activeWorkspace]);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import { invokeCmd } from '../lib/tauri';
|
||||
export function useTemplateTokensToString(tokens: Tokens) {
|
||||
return useQuery<string>({
|
||||
placeholderData: (prev) => prev, // Keep previous data on refetch
|
||||
refetchOnWindowFocus: false,
|
||||
queryKey: ['template_tokens_to_string', tokens],
|
||||
queryFn: () => templateTokensToString(tokens),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user