Skip to content

Commit

Permalink
fix: Get credential supported data based on oidc4vci draft type #2655 (
Browse files Browse the repository at this point in the history
  • Loading branch information
bibash28 authored May 21, 2024
1 parent 85762ea commit 0265f48
Showing 1 changed file with 67 additions and 62 deletions.
129 changes: 67 additions & 62 deletions packages/oidc4vc/lib/src/oidc4vc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ class OIDC4VC {
await getCredentialData(
openIdConfiguration: openIdConfiguration,
credential: credential,
oidc4vciDraftType: oidc4vciDraftType,
);

final credentialResponseData = <dynamic>[];
Expand Down Expand Up @@ -1013,6 +1014,7 @@ class OIDC4VC {
getCredentialData({
required OpenIdConfiguration openIdConfiguration,
required dynamic credential,
required OIDC4VCIDraftType oidc4vciDraftType,
}) async {
String? credentialType;
List<String>? types;
Expand All @@ -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<dynamic>) {
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<String, dynamic> &&
((e.containsKey('scope') &&
e['scope'].toString() == credentialType) ||
(e.containsKey('id') &&
e['id'].toString() == credentialType) ||
e.containsKey('types') &&
e['types'] is List<dynamic> &&
(e['types'] as List<dynamic>).contains(credentialType)),
)
.firstOrNull;
if (credentialsSupported is! Map<String, dynamic>) {
throw Exception('CREDENTIAL_SUPPORT_DATA_ERROR');
}

if (credentialSupported == null ||
credentialSupported is! Map<String, dynamic>) {
throw Exception('CREDENTIAL_SUPPORT_DATA_ERROR');
}
final credentialSupportedMapEntry = credentialsSupported.entries.where(
(entry) {
final dynamic ele = entry.key;

types = (credentialSupported['types'] as List<dynamic>)
.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<String, dynamic>) {
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<String, dynamic>) {
if (credentialSupported.containsKey('credential_definition')) {
credentialDefinition = credentialSupported['credential_definition']
as Map<String, dynamic>;
}

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<dynamic>) {
throw Exception('CREDENTIAL_SUPPORT_DATA_ERROR');
}

format = credentialSupported['format'].toString();
final credentialSupported = credentialsSupported
.where(
(dynamic e) =>
e is Map<String, dynamic> &&
((e.containsKey('scope') &&
e['scope'].toString() == credentialType) ||
(e.containsKey('id') &&
e['id'].toString() == credentialType) ||
e.containsKey('types') &&
e['types'] is List<dynamic> &&
(e['types'] as List<dynamic>)
.contains(credentialType)),
)
.firstOrNull;

if (credentialSupported is Map<String, dynamic>) {
if (credentialSupported.containsKey('credential_definition')) {
credentialDefinition = credentialSupported['credential_definition']
as Map<String, dynamic>;
if (credentialSupported == null ||
credentialSupported is! Map<String, dynamic>) {
throw Exception('CREDENTIAL_SUPPORT_DATA_ERROR');
}

if (credentialSupported.containsKey('vct')) {
vct = credentialSupported['vct'].toString();
}
types = (credentialSupported['types'] as List<dynamic>)
.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);
}

Expand Down

0 comments on commit 0265f48

Please sign in to comment.