Skip to content
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

feat: manual biometrics and background lock #201

Merged
merged 9 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions apps/easypid/src/app/_layout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { NoInternetToastProvider, Provider, useTransparentNavigationBar } from '@package/app'
import {
BackgroundLockProvider,
DeeplinkHandler,
NoInternetToastProvider,
Provider,
useTransparentNavigationBar,
} from '@package/app'
import { SecureUnlockProvider } from '@package/secure-store/secureUnlock'
import { DefaultTheme, ThemeProvider } from '@react-navigation/native'
import { Slot } from 'expo-router'
Expand Down Expand Up @@ -28,9 +34,13 @@ export default function RootLayout() {
},
}}
>
<NoInternetToastProvider>
<Slot />
</NoInternetToastProvider>
<BackgroundLockProvider>
<NoInternetToastProvider>
<DeeplinkHandler>
<Slot />
</DeeplinkHandler>
</NoInternetToastProvider>
</BackgroundLockProvider>
</ThemeProvider>
</SecureUnlockProvider>
</Provider>
Expand Down
27 changes: 25 additions & 2 deletions apps/easypid/src/app/authenticate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ import { Redirect } from 'expo-router'
import { WalletInvalidKeyError } from '@credo-ts/core'
import { initializeAppAgent, useSecureUnlock } from '@easypid/agent'
import { secureWalletKey } from '@package/secure-store/secureUnlock'
import { FlexPage, Heading, HeroIcons, PinDotsInput, type PinDotsInputRef, YStack } from '@package/ui'
import {
FlexPage,
Heading,
HeroIcons,
PinDotsInput,
type PinDotsInputRef,
YStack,
useToastController,
} from '@package/ui'
import * as SplashScreen from 'expo-splash-screen'
import { useEffect, useRef, useState } from 'react'
import { Circle } from 'tamagui'
Expand All @@ -15,17 +23,19 @@ import { useResetWalletDevMenu } from '../utils/resetWallet'
export default function Authenticate() {
useResetWalletDevMenu()

const toast = useToastController()
const secureUnlock = useSecureUnlock()
const pinInputRef = useRef<PinDotsInputRef>(null)
const [isInitializingAgent, setIsInitializingAgent] = useState(false)
const isLoading =
secureUnlock.state === 'acquired-wallet-key' || (secureUnlock.state === 'locked' && secureUnlock.isUnlocking)

// biome-ignore lint/correctness/useExhaustiveDependencies: canTryUnlockingUsingBiometrics not needed
useEffect(() => {
if (secureUnlock.state === 'locked' && secureUnlock.canTryUnlockingUsingBiometrics) {
secureUnlock.tryUnlockingUsingBiometrics()
}
}, [secureUnlock])
}, [])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem correct. What are you trying to do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, it prompts me for me biometrics right away, and if you cancel it prompts again until you reach 3.

Now it prompts once, if it fails or gets cancelled it stops, and you can prompt it again by pressing the biometrics button on the pinpad.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm okay. I see. We should still pass secureUnlock.state right?


useEffect(() => {
if (secureUnlock.state !== 'acquired-wallet-key') return
Expand Down Expand Up @@ -62,6 +72,18 @@ export default function Authenticate() {

void SplashScreen.hideAsync()

const unlockUsingBiometrics = async () => {
if (secureUnlock.state === 'locked') {
secureUnlock.tryUnlockingUsingBiometrics()
} else {
toast.show('You PIN is required to unlock the app', {
customData: {
preset: 'danger',
},
})
}
}

const unlockUsingPin = async (pin: string) => {
if (secureUnlock.state !== 'locked') return
await secureUnlock.unlockUsingPin(pin)
Expand All @@ -82,6 +104,7 @@ export default function Authenticate() {
ref={pinInputRef}
pinLength={6}
onPinComplete={unlockUsingPin}
onBiometricsTap={unlockUsingBiometrics}
useNativeKeyboard={false}
/>
</FlexPage>
Expand Down
39 changes: 39 additions & 0 deletions packages/app/src/provider/BackgroundLockProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { PropsWithChildren } from 'react'

import { useSecureUnlock } from '@package/secure-store/secure-wallet-key/SecureUnlockProvider'
import { useEffect, useRef } from 'react'
import React from 'react'
import { AppState, type AppStateStatus } from 'react-native'

const BACKGROUND_TIME_THRESHOLD = 3000 // FIXME

export function BackgroundLockProvider({ children }: PropsWithChildren) {
const secureUnlock = useSecureUnlock()
const backgroundTimeRef = useRef<Date | null>(null)

useEffect(() => {
const handleAppStateChange = (nextAppState: AppStateStatus) => {
if (nextAppState === 'background' || nextAppState === 'inactive') {
backgroundTimeRef.current = new Date()
} else if (nextAppState === 'active') {
if (backgroundTimeRef.current) {
const timeInBackground = new Date().getTime() - backgroundTimeRef.current.getTime()
janrtvld marked this conversation as resolved.
Show resolved Hide resolved

if (timeInBackground > BACKGROUND_TIME_THRESHOLD && secureUnlock.state === 'unlocked') {
janrtvld marked this conversation as resolved.
Show resolved Hide resolved
console.log('App was in background for more than 30 seconds, locking')
secureUnlock.lock()
}
backgroundTimeRef.current = null
}
}
}

const subscription = AppState.addEventListener('change', handleAppStateChange)

return () => {
subscription.remove()
}
}, [secureUnlock])

return <>{children}</>
}
1 change: 1 addition & 0 deletions packages/app/src/provider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './Provider'
export * from './ToastViewport'
export * from './NoInternetToastProvider'
export * from './ModalProvider'
export * from './BackgroundLockProvider'
2 changes: 1 addition & 1 deletion packages/app/src/utils/DeeplinkHandler.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useCallback } from 'react'
import type { ReactNode } from 'react'

import { InvitationQrTypes, type InvitationType } from '@package/agent'
import { InvitationQrTypes } from '@package/agent'
import { useToastController } from '@package/ui'
import { CommonActions } from '@react-navigation/native'
import * as Linking from 'expo-linking'
Expand Down
14 changes: 12 additions & 2 deletions packages/ui/src/components/PinDotsInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface PinDotsInputProps {
onPinComplete: (pin: string) => void
isLoading?: boolean
useNativeKeyboard?: boolean
onBiometricsTap?: () => void
}

export interface PinDotsInputRef {
Expand All @@ -28,7 +29,7 @@ export interface PinDotsInputRef {

export const PinDotsInput = forwardRef(
(
{ onPinComplete, pinLength, isLoading, useNativeKeyboard = true }: PinDotsInputProps,
{ onPinComplete, pinLength, isLoading, useNativeKeyboard = true, onBiometricsTap }: PinDotsInputProps,
ref: ForwardedRef<PinDotsInputRef>
) => {
const [pin, setPin] = useState('')
Expand Down Expand Up @@ -95,6 +96,11 @@ export const PinDotsInput = forwardRef(
return
}

if (character === PinValues.Biometrics && onBiometricsTap) {
onBiometricsTap()
return
}

setPin((currentPin) => {
const newPin = currentPin + character

Expand Down Expand Up @@ -156,7 +162,11 @@ export const PinDotsInput = forwardRef(
secureTextEntry
/>
) : (
<PinPad onPressPinNumber={onPressPinNumber} disabled={isInLoadingState} />
<PinPad
onPressPinNumber={onPressPinNumber}
disabled={isInLoadingState}
useBiometricsPad={!!onBiometricsTap}
/>
)}
</YStack>
)
Expand Down
19 changes: 13 additions & 6 deletions packages/ui/src/components/PinPad.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Text, useTheme } from 'tamagui'
import { Text } from 'tamagui'
import { Stack, XStack, YStack } from '../base'
import { HeroIcons } from '../content'

Expand All @@ -12,10 +12,12 @@ export enum PinValues {
Seven = '7',
Eight = '8',
Nine = '9',
Empty = '',
Zero = '0',
Backspace = 'backspace',
Empty = '',
Biometrics = 'biometrics',
}

const letterMap: Record<PinValues, string> = {
[PinValues.One]: '',
[PinValues.Two]: 'abc',
Expand All @@ -27,6 +29,7 @@ const letterMap: Record<PinValues, string> = {
[PinValues.Eight]: 'tuv',
[PinValues.Nine]: 'wxyz',
[PinValues.Zero]: '',
[PinValues.Biometrics]: '',
[PinValues.Empty]: '',
[PinValues.Backspace]: '',
}
Expand All @@ -41,9 +44,10 @@ const PinNumber = ({ character, onPressPinNumber, disabled }: PinNumberProps) =>
fg={1}
jc="center"
ai="center"
backgroundColor={character === PinValues.Backspace ? '$grey-200' : '$white'}
backgroundColor={
[PinValues.Backspace, PinValues.Biometrics, PinValues.Empty].includes(character) ? '$grey-200' : '$white'
}
pressStyle={{ opacity: 0.5, backgroundColor: '$grey-100' }}
opacity={character === PinValues.Empty ? 0 : 1}
onPress={() => onPressPinNumber(character)}
disabled={disabled}
h="$6"
Expand All @@ -53,6 +57,8 @@ const PinNumber = ({ character, onPressPinNumber, disabled }: PinNumberProps) =>
>
{character === PinValues.Backspace ? (
<HeroIcons.Backspace color="$grey-900" size={24} />
) : character === PinValues.Biometrics ? (
<HeroIcons.FingerPrint color="$grey-900" size={24} />
) : (
<YStack ai="center" gap="$1">
{/* NOTE: using fontSize $ values will crash on android due to an issue with react-native-reanimated (it seems the string value is sent to the native side, which shouldn't happen) */}
Expand All @@ -71,15 +77,16 @@ const PinNumber = ({ character, onPressPinNumber, disabled }: PinNumberProps) =>

export interface PinPadProps {
onPressPinNumber: (character: PinValues) => void
useBiometricsPad?: boolean
disabled?: boolean
}

export const PinPad = ({ onPressPinNumber, disabled }: PinPadProps) => {
export const PinPad = ({ onPressPinNumber, useBiometricsPad, disabled }: PinPadProps) => {
const pinValues = [
[PinValues.One, PinValues.Two, PinValues.Three],
[PinValues.Four, PinValues.Five, PinValues.Six],
[PinValues.Seven, PinValues.Eight, PinValues.Nine],
[PinValues.Empty, PinValues.Zero, PinValues.Backspace],
[useBiometricsPad ? PinValues.Biometrics : PinValues.Empty, PinValues.Zero, PinValues.Backspace],
janrtvld marked this conversation as resolved.
Show resolved Hide resolved
]

const pinNumbers = pinValues.map((rowItems, rowIndex) => (
Expand Down