Skip to content

Commit

Permalink
Merge pull request #2996 from TalaoDAO/tmp
Browse files Browse the repository at this point in the history
Tmp
  • Loading branch information
hawkbee1 authored Oct 25, 2024
2 parents 5ae88ce + fac5bf2 commit 48242f0
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 319 deletions.
172 changes: 44 additions & 128 deletions lib/app/shared/helper_functions/helper_functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1802,7 +1802,7 @@ List<String> getStringCredentialsForToken({
}

//(presentLdpVc, presentJwtVc, presentJwtVcJson, presentVcSdJwt)
(bool, bool, bool, bool) getPresentVCDetails({
List<VCFormatType> getPresentVCDetails({
required VCFormatType vcFormatType,
required PresentationDefinition presentationDefinition,
required Map<String, dynamic>? clientMetaData,
Expand All @@ -1811,154 +1811,70 @@ List<String> getStringCredentialsForToken({
bool presentLdpVc = false;
bool presentJwtVc = false;
bool presentJwtVcJson = false;
bool presentJwtVcJsonLd = false;
bool presentVcSdJwt = false;

if (vcFormatType == VCFormatType.auto) {
final credential = credentialsToBePresented.firstOrNull;
final supportingFormats = <VCFormatType>[];

if (credential == null) {
throw ResponseMessage(
data: {
'error': 'invalid_request',
'error_description': 'VC format is missing',
},
);
}
if (presentationDefinition.format != null) {
final format = presentationDefinition.format;

/// ldp_vc
presentLdpVc = format?.ldpVc != null || format?.ldpVp != null;

final credentialFormat = credential.getFormat;
/// jwt_vc
presentJwtVc = format?.jwtVc != null || format?.jwtVp != null;

if (credentialFormat == VCFormatType.ldpVc.vcValue) {
/// jwt_vc_json
presentJwtVcJson = format?.jwtVcJson != null || format?.jwtVpJson != null;

/// vc+sd-jwt
presentVcSdJwt = format?.vcSdJwt != null;
} else {
if (clientMetaData == null) {
/// credential manifest case
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;
presentJwtVcJsonLd = true;
presentVcSdJwt = true;
}
} else {
final supportingFormats = <String>[];

if (presentationDefinition.format != null) {
final format = presentationDefinition.format;
} else {
final vpFormats = clientMetaData['vp_formats'] as Map<String, dynamic>;

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

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

/// jwt_vc_json
presentJwtVcJson = format?.jwtVcJson != null || format?.jwtVpJson != null;

/// 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;
}
}
presentJwtVcJson = vpFormats.containsKey('jwt_vc_json');

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.vcSdJWT.vcValue);
/// jwt_vc_json-ld
presentJwtVcJson = vpFormats.containsKey('jwt_vc_json-ld');

/// 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;
/// vc+sd-jwt
presentVcSdJwt = vpFormats.containsKey('vc+sd-jwt');
}
}

if ((presentLdpVc && vcFormatType != VCFormatType.ldpVc) ||
(presentJwtVc && vcFormatType != VCFormatType.jwtVc) ||
presentJwtVcJson && vcFormatType != VCFormatType.jwtVcJson ||
presentVcSdJwt && vcFormatType != VCFormatType.vcSdJWT) {
throw ResponseMessage(
data: {
'error': 'invalid_request',
'error_description': 'Please switch to profile that supports format '
'${supportingFormats.join('/')}.',
},
);
}
if (!presentLdpVc && !presentJwtVc && !presentJwtVcJson && !presentVcSdJwt) {
throw ResponseMessage(
data: {
'error': 'invalid_request',
'error_description': 'VC format is missing',
},
);
}

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

return supportingFormats;
}

List<dynamic> collectSdValues(Map<String, dynamic> data) {
Expand Down
1 change: 1 addition & 0 deletions lib/app/shared/message_handler/response_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,7 @@ class ResponseMessage with MessageHandler {
case ResponseString
.RESPONSE_STRING_invalidPresentationDefinitionUriErrorDescription:
return ResponseString
// ignore: lines_longer_than_80_chars
.RESPONSE_STRING_invalidPresentationDefinitionUriErrorDescription
.localise(
context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,56 +134,130 @@ class DisplayCredentialField extends StatelessWidget {

@override
Widget build(BuildContext context) {
late Widget widget;
Widget? widget;
try {
final json = jsonDecode(data.toString());
if (json is Map<String, dynamic>) {
widget = Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title ?? '',
style: Theme.of(context).textTheme.bodyMedium!.copyWith(
color: Theme.of(context).colorScheme.onSurface,
fontWeight: FontWeight.bold,
),
),
Padding(
padding: const EdgeInsets.only(left: 8),
child: DisplayCredentialFieldMap(
showVertically: showVertically,
data: data,
type: type,
),
),
],
widget = IndentedCredentialFields(
title: title,
children: DisplayCredentialFieldMap(
showVertically: showVertically,
data: json,
type: type,
),
);
}

if (json is List<dynamic>) {
widget = IndentedCredentialFields(
title: title,
children: DisplayCredentialFieldList(
showVertically: showVertically,
data: json,
type: type,
),
);
}
} catch (e) {
widget = CredentialField(
padding: const EdgeInsets.only(top: 10),
title: title,
value: data.toString(),
titleColor: Theme.of(context).colorScheme.onSurface,
valueColor: Theme.of(context).colorScheme.onSurface,
showVertically: showVertically,
type: type,
/// Empty catch because data is not a valid json and
/// the return of the function will take care of
/// this usecase (widget is null)
}

return widget ??
CredentialField(
padding: const EdgeInsets.only(top: 10),
title: title,
value: data.toString(),
titleColor: Theme.of(context).colorScheme.onSurface,
valueColor: Theme.of(context).colorScheme.onSurface,
showVertically: showVertically,
type: type,
);
}

List<Widget> DisplayCredentialFieldMap({
required bool showVertically,
required Map<String, dynamic> data,
required String type,
}) {
final List<Widget> column = [];

/// for each element in Map data, call DisplayCredentialField
for (final element in data.entries) {
column.add(
DisplayCredentialField(
title: element.key,
data: element.value is String
? element.value
: jsonEncode(element.value),
type: type,
showVertically: showVertically,
),
);
}

return CredentialField(
padding: const EdgeInsets.only(top: 10),
title: title,
value: data.toString(),
titleColor: Theme.of(context).colorScheme.onSurface,
valueColor: Theme.of(context).colorScheme.onSurface,
showVertically: showVertically,
type: type,
);
return column;
}

List<Widget> DisplayCredentialFieldList({
required bool showVertically,
required List<dynamic> data,
required String type,
}) {
final List<Widget> column = [];

/// for each element in Map data, call DisplayCredentialField
for (final element in data) {
column.add(
DisplayCredentialField(
title: null,
data: element is String ? element : jsonEncode(element),
type: type,
showVertically: showVertically,
),
);
}

return column;
}
}

class IndentedCredentialFields extends StatelessWidget {
const IndentedCredentialFields({
super.key,
required this.children,
this.title,
});

Widget DisplayCredentialFieldMap(
{required bool showVertically, required data, required String type}) {
return Text('DisplayCredentialFieldMap');
final List<Widget> children;
final String? title;
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (title != null)
Padding(
padding: const EdgeInsets.only(top: 10),
child: Text(
title!,
style: Theme.of(context).textTheme.bodyMedium!.copyWith(
color: Theme.of(context).colorScheme.onSurface,
fontWeight: FontWeight.bold,
),
),
)
else
const SizedBox.shrink(),
Padding(
padding: const EdgeInsets.only(left: 8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: children,
),
),
],
);
}
}
Loading

0 comments on commit 48242f0

Please sign in to comment.