diff --git a/lib/ebsi/add_ebsi_credential.dart b/lib/ebsi/add_ebsi_credential.dart index 9b3cb1146..3579745ad 100644 --- a/lib/ebsi/add_ebsi_credential.dart +++ b/lib/ebsi/add_ebsi_credential.dart @@ -1,3 +1,6 @@ +import 'dart:convert'; + +import 'package:altme/app/app.dart'; import 'package:altme/credentials/credentials.dart'; import 'package:altme/dashboard/dashboard.dart'; import 'package:altme/dashboard/home/tab_bar/credentials/models/activity/activity.dart'; @@ -5,37 +8,52 @@ import 'package:credential_manifest/credential_manifest.dart'; import 'package:dio/dio.dart'; import 'package:jose/jose.dart'; -Future addEbsiCredential( - dynamic encodedCredentialFromEbsi, +Future addOIDC4VCCredential( + dynamic encodedCredentialFromOIDC4VC, Uri uri, CredentialsCubit credentialsCubit, + OIDC4VCType oidc4vcType, + String issuer, + String credentialTypeOrId, ) async { - //jwt_vc - final jws = JsonWebSignature.fromCompactSerialization( - encodedCredentialFromEbsi['credential'] as String, - ); + late Map credentialFromOIDC4VC; + if (oidc4vcType.issuerVcType == 'jwt_vc') { + //jwt_vc_json + final jws = JsonWebSignature.fromCompactSerialization( + encodedCredentialFromOIDC4VC['credential'] as String, + ); + + credentialFromOIDC4VC = + jws.unverifiedPayload.jsonContent['vc'] as Map; + } else if (oidc4vcType.issuerVcType == 'ldp_vc') { + //ldp_vc + + credentialFromOIDC4VC = + jsonDecode(encodedCredentialFromOIDC4VC['credential'].toString()) + as Map; + } else { + throw Exception(); + } - final credentialFromEbsi = - jws.unverifiedPayload.jsonContent['vc'] as Map; final Map newCredential = - Map.from(credentialFromEbsi); - newCredential['jwt'] = encodedCredentialFromEbsi['credential']; - newCredential['credentialPreview'] = credentialFromEbsi; + Map.from(credentialFromOIDC4VC); + + if (oidc4vcType.issuerVcType == 'jwt_vc') { + //jwt_vc_json + newCredential['jwt'] = encodedCredentialFromOIDC4VC['credential']; + } + + newCredential['credentialPreview'] = credentialFromOIDC4VC; /// added id as type to recognise the card newCredential['credentialPreview']['credentialSubject']['type'] = - credentialFromEbsi['credentialSchema']['id']; - - final String credentialSchema = uri.queryParameters['credential_type'] ?? ''; - final issuerAndCode = uri.queryParameters['issuer']; - final issuerAndCodeUri = Uri.parse(issuerAndCode!); - final issuer = - '${issuerAndCodeUri.scheme}://${issuerAndCodeUri.authority}${issuerAndCodeUri.path}'; + credentialFromOIDC4VC['credentialSchema']['id']; final CredentialManifest credentialManifest = await getCredentialManifest( Dio(), issuer, - credentialSchema, + credentialTypeOrId, + oidc4vcType.schemaForType, ); if (credentialManifest.outputDescriptors!.isNotEmpty) { @@ -51,9 +69,10 @@ Future addEbsiCredential( final credentialModel = CredentialModel.copyWithData( oldCredentialModel: newCredentialModel, - newData: credentialFromEbsi, + newData: credentialFromOIDC4VC, activities: [Activity(acquisitionAt: DateTime.now())], ); + // insert the credential in the wallet await credentialsCubit.insertCredential(credential: credentialModel); } diff --git a/lib/ebsi/initiate_ebsi_credential_issuance.dart b/lib/ebsi/initiate_ebsi_credential_issuance.dart index 9831cabc5..81c57eb49 100644 --- a/lib/ebsi/initiate_ebsi_credential_issuance.dart +++ b/lib/ebsi/initiate_ebsi_credential_issuance.dart @@ -22,7 +22,7 @@ Future initiateEbsiCredentialIssuance( String? preAuthorizedCode; late String issuer; - late String type; + late String credentialTypeOrId; late String did; late String kid; @@ -43,17 +43,18 @@ Future initiateEbsiCredentialIssuance( ['pre-authorized_code'] .toString(); issuer = credentialOfferJson['credential_issuer'].toString(); - type = credentialOfferJson['credentials'][0].toString(); + credentialTypeOrId = credentialOfferJson['credentials'][0].toString(); const didMethod = AltMeStrings.defaultDIDMethod; did = didKitProvider.keyToDID(didMethod, privateKey); kid = await didKitProvider.keyToVerificationMethod(didMethod, privateKey); + break; case OIDC4VCType.EBSIV2: preAuthorizedCode = uriFromScannedResponse.queryParameters['pre-authorized_code']; issuer = uriFromScannedResponse.queryParameters['issuer'].toString(); - type = + credentialTypeOrId = uriFromScannedResponse.queryParameters['credential_type'].toString(); final private = await oidc4vc.getPrivateKey(mnemonic, privateKey); @@ -70,10 +71,10 @@ Future initiateEbsiCredentialIssuance( } if (preAuthorizedCode != null) { - final dynamic encodedCredentialFromEbsi = await oidc4vc.getCredential( + final dynamic encodedCredentialFromOIDC4VC = await oidc4vc.getCredential( preAuthorizedCode, issuer, - type, + credentialTypeOrId, did, kid, uriFromScannedResponse, @@ -81,12 +82,13 @@ Future initiateEbsiCredentialIssuance( privateKey, ); - print(encodedCredentialFromEbsi); - - await addEbsiCredential( - encodedCredentialFromEbsi, + await addOIDC4VCCredential( + encodedCredentialFromOIDC4VC, uriFromScannedResponse, credentialsCubit, + oidc4vcType, + issuer, + credentialTypeOrId, ); } else { final Uri ebsiAuthenticationUri = diff --git a/packages/credential_manifest/lib/src/helpers/get_credential_manifest.dart b/packages/credential_manifest/lib/src/helpers/get_credential_manifest.dart index 3846b2c38..119a97bd0 100644 --- a/packages/credential_manifest/lib/src/helpers/get_credential_manifest.dart +++ b/packages/credential_manifest/lib/src/helpers/get_credential_manifest.dart @@ -7,7 +7,8 @@ import 'package:json_path/json_path.dart'; Future getCredentialManifest( Dio client, String baseUrl, - String type, + String credentialTypeOrId, + bool schemaForType, ) async { final dynamic wellKnown = await client.get( '$baseUrl/.well-known/openid-configuration', @@ -27,10 +28,12 @@ Future getCredentialManifest( credentialManifestMap, ); + final String key = schemaForType ? 'schema' : 'id'; + /// select wanted output desciptor final JsonPath outputDescriptorPath = JsonPath( // ignore: prefer_interpolation_to_compose_strings - r'$..output_descriptors[?(@.schema=="' + type + '")]', + r'$..output_descriptors[?(@.' + key + '=="' + credentialTypeOrId + '")]', ); /// There are some possible issues with this way of filtering :-/ diff --git a/packages/oidc4vc/lib/src/oidc4vc.dart b/packages/oidc4vc/lib/src/oidc4vc.dart index 93c4a37e7..c906449c4 100644 --- a/packages/oidc4vc/lib/src/oidc4vc.dart +++ b/packages/oidc4vc/lib/src/oidc4vc.dart @@ -186,7 +186,7 @@ class OIDC4VC { Future getCredential( String? preAuthorizedCode, String issuer, - String type, + String credentialTypeOrId, String did, String kid, Uri credentialRequestUri, @@ -224,9 +224,11 @@ class OIDC4VC { issuerTokenParameters, credentialRequestUri, openidConfigurationResponse, - type, + credentialTypeOrId, ); + /// sign proof + final credentialEndpoint = readCredentialEndpoint(openidConfigurationResponse); @@ -357,7 +359,7 @@ class OIDC4VC { IssuerTokenParameters issuerTokenParameters, Uri credentialRequestUri, Response> openidConfigurationResponse, - String type, + String credentialTypeOrId, ) async { final nonce = response['c_nonce'] as String; @@ -376,8 +378,8 @@ class OIDC4VC { // } final credentialData = { - 'type': type, - 'format': 'jwt_vc_json', + 'type': credentialTypeOrId, + 'format': oidc4vcModel.issuerVcType, 'proof': { 'proof_type': 'jwt', 'jwt': vcJwt,