Files
inji-wallet/machines/Issuers/IssuersMachine.ts
KiruthikaJeyashankar 003cc156c2 [INJIMOB-1192] onboarding of new issuer is affecting the existing issuers (#1476)
* [INJIMOB-1192] : use wellknown response instead of mimoto issuer config.
-- Remove hardcoding for sunbird issuer in vc activation and verification flow.
-- Render idType from wellknown response
-- Remove UIN/VID from default add-on fields

Signed-off-by: Swati Goel <meet2swati@gmail.com>

* [INJIMOB-1192] : fix propType and some refactoring

Signed-off-by: Swati Goel <meet2swati@gmail.com>

* [INJIMOB-1192] : add credentialType in VcMetadata

Signed-off-by: Swati Goel <meet2swati@gmail.com>

* [INJIMOB-1192] fix vc download via issuer flow due to credentialType mismatch

Co-authored-by: PuBHARGAVI <46226958+PuBHARGAVI@users.noreply.github.com>
Signed-off-by: KiruthikaJeyashankar <81218987+KiruthikaJeyashankar@users.noreply.github.com>

* [INJIMOB-1192] rename supported list of credential type in issuers model

Co-authored-by: PuBHARGAVI <46226958+PuBHARGAVI@users.noreply.github.com>
Signed-off-by: KiruthikaJeyashankar <81218987+KiruthikaJeyashankar@users.noreply.github.com>

* [INJIMOB-1192] display id type in history based on wellknown for issuers VC

Signed-off-by: KiruthikaJeyashankar <81218987+KiruthikaJeyashankar@users.noreply.github.com>

* [INJIMOB-1192] fix id type not shown for VC activation

Signed-off-by: KiruthikaJeyashankar <81218987+KiruthikaJeyashankar@users.noreply.github.com>

* [INJIMOB-1192] remove unused credentialType field from VCMetaData

Signed-off-by: KiruthikaJeyashankar <81218987+KiruthikaJeyashankar@users.noreply.github.com>

* [INJIMOB-1192] set default idType for logging activity

Signed-off-by: KiruthikaJeyashankar <81218987+KiruthikaJeyashankar@users.noreply.github.com>

* [INJIMOB-1192] move vc item machine events into model

Events should not be exported to other packages for direct use so that Xstate's createModel() can decorate the function appropriately

Signed-off-by: KiruthikaJeyashankar <81218987+KiruthikaJeyashankar@users.noreply.github.com>

* [INJIMOB-1192] show verify banner id type from wellknown

Signed-off-by: KiruthikaJeyashankar <81218987+KiruthikaJeyashankar@users.noreply.github.com>

* [INJIMOB-1192] refactor duplication and unused code

Co-authored-by: PuBHARGAVI <46226958+PuBHARGAVI@users.noreply.github.com>
Signed-off-by: KiruthikaJeyashankar <81218987+KiruthikaJeyashankar@users.noreply.github.com>

* [INJIMOB-1192] remove unused displayId in metadata

Signed-off-by: KiruthikaJeyashankar <81218987+KiruthikaJeyashankar@users.noreply.github.com>

* [INJIMOB-1192] revert the dimensions of camera scanner to old values to support face liveness verification

Co-authored-by: KiruthikaJeyashankar <81218987+KiruthikaJeyashankar@users.noreply.github.com>
Signed-off-by: PuBHARGAVI <46226958+PuBHARGAVI@users.noreply.github.com>

* [INJIMOB-1192] remove unused code & debug logs

Signed-off-by: KiruthikaJeyashankar <81218987+KiruthikaJeyashankar@users.noreply.github.com>

* [INJIMOB-1192] fix failing test cases

Co-authored-by: KiruthikaJeyashankar <81218987+KiruthikaJeyashankar@users.noreply.github.com>
Signed-off-by: PuBHARGAVI <46226958+PuBHARGAVI@users.noreply.github.com>

* [INJIMOB-1192] remove unused translations

Signed-off-by: KiruthikaJeyashankar <81218987+KiruthikaJeyashankar@users.noreply.github.com>

---------

Signed-off-by: Swati Goel <meet2swati@gmail.com>
Signed-off-by: KiruthikaJeyashankar <81218987+KiruthikaJeyashankar@users.noreply.github.com>
Signed-off-by: PuBHARGAVI <46226958+PuBHARGAVI@users.noreply.github.com>
Co-authored-by: Swati Goel <meet2swati@gmail.com>
Co-authored-by: PuBHARGAVI <46226958+PuBHARGAVI@users.noreply.github.com>
2024-06-04 16:37:54 +05:30

436 lines
15 KiB
TypeScript

import {EventFrom, send, sendParent} from 'xstate';
import {IssuersModel} from './IssuersModel';
import {IssuersActions} from './IssuersActions';
import {IssuersService} from './IssuersService';
import {IssuersGuards} from './IssuersGuards';
import {CredentialTypes} from '../VerifiableCredential/VCMetaMachine/vc';
const model = IssuersModel;
export const IssuerScreenTabEvents = model.events;
export const Issuer_Tab_Ref_Id = 'issuersMachine';
export const IssuersMachine = model.createMach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l9JNAM5wY0W6pnjJEXl-KwBzIWUsv5a9RTSs2K3OVOJFV6NtKkG4eVUytx+jymkZAeBFPgHCAlHwqWrIQAAWh-uaX1ZIw6WnuJVKIK0EhjNqGMaS7qP7-PWJaXMP0okmhTPmGOFJ4wVmyVnRkzJvWJsQC3IFOQNqJkKPmG4uZMiXF1JSTYhZ0i7DGReBsTYbwqCLUauMu4iRUh+pmIUG0km2gGQURMqNNibTzTg6UNS6LST0lAHt4TEDGJcoWWhI6xreACGSXNyKcmNQSs1S6+0tBrtjGGgZU6XqsqSMEc0ZJtwFRNAUHeGYz7Hvzae3a-V2oQ1aAka9VCwVROhS3PKfhlwFA+mNZMjxcgORskkI9IkUU8xzonPOpthb-LCW0gSoofB+C8GNFaLd4zLhjmMq+qCx5gYsoehcMcbi7g2imVW5H0yqrGjcPwlo6pzueZQfhMjGNYGY1-PwhISQFGeoJlILCwJeHWhmBVFahK-vnchCZBTSXYvjco4tCAch9LU7Va48ZHSUjORc4lUAsUlJk+sQZyYqrrgNFrZ9VrKR3uMR5TWZGf5tq0AzZsbnSO+0NNkNwbhHKQsEv7bcar8pgW8FsUI2r6zRZRhmNIMdVy5Dyuqyz84AhPXsqqlI0aSL5ZgjawdXSR26NOCmX2WxHS6k-Xxs+JQgA */
predictableActionArguments: true,
preserveActionOrder: true,
id: Issuer_Tab_Ref_Id,
context: model.initialContext,
initial: 'displayIssuers',
tsTypes: {} as import('./IssuersMachine.typegen').Typegen0,
schema: {
context: model.initialContext,
events: {} as EventFrom<typeof model>,
},
states: {
displayIssuers: {
description: 'displays the issuers downloaded from the server',
invoke: {
src: 'downloadIssuersList',
onDone: {
actions: [
'sendImpressionEvent',
'setIssuers',
'resetLoadingReason',
],
target: 'selectingIssuer',
},
onError: {
//loadingReason is not reset here so that we go to previous(Home) screen on back button press of error screen
actions: ['setError'],
target: 'error',
},
},
},
error: {
description: 'reaches here when any error happens',
on: {
TRY_AGAIN: [
{
description: 'not fetched issuers yet',
cond: 'shouldFetchIssuersAgain',
actions: ['setLoadingReasonAsDisplayIssuers', 'resetError'],
target: 'displayIssuers',
},
{
description:
'error is OIDC_CONFIG_ERROR_PREFIX or REQUEST_TIMEDOUT',
cond: 'canSelectIssuerAgain',
actions: 'resetError',
target: 'selectingIssuer',
},
{
description:
'issuers config is available and downloading credentials is retriable',
actions: ['setLoadingReasonAsSettingUp', 'resetError'],
target: 'downloadIssuerConfig',
},
],
RESET_ERROR: {
actions: 'resetError',
target: 'selectingIssuer',
},
},
},
selectingIssuer: {
description: 'waits for the user to select any issuer',
on: {
DOWNLOAD_ID: {
actions: sendParent('DOWNLOAD_ID'),
},
SELECTED_ISSUER: {
actions: ['setSelectedIssuerId', 'setLoadingReasonAsSettingUp'],
target: 'downloadIssuerConfig',
},
},
},
downloadIssuerConfig: {
description: 'downloads the configuration of the selected issuer',
invoke: {
src: 'downloadIssuerConfig',
onDone: {
actions: 'setSelectedIssuers',
target: 'downloadCredentialTypes',
},
onError: {
actions: ['setError', 'resetLoadingReason'],
target: 'error',
},
},
},
downloadCredentialTypes: {
description:
'downloads the credentials supported from the selected issuer',
invoke: {
src: 'downloadCredentialTypes',
onDone: [
{
actions: 'setSupportedCredentialTypes',
target: 'selectingCredentialType',
},
{
target: 'checkInternet',
},
],
onError: {
actions: ['setError', 'resetLoadingReason'],
target: 'error',
},
},
},
selectingCredentialType: {
on: {
CANCEL: {
target: 'displayIssuers',
},
SELECTED_CREDENTIAL_TYPE: {
actions: 'setSelectedCredentialType',
target: 'checkInternet',
},
},
},
checkInternet: {
description: 'checks internet before opening the web view',
invoke: {
src: 'checkInternet',
id: 'checkInternet',
onDone: [
{
cond: 'isInternetConnected',
target: 'performAuthorization',
},
{
actions: ['setNoInternet', 'resetLoadingReason'],
target: 'error',
},
],
onError: {
actions: () =>
console.error('Error Occurred while checking Internet'),
target: 'error',
},
},
},
performAuthorization: {
description:
'invokes the issuers authorization endpoint and gets the access token',
invoke: {
src: 'invokeAuthorization',
onDone: {
actions: [
'setTokenResponse',
'setLoadingReasonAsSettingUp',
'getKeyPairFromStore',
],
},
onError: [
{
cond: 'isOIDCflowCancelled',
actions: [
'resetSelectedCredentialType',
'resetError',
'resetLoadingReason',
],
target: 'selectingIssuer',
},
{
cond: 'isOIDCConfigError',
actions: ['setOIDCConfigError'],
target: 'error',
},
{
actions: [
'setError',
'resetLoadingReason',
(_, event) =>
console.error(
'Error Occurred while invoking Auth - ',
event.data,
),
],
target: 'error',
},
],
},
initial: 'idle',
states: {
idle: {
on: {
STORE_RESPONSE: {
actions: 'loadKeyPair',
target: '#issuersMachine.checkKeyPair',
},
BIOMETRIC_CANCELLED: {
target: 'userCancelledBiometric',
},
STORE_ERROR: {
target: '#issuersMachine.checkKeyPair',
},
},
},
userCancelledBiometric: {
on: {
TRY_AGAIN: [
{
actions: ['getKeyPairFromStore'],
target: 'idle',
},
],
RESET_ERROR: {
actions: 'resetLoadingReason',
target: '#issuersMachine.selectingIssuer',
},
},
},
},
},
checkKeyPair: {
description: 'checks whether key pair is generated',
entry: [
'setLoadingReasonAsDownloadingCredentials',
send('CHECK_KEY_PAIR'),
],
on: {
CHECK_KEY_PAIR: [
{
cond: 'hasKeyPair',
target: 'downloadCredentials',
},
{
target: 'generateKeyPair',
},
],
},
},
generateKeyPair: {
description:
'if keypair is not generated, new one is created and stored',
invoke: {
src: 'generateKeyPair',
onDone: [
{
actions: [
'setPublicKey',
'setLoadingReasonAsDownloadingCredentials',
'storeKeyPair',
],
cond: 'isCustomSecureKeystore',
target: 'downloadCredentials',
},
{
actions: [
'setPublicKey',
'setLoadingReasonAsDownloadingCredentials',
'setPrivateKey',
'storeKeyPair',
],
target: 'downloadCredentials',
},
],
},
},
downloadCredentials: {
description: 'credential is downloaded from the selected issuer',
invoke: {
src: 'downloadCredential',
onDone: {
actions: ['setVerifiableCredential', 'setCredentialWrapper'],
target: 'verifyingCredential',
},
onError: [
{
cond: 'hasUserCancelledBiometric',
target: '.userCancelledBiometric',
},
{
cond: 'isGenericError',
target: 'selectingIssuer',
actions: [
'resetSelectedCredentialType',
'setError',
'resetLoadingReason',
'sendDownloadingFailedToVcMeta',
],
},
{
actions: ['setError', 'resetLoadingReason'],
target: 'error',
},
],
},
on: {
CANCEL: {
target: 'selectingIssuer',
actions: 'resetSelectedCredentialType',
},
},
initial: 'idle',
states: {
idle: {},
userCancelledBiometric: {
on: {
TRY_AGAIN: [
{
actions: ['setLoadingReasonAsDownloadingCredentials'],
target: '#issuersMachine.downloadCredentials',
},
],
RESET_ERROR: {
actions: 'resetLoadingReason',
target: '#issuersMachine.selectingIssuer',
},
},
},
},
},
verifyingCredential: {
description:
'once the credential is downloaded, it is verified before saving',
invoke: {
src: 'verifyCredential',
onDone: [
{
actions: ['sendSuccessEndEvent', 'setIsVerified'],
target: 'storing',
},
],
onError: [
{
cond: 'isVerificationPendingBecauseOfNetworkIssue',
actions: ['resetLoadingReason', 'resetIsVerified'],
target: 'storing',
},
{
actions: [
'resetLoadingReason',
'sendErrorEndEvent',
'updateVerificationErrorMessage',
],
target: 'handleVCVerificationFailure',
},
],
},
},
handleVCVerificationFailure: {
on: {
RESET_VERIFY_ERROR: {
actions: ['resetVerificationErrorMessage'],
},
},
},
storing: {
description: 'all the verified credential is stored.',
entry: [
'setVCMetadata',
'setMetadataInCredentialData',
'storeVerifiableCredentialMeta',
'storeVerifiableCredentialData',
'storeVcsContext',
'storeVcMetaContext',
'logDownloaded',
],
invoke: {
src: 'isUserSignedAlready',
onDone: {
cond: 'isSignedIn',
actions: ['sendBackupEvent'],
},
},
},
idle: {
on: {
COMPLETED: {
target: 'done',
},
CANCEL: {
target: 'selectingIssuer',
},
},
},
done: {
type: 'final',
},
},
},
{
actions: IssuersActions(model),
services: IssuersService(),
guards: IssuersGuards(),
},
);
export interface logoType {
url: string;
alt_text: string;
}
export interface displayType {
name: string;
locale: string;
language: string;
logo: logoType;
background_color: string;
text_color: string;
title: string;
description: string;
}
export interface issuerType {
credential_issuer: string;
protocol: string;
client_id: string;
'.well-known': string;
redirect_uri: string;
additional_headers: object;
authorization_endpoint: string;
token_endpoint: string;
proxy_token_endpoint: string;
credential_endpoint: string;
credential_audience: string;
display: [displayType];
credentialTypes: [CredentialTypes];
}