Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

refactor(AppLock): enhance device lock setup and add detailed comments

Aswin Asokfe464b7c531e
diff --git a/web/packages/new/photos/components/sidebar/AppLockSettings.tsx b/web/packages/new/photos/components/sidebar/AppLockSettings.tsx
index aa3cd75c60..cf0fbd617c 100644
--- a/web/packages/new/photos/components/sidebar/AppLockSettings.tsx
+++ b/web/packages/new/photos/components/sidebar/AppLockSettings.tsx
@@ -77,6 +77,9 @@ export const AppLockSettings: React.FC<NestedSidebarDrawerVisibilityProps> = ({
};
}, []);
+ /**
+ * Close both levels of the nested drawer.
+ */
const handleRootClose = () => {
onClose();
onRootClose();
@@ -108,10 +111,14 @@ export const AppLockSettings: React.FC<NestedSidebarDrawerVisibilityProps> = ({
}, []);
const handleSelectDeviceLock = useCallback(async () => {
+ // Ignore repeated clicks while setup is already running.
if (isSettingDeviceLock) return;
+ // Show loading state while native device-lock setup runs.
setIsSettingDeviceLock(true);
try {
+ // setupDeviceLock performs capability checks and returns typed
+ // failure reasons when setup is unavailable or not completed.
const result = await setupDeviceLock();
if (result.status === "success") return;
@@ -128,10 +135,12 @@ export const AppLockSettings: React.FC<NestedSidebarDrawerVisibilityProps> = ({
}
}, [isSettingDeviceLock, showMiniDialog]);
+ // Close the PIN setup dialog after a successful setup.
const handlePinSetupComplete = useCallback(() => {
setPinDialogOpen(false);
}, []);
+ // Close the password setup dialog after a successful setup.
const handlePasswordSetupComplete = useCallback(() => {
setPasswordDialogOpen(false);
}, []);
diff --git a/web/packages/new/photos/services/app-lock.ts b/web/packages/new/photos/services/app-lock.ts
index d1345eaaea..80f86311b0 100644
--- a/web/packages/new/photos/services/app-lock.ts
+++ b/web/packages/new/photos/services/app-lock.ts
@@ -575,6 +575,12 @@ const clearPassphraseMaterial = async () =>
removeKV(kvKeyMemLimit),
]);
+/**
+ * Reset brute-force protection state.
+ *
+ * Clears persisted invalid-attempt and cooldown values in KV, updates the
+ * in-memory snapshot to zero, and optionally broadcasts the reset to other tabs.
+ */
const resetBruteForceState = async (shouldBroadcast = false) => {
await Promise.all([
setKV(kvKeyInvalidAttempts, 0),
@@ -665,13 +671,19 @@ export const setupPassword = async (password: string) =>
* Set up native device lock authentication for app lock (macOS only).
*/
export const setupDeviceLock = async (): Promise<SetupDeviceLockResult> => {
+ // Resolve whether native device lock is currently usable in this environment.
+ // Flow: resolveDeviceLockCapability() -> nativeDeviceLockCapability() ->
+ // globalThis.electron.getNativeDeviceLockCapability().
const capability = await resolveDeviceLockCapability();
+
+ // Surface unsupported reasons to callers/UI.
if (!capability.usable) {
logDeviceLockUnsupported("setup", capability.reason);
return { status: "not-supported", reason: capability.reason };
}
try {
+ // Trigger the OS-native authentication prompt (for example, Touch ID).
const unlocked = await globalThis.electron?.promptDeviceLock(
deviceLockEnablePromptReason,
);
@@ -680,11 +692,14 @@ export const setupDeviceLock = async (): Promise<SetupDeviceLockResult> => {
return { status: "failed", reason: "native-prompt-failed" };
}
+ // Reset brute-force lockout/cooldown state in KV and in memory.
await resetBruteForceState(true);
+ // Save the selected app-lock method and enabled state in localStorage.
localStorage.setItem(lsKeyAppLockMethod, "device");
localStorage.setItem(lsKeyEnabled, "true");
+ // Update the in-memory app-lock snapshot.
setSnapshot({
..._state.snapshot,
enabled: true,
@@ -692,6 +707,8 @@ export const setupDeviceLock = async (): Promise<SetupDeviceLockResult> => {
invalidAttemptCount: 0,
cooldownExpiresAt: 0,
});
+
+ // Stop ongoing brute-force hydration and sync config to other tabs/windows.
stopBruteForceStateHydration();
syncConfigAcrossTabs(_state.snapshot);