Some checks failed
App Prod-Parity E2E Dispatch / dispatch (push) Failing after 10s
CD Storybook UI / build (push) Failing after 3m25s
CD Storybook UI / dispatch (push) Has been skipped
CD deploy main / website-changed-files (push) Successful in 18m8s
CI Agent Skills / changed-files-check (push) Successful in 18m9s
CI Create App E2E minimal / changed-files-check (push) Successful in 24s
CI Create App / changed-files-check (push) Successful in 26s
CI Docs / changed-files-check (push) Successful in 26s
CI Emails / changed-files-check (push) Successful in 28s
CI Example App Hello World / changed-files-check (push) Successful in 23s
CI Front / changed-files-check (push) Has been skipped
CI Example App Postcard / changed-files-check (push) Successful in 27s
CI UI / changed-files-check (push) Has been skipped
CI Utils Test / changed-files-check (push) Successful in 45s
CI Twenty Apps / discover (push) Successful in 1m3s
CD deploy main / deploy-main (push) Failing after 3s
CI Agent Skills / agent-skills-validate (push) Has been skipped
CI Create App / create-app-test (lint) (push) Has been skipped
CI Emails / emails-test (push) Has been skipped
CI Front / front-task (lingui:compile) (push) Has been skipped
CI Front / front-task (lint) (push) Has been skipped
CI Example App Postcard / example-app-postcard (push) Has been skipped
CI UI / ui-task (lint) (push) Has been skipped
CI UI / ui-task (pack-size) (push) Has been skipped
CI UI / ui-task (size) (push) Has been skipped
CI UI / ui-task (test) (push) Has been skipped
CI UI / ui-task (typecheck) (push) Has been skipped
CI Utils Test / utils-test (push) Has been skipped
CI Twenty Apps / ${{ matrix.app.name }} (push) Has been skipped
CI Emails / ci-emails-status-check (push) Successful in 2s
CI Example App Hello World / ci-example-app-hello-world-status-check (push) Successful in 2s
CI Example App Postcard / ci-example-app-postcard-status-check (push) Successful in 3s
CI UI / ui-sb-test (push) Has been skipped
CI Utils Test / ci-utils-test-status-check (push) Successful in 2s
CI Twenty Apps / ci-twenty-apps-status-check (push) Successful in 2s
CI UI / ci-ui-status-check (push) Successful in 2s
CI Desktop / desktop (push) Failing after 1m8s
Push docs to Crowdin / Push documentation to Crowdin (push) Failing after 55s
Push translations to Crowdin / Extract and upload translations (push) Failing after 5s
Push website translations to Crowdin / Extract and upload website translations (push) Failing after 5s
CI Create App / create-app-test (test) (push) Has been skipped
CI Create App / create-app-test (typecheck) (push) Has been skipped
CI Docs / docs-lint (push) Has been skipped
CI Example App Hello World / example-app-hello-world (push) Has been skipped
CI Front / front-task (lingui:extract) (push) Has been skipped
CI Front / front-task (test) (push) Has been skipped
CI Front / front-task (typecheck) (push) Has been skipped
CI UI / ui-sb-build (push) Failing after 1m32s
CI Twenty Apps / ${{ matrix.app.name }} (${{ matrix.server-source }}) (push) Has been skipped
CI Agent Skills / agent-skills-publish (push) Has been skipped
CI Create App / ci-create-app-status-check (push) Successful in 2s
CI Agent Skills / agent-skills-remote-source (push) Has been skipped
CI Create App E2E minimal / create-app-e2e-minimal (push) Has been cancelled
CI E2E Main / e2e-test (push) Has been cancelled
CI Front / front-sb-build (push) Has been cancelled
CI Front / front-build (push) Has been cancelled
CI Create App E2E minimal / ci-create-app-e2e-minimal-status-check (push) Has been cancelled
CI E2E Main / ci-e2e-main-status-check (push) Has been cancelled
CI E2E Main / notify-main-ci-failure (push) Has been cancelled
CI E2E Main / qa-scout-comment (push) Has been cancelled
CI Front / front-sb-test (push) Has been cancelled
CI Front / front-sb-screenshots (push) Has been cancelled
CI Front / ci-front-status-check (push) Has been cancelled
DPA Sub-Processor Sync / sync-subprocessors (push) Failing after 3s
AI Catalog Sync / sync-catalog (push) Failing after 2s
158 lines
4.9 KiB
TypeScript
158 lines
4.9 KiB
TypeScript
import { randomUUID } from 'node:crypto';
|
|
import {
|
|
readFile,
|
|
writeFile,
|
|
rename,
|
|
mkdir,
|
|
unlink,
|
|
copyFile,
|
|
} from 'node:fs/promises';
|
|
import { join } from 'node:path';
|
|
import { safeStorage } from 'electron';
|
|
import { z } from 'zod';
|
|
import { DEFAULT_SETTINGS } from '../../shared/constants/DEFAULT_SETTINGS';
|
|
import { settingsSchema } from '../../shared/validation-schemas/settingsSchema';
|
|
import { type Settings } from '../../shared/types/Settings';
|
|
import { credentialsSchema } from '../validation-schemas/credentialsSchema';
|
|
import { type Credentials } from '../types/Credentials';
|
|
import { SettingsRecoveryError } from '../errors/SettingsRecoveryError';
|
|
|
|
export class SecureStore {
|
|
private handledWrite: Promise<void> = Promise.resolve();
|
|
|
|
constructor(private directory: string) {}
|
|
|
|
async readCredentials(): Promise<Credentials | null> {
|
|
try {
|
|
return credentialsSchema.parse(
|
|
JSON.parse(
|
|
safeStorage.decryptString(
|
|
await readFile(join(this.directory, 'credentials')),
|
|
),
|
|
),
|
|
);
|
|
} catch (error) {
|
|
if ((error as NodeJS.ErrnoException).code === 'ENOENT') return null;
|
|
throw new Error(
|
|
'Could not unlock your Twenty connection. Reconnect to Twenty.',
|
|
);
|
|
}
|
|
}
|
|
|
|
async writeCredentials(credentials: Credentials): Promise<void> {
|
|
if (!safeStorage.isEncryptionAvailable())
|
|
throw new Error(
|
|
'macOS Keychain is unavailable. Your connection could not be saved.',
|
|
);
|
|
await this.writeAtomic(
|
|
'credentials',
|
|
safeStorage.encryptString(JSON.stringify(credentials)),
|
|
);
|
|
}
|
|
|
|
async clearCredentials(): Promise<void> {
|
|
await unlink(join(this.directory, 'credentials')).catch(
|
|
(error: NodeJS.ErrnoException) => {
|
|
if (error.code !== 'ENOENT') throw error;
|
|
},
|
|
);
|
|
}
|
|
|
|
async readSettings(): Promise<Settings> {
|
|
const path = join(this.directory, 'settings.json');
|
|
let contents: string;
|
|
try {
|
|
contents = await readFile(path, 'utf8');
|
|
} catch (error) {
|
|
if ((error as NodeJS.ErrnoException).code === 'ENOENT')
|
|
return { ...DEFAULT_SETTINGS };
|
|
throw new SettingsRecoveryError(
|
|
{ ...DEFAULT_SETTINGS, autoJoin: false, autoRecord: false },
|
|
'Could not read your settings. Automatic joining and recording are off. Check Settings before continuing.',
|
|
);
|
|
}
|
|
let saved: unknown;
|
|
try {
|
|
saved = JSON.parse(contents);
|
|
} catch {
|
|
saved = null;
|
|
}
|
|
const savedObject = z.record(z.string(), z.unknown()).safeParse(saved);
|
|
const migrated = settingsSchema.safeParse({
|
|
...DEFAULT_SETTINGS,
|
|
...(savedObject.success ? savedObject.data : {}),
|
|
});
|
|
if (savedObject.success && migrated.success) return migrated.data;
|
|
|
|
const recovered = {
|
|
...DEFAULT_SETTINGS,
|
|
autoJoin: false,
|
|
autoRecord: false,
|
|
};
|
|
if (savedObject.success) {
|
|
for (const [key, schema] of Object.entries(settingsSchema.shape)) {
|
|
const field = schema.safeParse(savedObject.data[key]);
|
|
if (field.success) Object.assign(recovered, { [key]: field.data });
|
|
}
|
|
}
|
|
// Keep the original available even if a later preference change saves the recovered settings.
|
|
await copyFile(path, `${path}.damaged-${randomUUID()}`);
|
|
throw new SettingsRecoveryError(
|
|
recovered,
|
|
'Some settings could not be restored. A backup was saved and valid preferences were kept. Review Settings before continuing.',
|
|
);
|
|
}
|
|
|
|
async writeSettings(settings: Settings): Promise<void> {
|
|
await this.writeAtomic(
|
|
'settings.json',
|
|
JSON.stringify(settingsSchema.parse(settings)),
|
|
);
|
|
}
|
|
|
|
async readHandledMeetings(): Promise<string[]> {
|
|
try {
|
|
return z
|
|
.array(z.string())
|
|
.parse(
|
|
JSON.parse(
|
|
await readFile(
|
|
join(this.directory, 'handled-meetings.json'),
|
|
'utf8',
|
|
),
|
|
),
|
|
);
|
|
} catch (error) {
|
|
if ((error as NodeJS.ErrnoException).code === 'ENOENT') return [];
|
|
throw new Error(
|
|
'Could not restore skipped meetings. Automatic joining is off. Review Settings before continuing.',
|
|
{ cause: error },
|
|
);
|
|
}
|
|
}
|
|
|
|
async writeHandledMeetings(ids: string[]): Promise<void> {
|
|
const contents = JSON.stringify(ids.slice(-500));
|
|
const write = this.handledWrite.then(() =>
|
|
this.writeAtomic('handled-meetings.json', contents),
|
|
);
|
|
this.handledWrite = write.catch(() => undefined);
|
|
await write;
|
|
}
|
|
|
|
private async writeAtomic(
|
|
name: string,
|
|
data: string | Buffer,
|
|
): Promise<void> {
|
|
await mkdir(this.directory, { recursive: true, mode: 0o700 });
|
|
const path = join(this.directory, name);
|
|
const temporaryPath = `${path}.${randomUUID()}.tmp`;
|
|
try {
|
|
await writeFile(temporaryPath, data, { mode: 0o600 });
|
|
await rename(temporaryPath, path);
|
|
} finally {
|
|
await unlink(temporaryPath).catch(() => undefined);
|
|
}
|
|
}
|
|
}
|