Skip to content

Commit

Permalink
[INJIMOB-2571]: refactor storage class (#1746)
Browse files Browse the repository at this point in the history
* [INJIMOB-2571]: refactor backed up data restoration method

Signed-off-by: Alka Prasad <[email protected]>

* [INJIMOB-2571]: refactor the storage class to make it more readable and for seperation of concerns

Signed-off-by: Alka Prasad <[email protected]>

* [INJIMOB-2571]: rename a few methods and throw the caught error along with logging it

Signed-off-by: Alka Prasad <[email protected]>

* [INJIMOB-2571]: refactor verifyCredential method and its usages

Signed-off-by: Alka Prasad <[email protected]>

* [INJIMOB-2571]: move verifyCredentialData method under OpenIdVCI

Signed-off-by: Alka Prasad <[email protected]>

---------

Signed-off-by: Alka Prasad <[email protected]>
  • Loading branch information
Alka1703 authored Dec 20, 2024
1 parent 53da105 commit e057904
Show file tree
Hide file tree
Showing 16 changed files with 866 additions and 1,105 deletions.
51 changes: 17 additions & 34 deletions machines/Issuers/IssuersService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import Cloud from '../../shared/CloudBackupAndRestoreUtils';
import {CACHED_API} from '../../shared/api';
import NetInfo from '@react-native-community/netinfo';
import { NativeModules } from 'react-native';
import { authorize } from 'react-native-app-auth';
import Cloud from '../../shared/CloudBackupAndRestoreUtils';
import { CACHED_API } from '../../shared/api';
import {
fetchKeyPair,
generateKeyPair,
} from '../../shared/cryptoutil/cryptoUtil';
import {
constructAuthorizationConfiguration,
constructIssuerMetaData,
Expand All @@ -9,26 +15,14 @@ import {
OIDCErrors,
updateCredentialInformation,
vcDownloadTimeout,
verifyCredentialData
} from '../../shared/openId4VCI/Utils';
import {authorize} from 'react-native-app-auth';
import {
fetchKeyPair,
generateKeyPair,
} from '../../shared/cryptoutil/cryptoUtil';
import {NativeModules} from 'react-native';
import {
VerificationErrorMessage,
VerificationErrorType,
verifyCredential,
} from '../../shared/vcjs/verifyCredential';
import { TelemetryConstants } from '../../shared/telemetry/TelemetryConstants';
import {
getImpressionEventData,
sendImpressionEvent,
} from '../../shared/telemetry/TelemetryUtils';
import {TelemetryConstants} from '../../shared/telemetry/TelemetryConstants';
import {VciClient} from '../../shared/vciClient/VciClient';
import {isMockVC} from '../../shared/Utils';
import {VCFormat} from '../../shared/VCFormat';
import { VciClient } from '../../shared/vciClient/VciClient';

export const IssuersService = () => {
return {
Expand Down Expand Up @@ -152,26 +146,15 @@ export const IssuersService = () => {
},

verifyCredential: async (context: any) => {
//TODO: Remove bypassing verification of mock VCs once mock VCs are verifiable
if (
context.selectedCredentialType.format === VCFormat.mso_mdoc ||
!isMockVC(context.selectedIssuerId)
) {
const verificationResult = await verifyCredential(
context.verifiableCredential?.credential,
context.selectedCredentialType.format,
);
if (!verificationResult.isVerified) {
const verificationResult = await verifyCredentialData(
context.verifiableCredential?.credential,
context.selectedCredentialType.format,
context.selectedIssuerId
);
if(!verificationResult.isVerified) {
throw new Error(verificationResult.verificationErrorCode);
}
return verificationResult;
} else {
return {
isVerified: true,
verificationMessage: VerificationErrorMessage.NO_ERROR,
verificationErrorCode: VerificationErrorType.NO_ERROR,
};
}
},
};
};
629 changes: 146 additions & 483 deletions machines/VerifiableCredential/VCItemMachine/VCItemMachine.typegen.ts

Large diffs are not rendered by default.

48 changes: 15 additions & 33 deletions machines/VerifiableCredential/VCItemMachine/VCItemServices.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
import {NativeModules} from 'react-native';
import Cloud from '../../../shared/CloudBackupAndRestoreUtils';
import { NativeModules } from 'react-native';
import getAllConfigurations, {
API_URLS,
CACHED_API,
DownloadProps,
} from '../../../shared/api';
import Cloud from '../../../shared/CloudBackupAndRestoreUtils';
import { isIOS } from '../../../shared/constants';
import {
fetchKeyPair,
generateKeyPair,
} from '../../../shared/cryptoutil/cryptoUtil';
import {CredentialDownloadResponse, request} from '../../../shared/request';
import {WalletBindingResponse} from '../VCMetaMachine/vc';
import {
VerificationErrorMessage,
VerificationErrorType,
verifyCredential,
} from '../../../shared/vcjs/verifyCredential';
import {getVerifiableCredential} from './VCItemSelectors';
import {getMatchingCredentialIssuerMetadata} from '../../../shared/openId4VCI/Utils';
import {isIOS} from '../../../shared/constants';
import {VCMetadata} from '../../../shared/VCMetadata';
import {VCFormat} from '../../../shared/VCFormat';
import {isMockVC} from '../../../shared/Utils';
import { getMatchingCredentialIssuerMetadata, verifyCredentialData } from '../../../shared/openId4VCI/Utils';
import { CredentialDownloadResponse, request } from '../../../shared/request';
import { WalletBindingResponse } from '../VCMetaMachine/vc';
import { getVerifiableCredential } from './VCItemSelectors';

const {RNSecureKeystoreModule} = NativeModules;
export const VCItemServices = model => {
Expand Down Expand Up @@ -200,26 +192,16 @@ export const VCItemServices = model => {
},

verifyCredential: async (context: any) => {
if (context.verifiableCredential) {
//TODO: Remove bypassing verification of mock VCs once mock VCs are verifiable
if (
context.selectedCredentialType.format === VCFormat.mso_mdoc ||
!isMockVC(context.selectedIssuerId)
) {
const verificationResult = await verifyCredential(
getVerifiableCredential(context.verifiableCredential),
(context.vcMetadata as VCMetadata).format,
);
if (!verificationResult.isVerified) {
if(context.verifiableCredential){
const verificationResult = await verifyCredentialData(
getVerifiableCredential(context.verifiableCredential),
context.selectedCredentialType.format,
context.selectedIssuerId
);
if(!verificationResult.isVerified) {
throw new Error(verificationResult.verificationErrorCode);
}
} else {
return {
isVerified: true,
verificationMessage: VerificationErrorMessage.NO_ERROR,
verificationErrorCode: VerificationErrorType.NO_ERROR,
};
}
return verificationResult;
}
},
};
Expand Down
4 changes: 2 additions & 2 deletions machines/backupAndRestore/restore/restoreService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import NetInfo from '@react-native-community/netinfo';
import Cloud from '../../../shared/CloudBackupAndRestoreUtils';
import Storage from '../../../shared/storage';
import { isMinimumStorageLimitReached } from '../../../shared/storage';
import fileStorage, {
getBackupFilePath,
unZipAndRemoveFile,
Expand All @@ -11,7 +11,7 @@ export const restoreService = model => {
checkInternet: async () => await NetInfo.fetch(),

checkStorageAvailability: () => async () => {
return await Storage.isMinimumLimitReached('minStorageRequired');
return await isMinimumStorageLimitReached('minStorageRequired');
},

downloadLatestBackup: () => async () => {
Expand Down
6 changes: 2 additions & 4 deletions machines/bleShare/request/requestMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {VcMetaEvents} from '../../VerifiableCredential/VCMetaMachine/VCMetaMachi
import {subscribe} from '../../../shared/openIdBLE/verifierEventHandler';
import {VerifierDataEvent} from '../../../shared/tuvali/types/events';
import {BLEError} from '../types';
import Storage from '../../../shared/storage';
import Storage, { isMinimumStorageLimitReached } from '../../../shared/storage';
import {VCMetadata} from '../../../shared/VCMetadata';
import {
getEndEventData,
Expand Down Expand Up @@ -885,9 +885,7 @@ export const requestMachine =
},

checkStorageAvailability: () => async () => {
return Promise.resolve(
Storage.isMinimumLimitReached('minStorageRequired'),
);
return Promise.resolve(isMinimumStorageLimitReached('minStorageRequired'));
},
},

Expand Down
6 changes: 2 additions & 4 deletions machines/bleShare/scan/scanServices.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {isLocationEnabled} from 'react-native-device-info';
import Storage from '../../../shared/storage';
import Storage, { isMinimumStorageLimitReached } from '../../../shared/storage';
import BluetoothStateManager from 'react-native-bluetooth-state-manager';
import {
check,
Expand Down Expand Up @@ -188,9 +188,7 @@ export const ScanServices = (model: any) => {
},

checkStorageAvailability: () => async () => {
return Promise.resolve(
Storage.isMinimumLimitReached('minStorageRequiredForAuditEntry'),
);
return Promise.resolve(isMinimumStorageLimitReached('minStorageRequiredForAuditEntry'));
},
};
};
41 changes: 19 additions & 22 deletions machines/store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Storage, {MMKV} from '../shared/storage';
import binaryToBase64 from 'react-native/Libraries/Utilities/binaryToBase64';
import {
EventFrom,
Receiver,
Expand Down Expand Up @@ -36,6 +35,7 @@ import {
} from '../shared/telemetry/TelemetryUtils';
import {Buffer} from 'buffer';
import {VC} from './VerifiableCredential/VCMetaMachine/vc';
import { isVCStorageInitialised } from '../shared/fileStorage';

export const keyinvalidatedString =
'Key Invalidated due to biometric enrollment';
Expand Down Expand Up @@ -276,16 +276,17 @@ export const storeMachine =
BIOMETRIC_CANCELLED: {
actions: [
send(
(_, event) => model.events.BIOMETRIC_CANCELLED(event.requester),
(_, event) =>
model.events.BIOMETRIC_CANCELLED(event.requester),
{
to: (_, event) => event.requester,
},
),
sendUpdate(),
sendParent('BIOMETRIC_CANCELLED')
sendParent('BIOMETRIC_CANCELLED'),
],
target: 'checkFreshInstall',
}
},
},
},
},
Expand All @@ -303,10 +304,10 @@ export const storeMachine =
},
BIOMETRIC_CANCELLED: {
actions: [sendParent('BIOMETRIC_CANCELLED')],
target: 'checkFreshInstall'
target: 'checkFreshInstall',
},
},
},
},
{
actions: {
notifyParent: sendParent(model.events.READY()),
Expand Down Expand Up @@ -334,11 +335,10 @@ export const storeMachine =
return;
},
checkFreshInstall: () => async callback => {
try{
return await getItem('auth', null, '');
}
catch(e){
if(e instanceof BiometricCancellationError){
try {
return await getItem('auth', null, '');
} catch (e) {
if (e instanceof BiometricCancellationError) {
callback(model.events.BIOMETRIC_CANCELLED());
} else {
callback(model.events.STORE_ERROR(e));
Expand All @@ -363,7 +363,7 @@ export const storeMachine =
base64EncodedString,
);
} catch (e) {
if(e instanceof BiometricCancellationError){
if (e instanceof BiometricCancellationError) {
callback(model.events.BIOMETRIC_CANCELLED(event.requester));
}
sendErrorEvent(getErrorEventData('ENCRYPTION', '', e));
Expand Down Expand Up @@ -394,7 +394,7 @@ export const storeMachine =
},

checkStorageInitialisedOrNot: () => async callback => {
const isDirectoryExist = await Storage.isVCStorageInitialised();
const isDirectoryExist = await isVCStorageInitialised();
if (!isDirectoryExist) {
callback(model.events.READY());
} else {
Expand Down Expand Up @@ -422,7 +422,7 @@ export const storeMachine =
break;
}
case 'EXPORT': {
response = await exportData(context.encryptionKey);
response = await backupAndExportData(context.encryptionKey);
break;
}
case 'GET_VCS_DATA': {
Expand All @@ -431,10 +431,7 @@ export const storeMachine =
}
case 'RESTORE_BACKUP': {
// the backup data is in plain text
response = await loadBackupData(
event.data,
context.encryptionKey,
);
await restoreBackedUpData(event.data, context.encryptionKey);
break;
}
case 'SET': {
Expand Down Expand Up @@ -622,12 +619,12 @@ export async function setItem(
}
}

export async function exportData(encryptionKey: string) {
return Storage.exportData(encryptionKey);
export async function backupAndExportData(encryptionKey: string) {
return Storage.backupData(encryptionKey);
}

export async function loadBackupData(data, encryptionKey) {
await Storage.loadBackupData(data, encryptionKey);
export async function restoreBackedUpData(data, encryptionKey) {
await Storage.restoreBackUpData(data, encryptionKey);
}

export async function fetchAllWellknownConfig(encryptionKey: string) {
Expand Down
Loading

0 comments on commit e057904

Please sign in to comment.