Skip to content

Commit

Permalink
feat: Support all types presentaton wth VCFormat auto #2779
Browse files Browse the repository at this point in the history
  • Loading branch information
bibash28 committed Jul 15, 2024
1 parent 9160f58 commit c39d2e7
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 108 deletions.
194 changes: 113 additions & 81 deletions lib/app/shared/helper_functions/helper_functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1793,112 +1793,144 @@ List<String> getStringCredentialsForToken({
required VCFormatType vcFormatType,
required PresentationDefinition presentationDefinition,
required Map<String, dynamic>? clientMetaData,
required List<CredentialModel> credentialsToBePresented,
}) {
bool presentLdpVc = false;
bool presentJwtVc = false;
bool presentJwtVcJson = false;
bool presentVcSdJwt = false;

final supportingFormats = <String>[];

if (presentationDefinition.format != null) {
final format = presentationDefinition.format;

/// ldp_vc
presentLdpVc = format?.ldpVc != null || format?.ldpVp != null;
if (vcFormatType == VCFormatType.auto) {
final credential = credentialsToBePresented.firstOrNull;

/// jwt_vc
presentJwtVc = format?.jwtVc != null || format?.jwtVp != null;
if (credential == null) {
throw ResponseMessage(
data: {
'error': 'invalid_request',
'error_description': 'VC format is missing',
},
);
}

/// jwt_vc_json
presentJwtVcJson = format?.jwtVcJson != null || format?.jwtVpJson != null;
final credentialFormat = credential.getFormat;

/// vc+sd-jwt
presentVcSdJwt = format?.vcSdJwt != null;
if (credentialFormat == VCFormatType.ldpVc.vcValue) {
presentLdpVc = true;
presentJwtVc = false;
presentJwtVcJson = false;
presentVcSdJwt = false;
} else if (credentialFormat == VCFormatType.jwtVc.vcValue) {
presentLdpVc = false;
presentJwtVc = true;
presentJwtVcJson = false;
presentVcSdJwt = false;
} else if (credentialFormat == VCFormatType.jwtVcJson.vcValue) {
presentLdpVc = false;
presentJwtVc = false;
presentJwtVcJson = true;
presentVcSdJwt = false;
} else if (credentialFormat == VCFormatType.vcSdJWT.vcValue) {
presentLdpVc = false;
presentJwtVc = false;
presentJwtVcJson = false;
presentVcSdJwt = true;
}
} else {
if (clientMetaData == null) {
/// credential manifest case
if (vcFormatType == VCFormatType.ldpVc) {
presentLdpVc = true;
} else if (vcFormatType == VCFormatType.jwtVc) {
presentJwtVc = true;
} else if (vcFormatType == VCFormatType.jwtVcJson) {
presentJwtVcJson = true;
} else if (vcFormatType == VCFormatType.vcSdJWT) {
presentVcSdJwt = true;
}
} else {
final vpFormats = clientMetaData['vp_formats'] as Map<String, dynamic>;
final supportingFormats = <String>[];

if (presentationDefinition.format != null) {
final format = presentationDefinition.format;

/// ldp_vc
presentLdpVc = vpFormats.containsKey('ldp_vc');
presentLdpVc = format?.ldpVc != null || format?.ldpVp != null;

/// jwt_vc
presentJwtVc = vpFormats.containsKey('jwt_vc');
presentJwtVc = format?.jwtVc != null || format?.jwtVp != null;

/// jwt_vc_json
presentJwtVcJson = vpFormats.containsKey('jwt_vc_json');
presentJwtVcJson = format?.jwtVcJson != null || format?.jwtVpJson != null;

/// vc+sd-jwt
presentVcSdJwt = vpFormats.containsKey('vc+sd-jwt');
presentVcSdJwt = format?.vcSdJwt != null;
} else {
if (clientMetaData == null) {
/// credential manifest case
if (vcFormatType == VCFormatType.ldpVc) {
presentLdpVc = true;
} else if (vcFormatType == VCFormatType.jwtVc) {
presentJwtVc = true;
} else if (vcFormatType == VCFormatType.jwtVcJson) {
presentJwtVcJson = true;
} else if (vcFormatType == VCFormatType.vcSdJWT) {
presentVcSdJwt = true;
}
} else {
final vpFormats = clientMetaData['vp_formats'] as Map<String, dynamic>;

/// ldp_vc
presentLdpVc = vpFormats.containsKey('ldp_vc');

/// jwt_vc
presentJwtVc = vpFormats.containsKey('jwt_vc');

/// jwt_vc_json
presentJwtVcJson = vpFormats.containsKey('jwt_vc_json');

/// vc+sd-jwt
presentVcSdJwt = vpFormats.containsKey('vc+sd-jwt');
}
if (!presentLdpVc && vcFormatType == VCFormatType.ldpVc) {
presentLdpVc = true;
} else if (!presentJwtVc && vcFormatType == VCFormatType.jwtVc) {
presentJwtVc = true;
} else if (!presentJwtVcJson && vcFormatType == VCFormatType.jwtVcJson) {
presentJwtVcJson = true;
} else if (!presentVcSdJwt && vcFormatType == VCFormatType.vcSdJWT) {
presentVcSdJwt = true;
}
}

if (!presentLdpVc &&
!presentJwtVc &&
!presentJwtVcJson &&
!presentVcSdJwt) {
throw ResponseMessage(
data: {
'error': 'invalid_request',
'error_description': 'VC format is missing',
},
);
}
if (!presentLdpVc && vcFormatType == VCFormatType.ldpVc) {

/// create list of supported formats
if (presentLdpVc) supportingFormats.add(VCFormatType.ldpVc.vcValue);
if (presentJwtVc) supportingFormats.add(VCFormatType.jwtVc.vcValue);
if (presentJwtVcJson) supportingFormats.add(VCFormatType.jwtVcJson.vcValue);
if (presentVcSdJwt) supportingFormats.add(VCFormatType.vcSdJWT.vcValue);

/// make sure only one of all are true
if (presentLdpVc && vcFormatType == VCFormatType.ldpVc) {
presentLdpVc = true;
} else if (!presentJwtVc && vcFormatType == VCFormatType.jwtVc) {
presentJwtVc = false;
presentJwtVcJson = false;
presentVcSdJwt = false;
} else if (presentJwtVc && vcFormatType == VCFormatType.jwtVc) {
presentLdpVc = false;
presentJwtVc = true;
} else if (!presentJwtVcJson && vcFormatType == VCFormatType.jwtVcJson) {
presentJwtVcJson = false;
presentVcSdJwt = false;
} else if (presentJwtVcJson && vcFormatType == VCFormatType.jwtVcJson) {
presentLdpVc = false;
presentJwtVc = false;
presentJwtVcJson = true;
} else if (!presentVcSdJwt && vcFormatType == VCFormatType.vcSdJWT) {
presentVcSdJwt = false;
} else if (presentVcSdJwt && vcFormatType == VCFormatType.vcSdJWT) {
presentLdpVc = false;
presentJwtVc = false;
presentJwtVcJson = false;
presentVcSdJwt = true;
}
}

if (vcFormatType == VCFormatType.auto) {
presentLdpVc = true;
presentJwtVc = true;
presentJwtVcJson = true;
presentVcSdJwt = true;
}

if (!presentLdpVc && !presentJwtVc && !presentJwtVcJson && !presentVcSdJwt) {
throw ResponseMessage(
data: {
'error': 'invalid_request',
'error_description': 'VC format is missing',
},
);
}

/// create list of supported formats
if (presentLdpVc) supportingFormats.add(VCFormatType.ldpVc.vcValue);
if (presentJwtVc) supportingFormats.add(VCFormatType.jwtVc.vcValue);
if (presentJwtVcJson) supportingFormats.add(VCFormatType.jwtVcJson.vcValue);
if (presentVcSdJwt) supportingFormats.add(VCFormatType.jwtVcJson.vcValue);

/// make sure only one of all are true
if (presentLdpVc && vcFormatType == VCFormatType.ldpVc) {
presentLdpVc = true;
presentJwtVc = false;
presentJwtVcJson = false;
presentVcSdJwt = false;
} else if (presentJwtVc && vcFormatType == VCFormatType.jwtVc) {
presentLdpVc = false;
presentJwtVc = true;
presentJwtVcJson = false;
presentVcSdJwt = false;
} else if (presentJwtVcJson && vcFormatType == VCFormatType.jwtVcJson) {
presentLdpVc = false;
presentJwtVc = false;
presentJwtVcJson = true;
presentVcSdJwt = false;
} else if (presentVcSdJwt && vcFormatType == VCFormatType.vcSdJWT) {
presentLdpVc = false;
presentJwtVc = false;
presentJwtVcJson = false;
presentVcSdJwt = true;
}

if (vcFormatType != VCFormatType.auto) {
if ((presentLdpVc && vcFormatType != VCFormatType.ldpVc) ||
(presentJwtVc && vcFormatType != VCFormatType.jwtVc) ||
presentJwtVcJson && vcFormatType != VCFormatType.jwtVcJson ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ List<CredentialModel> filterCredenialListByFormat({
required PresentationDefinition presentationDefinition,
required List<Field> filterList,
}) {
if (vcFormatType == VCFormatType.auto) {
return credentialList;
}

final credentials = List<CredentialModel>.from(credentialList);
if (filterList.isNotEmpty) {
final isJwtVpInJwtVCRequired = presentationDefinition.format?.jwtVp != null;
Expand All @@ -25,35 +29,34 @@ List<CredentialModel> filterCredenialListByFormat({
clientMetaData: clientMetaData,
presentationDefinition: presentationDefinition,
vcFormatType: vcFormatType,
credentialsToBePresented: credentials,
);

if (vcFormatType != VCFormatType.auto) {
credentials.removeWhere(
(CredentialModel credentialModel) {
/// remove ldpVc
if (presentLdpVc) {
return credentialModel.getFormat != VCFormatType.ldpVc.vcValue;
}

/// remove jwtVc
if (presentJwtVc) {
return credentialModel.getFormat != VCFormatType.jwtVc.vcValue;
}

/// remove JwtVcJson
if (presentJwtVcJson) {
return credentialModel.getFormat != VCFormatType.jwtVcJson.vcValue;
}

/// remove vcSdJwt
if (presentVcSdJwt) {
return credentialModel.getFormat != VCFormatType.vcSdJWT.vcValue;
}

return false;
},
);
}
credentials.removeWhere(
(CredentialModel credentialModel) {
/// remove ldpVc
if (presentLdpVc) {
return credentialModel.getFormat != VCFormatType.ldpVc.vcValue;
}

/// remove jwtVc
if (presentJwtVc) {
return credentialModel.getFormat != VCFormatType.jwtVc.vcValue;
}

/// remove JwtVcJson
if (presentJwtVcJson) {
return credentialModel.getFormat != VCFormatType.jwtVcJson.vcValue;
}

/// remove vcSdJwt
if (presentVcSdJwt) {
return credentialModel.getFormat != VCFormatType.vcSdJWT.vcValue;
}

return false;
},
);
}
return credentials;
}
1 change: 1 addition & 0 deletions lib/scan/cubit/scan_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,7 @@ class ScanCubit extends Cubit<ScanState> {
clientMetaData: clientMetaData,
presentationDefinition: presentationDefinition,
vcFormatType: vcFormatType,
credentialsToBePresented: credentialsToBePresented,
);

if (presentLdpVc) {
Expand Down
7 changes: 7 additions & 0 deletions test/app/shared/helper_functions/helper_functions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,7 @@ void main() {
),
),
clientMetaData: null,
credentialsToBePresented: [],
),
(true, false, false, false),
);
Expand All @@ -1102,6 +1103,7 @@ void main() {
vcFormatType: VCFormatType.ldpVc,
presentationDefinition: presentationDefinition,
clientMetaData: null,
credentialsToBePresented: [],
),
throwsA(
isA<ResponseMessage>().having((e) => e.data, '', {
Expand All @@ -1124,6 +1126,7 @@ void main() {
format: null,
),
clientMetaData: null,
credentialsToBePresented: [],
),
(false, true, false, false),
);
Expand All @@ -1141,6 +1144,7 @@ void main() {
format: null,
),
clientMetaData: null,
credentialsToBePresented: [],
),
(false, false, true, false),
);
Expand All @@ -1158,6 +1162,7 @@ void main() {
format: null,
),
clientMetaData: null,
credentialsToBePresented: [],
),
(false, false, false, true),
);
Expand All @@ -1179,6 +1184,7 @@ void main() {
'jwt_vc_json': 'here',
},
},
credentialsToBePresented: [],
),
(false, false, true, false),
);
Expand All @@ -1200,6 +1206,7 @@ void main() {
'vc+sd-jwt': 'here',
},
},
credentialsToBePresented: [],
),
(false, false, false, true),
);
Expand Down

0 comments on commit c39d2e7

Please sign in to comment.