Skip to content
This repository was archived by the owner on Feb 13, 2024. It is now read-only.

Commit 5f016d9

Browse files
author
Philipp Etschel
committed
converter fixes and tests that consider the json-ld contexts sand types
Signed-off-by: Philipp Etschel <philipp.etschel@ch.bosch.com>
1 parent 8b45502 commit 5f016d9

File tree

9 files changed

+192
-104
lines changed

9 files changed

+192
-104
lines changed

backend/business-partner-agent/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
<dependency><!-- Overwrite for jakarta-annotations-api -->
108108
<groupId>org.apache.tomcat</groupId>
109109
<artifactId>tomcat-annotations-api</artifactId>
110-
<version>10.1.0-M7</version>
110+
<version>10.1.0-M8</version>
111111
</dependency>
112112

113113
<!-- Logging -->
@@ -150,7 +150,7 @@
150150
<dependency>
151151
<groupId>com.squareup.okhttp3</groupId>
152152
<artifactId>okhttp</artifactId>
153-
<version>4.9.2</version>
153+
<version>${okhttp.version}</version>
154154
</dependency>
155155
<dependency>
156156
<groupId>org.springframework.security</groupId>
@@ -222,7 +222,7 @@
222222
<dependency>
223223
<groupId>com.squareup.okhttp3</groupId>
224224
<artifactId>mockwebserver</artifactId>
225-
<version>4.9.3</version>
225+
<version>${okhttp.version}</version>
226226
<scope>test</scope>
227227
</dependency>
228228
</dependencies>

backend/business-partner-agent/src/main/java/org/hyperledger/bpa/api/ApiConstants.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,20 @@ public class ApiConstants {
2525

2626
public static final String DEFAULT_VERIFICATION_KEY_TYPE = "Ed25519VerificationKey2018";
2727

28-
public static final String CREDENTIALS_V1 = "https://www.w3.org/2018/credentials/v1";
28+
public static final String VERIFIABLE_CREDENTIAL_NAME = "VerifiableCredential";
2929

30-
public static final String BBS_V1 = "https://w3id.org/security/bbs/v1";
30+
public static final String LABELED_CREDENTIAL_NAME = "LabeledCredential";
31+
32+
public static final String ORG_PROFILE_NAME = "OrganizationalProfileCredential";
33+
34+
// Schema Links
35+
public static final String CREDENTIALS_V1_SCHEMA = "https://www.w3.org/2018/credentials/v1";
36+
37+
public static final String BBS_V1_SCHEMA = "https://w3id.org/security/bbs/v1";
3138

3239
public static final String INDY_CREDENTIAL_SCHEMA = "https://raw.githubusercontent.com/iil-network/contexts/master/indycredential.jsonld";
40+
41+
public static final String LABELED_CREDENTIAL_SCHEMA = "https://raw.githubusercontent.com/iil-network/contexts/master/labeled-credential.jsonld";
42+
43+
public static final String MASTER_DATA_SCHEMA = "https://raw.githubusercontent.com/iil-network/contexts/master/masterdata.jsonld";
3344
}

backend/business-partner-agent/src/main/java/org/hyperledger/bpa/api/CredentialType.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,37 @@ public enum CredentialType {
3737
*/
3838
ORGANIZATIONAL_PROFILE_CREDENTIAL(
3939
List.of(
40-
ApiConstants.CREDENTIALS_V1,
41-
"https://raw.githubusercontent.com/iil-network/contexts/master/masterdata.jsonld",
42-
"https://raw.githubusercontent.com/iil-network/contexts/master/labeled-credential.jsonld"),
40+
ApiConstants.CREDENTIALS_V1_SCHEMA,
41+
ApiConstants.LABELED_CREDENTIAL_SCHEMA,
42+
ApiConstants.MASTER_DATA_SCHEMA),
4343
List.of(
44-
"VerifiableCredential",
45-
"LabeledCredential",
46-
"OrganizationalProfileCredential")),
44+
ApiConstants.VERIFIABLE_CREDENTIAL_NAME,
45+
ApiConstants.LABELED_CREDENTIAL_NAME,
46+
ApiConstants.ORG_PROFILE_NAME)),
4747
/**
4848
* A document or indy credential that is linked to a ledger schema and uses an
4949
* embedded context
5050
*/
5151
INDY(
52-
List.of(ApiConstants.CREDENTIALS_V1),
53-
List.of("VerifiableCredential")),
52+
List.of(
53+
ApiConstants.CREDENTIALS_V1_SCHEMA,
54+
ApiConstants.LABELED_CREDENTIAL_SCHEMA),
55+
List.of(
56+
ApiConstants.VERIFIABLE_CREDENTIAL_NAME,
57+
ApiConstants.LABELED_CREDENTIAL_NAME)),
5458

5559
/**
5660
* A document or json-ld credential that is not linked to any ledger and uses an
5761
* external or embedded context
5862
*/
5963
JSON_LD(
6064
List.of(
61-
ApiConstants.CREDENTIALS_V1,
62-
ApiConstants.BBS_V1),
63-
List.of("VerifiableCredential"));
65+
ApiConstants.CREDENTIALS_V1_SCHEMA,
66+
ApiConstants.LABELED_CREDENTIAL_SCHEMA,
67+
ApiConstants.BBS_V1_SCHEMA),
68+
List.of(
69+
ApiConstants.VERIFIABLE_CREDENTIAL_NAME,
70+
ApiConstants.LABELED_CREDENTIAL_NAME));
6471

6572
private final List<Object> context;
6673
private final List<String> type;
@@ -72,10 +79,10 @@ public enum CredentialType {
7279
* @return {@link CredentialType} or null when no match was found
7380
*/
7481
public static CredentialType fromCredential(@NonNull VerifiableCredential.VerifiableIndyCredential c) {
75-
if (c.getType().stream().anyMatch(t -> "OrganizationalProfileCredential".equals(t))) {
82+
if (c.getType().stream().anyMatch(ApiConstants.ORG_PROFILE_NAME::equals)) {
7683
return CredentialType.ORGANIZATIONAL_PROFILE_CREDENTIAL;
7784
}
78-
if (c.getContext().stream().anyMatch(ctx -> ApiConstants.BBS_V1.equals(ctx))) {
85+
if (c.getContext().stream().anyMatch(ApiConstants.BBS_V1_SCHEMA::equals)) {
7986
return CredentialType.JSON_LD;
8087
}
8188
return CredentialType.INDY;

backend/business-partner-agent/src/main/java/org/hyperledger/bpa/impl/util/Converter.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,17 @@ public PartnerAPI toAPIObject(@NonNull VerifiablePresentation<VerifiableIndyCred
114114
throw new RuntimeException(e);
115115
}
116116

117-
boolean indyCredential = false;
117+
boolean verifiedCredential = false;
118118
if (CollectionUtils.isNotEmpty(c.getType())) {
119-
indyCredential = c.getType().stream().anyMatch("IndyCredential"::equals);
119+
verifiedCredential = c.getType().stream().anyMatch("IndyCredential"::equals);
120120
}
121121

122122
CredentialType type = CredentialType.fromCredential(c);
123123

124124
String schemaId = null;
125-
if (indyCredential) {
125+
if (verifiedCredential) { // indy credential signed by 3rd party
126126
schemaId = c.getSchemaId();
127-
} else if (CredentialType.INDY.equals(type)) {
127+
} else if (CredentialType.INDY.equals(type)) { // plain document, indy based and self-signed
128128
schemaId = getSchemaIdFromContext(c);
129129
} else if (CredentialType.JSON_LD.equals(type)) {
130130
List<Object> ctx = new ArrayList<>(c.getContext());
@@ -134,23 +134,16 @@ public PartnerAPI toAPIObject(@NonNull VerifiablePresentation<VerifiableIndyCred
134134
}
135135
}
136136

137-
String typeLabel = resolveTypeLabel(type, schemaId);
138-
if (StringUtils.isEmpty(typeLabel) && CredentialType.JSON_LD.equals(type)) {
139-
List<String> types = new ArrayList<>(c.getType());
140-
types.removeAll(CredentialType.JSON_LD.getType());
141-
if (CollectionUtils.isNotEmpty(types)) {
142-
typeLabel = types.get(0);
143-
}
144-
}
137+
String typeLabel = resolveTypeLabel(type, schemaId, c);
145138

146139
final PartnerCredential pCred = PartnerCredential
147140
.builder()
148141
.type(type)
149142
.typeLabel(typeLabel)
150-
.issuer(indyCredential ? c.getIndyIssuer() : c.getIssuer())
143+
.issuer(verifiedCredential ? c.getIndyIssuer() : c.getIssuer())
151144
.schemaId(schemaId)
152145
.credentialData(node)
153-
.indyCredential(indyCredential)
146+
.indyCredential(verifiedCredential)
154147
.build();
155148
pc.add(pCred);
156149
}
@@ -208,7 +201,7 @@ public MyDocumentAPI toApiObject(@NonNull MyDocument myDoc) {
208201
.documentData(myDoc.getDocument() != null ? fromMap(myDoc.getDocument(), JsonNode.class) : null)
209202
.isPublic(myDoc.getIsPublic())
210203
.type(myDoc.getType())
211-
.typeLabel(resolveTypeLabel(myDoc.getType(), myDoc.getSchemaId()))
204+
.typeLabel(resolveTypeLabel(myDoc.getType(), myDoc.getSchemaId(), null))
212205
.schemaId(myDoc.getSchemaId())
213206
.label(myDoc.getLabel())
214207
.build();
@@ -313,11 +306,22 @@ private String resolveTypeLabel(@NonNull PartnerProof p) {
313306
return defaultLabel;
314307
}
315308

316-
private String resolveTypeLabel(@NonNull CredentialType type, @Nullable String schemaId) {
309+
private String resolveTypeLabel(@NonNull CredentialType type, @Nullable String schemaId,
310+
@Nullable VerifiableIndyCredential c) {
317311
String result = null;
318-
if (!CredentialType.ORGANIZATIONAL_PROFILE_CREDENTIAL.equals(type)
319-
&& StringUtils.isNotEmpty(schemaId)) {
312+
if (CredentialType.INDY.equals(type) && StringUtils.isNotEmpty(schemaId)) {
320313
result = schemaService.getSchemaLabel(schemaId);
314+
} else if (CredentialType.JSON_LD.equals(type)) {
315+
if (StringUtils.isNotEmpty(schemaId)) {
316+
result = schemaService.getSchemaLabel(schemaId);
317+
}
318+
if (result == null && c != null) {
319+
List<String> types = new ArrayList<>(c.getType());
320+
types.removeAll(CredentialType.JSON_LD.getType());
321+
if (CollectionUtils.isNotEmpty(types)) {
322+
result = types.get(0);
323+
}
324+
}
321325
} else if (CredentialType.ORGANIZATIONAL_PROFILE_CREDENTIAL.equals(type)) {
322326
result = msg.getMessage("api.org.profile.name");
323327
}

backend/business-partner-agent/src/test/java/org/hyperledger/bpa/impl/activity/VPManagerTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,9 @@ void testBuildFromDocumentIndyWithSchema() throws Exception {
131131
final VerifiableCredential vp = vpm.buildFromDocument(doc, "xxyyyzzz");
132132

133133
String actual = gson.toJson(vp.getContext());
134-
String expected = "[\"https://www.w3.org/2018/credentials/v1\",{\"@context\":{\"sc\":\"did:iil:1234\"," +
135-
"\"key1\":{\"@id\":\"sc:key1\"},\"key2\":{\"@id\":\"sc:key2\"}}}]";
134+
String expected = "[\"https://www.w3.org/2018/credentials/v1\",\"https://raw.githubusercontent.com/iil-network/contexts/master/labeled-credential.jsonld\""
135+
+
136+
",{\"@context\":{\"sc\":\"did:iil:1234\",\"key1\":{\"@id\":\"sc:key1\"},\"key2\":{\"@id\":\"sc:key2\"}}}]";
136137

137138
assertEquals(expected, actual);
138139
}
@@ -154,8 +155,8 @@ void buildFromCredentialCommReg() {
154155
VerifiableCredential.VerifiableIndyCredential indyCred = vpm.buildFromCredential(myCredential);
155156
assertNotNull(indyCred.getCredentialSubject());
156157
assertEquals(2, indyCred.getCredentialSubject().size());
157-
assertEquals(2, indyCred.getType().size());
158-
assertEquals(2, indyCred.getContext().size());
158+
assertEquals(3, indyCred.getType().size());
159+
assertEquals(3, indyCred.getContext().size());
159160
// System.out.println(GsonConfig.prettyPrinter().toJson(indyCred));
160161
}
161162

backend/business-partner-agent/src/test/java/org/hyperledger/bpa/impl/aries/CredentialManagerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void testResolveIssuerByVP() throws Exception {
8686
.build());
8787

8888
String iss = mgmt.resolveIssuer(c);
89-
assertEquals("Bosch Healthcare", iss);
89+
assertEquals("Test Corp", iss);
9090
}
9191

9292
@Test

backend/business-partner-agent/src/test/java/org/hyperledger/bpa/impl/util/ConverterTest.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,28 +64,48 @@ void testConvertWalletDocumentRequest() throws Exception {
6464

6565
@Test
6666
void testConvertVPToPartnerApi() throws Exception {
67+
String issuerIsSelf = "did:sov:F6dB7dMVHUQSC64qemnBi7";
68+
String expectedIndySchemaId = "M6Mbe3qx7vB4wpZF4sBRjt:2:bank_account:1.0";
6769
VerifiablePresentation<VerifiableIndyCredential> vp = loadAndConvertTo("files/verifiablePresentation.json",
6870
Converter.VP_TYPEREF);
6971
final PartnerAPI partner = conv.toAPIObject(vp);
7072

71-
assertEquals(3, partner.getCredential().size());
73+
// expecting all four types that are currently supported
74+
assertEquals(4, partner.getCredential().size());
7275

76+
// JSON-LD Type Document
7377
PartnerAPI.PartnerCredential c1 = partner.getCredential().get(0);
74-
assertEquals(CredentialType.INDY, c1.getType());
75-
assertEquals("did:sov:Ni2hE7fEHJ25xUBc7ZESf6", c1.getIssuer());
78+
assertEquals(CredentialType.JSON_LD, c1.getType());
79+
assertEquals(issuerIsSelf, c1.getIssuer());
7680
assertFalse(c1.getIndyCredential());
77-
assertNotNull(c1.getTypeLabel());
81+
assertEquals("Person", c1.getTypeLabel());
82+
assertTrue(c1.getSchemaId().startsWith("https://schema.org/"));
7883

84+
// Org Profile Document
7985
PartnerAPI.PartnerCredential c2 = partner.getCredential().get(1);
8086
assertEquals(CredentialType.ORGANIZATIONAL_PROFILE_CREDENTIAL, c2.getType());
87+
assertEquals(issuerIsSelf, c2.getIssuer());
8188
assertFalse(c2.getIndyCredential());
82-
assertNotNull(c2.getTypeLabel());
89+
assertEquals("Organizational Profile", c2.getTypeLabel());
90+
assertNull(c2.getSchemaId());
8391

92+
// Indy based Document
8493
PartnerAPI.PartnerCredential c3 = partner.getCredential().get(2);
8594
assertEquals(CredentialType.INDY, c3.getType());
86-
assertEquals("did:sov:M6Mbe3qx7vB4wpZF4sBRjt", c3.getIssuer());
95+
assertEquals(issuerIsSelf, c3.getIssuer());
8796
assertNotNull(c3.getTypeLabel());
88-
assertTrue(c3.getIndyCredential());
97+
assertEquals("bank_account", c3.getTypeLabel());
98+
assertFalse(c3.getIndyCredential());
99+
assertEquals(expectedIndySchemaId, c3.getSchemaId());
100+
101+
// Indy based credential
102+
PartnerAPI.PartnerCredential c4 = partner.getCredential().get(3);
103+
assertEquals(CredentialType.INDY, c4.getType());
104+
assertEquals("did:sov:Uv53vZ1SnS3NPYMMSr4BaQ", c4.getIssuer());
105+
assertNotNull(c4.getTypeLabel());
106+
assertEquals("bank_account", c4.getTypeLabel());
107+
assertTrue(c4.getIndyCredential());
108+
assertEquals(expectedIndySchemaId, c4.getSchemaId());
89109
}
90110

91111
@Test

0 commit comments

Comments
 (0)