From 5da4e0a86430c77cd84fec301c01537bd0640a66 Mon Sep 17 00:00:00 2001 From: Bibash Shrestha Date: Thu, 15 Feb 2024 16:01:40 +0545 Subject: [PATCH] feat: Draft 13, authorization code flow fails with scope #2388 --- packages/oidc4vc/lib/src/oidc4vc.dart | 90 +++++++++++++++++++-------- 1 file changed, 65 insertions(+), 25 deletions(-) diff --git a/packages/oidc4vc/lib/src/oidc4vc.dart b/packages/oidc4vc/lib/src/oidc4vc.dart index ca5bf54e7..beade19c7 100644 --- a/packages/oidc4vc/lib/src/oidc4vc.dart +++ b/packages/oidc4vc/lib/src/oidc4vc.dart @@ -203,38 +203,78 @@ class OIDC4VC { for (final credential in selectedCredentials) { late Map data; if (credential is String) { - // - final credentialsSupported = openIdConfiguration.credentialsSupported; + if (openIdConfiguration.credentialsSupported != null) { + final credentialsSupported = openIdConfiguration.credentialsSupported; + + dynamic credentailData; + + for (final dynamic cred in credentialsSupported!) { + if (cred is Map && + ((cred.containsKey('scope') && + cred['scope'].toString() == credential) || + (cred.containsKey('id') && + cred['id'].toString() == credential))) { + credentailData = cred; + break; + } + } - if (credentialsSupported == null) { - throw Exception(); - } + if (credentailData == null) { + throw Exception('CREDENTIAL_SUPPORT_DATA_ERROR'); + } + + data = { + 'type': 'openid_credential', + 'locations': [issuer], + 'format': credentailData['format'], + 'types': credentailData['types'], + }; + + credentials.add((credentailData['types'] as List).last); + } else if (openIdConfiguration.credentialConfigurationsSupported != + null) { + // draft 13 case + final credentialsSupported = + openIdConfiguration.credentialConfigurationsSupported; + + if (credentialsSupported is! Map) { + throw Exception('CREDENTIAL_SUPPORT_DATA_ERROR'); + } + + final credentialSupportedMapEntry = + credentialsSupported.entries.where( + (entry) { + final dynamic ele = entry.key; + + if (ele == credential) return true; - dynamic credentailData; + return false; + }, + ).firstOrNull; - for (final dynamic cred in credentialsSupported) { - if (cred is Map && - ((cred.containsKey('scope') && - cred['scope'].toString() == credential) || - (cred.containsKey('id') && - cred['id'].toString() == credential))) { - credentailData = cred; - break; + if (credentialSupportedMapEntry == null) { + throw Exception('CREDENTIAL_SUPPORT_DATA_ERROR'); } - } - if (credentailData == null) { - throw Exception(); - } + final credentialSupported = credentialSupportedMapEntry.value; - data = { - 'type': 'openid_credential', - 'locations': [issuer], - 'format': credentailData['format'], - 'types': credentailData['types'], - }; + data = { + 'type': 'openid_credential', + 'locations': [issuer], + 'format': credentialSupported['format'], + //'types': credential['types'], + }; - credentials.add((credentailData['types'] as List).last); + final scope = credentialSupported['scope']; + + if (scope == null) { + throw Exception('CREDENTIAL_SUPPORT_DATA_ERROR'); + } + + credentials.add(scope); + } else { + throw Exception('CREDENTIAL_SUPPORT_DATA_ERROR'); + } } else if (credential is Map) { data = { 'type': 'openid_credential',