From 0265f487d4857cee4eb3526f005540c42a931d17 Mon Sep 17 00:00:00 2001 From: Bibash Shrestha Date: Tue, 21 May 2024 14:27:49 +0545 Subject: [PATCH] fix: Get credential supported data based on oidc4vci draft type #2655 (#2656) --- packages/oidc4vc/lib/src/oidc4vc.dart | 129 +++++++++++++------------- 1 file changed, 67 insertions(+), 62 deletions(-) diff --git a/packages/oidc4vc/lib/src/oidc4vc.dart b/packages/oidc4vc/lib/src/oidc4vc.dart index 3163f4ba4..cd1e02c72 100644 --- a/packages/oidc4vc/lib/src/oidc4vc.dart +++ b/packages/oidc4vc/lib/src/oidc4vc.dart @@ -454,6 +454,7 @@ class OIDC4VC { await getCredentialData( openIdConfiguration: openIdConfiguration, credential: credential, + oidc4vciDraftType: oidc4vciDraftType, ); final credentialResponseData = []; @@ -1013,6 +1014,7 @@ class OIDC4VC { getCredentialData({ required OpenIdConfiguration openIdConfiguration, required dynamic credential, + required OIDC4VCIDraftType oidc4vciDraftType, }) async { String? credentialType; List? types; @@ -1032,87 +1034,90 @@ class OIDC4VC { throw Exception('CREDENTIAL_SUPPORT_DATA_ERROR'); } - if (credentialType.startsWith('https://api.preprod.ebsi.eu')) { - format = 'jwt_vc'; - types = []; - } else if (openIdConfiguration.credentialsSupported != null) { - final credentialsSupported = JsonPath(r'$..credentials_supported') - .read(jsonDecode(jsonEncode(openIdConfiguration))) - .first - .value; + if (oidc4vciDraftType == OIDC4VCIDraftType.draft13) { + if (openIdConfiguration.credentialConfigurationsSupported != null) { + // draft 13 case - if (credentialsSupported is! List) { - throw Exception('CREDENTIAL_SUPPORT_DATA_ERROR'); - } + final credentialsSupported = + JsonPath(r'$..credential_configurations_supported') + .read(jsonDecode(jsonEncode(openIdConfiguration))) + .first + .value; - final credentialSupported = credentialsSupported - .where( - (dynamic e) => - e is Map && - ((e.containsKey('scope') && - e['scope'].toString() == credentialType) || - (e.containsKey('id') && - e['id'].toString() == credentialType) || - e.containsKey('types') && - e['types'] is List && - (e['types'] as List).contains(credentialType)), - ) - .firstOrNull; + if (credentialsSupported is! Map) { + throw Exception('CREDENTIAL_SUPPORT_DATA_ERROR'); + } - if (credentialSupported == null || - credentialSupported is! Map) { - throw Exception('CREDENTIAL_SUPPORT_DATA_ERROR'); - } + final credentialSupportedMapEntry = credentialsSupported.entries.where( + (entry) { + final dynamic ele = entry.key; - types = (credentialSupported['types'] as List) - .map((e) => e.toString()) - .toList(); - format = credentialSupported['format'].toString(); - } else if (openIdConfiguration.credentialConfigurationsSupported != null) { - // draft 13 case + if (ele == credentialType) return true; - final credentialsSupported = - JsonPath(r'$..credential_configurations_supported') - .read(jsonDecode(jsonEncode(openIdConfiguration))) - .first - .value; + return false; + }, + ).firstOrNull; - if (credentialsSupported is! Map) { - throw Exception('CREDENTIAL_SUPPORT_DATA_ERROR'); - } + if (credentialSupportedMapEntry == null) { + throw Exception('CREDENTIAL_SUPPORT_DATA_ERROR'); + } - final credentialSupportedMapEntry = credentialsSupported.entries.where( - (entry) { - final dynamic ele = entry.key; + final credentialSupported = credentialSupportedMapEntry.value; - if (ele == credentialType) return true; + format = credentialSupported['format'].toString(); - return false; - }, - ).firstOrNull; + if (credentialSupported is Map) { + if (credentialSupported.containsKey('credential_definition')) { + credentialDefinition = credentialSupported['credential_definition'] + as Map; + } - if (credentialSupportedMapEntry == null) { + if (credentialSupported.containsKey('vct')) { + vct = credentialSupported['vct'].toString(); + } + } + } else { throw Exception('CREDENTIAL_SUPPORT_DATA_ERROR'); } + } else { + if (openIdConfiguration.credentialsSupported != null) { + final credentialsSupported = JsonPath(r'$..credentials_supported') + .read(jsonDecode(jsonEncode(openIdConfiguration))) + .first + .value; - final credentialSupported = credentialSupportedMapEntry.value; + if (credentialsSupported is! List) { + throw Exception('CREDENTIAL_SUPPORT_DATA_ERROR'); + } - format = credentialSupported['format'].toString(); + final credentialSupported = credentialsSupported + .where( + (dynamic e) => + e is Map && + ((e.containsKey('scope') && + e['scope'].toString() == credentialType) || + (e.containsKey('id') && + e['id'].toString() == credentialType) || + e.containsKey('types') && + e['types'] is List && + (e['types'] as List) + .contains(credentialType)), + ) + .firstOrNull; - if (credentialSupported is Map) { - if (credentialSupported.containsKey('credential_definition')) { - credentialDefinition = credentialSupported['credential_definition'] - as Map; + if (credentialSupported == null || + credentialSupported is! Map) { + throw Exception('CREDENTIAL_SUPPORT_DATA_ERROR'); } - if (credentialSupported.containsKey('vct')) { - vct = credentialSupported['vct'].toString(); - } + types = (credentialSupported['types'] as List) + .map((e) => e.toString()) + .toList(); + format = credentialSupported['format'].toString(); + } else { + throw Exception('CREDENTIAL_SUPPORT_DATA_ERROR'); } - } else { - throw Exception('CREDENTIAL_SUPPORT_DATA_ERROR'); } - return (credentialType, types, credentialDefinition, vct, format); }