Skip to content

Commit

Permalink
Merge pull request #276 from idpass/fix/384-biometrics
Browse files Browse the repository at this point in the history
Fix biometrics and cancel flow
  • Loading branch information
pmigueld authored Jan 13, 2023
2 parents b78ee3b + 334c0cb commit d4ec88c
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 35 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ buildscript {
jcenter()
}
dependencies {
classpath("com.android.tools.build:gradle:4.1.0")
classpath("com.android.tools.build:gradle:4.2.2")
classpath 'com.google.gms:google-services:4.3.5'
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.4.1'

Expand Down
8 changes: 5 additions & 3 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
},
"rejected": {
"title": "Notice",
"message": "You rejected {{sender}}'s {{vcLabel}}"
"message": "You discarded {{sender}}'s {{vcLabel}}"
},
"disconnected": {
"title": "Disconnected",
Expand Down Expand Up @@ -240,7 +240,9 @@
"exchangingDeviceInfo": "Exchanging device info...",
"exchangingDeviceInfoTimeout": "It's taking a while to exchange device info. You may have to reconnect.",
"invalid": "Invalid QR Code",
"offline": "Please connect to the internet to scan QR codes using Online sharing mode"
"offline": "Please connect to the internet to scan QR codes using Online sharing mode",
"sent": "{{ vcLabel }} has been sent...",
"sentHint": "Waiting for receiver to save or discard your {{ vcLabel }}"
}
},
"SelectVcOverlay": {
Expand All @@ -266,7 +268,7 @@
},
"rejected": {
"title": "Notice",
"message": "Your {{vcLabel}} was rejected by {{receiver}}"
"message": "Your {{vcLabel}} was discarded by {{receiver}}"
}
},
"consentToPhotoVerification": "I give consent to have my photo taken for authentication"
Expand Down
8 changes: 5 additions & 3 deletions locales/fil.json
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
},
"rejected": {
"title": "Paunawa",
"message": "Tinanggihan mo ang {{vcLabel}} ni {{sender}}"
"message": "Iwinaksi ang {{vcLabel}} ni {{sender}}"
},
"disconnected": {
"title": "Nadiskonekta",
Expand Down Expand Up @@ -240,7 +240,9 @@
"exchangingDeviceInfo": "Nagpapalitan ng impormasyon ng device...",
"exchangingDeviceInfoTimeout": "Medyo nagtatagal ang paglabas ng impormasyon ng device. Bukas ba ang ibang device para sa mga koneksyon?",
"invalid": "Di-wasto ang QR Code",
"offline": "Mangyaring kumonekta sa internet upang makapag-scan ng QR codes na gumagamit ng Online sharing mode"
"offline": "Mangyaring kumonekta sa internet upang makapag-scan ng QR codes na gumagamit ng Online sharing mode",
"sent": "Naibahagi na ang {{vcLabel}}...",
"sentHint": "Iniintay ang nakatanggap na itabi o iwaksi ang iyong {{vcLabel}}"
}
},
"SelectVcOverlay": {
Expand All @@ -266,7 +268,7 @@
},
"rejected": {
"title": "Pansinin",
"message": "Ang iyong {{vcLabel}} ay tinanggihan ng {{receiver}}"
"message": "Iwinaksi ni {{receiver}} ang iyong {{vcLabel}}"
}
},
"consentToPhotoVerification": "Nagbibigay ako ng pahintulot na kunin ang aking larawan para sa pagpapatunay"
Expand Down
11 changes: 11 additions & 0 deletions machines/biometrics.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createModel } from 'xstate/lib/model';
import * as LocalAuthentication from 'expo-local-authentication';
import { EventFrom, MetaObject, StateFrom } from 'xstate';
import { Platform } from 'react-native';

// ----- CREATE MODEL ---------------------------------------------------------
const model = createModel(
Expand Down Expand Up @@ -97,6 +98,9 @@ export const biometricsMachine = model.createMachine(
authenticating: {
invoke: {
src: () => async () => {
if (Platform.OS === 'android') {
await LocalAuthentication.cancelAuthenticate();
}
const res = await LocalAuthentication.authenticateAsync({
promptMessage: 'Biometric Authentication',

Expand All @@ -112,6 +116,9 @@ export const biometricsMachine = model.createMachine(
actions: ['setStatus'],
},
},
on: {
AUTHENTICATE: 'authenticating',
},
},

reauthenticating: {
Expand Down Expand Up @@ -145,6 +152,7 @@ export const biometricsMachine = model.createMachine(
SET_IS_AVAILABLE: {
target: '#biometrics.available',
},
AUTHENTICATE: 'authenticating',
},
},

Expand Down Expand Up @@ -186,6 +194,9 @@ export const biometricsMachine = model.createMachine(
},
},
},
on: {
AUTHENTICATE: 'authenticating',
},
},
},
},
Expand Down
15 changes: 11 additions & 4 deletions machines/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,12 @@ export const requestMachine =
},

reviewing: {
invoke: {
src: 'sendVcResponse',
data: {
status: 'RECEIVED',
},
},
exit: 'disconnect',
initial: 'idle',
states: {
Expand Down Expand Up @@ -399,7 +405,7 @@ export const requestMachine =
},
},
accepted: {
entry: ['sendVcReceived', 'logReceived'],
entry: ['updateReceivedVcs', 'logReceived'],
invoke: {
src: 'sendVcResponse',
data: {
Expand Down Expand Up @@ -634,7 +640,7 @@ export const requestMachine =
{ to: (context) => context.serviceRefs.activityLog }
),

sendVcReceived: send(
updateReceivedVcs: send(
(context) => {
return VcEvents.VC_RECEIVED(VC_ITEM_STORE_KEY(context.incomingVc));
},
Expand Down Expand Up @@ -796,7 +802,7 @@ export const requestMachine =
}
},

sendVcResponse: (context, _event, meta) => () => {
sendVcResponse: (context, _event, meta) => async () => {
const event: SendVcResponseEvent = {
type: 'send-vc:response',
data: meta.data.status,
Expand All @@ -807,7 +813,8 @@ export const requestMachine =
// pass
});
} else {
onlineSend(event);
await GoogleNearbyMessages.unpublish();
await onlineSend(event);
}
},

Expand Down
7 changes: 4 additions & 3 deletions machines/request.typegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export interface Typegen0 {
sendDisconnect: 'done.invoke.request.cancelling:invocation[0]';
sendVcResponse:
| 'done.invoke.request.reviewing.accepted:invocation[0]'
| 'done.invoke.request.reviewing.rejected:invocation[0]';
| 'done.invoke.request.reviewing.rejected:invocation[0]'
| 'done.invoke.request.reviewing:invocation[0]';
verifyVp: 'done.invoke.request.reviewing.verifyingVp:invocation[0]';
};
'missingImplementations': {
Expand Down Expand Up @@ -86,7 +87,6 @@ export interface Typegen0 {
| 'FACE_VALID'
| 'done.invoke.request.reviewing.verifyingVp:invocation[0]';
requestReceiverInfo: 'CONNECTED';
sendVcReceived: 'STORE_RESPONSE';
setIncomingVc: 'VC_RECEIVED';
setReceiveLogTypeDiscarded: 'CANCEL' | 'REJECT';
setReceiveLogTypeRegular: 'ACCEPT';
Expand All @@ -96,6 +96,7 @@ export interface Typegen0 {
setSenderInfo: 'EXCHANGE_DONE';
storeVc: 'STORE_RESPONSE';
switchProtocol: 'SWITCH_PROTOCOL';
updateReceivedVcs: 'STORE_RESPONSE';
};
'eventsCausingDelays': {
CANCEL_TIMEOUT: 'CANCEL';
Expand All @@ -122,7 +123,7 @@ export interface Typegen0 {
receiveVc: 'EXCHANGE_DONE';
requestBluetooth: 'BLUETOOTH_DISABLED';
sendDisconnect: 'CANCEL';
sendVcResponse: 'CANCEL' | 'REJECT' | 'STORE_RESPONSE';
sendVcResponse: 'CANCEL' | 'REJECT' | 'STORE_RESPONSE' | 'VC_RECEIVED';
verifyVp: never;
};
'matchesStates':
Expand Down
62 changes: 52 additions & 10 deletions machines/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const model = createModel(
VERIFY_AND_ACCEPT_REQUEST: () => ({}),
VC_ACCEPTED: () => ({}),
VC_REJECTED: () => ({}),
VC_SENT: () => ({}),
CANCEL: () => ({}),
DISMISS: () => ({}),
CONNECTED: () => ({}),
Expand Down Expand Up @@ -232,12 +233,15 @@ export const scanMachine =
initial: 'selectingVc',
states: {
selectingVc: {
invoke: {
src: 'monitorCancellation',
},
on: {
UPDATE_REASON: {
actions: 'setReason',
},
DISCONNECT: {
target: '#scan.findingConnection',
target: '#scan.disconnected',
},
SELECT_VC: {
actions: 'setSelectedVc',
Expand All @@ -256,6 +260,7 @@ export const scanMachine =
actions: 'toggleShouldVerifyPresence',
},
},
exit: ['onlineUnsubscribe'],
},
cancelling: {
invoke: {
Expand Down Expand Up @@ -291,16 +296,26 @@ export const scanMachine =
},
},
},
sent: {
description:
'VC data has been shared and the receiver should now be viewing it',
on: {
VC_ACCEPTED: {
target: '#scan.reviewing.accepted',
},
VC_REJECTED: {
target: '#scan.reviewing.rejected',
},
},
},
},
on: {
DISCONNECT: {
target: '#scan.findingConnection',
},
VC_ACCEPTED: {
target: 'accepted',
},
VC_REJECTED: {
target: 'rejected',
VC_SENT: {
target: '#scan.reviewing.sendingVc.sent',
internal: true,
},
},
},
Expand Down Expand Up @@ -610,6 +625,10 @@ export const scanMachine =
shouldVerifyPresence: false,
}),
}),

onlineUnsubscribe: () => {
GoogleNearbyMessages.unsubscribe();
},
},

services: {
Expand Down Expand Up @@ -649,6 +668,14 @@ export const scanMachine =
}
},

monitorCancellation: (context) => async (callback) => {
if (context.sharingProtocol === 'ONLINE') {
await onlineSubscribe('disconnect', null, () =>
callback({ type: 'DISCONNECT' })
);
}
},

checkLocationStatus: () => (callback) => {
checkLocation(
() => callback(model.events.LOCATION_ENABLED()),
Expand Down Expand Up @@ -737,10 +764,15 @@ export const scanMachine =
};

const statusCallback = (status: SendVcStatus) => {
console.log('[scan] statusCallback', status);
if (typeof status === 'number') return;
callback({
type: status === 'ACCEPTED' ? 'VC_ACCEPTED' : 'VC_REJECTED',
});
if (status === 'RECEIVED') {
callback({ type: 'VC_SENT' });
} else {
callback({
type: status === 'ACCEPTED' ? 'VC_ACCEPTED' : 'VC_REJECTED',
});
}
};

if (context.sharingProtocol === 'OFFLINE') {
Expand Down Expand Up @@ -896,6 +928,10 @@ export function selectIsRejected(state: State) {
return state.matches('reviewing.rejected');
}

export function selectIsSent(state: State) {
return state.matches('reviewing.sendingVc.sent');
}

export function selectIsInvalid(state: State) {
return state.matches('invalid');
}
Expand Down Expand Up @@ -928,6 +964,10 @@ export function selectIsOffline(state: State) {
return state.matches('offline');
}

export function selectIsDisconnected(state: State) {
return state.matches('disconnected');
}

async function sendVc(
vc: VC,
callback: (status: SendVcStatus) => void,
Expand Down Expand Up @@ -968,7 +1008,9 @@ async function sendVc(
},
});
} else if (typeof status === 'string') {
GoogleNearbyMessages.unsubscribe();
if (status === 'ACCEPTED' || status === 'REJECTED') {
GoogleNearbyMessages.unsubscribe();
}
callback(status);
}
},
Expand Down
Loading

0 comments on commit d4ec88c

Please sign in to comment.