-
Notifications
You must be signed in to change notification settings - Fork 18
App Configurable Auto lock #905
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
723206e
add initial version of app auto lock
leofelix077 1b7d43c
update state handling when coming from background and comments
leofelix077 f50bf1f
Merge branch 'main' into lf-add-auto-lock
leofelix077 996b9a9
add secure flags for app background state to avoid snapshotting
leofelix077 820f7db
fix loading account states when coming from background
leofelix077 14fcdc9
add auto lock timer in seconds debug option and verification for lock…
leofelix077 ba81a3c
preserve sign in method from unlock and improve debug auto lock options
leofelix077 44f10d9
add visible countdown timers for debugging oh physical device
leofelix077 96ad730
add ios deterministic privacy shield
leofelix077 5cf10fd
add android auto lock privacy screen
leofelix077 98de964
adjust lock on foreground and background behaviors
leofelix077 8281249
capture presses and nav to reset idle lock count
leofelix077 e781e13
adjust comments from codex for stale state
leofelix077 3d82b3c
suprress biometric prompt on manual logout
leofelix077 9053a4e
wait for lock screen to show to ask for biometric prompt
leofelix077 288f47e
Merge branch 'main' into lf-add-auto-lock
leofelix077 7ab2ad5
clear auto lock values on wallet import and new sign up
leofelix077 08566bc
Merge branch 'lf-add-auto-lock' of github.com:stellar/freighter-mobil…
leofelix077 c80f602
harden background and lock security, change default to 12h and add mo…
leofelix077 ccf97d2
Merge remote-tracking branch 'origin/main' into lf-add-auto-lock
leofelix077 5a5e9e2
remove dev lock timers
leofelix077 d6d05ad
fix unlock toast error id
leofelix077 2e0f538
remove unintentional commited doc
leofelix077 5fa28e8
Merge remote-tracking branch 'origin/main' into lf-add-auto-lock
leofelix077 1f13577
Merge branch 'main' into lf-add-auto-lock
piyalbasu 3d72965
Cap hash-key hard-expiry at 48h instead of 7 days
piyalbasu 8de23cc
Abort cleanly (not error) when auto-lock engages during signing
piyalbasu 0045ac7
Match auto-lock timer options exactly to the Freighter extension
piyalbasu 8150ae1
Fix three soft-lock gaps from review triage
piyalbasu 51e4790
Fix three soft-lock timing/ordering races from review triage
piyalbasu 1c6ea0e
Harden hash-key hard-expiry against clock rollback
piyalbasu 78455db
Fix AboutScreen test: mock getBundleId (unblocks CI)
piyalbasu e1becc7
Widen hash-key hard-expiry to 72h
piyalbasu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { act } from "@testing-library/react-native"; | ||
| import { LockScreenOverlay } from "components/LockScreenOverlay"; | ||
| import { AUTH_STATUS } from "config/types"; | ||
| import { useAuthenticationStore } from "ducks/auth"; | ||
| import { renderWithProviders } from "helpers/testUtils"; | ||
| import React from "react"; | ||
|
|
||
| jest.mock("ducks/auth", () => { | ||
| const actual = jest.requireActual("ducks/auth"); | ||
| return { | ||
| ...actual, | ||
| getActiveAccountPublicKey: jest.fn().mockResolvedValue(null), | ||
| }; | ||
| }); | ||
|
|
||
| jest.mock("services/autoLock", () => ({ | ||
| persistAutoLockTimer: jest.fn().mockResolvedValue(undefined), | ||
| applyAutoLockTimerToHashKey: jest.fn().mockResolvedValue(undefined), | ||
| })); | ||
|
|
||
| describe("LockScreenOverlay", () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it("renders nothing when the wallet is not soft-locked", () => { | ||
| useAuthenticationStore.setState({ | ||
| authStatus: AUTH_STATUS.AUTHENTICATED, | ||
| isSoftLocked: false, | ||
| }); | ||
|
|
||
| const { queryByTestId } = renderWithProviders(<LockScreenOverlay />); | ||
|
|
||
| expect(queryByTestId("lock-screen-overlay")).toBeNull(); | ||
| expect(queryByTestId("lock-screen")).toBeNull(); | ||
| }); | ||
|
|
||
| it("renders the lock UI above the app when soft-locked", () => { | ||
| useAuthenticationStore.setState({ | ||
| authStatus: AUTH_STATUS.LOCKED, | ||
| isSoftLocked: true, | ||
| }); | ||
|
|
||
| const { getByTestId } = renderWithProviders(<LockScreenOverlay />); | ||
|
|
||
| expect(getByTestId("lock-screen-overlay")).toBeTruthy(); | ||
| expect(getByTestId("lock-screen")).toBeTruthy(); | ||
| expect(getByTestId("unlock-button")).toBeTruthy(); | ||
| }); | ||
|
|
||
| it("disappears when the wallet is unlocked", () => { | ||
| useAuthenticationStore.setState({ | ||
| authStatus: AUTH_STATUS.LOCKED, | ||
| isSoftLocked: true, | ||
| }); | ||
|
|
||
| const { getByTestId, queryByTestId } = renderWithProviders( | ||
| <LockScreenOverlay />, | ||
| ); | ||
| expect(getByTestId("lock-screen-overlay")).toBeTruthy(); | ||
|
|
||
| // Unlock: signIn success resets the store which clears isSoftLocked | ||
| act(() => { | ||
| useAuthenticationStore.setState({ | ||
| authStatus: AUTH_STATUS.AUTHENTICATED, | ||
| isSoftLocked: false, | ||
| }); | ||
| }); | ||
|
|
||
| expect(queryByTestId("lock-screen-overlay")).toBeNull(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| import { NativeStackScreenProps } from "@react-navigation/native-stack"; | ||
| import { waitFor } from "@testing-library/react-native"; | ||
| import { LockScreen } from "components/screens/LockScreen"; | ||
| import { LoginType } from "config/constants"; | ||
| import { ROOT_NAVIGATOR_ROUTES, RootStackParamList } from "config/routes"; | ||
| import { useAuthenticationStore } from "ducks/auth"; | ||
| import { usePreferencesStore } from "ducks/preferences"; | ||
| import { renderWithProviders } from "helpers/testUtils"; | ||
| import React from "react"; | ||
| import { AppState } from "react-native"; | ||
|
|
||
| jest.mock("ducks/auth", () => { | ||
| const actual = jest.requireActual("ducks/auth"); | ||
| return { | ||
| ...actual, | ||
| getActiveAccountPublicKey: jest.fn().mockResolvedValue(null), | ||
| }; | ||
| }); | ||
|
|
||
| jest.mock("services/autoLock", () => ({ | ||
| persistAutoLockTimer: jest.fn().mockResolvedValue(undefined), | ||
| applyAutoLockTimerToHashKey: jest.fn().mockResolvedValue(undefined), | ||
| })); | ||
|
|
||
| type LockScreenNavigationProp = NativeStackScreenProps< | ||
| RootStackParamList, | ||
| typeof ROOT_NAVIGATOR_ROUTES.LOCK_SCREEN | ||
| >["navigation"]; | ||
|
|
||
| type LockScreenRouteProp = NativeStackScreenProps< | ||
| RootStackParamList, | ||
| typeof ROOT_NAVIGATOR_ROUTES.LOCK_SCREEN | ||
| >["route"]; | ||
|
|
||
| const mockNavigation = { | ||
| replace: jest.fn(), | ||
| goBack: jest.fn(), | ||
| setOptions: jest.fn(), | ||
| } as unknown as LockScreenNavigationProp; | ||
|
|
||
| const mockRoute = { | ||
| key: "lock-screen", | ||
| name: ROOT_NAVIGATOR_ROUTES.LOCK_SCREEN, | ||
| } as unknown as LockScreenRouteProp; | ||
|
|
||
| describe("LockScreen", () => { | ||
| const mockSignIn = jest.fn(); | ||
| const mockVerifyActionWithBiometrics = jest.fn( | ||
| (callback: (password?: string) => Promise<unknown>) => | ||
| callback("biometric-password"), | ||
| ); | ||
|
|
||
| const previousAppState = AppState.currentState; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| (AppState as { currentState: string }).currentState = "active"; | ||
|
|
||
| useAuthenticationStore.setState({ | ||
| signIn: mockSignIn, | ||
| verifyActionWithBiometrics: | ||
| mockVerifyActionWithBiometrics as unknown as ReturnType< | ||
| typeof useAuthenticationStore.getState | ||
| >["verifyActionWithBiometrics"], | ||
| signInMethod: LoginType.FACE, | ||
| isLoading: false, | ||
| error: null, | ||
| }); | ||
| usePreferencesStore.setState({ isBiometricsEnabled: true }); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| (AppState as { currentState: typeof previousAppState }).currentState = | ||
| previousAppState; | ||
| }); | ||
|
|
||
| const renderLockScreen = () => | ||
| renderWithProviders( | ||
| <LockScreen navigation={mockNavigation} route={mockRoute} />, | ||
| ); | ||
|
|
||
| it("auto-prompts biometrics on mount and unlocks with the stored password", async () => { | ||
| renderLockScreen(); | ||
|
|
||
| await waitFor(() => { | ||
| expect(mockVerifyActionWithBiometrics).toHaveBeenCalledTimes(1); | ||
| }); | ||
| expect(mockSignIn).toHaveBeenCalledWith({ | ||
| password: "biometric-password", | ||
| }); | ||
| }); | ||
|
|
||
| it("does not auto-prompt when biometrics are disabled", async () => { | ||
| usePreferencesStore.setState({ isBiometricsEnabled: false }); | ||
| useAuthenticationStore.setState({ signInMethod: LoginType.PASSWORD }); | ||
|
|
||
| renderLockScreen(); | ||
|
|
||
| await waitFor(() => { | ||
| expect(mockVerifyActionWithBiometrics).not.toHaveBeenCalled(); | ||
| }); | ||
| expect(mockSignIn).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("re-prompts biometrics when the app returns from the background", async () => { | ||
| renderLockScreen(); | ||
|
|
||
| await waitFor(() => { | ||
| expect(mockVerifyActionWithBiometrics).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| // Simulate the app going to the background and returning to the foreground | ||
| const appStateHandlers = ( | ||
| AppState.addEventListener as jest.Mock | ||
| ).mock.calls.map(([, handler]) => handler as (state: string) => void); | ||
|
|
||
| appStateHandlers.forEach((handler) => handler("background")); | ||
| appStateHandlers.forEach((handler) => handler("active")); | ||
|
|
||
| await waitFor(() => { | ||
| expect(mockVerifyActionWithBiometrics).toHaveBeenCalledTimes(2); | ||
| }); | ||
| }); | ||
|
|
||
| it("does not re-prompt on inactive-to-active transitions (e.g. the biometric overlay itself)", async () => { | ||
| renderLockScreen(); | ||
|
|
||
| await waitFor(() => { | ||
| expect(mockVerifyActionWithBiometrics).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| const appStateHandlers = ( | ||
| AppState.addEventListener as jest.Mock | ||
| ).mock.calls.map(([, handler]) => handler as (state: string) => void); | ||
|
|
||
| appStateHandlers.forEach((handler) => handler("inactive")); | ||
| appStateHandlers.forEach((handler) => handler("active")); | ||
|
|
||
| await waitFor(() => { | ||
| expect(mockVerifyActionWithBiometrics).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); | ||
| }); |
96 changes: 96 additions & 0 deletions
96
__tests__/components/screens/SettingsScreen/AutoLockTimerScreen.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import { NativeStackScreenProps } from "@react-navigation/native-stack"; | ||
| import { userEvent } from "@testing-library/react-native"; | ||
| import AutoLockTimerScreen from "components/screens/SettingsScreen/SecurityScreen/AutoLockTimerScreen"; | ||
| import { AUTO_LOCK_TIMER, DEFAULT_AUTO_LOCK_TIMER } from "config/constants"; | ||
| import { SETTINGS_ROUTES, SettingsStackParamList } from "config/routes"; | ||
| import { usePreferencesStore } from "ducks/preferences"; | ||
| import { renderWithProviders } from "helpers/testUtils"; | ||
| import React from "react"; | ||
|
|
||
| jest.mock("services/autoLock", () => ({ | ||
| persistAutoLockTimer: jest.fn().mockResolvedValue(undefined), | ||
| applyAutoLockTimerToHashKey: jest.fn().mockResolvedValue(undefined), | ||
| })); | ||
|
|
||
| type AutoLockTimerScreenNavigationProp = NativeStackScreenProps< | ||
| SettingsStackParamList, | ||
| typeof SETTINGS_ROUTES.AUTO_LOCK_TIMER_SCREEN | ||
| >["navigation"]; | ||
|
|
||
| type AutoLockTimerScreenRouteProp = NativeStackScreenProps< | ||
| SettingsStackParamList, | ||
| typeof SETTINGS_ROUTES.AUTO_LOCK_TIMER_SCREEN | ||
| >["route"]; | ||
|
|
||
| const mockNavigation = { | ||
| goBack: jest.fn(), | ||
| setOptions: jest.fn(), | ||
| } as unknown as AutoLockTimerScreenNavigationProp; | ||
|
|
||
| const mockRoute = { | ||
| key: "auto-lock-timer", | ||
| name: SETTINGS_ROUTES.AUTO_LOCK_TIMER_SCREEN, | ||
| } as unknown as AutoLockTimerScreenRouteProp; | ||
|
|
||
| describe("AutoLockTimerScreen", () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| usePreferencesStore.setState({ autoLockTimer: DEFAULT_AUTO_LOCK_TIMER }); | ||
| }); | ||
|
|
||
| const renderAutoLockTimerScreen = () => | ||
| renderWithProviders( | ||
| <AutoLockTimerScreen navigation={mockNavigation} route={mockRoute} />, | ||
| ); | ||
|
|
||
| it("renders all timer options", () => { | ||
| const { getByTestId } = renderAutoLockTimerScreen(); | ||
|
|
||
| expect( | ||
| getByTestId(`auto-lock-option-${AUTO_LOCK_TIMER.IMMEDIATELY}`), | ||
| ).toBeTruthy(); | ||
| expect( | ||
| getByTestId(`auto-lock-option-${AUTO_LOCK_TIMER.ONE_MINUTE}`), | ||
| ).toBeTruthy(); | ||
| expect( | ||
| getByTestId(`auto-lock-option-${AUTO_LOCK_TIMER.FIFTEEN_MINUTES}`), | ||
| ).toBeTruthy(); | ||
| expect( | ||
| getByTestId(`auto-lock-option-${AUTO_LOCK_TIMER.THIRTY_MINUTES}`), | ||
| ).toBeTruthy(); | ||
| expect( | ||
| getByTestId(`auto-lock-option-${AUTO_LOCK_TIMER.ONE_HOUR}`), | ||
| ).toBeTruthy(); | ||
| expect( | ||
| getByTestId(`auto-lock-option-${AUTO_LOCK_TIMER.TWELVE_HOURS}`), | ||
| ).toBeTruthy(); | ||
| expect( | ||
| getByTestId(`auto-lock-option-${AUTO_LOCK_TIMER.TWENTY_FOUR_HOURS}`), | ||
| ).toBeTruthy(); | ||
| expect( | ||
| getByTestId(`auto-lock-option-${AUTO_LOCK_TIMER.NONE}`), | ||
| ).toBeTruthy(); | ||
| }); | ||
|
|
||
| it("renders the footer explanation", () => { | ||
| const { getByText } = renderAutoLockTimerScreen(); | ||
|
|
||
| expect( | ||
| getByText( | ||
| "After a set time, you will be prompted for your password again as an extra security measure.", | ||
| ), | ||
| ).toBeTruthy(); | ||
| }); | ||
|
|
||
| it("updates the preference when an option is tapped", async () => { | ||
| const { getByTestId } = renderAutoLockTimerScreen(); | ||
|
|
||
| await userEvent.press( | ||
| getByTestId(`auto-lock-option-${AUTO_LOCK_TIMER.FIFTEEN_MINUTES}`), | ||
| ); | ||
|
|
||
| expect(usePreferencesStore.getState().autoLockTimer).toBe( | ||
| AUTO_LOCK_TIMER.FIFTEEN_MINUTES, | ||
| ); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.