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

Commit 9097f5c

Browse files
author
Philipp Etschel
authored
Merge pull request #839 from boschresearch/feature/self-attested-2
Support for self attested attributes in the proof template
2 parents 8caaef8 + 6cea350 commit 9097f5c

File tree

20 files changed

+250
-91
lines changed

20 files changed

+250
-91
lines changed

backend/business-partner-agent/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149
<dependency>
150150
<groupId>network.idu.acapy</groupId>
151151
<artifactId>aries-client-python</artifactId>
152-
<version>0.7.30</version>
152+
<version>0.7.31</version>
153153
</dependency>
154154
<dependency>
155155
<groupId>org.hyperledger.business-partner-agent</groupId>

backend/business-partner-agent/src/main/java/org/hyperledger/bpa/controller/api/prooftemplates/AttributeGroup.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
package org.hyperledger.bpa.controller.api.prooftemplates;
1919

2020
import io.micronaut.core.annotation.Introspected;
21-
import io.micronaut.core.util.CollectionUtils;
21+
import io.micronaut.core.annotation.Nullable;
2222
import lombok.*;
2323

2424
import javax.validation.Valid;
@@ -46,15 +46,11 @@ public class AttributeGroup {
4646
@Builder.Default
4747
private Boolean nonRevoked = Boolean.FALSE;
4848

49-
@NotNull
49+
@Nullable
5050
@Builder.Default
51+
private Boolean allowSelfAttested = Boolean.FALSE;
52+
5153
@Valid
52-
private List<SchemaRestrictions> schemaLevelRestrictions = List.of(SchemaRestrictions.builder().build());
53-
54-
public List<SchemaRestrictions> getSchemaLevelRestrictions() {
55-
if (CollectionUtils.isEmpty(schemaLevelRestrictions)) {
56-
return List.of(SchemaRestrictions.builder().build());
57-
}
58-
return schemaLevelRestrictions;
59-
}
54+
@Nullable
55+
private List<SchemaRestrictions> schemaLevelRestrictions;
6056
}

backend/business-partner-agent/src/main/java/org/hyperledger/bpa/controller/api/prooftemplates/SchemaRestrictions.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020-2021 - for information on the respective copyright owner
2+
* Copyright (c) 2020-2022 - for information on the respective copyright owner
33
* see the NOTICE file and/or the repository at
44
* https://github.com/hyperledger-labs/business-partner-agent
55
*
@@ -15,21 +15,21 @@
1515
* See the License for the specific language governing permissions and
1616
* limitations under the License.
1717
*/
18-
1918
package org.hyperledger.bpa.controller.api.prooftemplates;
2019

20+
import io.micronaut.core.annotation.Introspected;
2121
import io.micronaut.core.annotation.Nullable;
2222
import lombok.AllArgsConstructor;
2323
import lombok.Builder;
2424
import lombok.Data;
2525
import lombok.NoArgsConstructor;
26-
import org.hyperledger.aries.api.present_proof.PresentProofRequest;
2726
import org.hyperledger.bpa.impl.verification.ValidUUID;
2827

2928
@Data
3029
@NoArgsConstructor
3130
@AllArgsConstructor
3231
@Builder
32+
@Introspected
3333
public class SchemaRestrictions {
3434

3535
@Nullable
@@ -45,16 +45,4 @@ public class SchemaRestrictions {
4545
private String credentialDefinitionId;
4646
@Nullable
4747
private String issuerDid;
48-
49-
public static SchemaRestrictions fromProofRestrictions(PresentProofRequest.ProofRequest.ProofRestrictions r) {
50-
return SchemaRestrictions
51-
.builder()
52-
.schemaId(r.getSchemaId())
53-
.schemaName(r.getSchemaName())
54-
.schemaVersion(r.getSchemaVersion())
55-
.schemaIssuerDid(r.getSchemaIssuerDid())
56-
.credentialDefinitionId(r.getCredentialDefinitionId())
57-
.issuerDid(r.getIssuerDid())
58-
.build();
59-
}
6048
}

backend/business-partner-agent/src/main/java/org/hyperledger/bpa/impl/aries/proof/VerifierLDManager.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
import java.util.List;
4040
import java.util.Map;
41+
import java.util.Objects;
4142
import java.util.UUID;
4243
import java.util.stream.Collectors;
4344
import java.util.stream.Stream;
@@ -97,7 +98,9 @@ private V2DIFProofRequest.PresentationDefinition.InputDescriptors groupToDescrip
9798
}
9899

99100
private Map<UUID, DIFField> buildDifFieldsFromGroup(@NonNull BPAAttributeGroup group) {
100-
Map<UUID, DIFField> issuerRestrictions = group.getSchemaLevelRestrictions()
101+
List<BPASchemaRestrictions> restrictions = Objects.requireNonNullElse(group.getSchemaLevelRestrictions(),
102+
List.of());
103+
Map<UUID, DIFField> issuerRestrictions = restrictions
101104
.stream()
102105
.map(BPASchemaRestrictions::getIssuerDid)
103106
.filter(StringUtils::isNotEmpty)

backend/business-partner-agent/src/main/java/org/hyperledger/bpa/impl/aries/prooftemplates/Attributes.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
import lombok.Builder;
2121
import lombok.Data;
2222
import lombok.Singular;
23-
import org.hyperledger.aries.api.present_proof.PresentProofRequest;
23+
import org.hyperledger.aries.api.present_proof.PresentProofRequest.ProofRequest.ProofRequestedAttributes;
24+
import org.hyperledger.aries.api.present_proof.PresentProofRequest.ProofRequest.ProofRestrictions.ProofRestrictionsBuilder;
2425
import org.hyperledger.bpa.persistence.model.prooftemplate.BPASchemaRestrictions;
2526

2627
import java.util.List;
@@ -39,21 +40,17 @@ public class Attributes {
3940
@Singular
4041
private Map<String, String> equals;
4142

42-
public void addToBuilder(
43-
BiConsumer<String, PresentProofRequest.ProofRequest.ProofRequestedAttributes> builderSink) {
44-
List<PresentProofRequest.ProofRequest.ProofRestrictions.ProofRestrictionsBuilder> restrictionsBuilder = ProofTemplateElementVisitor
45-
.asProofRestrictionsBuilder(
46-
schemaRestrictions);
43+
public void addToBuilder(BiConsumer<String, ProofRequestedAttributes> builderSink) {
44+
List<ProofRestrictionsBuilder> restrictionsBuilder = ProofTemplateElementVisitor
45+
.asProofRestrictionsBuilder(schemaRestrictions);
4746
restrictionsBuilder.forEach(r -> equals.forEach(r::addAttributeValueRestriction));
4847

49-
PresentProofRequest.ProofRequest.ProofRequestedAttributes.ProofRequestedAttributesBuilder builder = PresentProofRequest.ProofRequest.ProofRequestedAttributes
50-
.builder()
48+
ProofRequestedAttributes.ProofRequestedAttributesBuilder builder = ProofRequestedAttributes.builder()
5149
.names(names)
52-
// TODO only set when set in restriction
53-
.restrictions(restrictionsBuilder.stream().map(res -> res.schemaId(schemaId).build().toJsonObject())
50+
.restrictions(restrictionsBuilder.stream()
51+
.map(res -> res.schemaId(schemaId).build().toJsonObject())
5452
.collect(Collectors.toList()));
5553

5654
builderSink.accept(schemaId, revocationApplicator.applyOn(builder).build());
57-
5855
}
5956
}

backend/business-partner-agent/src/main/java/org/hyperledger/bpa/impl/aries/prooftemplates/ProofTemplateConversion.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import jakarta.inject.Singleton;
2323
import lombok.extern.slf4j.Slf4j;
2424
import org.hyperledger.aries.api.present_proof.PresentProofRequest;
25+
import org.hyperledger.aries.api.present_proof.PresentProofRequestHelper;
2526
import org.hyperledger.bpa.api.aries.SchemaAPI;
2627
import org.hyperledger.bpa.api.exception.PartnerException;
2728
import org.hyperledger.bpa.config.BPAMessageSource;
@@ -80,6 +81,11 @@ public PresentProofRequest proofRequestViaVisitorFrom(@NonNull UUID partnerId,
8081
@NonNull
8182
public PresentProofRequest.PresentProofRequestBuilder templateToProofRequest(
8283
@NonNull @Valid BPAProofTemplate proofTemplate) {
84+
85+
if (proofTemplate.allowSelfAttested()) {
86+
return buildForSelfAttestation(proofTemplate);
87+
}
88+
8389
ProofTemplateElementVisitor proofTemplateElementVisitor = new ProofTemplateElementVisitor(
8490
this::resolveLedgerSchemaId,
8591
new RevocationTimeStampProvider(clock));
@@ -110,4 +116,10 @@ private Stream<Pair<String, BPAAttribute>> pairSchemaIdWithAttributes(@NonNull B
110116
.orElse(Stream.empty());
111117
}
112118

119+
PresentProofRequest.PresentProofRequestBuilder buildForSelfAttestation(@NonNull BPAProofTemplate proofTemplate) {
120+
return PresentProofRequestHelper.buildForSelfAttestation(
121+
proofTemplate.getName(),
122+
proofTemplate.collectSelfAttestedAttributes());
123+
}
124+
113125
}

backend/business-partner-agent/src/main/java/org/hyperledger/bpa/impl/aries/prooftemplates/ProofTemplateElementVisitor.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package org.hyperledger.bpa.impl.aries.prooftemplates;
1919

20+
import lombok.NonNull;
2021
import org.hyperledger.aries.api.present_proof.PresentProofRequest;
2122
import org.hyperledger.bpa.impl.util.Pair;
2223
import org.hyperledger.bpa.persistence.model.BPAProofTemplate;
@@ -66,14 +67,16 @@ public class ProofTemplateElementVisitor {
6667
}
6768

6869
static List<PresentProofRequest.ProofRequest.ProofRestrictions.ProofRestrictionsBuilder> asProofRestrictionsBuilder(
69-
List<BPASchemaRestrictions> schemaRestrictions) {
70-
return schemaRestrictions.stream().map(res -> PresentProofRequest.ProofRequest.ProofRestrictions.builder()
71-
.schemaId(res.getSchemaId())
72-
.schemaName(res.getSchemaName())
73-
.schemaVersion(res.getSchemaVersion())
74-
.schemaIssuerDid(res.getSchemaIssuerDid())
75-
.credentialDefinitionId(res.getCredentialDefinitionId())
76-
.issuerDid(res.getIssuerDid())).collect(Collectors.toList());
70+
@NonNull List<BPASchemaRestrictions> schemaRestrictions) {
71+
return schemaRestrictions.stream()
72+
.map(res -> PresentProofRequest.ProofRequest.ProofRestrictions.builder()
73+
.schemaId(res.getSchemaId())
74+
.schemaName(res.getSchemaName())
75+
.schemaVersion(res.getSchemaVersion())
76+
.schemaIssuerDid(res.getSchemaIssuerDid())
77+
.credentialDefinitionId(res.getCredentialDefinitionId())
78+
.issuerDid(res.getIssuerDid()))
79+
.collect(Collectors.toList());
7780
}
7881

7982
private NonRevocationApplicator getRevocationApplicator(Pair<String, ?> schemaAndAttributesBuilder) {

backend/business-partner-agent/src/main/java/org/hyperledger/bpa/impl/verification/ValidatorFactory.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@ ConstraintValidator<SameSchemaType, BPAProofTemplate> sameSchemaType(SchemaServi
7979
};
8080
}
8181

82+
@Singleton
83+
ConstraintValidator<SameGroupType, BPAProofTemplate> sameAttributeGroupType() {
84+
return (value, annotationMetadata, context) -> {
85+
if (value == null || value.getAttributeGroups() == null) {
86+
return true;
87+
}
88+
int selfAttested = value.streamAttributeGroups()
89+
.filter(BPAAttributeGroup::allowSelfAttested).toList().size();
90+
return selfAttested == 0 || selfAttested == value.getAttributeGroups().getAttributeGroups().size();
91+
};
92+
}
93+
8294
// TODO find a way to validate single list entries in isolation. This shows the
8395
// whole list as invalid, even if it's only one entry.
8496
@Singleton
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) 2020-2022 - for information on the respective copyright owner
3+
* see the NOTICE file and/or the repository at
4+
* https://github.com/hyperledger-labs/business-partner-agent
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.hyperledger.bpa.impl.verification.prooftemplates;
19+
20+
import javax.validation.Constraint;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
import static java.lang.annotation.ElementType.FIELD;
26+
import static java.lang.annotation.ElementType.TYPE_USE;
27+
28+
@Retention(RetentionPolicy.RUNTIME)
29+
@Target({ TYPE_USE, FIELD, })
30+
@Constraint(validatedBy = {})
31+
public @interface SameGroupType {
32+
33+
String message() default "Either all groups need to allow self-attestation, or none";
34+
}

backend/business-partner-agent/src/main/java/org/hyperledger/bpa/persistence/model/BPAProofTemplate.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
import lombok.NoArgsConstructor;
3030
import org.hyperledger.bpa.api.CredentialType;
3131
import org.hyperledger.bpa.controller.api.prooftemplates.ProofTemplate;
32+
import org.hyperledger.bpa.impl.verification.prooftemplates.SameGroupType;
3233
import org.hyperledger.bpa.impl.verification.prooftemplates.SameSchemaType;
34+
import org.hyperledger.bpa.persistence.model.prooftemplate.BPAAttribute;
3335
import org.hyperledger.bpa.persistence.model.prooftemplate.BPAAttributeGroup;
3436
import org.hyperledger.bpa.persistence.model.prooftemplate.BPAAttributeGroups;
3537

@@ -38,7 +40,10 @@
3840
import javax.validation.constraints.NotEmpty;
3941
import javax.validation.constraints.NotNull;
4042
import java.time.Instant;
43+
import java.util.List;
44+
import java.util.Set;
4145
import java.util.UUID;
46+
import java.util.stream.Collectors;
4247
import java.util.stream.Stream;
4348

4449
@Data
@@ -48,6 +53,7 @@
4853
@Introspected
4954
@Entity
5055
@SameSchemaType
56+
@SameGroupType
5157
@Table(name = "bpa_proof_template")
5258
public class BPAProofTemplate {
5359
@Id
@@ -104,4 +110,21 @@ public boolean typeIsIndy() {
104110
public boolean typeIsJsonLD() {
105111
return CredentialType.JSON_LD.equals(type);
106112
}
113+
114+
public boolean allowSelfAttested() {
115+
return streamAttributeGroups()
116+
.map(BPAAttributeGroup::allowSelfAttested)
117+
.findFirst()
118+
.orElse(Boolean.FALSE);
119+
}
120+
121+
public Set<String> collectSelfAttestedAttributes() {
122+
return streamAttributeGroups()
123+
.filter(BPAAttributeGroup::allowSelfAttested)
124+
.map(BPAAttributeGroup::getAttributes)
125+
.flatMap(List::stream)
126+
.map(BPAAttribute::getName)
127+
.collect(Collectors.toSet());
128+
129+
}
107130
}

0 commit comments

Comments
 (0)