mirror of
https://github.com/WonderAliceUwU/OmniArr.git
synced 2026-11-24 19:35:56 +00:00
⭐️ feature: first app steps
This commit is contained in:
commit
7d9343ead3
51 files changed
+8226
No files matched your search
@@ -0,0 +1,68 @@
|
||||
import { createCipheriv, createDecipheriv, randomBytes, scryptSync, timingSafeEqual } from 'node:crypto';
|
||||
|
||||
const KEY_LENGTH = 32;
|
||||
|
||||
export interface PasswordRecord {
|
||||
passwordHash: string;
|
||||
passwordSalt: string;
|
||||
encryptionSalt: string;
|
||||
}
|
||||
|
||||
export function createPasswordRecord(password: string): PasswordRecord {
|
||||
const passwordSalt = randomBase64(16);
|
||||
const encryptionSalt = randomBase64(16);
|
||||
const passwordHash = derivePasswordHash(password, passwordSalt);
|
||||
|
||||
return {
|
||||
passwordHash,
|
||||
passwordSalt,
|
||||
encryptionSalt
|
||||
};
|
||||
}
|
||||
|
||||
export function verifyPassword(password: string, record: PasswordRecord): boolean {
|
||||
const expected = Buffer.from(record.passwordHash, 'base64');
|
||||
const actual = Buffer.from(derivePasswordHash(password, record.passwordSalt), 'base64');
|
||||
|
||||
return expected.length === actual.length && timingSafeEqual(expected, actual);
|
||||
}
|
||||
|
||||
export function deriveEncryptionKey(password: string, encryptionSalt: string): Buffer {
|
||||
return scryptSync(password, encryptionSalt, KEY_LENGTH);
|
||||
}
|
||||
|
||||
export function encryptJson(value: unknown, key: Buffer): { iv: string; data: string } {
|
||||
const iv = randomBytes(12);
|
||||
const cipher = createCipheriv('aes-256-gcm', key, iv);
|
||||
const plaintext = Buffer.from(JSON.stringify(value), 'utf8');
|
||||
const encrypted = Buffer.concat([cipher.update(plaintext), cipher.final()]);
|
||||
const tag = cipher.getAuthTag();
|
||||
|
||||
return {
|
||||
iv: iv.toString('base64'),
|
||||
data: Buffer.concat([encrypted, tag]).toString('base64')
|
||||
};
|
||||
}
|
||||
|
||||
export function decryptJson<T>(payload: { iv: string; data: string }, key: Buffer): T {
|
||||
const raw = Buffer.from(payload.data, 'base64');
|
||||
const encrypted = raw.subarray(0, raw.length - 16);
|
||||
const tag = raw.subarray(raw.length - 16);
|
||||
const decipher = createDecipheriv('aes-256-gcm', key, Buffer.from(payload.iv, 'base64'));
|
||||
decipher.setAuthTag(tag);
|
||||
const json = Buffer.concat([decipher.update(encrypted), decipher.final()]).toString('utf8');
|
||||
|
||||
return JSON.parse(json) as T;
|
||||
}
|
||||
|
||||
export function randomToken(): string {
|
||||
return randomBase64(32).replace(/[+/=]/g, '');
|
||||
}
|
||||
|
||||
function derivePasswordHash(password: string, salt: string): string {
|
||||
return scryptSync(password, salt, 64).toString('base64');
|
||||
}
|
||||
|
||||
function randomBase64(size: number): string {
|
||||
return randomBytes(size).toString('base64');
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,84 @@
|
||||
import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
||||
import { dirname, resolve } from 'node:path';
|
||||
interface StoredAdmin {
|
||||
passwordHash: string;
|
||||
passwordSalt: string;
|
||||
encryptionSalt: string;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
interface EncryptedConfig {
|
||||
iv: string;
|
||||
data: string;
|
||||
}
|
||||
|
||||
interface StoredState {
|
||||
version: 1;
|
||||
admin: StoredAdmin | null;
|
||||
config: EncryptedConfig | null;
|
||||
}
|
||||
|
||||
const DATA_DIR = resolve(process.env.OMNIARR_DATA_DIR || resolve(process.cwd(), 'data'));
|
||||
const STORE_FILE = resolve(DATA_DIR, 'omniarr.secure.json');
|
||||
|
||||
const EMPTY_STATE: StoredState = {
|
||||
version: 1,
|
||||
admin: null,
|
||||
config: null
|
||||
};
|
||||
|
||||
export async function loadState(): Promise<StoredState> {
|
||||
try {
|
||||
const raw = await readFile(STORE_FILE, 'utf8');
|
||||
const parsed = JSON.parse(raw) as Partial<StoredState>;
|
||||
|
||||
return {
|
||||
version: 1,
|
||||
admin: parsed.admin ?? null,
|
||||
config: parsed.config ?? null
|
||||
};
|
||||
} catch {
|
||||
return EMPTY_STATE;
|
||||
}
|
||||
}
|
||||
|
||||
export async function saveState(state: StoredState): Promise<void> {
|
||||
await mkdir(dirname(STORE_FILE), { recursive: true });
|
||||
await writeFile(STORE_FILE, JSON.stringify(state, null, 2), 'utf8');
|
||||
}
|
||||
|
||||
export async function needsSetup(): Promise<boolean> {
|
||||
const state = await loadState();
|
||||
return !state.admin;
|
||||
}
|
||||
|
||||
export async function readEncryptedConfig(): Promise<EncryptedConfig | null> {
|
||||
const state = await loadState();
|
||||
return state.config;
|
||||
}
|
||||
|
||||
export async function saveAdminAndConfig(
|
||||
admin: StoredAdmin,
|
||||
config: EncryptedConfig
|
||||
): Promise<void> {
|
||||
await saveState({
|
||||
version: 1,
|
||||
admin,
|
||||
config
|
||||
});
|
||||
}
|
||||
|
||||
export async function updateEncryptedConfig(config: EncryptedConfig): Promise<void> {
|
||||
const state = await loadState();
|
||||
await saveState({
|
||||
...state,
|
||||
config
|
||||
});
|
||||
}
|
||||
|
||||
export async function readAdmin(): Promise<StoredAdmin | null> {
|
||||
const state = await loadState();
|
||||
return state.admin;
|
||||
}
|
||||
|
||||
export type { EncryptedConfig, StoredAdmin, StoredState };
|
||||
Reference in new issue
Block a user