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

fix: correctly handle pin flows #297

Merged
merged 3 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export function OnboardingDataProtection({ goToNextStep }: OnboardingDataProtect
}

const onToggleCloudHsm = () => {
if (isLoading) return
const newShouldUseCloudHsm = !shouldUseCloudHsm

toast.show(
Expand Down
14 changes: 9 additions & 5 deletions apps/easypid/src/features/onboarding/screens/welcome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ export default function OnboardingWelcome({ goToNextStep }: OnboardingWelcomePro
left="50%"
ai="center"
jc="center"
shadowOffset={{ width: 5, height: 5 }}
shadowColor="$grey-400"
shadowOpacity={0.5}
shadowRadius={24}
>
<Stack br="$7" ov="hidden">
<Stack
br="$7"
ov="hidden"
bg="$primary-500"
shadowOffset={{ width: 5, height: 5 }}
shadowColor="$grey-400"
shadowOpacity={0.5}
shadowRadius={24}
>
<Image height={96} width={96} src={appIcon} />
</Stack>
</YStack>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export function FunkeCredentialNotificationScreen() {
[isDevelopmentModeEnabled]
)

const shouldUsePinForPresentation = useShouldUsePinForSubmission(credentialsForRequest)
const shouldUsePinForPresentation = useShouldUsePinForSubmission(credentialsForRequest?.formattedSubmission)
const preAuthGrant =
resolvedCredentialOffer?.credentialOfferPayload.grants?.['urn:ietf:params:oauth:grant-type:pre-authorized_code']
const txCode = preAuthGrant?.tx_code
Expand Down
43 changes: 25 additions & 18 deletions apps/easypid/src/features/share/FunkeMdocOfflineSharingScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useAppAgent } from '@easypid/agent'
import { setWalletServiceProviderPin } from '@easypid/crypto/WalletServiceProviderClient'
import { InvalidPinError } from '@easypid/crypto/error'
import { useDevelopmentMode } from '@easypid/hooks'
import { useShouldUsePinForSubmission } from '@easypid/hooks/useShouldUsePinForPresentation'
import { type FormattedSubmission, getSubmissionForMdocDocumentRequest } from '@package/agent'
import { usePushToWallet } from '@package/app/src/hooks/usePushToWallet'
import { useToastController } from '@package/ui'
Expand All @@ -28,6 +29,7 @@ export function FunkeMdocOfflineSharingScreen({

const [submission, setSubmission] = useState<FormattedSubmission>()
const [isProcessing, setIsProcessing] = useState(false)
const shouldUsePin = useShouldUsePinForSubmission(submission)

useEffect(() => {
getSubmissionForMdocDocumentRequest(agent, deviceRequest)
Expand Down Expand Up @@ -55,30 +57,34 @@ export function FunkeMdocOfflineSharingScreen({
[toast, pushToWallet]
)

const onProofAccept = async ({ pin, onPinComplete, onPinError }: onPinSubmitProps) => {
const onProofAccept = async ({ pin, onPinComplete, onPinError }: onPinSubmitProps = {}) => {
// Already checked for submission in the useEffect
if (!submission) return
if (!pin) {
onPinError?.()
return
}

setIsProcessing(true)

try {
await setWalletServiceProviderPin(pin.split('').map(Number))
} catch (e) {
setIsProcessing(false)
if (e instanceof InvalidPinError) {
if (shouldUsePin) {
Copy link
Member

Choose a reason for hiding this comment

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

Doest this also handle the biometric prompt canceled / incorrect error?

There's two biometric errors. Not sure if they're handled somewhere higher/ lower. It would probably be good if we can create a proof sharing pipeline at some point, that will be used for ALL proof sharing, and you can toggle the options that are needed. As with the offline / openid /dc api / whatever it's hard to keep everything in sync

if (e instanceof BiometricAuthenticationCancelledError) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How can I test the biometrics flow? None of the funke flows seem to trigger it? Or do I need to be on secure enclave to test that

Copy link
Member

Choose a reason for hiding this comment

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

Yes secure env

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay i handled it the same way as the other flows, but unable to test because secure env is broken.

if (!pin) {
onPinError?.()
return
}

handleError({
reason: 'Authentication Error',
redirect: true,
description:
e instanceof Error && isDevelopmentModeEnabled ? `Development mode error: ${e.message}` : undefined,
})
setIsProcessing(true)

try {
await setWalletServiceProviderPin(pin.split('').map(Number))
} catch (e) {
setIsProcessing(false)
if (e instanceof InvalidPinError) {
onPinError?.()
return handleError({ reason: 'Invalid PIN entered', redirect: false })
}

return handleError({
reason: 'Authentication Error',
redirect: true,
description:
e instanceof Error && isDevelopmentModeEnabled ? `Development mode error: ${e.message}` : undefined,
})
}
}

// Once this returns we just assume it's successful
Expand Down Expand Up @@ -147,6 +153,7 @@ export function FunkeMdocOfflineSharingScreen({
onAccept={onProofAccept}
onDecline={onProofDecline}
onComplete={onProofComplete}
usePin={shouldUsePin ?? false}
/>
)
}
76 changes: 40 additions & 36 deletions apps/easypid/src/features/share/FunkeOfflineSharingScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,60 +1,64 @@
import type { FormattedSubmission } from 'packages/agent/src'
import { SlideWizard } from 'packages/app/src/components'
import { type SlideStep, SlideWizard } from 'packages/app/src/components'
import { LoadingRequestSlide } from '../receive/slides/LoadingRequestSlide'
import { PinSlide, type onPinSubmitProps } from './slides/PinSlide'
import { PinSlide } from './slides/PinSlide'
import { PresentationSuccessSlide } from './slides/PresentationSuccessSlide'
import { ShareCredentialsSlide } from './slides/ShareCredentialsSlide'

interface FunkeOfflineSharingScreenProps {
submission?: FormattedSubmission
usePin: boolean
isAccepting: boolean
onAccept: ({ pin, onPinComplete, onPinError }: onPinSubmitProps) => Promise<void>
onAccept: () => Promise<void>
onDecline: () => void
onComplete: () => void
}

export function FunkeOfflineSharingScreen({
submission,
usePin,
isAccepting,
onAccept,
onDecline,
onComplete,
}: FunkeOfflineSharingScreenProps) {
return (
<SlideWizard
steps={[
{
step: 'loading-request',
progress: 25,
screen: <LoadingRequestSlide key="loading-request" isLoading={!submission} isError={false} />,
},
{
step: 'share-credentials',
progress: 50,
backIsCancel: true,
screen: (
<ShareCredentialsSlide
key="share-credentials"
onAccept={undefined} // onAccept is handled in the next slide
onDecline={onDecline}
submission={submission as FormattedSubmission}
isAccepting={isAccepting}
isOffline
/>
),
},
{
step: 'pin-enter',
progress: 75,
screen: <PinSlide key="pin-enter" isLoading={isAccepting} onPinSubmit={onAccept} />,
},
{
step: 'success',
progress: 100,
backIsCancel: true,
screen: <PresentationSuccessSlide onComplete={onComplete} />,
},
]}
steps={
[
{
step: 'loading-request',
progress: 30,
screen: <LoadingRequestSlide key="loading-request" isLoading={!submission} isError={false} />,
},
{
step: 'share-credentials',
progress: 60,
backIsCancel: true,
screen: (
<ShareCredentialsSlide
key="share-credentials"
onAccept={usePin ? undefined : onAccept}
onDecline={onDecline}
submission={submission as FormattedSubmission}
isAccepting={isAccepting}
isOffline
/>
),
},
usePin && {
step: 'pin-enter',
progress: 80,
screen: <PinSlide key="pin-enter" isLoading={isAccepting} onPinSubmit={onAccept} />,
},
{
step: 'success',
progress: 100,
backIsCancel: true,
screen: <PresentationSuccessSlide onComplete={onComplete} />,
},
].filter(Boolean) as SlideStep[]
}
onCancel={onDecline}
/>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function FunkeOpenIdPresentationNotificationScreen() {
filters: { entityId: credentialsForRequest?.verifier.entityId ?? 'NO MATCH' },
})
const lastInteractionDate = activities?.[0]?.date
const shouldUsePin = useShouldUsePinForSubmission(credentialsForRequest)
const shouldUsePin = useShouldUsePinForSubmission(credentialsForRequest?.formattedSubmission)

useEffect(() => {
if (credentialsForRequest) return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ export const ShareCredentialsSlide = ({
const { onNext, onCancel } = useWizard()
const [scrollViewHeight, setScrollViewHeight] = useState(0)
const { isScrolledByOffset, handleScroll, scrollEventThrottle } = useScrollViewPosition()
const [isProcessing, setIsProcessing] = useState(isAccepting)

const handleAccept = async () => {
// Manually set to instantly show the loading state
setIsProcessing(true)

await onAccept?.()
onNext()
}
Expand Down Expand Up @@ -96,7 +100,7 @@ export const ShareCredentialsSlide = ({
declineText="Stop"
onAccept={handleAccept}
onDecline={handleDecline}
isLoading={isAccepting}
isLoading={isProcessing}
/>
) : (
<YStack gap="$3">
Expand Down
8 changes: 4 additions & 4 deletions apps/easypid/src/hooks/useShouldUsePinForPresentation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import type { CredentialsForProofRequest } from '@package/agent'
import { useMemo } from 'react'
import { useShouldUseCloudHsm } from '../features/onboarding/useShouldUseCloudHsm'

export const useShouldUsePinForSubmission = (credentialsForRequest?: CredentialsForProofRequest) => {
export const useShouldUsePinForSubmission = (submission?: CredentialsForProofRequest['formattedSubmission']) => {
const [shouldUseCloudHsm] = useShouldUseCloudHsm()

const shouldUsePin = useMemo(() => {
if (shouldUseCloudHsm) return true
const isPidInSubmission =
credentialsForRequest?.formattedSubmission?.entries.some((entry) =>
submission?.entries.some((entry) =>
// TODO: should store this on the category as well so we can easily detect auth
entry.isSatisfied ? entry.credentials[0].credential.category?.credentialCategory === 'DE-PID' : false
) ?? false

return !isPidInSubmission
}, [credentialsForRequest, shouldUseCloudHsm])
}, [submission, shouldUseCloudHsm])

return credentialsForRequest === undefined ? undefined : shouldUsePin
return submission === undefined ? undefined : shouldUsePin
}