feat: MFA tools, single-instance, tray polish, prefs persistence

Session highlights:

- feat(prefs): DPAPI-backed key/value store (PM.UserPrefs) — fixes
  rememberedUsername being lost across reboots due to the random
  ephemeral HTTP port changing the localStorage origin every launch.
  Bridge cmd://prefs/{get,set} round-trips through Delphi.

- feat(tray): icon visible from startup (NIM_ADD at constructor, not
  at first minimize). Tray context menu themed via uxtheme!135
  SetPreferredAppMode so it follows the app's dark/light setting.

- feat(single-instance): named mutex + RegisterWindowMessage broadcast.
  Second launch posts WM_PMSHOW to HWND_BROADCAST and exits; the
  running bridge restores the window from tray. Mutex lives in Local\
  namespace so distinct Windows users can still each run one.

- feat(mfa): Authenticator sidebar view (live TOTP codes for every
  entry with a secret) + standalone TOTP generator modal (paste
  base32 / otpauth:// URI, or generate a random 20-byte secret).

- feat(sidebar): Folders / Tags / Tools sections collapsible with
  chevron toggle. Badge counts stay visible when collapsed. State
  persisted in settings_json (synced across devices).

- feat(autofill): hotkey when vault is locked now restores the app
  and focuses the master password input instead of no-op'ing
  silently. Cleaner UX for the common "I hit Ctrl+Shift+L but the
  vault was locked" path.

- feat(quick-unlock): when enabled, skip lockVault on Windows lock /
  sleep. Rationale: the DPAPI blob already gates access via the
  Windows account, so re-locking on top of the OS lock is redundant.
  Idle auto-lock still fires (separate opt-in).

- fix(quick-unlock): re-sync state.quickUnlockEnabled from DPAPI
  source-of-truth at boot, instead of trusting (now-volatile)
  localStorage.

- docs: CLAUDE.md updated with all new modules, bridge commands,
  and the port-ephemeral pitfall.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-06-08 21:31:39 +01:00
parent 664db65437
commit 40b3154a34
38 changed files with 8165 additions and 548 deletions
+180
View File
@@ -0,0 +1,180 @@
unit PM.UserPrefs;
{
Device-bound key/value prefs persisted across launches.
Problem solved: the HTTP server binds an ephemeral port that changes on
every start (49152-65535). localStorage is keyed by origin (scheme+host
+port) so a different port = a fresh localStorage = anything persisted
there is lost between launches. For prefs that must survive a reboot
(remembered username, etc.) we persist them via this unit instead.
Storage: %LOCALAPPDATA%\PMServer\prefs.bin
Format: DPAPI-encrypted UTF-8 JSON object {"key":"value",....
Scope: current Windows user (same threat model as PM.QuickUnlock).
}
interface
uses
System.SysUtils, System.Classes, System.IOUtils, System.JSON,
Winapi.Windows;
function GetPref(const AKey: string): string;
procedure SetPref(const AKey, AValue: string);
implementation
type
TDataBlob = record
cbData: DWORD;
pbData: PByte;
end;
PDataBlob = ^TDataBlob;
function CryptProtectData(pDataIn: PDataBlob; szDataDescr: PWideChar;
pOptionalEntropy: PDataBlob; pvReserved: Pointer; pPromptStruct: Pointer;
dwFlags: DWORD; pDataOut: PDataBlob): BOOL; stdcall;
external 'crypt32.dll' name 'CryptProtectData';
function CryptUnprotectData(pDataIn: PDataBlob; ppszDataDescr: PPWideChar;
pOptionalEntropy: PDataBlob; pvReserved: Pointer; pPromptStruct: Pointer;
dwFlags: DWORD; pDataOut: PDataBlob): BOOL; stdcall;
external 'crypt32.dll' name 'CryptUnprotectData';
function LocalFree(hMem: HLOCAL): HLOCAL; stdcall;
external 'kernel32.dll' name 'LocalFree';
function StorageDir: string;
begin
Result := TPath.Combine(GetEnvironmentVariable('LOCALAPPDATA'), 'PMServer');
end;
function StorageFile: string;
begin
Result := TPath.Combine(StorageDir, 'prefs.bin');
end;
procedure EnsureStorageDir;
begin
if not TDirectory.Exists(StorageDir) then
TDirectory.CreateDirectory(StorageDir);
end;
function LoadAll: TJSONObject;
var
LEncrypted: TBytes;
LIn, LOut: TDataBlob;
LStream: TFileStream;
LPlain: string;
LValue: TJSONValue;
begin
// Default to an empty object; every error path just Exits with this.
// Only the success path replaces it with the parsed JSON.
Result := TJSONObject.Create;
if not TFile.Exists(StorageFile) then Exit;
try
LStream := TFileStream.Create(StorageFile, fmOpenRead or fmShareDenyWrite);
try
SetLength(LEncrypted, LStream.Size);
if Length(LEncrypted) > 0 then
LStream.ReadBuffer(LEncrypted[0], LStream.Size);
finally
LStream.Free;
end;
except
Exit;
end;
if Length(LEncrypted) = 0 then Exit;
LIn.cbData := Length(LEncrypted);
LIn.pbData := @LEncrypted[0];
LOut.pbData := nil;
LOut.cbData := 0;
if not CryptUnprotectData(@LIn, nil, nil, nil, nil, 0, @LOut) then Exit;
try
SetString(LPlain, PAnsiChar(LOut.pbData), LOut.cbData);
LValue := TJSONObject.ParseJSONValue(TEncoding.UTF8.GetBytes(LPlain), 0);
if LValue is TJSONObject then
begin
// Replace the default empty object with the parsed one.
Result.Free;
Result := TJSONObject(LValue);
end
else if LValue <> nil then
LValue.Free;
finally
if LOut.pbData <> nil then LocalFree(HLOCAL(LOut.pbData));
end;
end;
procedure SaveAll(AObj: TJSONObject);
var
LBytes: TBytes;
LIn, LOut: TDataBlob;
LStream: TFileStream;
LJsonStr: string;
begin
LJsonStr := AObj.ToJSON;
LBytes := TEncoding.UTF8.GetBytes(LJsonStr);
if Length(LBytes) = 0 then Exit;
LIn.cbData := Length(LBytes);
LIn.pbData := @LBytes[0];
LOut.pbData := nil;
LOut.cbData := 0;
if not CryptProtectData(@LIn, nil, nil, nil, nil, 0, @LOut) then Exit;
try
EnsureStorageDir;
LStream := TFileStream.Create(StorageFile, fmCreate);
try
LStream.WriteBuffer(LOut.pbData^, LOut.cbData);
finally
LStream.Free;
end;
finally
if LOut.pbData <> nil then LocalFree(HLOCAL(LOut.pbData));
end;
end;
function GetPref(const AKey: string): string;
var
LObj: TJSONObject;
LValue: TJSONValue;
begin
Result := '';
LObj := LoadAll;
try
if LObj = nil then Exit;
LValue := LObj.GetValue(AKey);
if LValue <> nil then
Result := LValue.Value;
finally
LObj.Free;
end;
end;
procedure SetPref(const AKey, AValue: string);
var
LObj: TJSONObject;
LExisting: TJSONValue;
begin
LObj := LoadAll;
try
if LObj = nil then LObj := TJSONObject.Create;
LExisting := LObj.GetValue(AKey);
if LExisting <> nil then
LObj.RemovePair(AKey).Free;
LObj.AddPair(AKey, AValue);
SaveAll(LObj);
finally
LObj.Free;
end;
end;
end.