π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
refactor(AppLock): enhance native device lock handling and update IPC methods
Aswin Asokc2e1d0ae08ed
diff --git a/desktop/src/main/ipc.ts b/desktop/src/main/ipc.tsindex c2d1c8c9d7..89abe36c44 100644--- a/desktop/src/main/ipc.ts+++ b/desktop/src/main/ipc.ts@@ -133,12 +133,24 @@ export const attachIPCHandlers = () => {ipcMain.handle("toggleAutoLaunch", () => autoLauncher.toggleAutoLaunch());+ // - Desktop app lock (native device authentication)+ //+ // These handlers are the main-process bridge for the desktop app lock flow:+ // the renderer asks about native auth capability/support, then requests a+ // native unlock prompt when the user needs to unlock the app.++ // Returns richer capability details (for example, available prompt type)+ // so the UI can decide which app-lock option to show.ipcMain.handle("getNativeDeviceLockCapability", () =>getNativeDeviceLockCapability(),);+ // Returns a simple yes/no for whether native device lock can be used on+ // this machine. Useful for quick feature gating in settings.ipcMain.handle("isDeviceLockSupported", () => isDeviceLockSupported());+ // Triggers the OS-native authentication prompt (Touch ID / Windows Hello /+ // etc.) and returns the auth result back to the renderer.ipcMain.handle("promptDeviceLock", (_, reason: string) =>promptDeviceLock(reason),);diff --git a/desktop/src/types/ipc.ts b/desktop/src/types/ipc.tsindex 776dde56dc..0c3f284327 100644--- a/desktop/src/types/ipc.ts+++ b/desktop/src/types/ipc.ts@@ -12,16 +12,31 @@ export interface AppUpdate {version: string;}+/**+ * Native provider used for desktop app lock authentication.+ *+ * `touchid` means we can use the platform-native auth prompt.+ * `none` means no supported native provider is currently available.+ */export type NativeDeviceLockProvider = "touchid" | "none";+/**+ * Why native device lock is unavailable on this machine/session.+ */export type NativeDeviceLockUnavailableReason =| "unsupported-platform"| "touchid-not-enrolled"| "touchid-api-error";+/**+ * Capability metadata returned by main-process device-lock checks.+ */export interface NativeDeviceLockCapability {+ /** True when native auth can be prompted right now. */available: boolean;+ /** Which native provider backs authentication (if available). */provider: NativeDeviceLockProvider;+ /** Present when unavailable, with a machine-readable reason. */reason?: NativeDeviceLockUnavailableReason;}diff --git a/web/packages/base/types/ipc.ts b/web/packages/base/types/ipc.tsindex d96b09e40b..80a16d9824 100644--- a/web/packages/base/types/ipc.ts+++ b/web/packages/base/types/ipc.ts@@ -3,16 +3,32 @@//// See [Note: types.ts <-> preload.ts <-> ipc.ts]+/**+ * Native provider used for desktop app lock authentication.+ *+ * `touchid` means we can use the platform-native auth prompt.+ * `none` means no supported native provider is currently available.+ */export type NativeDeviceLockProvider = "touchid" | "none";+/**+ * Why native device lock is unavailable on this machine/session.+ */export type NativeDeviceLockUnavailableReason =| "unsupported-platform"| "touchid-not-enrolled"| "touchid-api-error";+/**+ * Capability metadata used by the renderer to decide whether to show and use+ * "Device lock" as an app-lock mode.+ */export interface NativeDeviceLockCapability {+ /** True when native auth can be prompted right now. */available: boolean;+ /** Which native provider backs authentication (if available). */provider: NativeDeviceLockProvider;+ /** Present when unavailable, with a machine-readable reason. */reason?: NativeDeviceLockUnavailableReason;}@@ -172,20 +188,17 @@ export interface Electron {toggleAutoLaunch: () => Promise<void>;/**- * Return native device lock capability details for the current platform.- */- getNativeDeviceLockCapability: () => Promise<NativeDeviceLockCapability>;-- /**- * Return true if native device lock authentication is available.+ * Return native device lock capability details for the current machine.*- * @deprecated Prefer {@link getNativeDeviceLockCapability}.+ * Includes whether native auth is available, provider metadata, and a+ * reason when unavailable.*/- isDeviceLockSupported: () => Promise<boolean>;+ getNativeDeviceLockCapability: () => Promise<NativeDeviceLockCapability>;/*** Prompt native device lock authentication.*+ * The {@link reason} can be shown by the OS in the native auth prompt.* Returns true on successful authentication.*/promptDeviceLock: (reason: string) => Promise<boolean>;diff --git a/web/packages/new/photos/components/sidebar/AppLockSettings.tsx b/web/packages/new/photos/components/sidebar/AppLockSettings.tsxindex de90ec178b..aa3cd75c60 100644--- a/web/packages/new/photos/components/sidebar/AppLockSettings.tsx+++ b/web/packages/new/photos/components/sidebar/AppLockSettings.tsx@@ -1,11 +1,6 @@import CheckIcon from "@mui/icons-material/Check";import ChevronRightIcon from "@mui/icons-material/ChevronRight";-import {- DialogActions,- Stack,- TextField,- Typography,-} from "@mui/material";+import { DialogActions, Stack, TextField, Typography } from "@mui/material";import { TitledMiniDialog } from "ente-base/components/MiniDialog";import { FocusVisibleButton } from "ente-base/components/mui/FocusVisibleButton";import { ShowHidePasswordInputAdornment } from "ente-base/components/mui/PasswordInputAdornment";@@ -43,21 +38,27 @@ export const AppLockSettings: React.FC<NestedSidebarDrawerVisibilityProps> = ({}) => {const state = useAppLockSnapshot();+ // For the 4-digit PIN setup and confirmation dialog.const [pinDialogOpen, setPinDialogOpen] = useState(false);+ // For the password setup and confirmation dialog.const [passwordDialogOpen, setPasswordDialogOpen] = useState(false);+ // Loading/disable flag while async device-lock setup is running.const [isSettingDeviceLock, setIsSettingDeviceLock] = useState(false);+ // Controls whether the Device lock option should be shown after a compatibility check.const [showDeviceLockOption, setShowDeviceLockOption] = useState(false);+ // For the auto-lock duration selection dialog.const [autoLockDialogOpen, setAutoLockDialogOpen] = useState(false);- const deviceLockOptionRequestGeneration = useRef(0);+ // Cancel flag used to avoid state updates after unmount.+ const isDeviceLockOptionRequestCancelled = useRef(false);const { showMiniDialog } = useBaseContext();useEffect(() => {- const generation = ++deviceLockOptionRequestGeneration.current;+ isDeviceLockOptionRequestCancelled.current = false;void (async () => {try {const visible = await shouldShowDeviceLockOption();- if (deviceLockOptionRequestGeneration.current === generation) {+ if (!isDeviceLockOptionRequestCancelled.current) {setShowDeviceLockOption(visible);}} catch (e) {@@ -65,14 +66,14 @@ export const AppLockSettings: React.FC<NestedSidebarDrawerVisibilityProps> = ({"Failed to determine device lock option visibility",e,);- if (deviceLockOptionRequestGeneration.current === generation) {+ if (!isDeviceLockOptionRequestCancelled.current) {setShowDeviceLockOption(false);}}})();return () => {- deviceLockOptionRequestGeneration.current += 1;+ isDeviceLockOptionRequestCancelled.current = true;};}, []);diff --git a/web/packages/new/photos/components/utils/use-snapshot.ts b/web/packages/new/photos/components/utils/use-snapshot.tsindex 7eaee6a8c1..0b76c01b3a 100644--- a/web/packages/new/photos/components/utils/use-snapshot.ts+++ b/web/packages/new/photos/components/utils/use-snapshot.ts@@ -21,7 +21,7 @@ import {* to updates.*/export const useAppLockSnapshot = () =>- useSyncExternalStore(appLockSubscribe, appLockSnapshot);+ useSyncExternalStore(appLockSubscribe, appLockSnapshot, appLockSnapshot);/*** A convenience hook that returns {@link settingsSnapshot}, and alsodiff --git a/web/packages/new/photos/services/app-lock.ts b/web/packages/new/photos/services/app-lock.tsindex 05f9e29fe1..d1345eaaea 100644--- a/web/packages/new/photos/services/app-lock.ts+++ b/web/packages/new/photos/services/app-lock.ts@@ -500,6 +500,8 @@ const unsupportedNativeDeviceLockCapability: NativeDeviceLockCapability = {const nativeDeviceLockCapability =async (): Promise<NativeDeviceLockCapability> => {+ // Native device lock is available only in the desktop (Electron) app.+ // On web, we always treat it as unsupported.if (!globalThis.electron) return unsupportedNativeDeviceLockCapability;try {@@ -510,12 +512,7 @@ const nativeDeviceLockCapability =return await globalThis.electron.getNativeDeviceLockCapability();}- // Legacy desktop bridge compatibility.- // eslint-disable-next-line @typescript-eslint/no-deprecated- const supported = await globalThis.electron.isDeviceLockSupported();- return supported- ? { available: true, provider: "touchid" }- : unsupportedNativeDeviceLockCapability;+ return unsupportedNativeDeviceLockCapability;} catch (e) {log.warn("Failed to query native device lock support", e);return unsupportedNativeDeviceLockCapability;@@ -557,6 +554,14 @@ export const isDeviceLockSupported = async () => {return capability.usable;};+/**+ * Return true if the current environment should show "Device lock" in the+ * lock-type picker.+ *+ * We show it when native auth is currently available, or when the platform+ * supports it but setup/auth is temporarily unavailable (for example, Touch ID+ * not enrolled).+ */export const shouldShowDeviceLockOption = async () => {const capability = await nativeDeviceLockCapability();return capability.available || capability.reason !== "unsupported-platform";