Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for optional disjunctions #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/main/java/org/irmacard/api/common/AttributeDisjunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ public class AttributeDisjunction extends ArrayList<AttributeIdentifier> {

private String label;
private HashMap<AttributeIdentifier, String> values = new HashMap<>();
private boolean optional;

private transient AttributeIdentifier selected;
private transient boolean satisfied = false;

public AttributeDisjunction(String label) {
public AttributeDisjunction(String label, boolean optional) {
this.label = label;
this.optional = optional;
}

public AttributeDisjunction(String label, AttributeIdentifier value) {
Expand Down Expand Up @@ -84,6 +86,14 @@ public void setLabel(String label) {
this.label = label;
}

public boolean isOptional() {
return optional;
}

public void setOptional(boolean optional) {
this.optional = optional;
}

public AttributeIdentifier getSelected() {
return selected;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,28 +147,33 @@ protected DisclosureProofResult verify(ProofList proofs, Date validityDate, bool
result.setStatus(DisclosureProofResult.Status.INVALID);
return result;
}

for (AttributeIdentifier ai : foundAttrs.keySet()) {
// For each of the disclosed attributes in this proof, see if they satisfy one of
// the AttributeDisjunctions that we asked for
AttributeDisjunction disjunction = content.find(ai);
if (disjunction == null || disjunction.isSatisfied())
continue;

String value = foundAttrs.get(ai);
if (!disjunction.hasValues()) {
disjunction.setSatisfied(true);
attributes.put(ai, value);
}
else {
// If the request indicated that the attribute should have a specific value, then the containing
// disjunction is only satisfied if the actual value of the attribute is correct.
String requiredValue = disjunction.getValues().get(ai);
if (requiredValue.equals(value)) {
disjunction.setSatisfied(true);
attributes.put(ai, value);

// For each of the disjunctions, lookup attributes satisfying them
for (AttributeDisjunction disjunction : content) {
for (AttributeIdentifier ai : disjunction) {
// Is this attribute given?
if (foundAttrs.containsKey(ai)) {
String value = foundAttrs.get(ai);
if (!disjunction.hasValues()) {
disjunction.setSatisfied(true);
attributes.put(ai, value);
break; // Done with disjunction
} else {
// If the request indicated that the attribute should have a specific value, then the containing
// disjunction is only satisfied if the actual value of the attribute is correct.
String requiredValue = disjunction.getValues().get(ai);
if (requiredValue.equals(value)) {
disjunction.setSatisfied(true);
attributes.put(ai, value);
break; // Done with disjunction
}
}
}
}

// If it is optional, this disjunction is always satisfied
if (disjunction.isOptional())
disjunction.setSatisfied(true);
}

for (AttributeDisjunction disjunction : content)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class AttributeDisjuctionSerializer
public JsonElement serialize(AttributeDisjunction src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject object = new JsonObject();
object.addProperty("label", src.getLabel());
object.addProperty("optional", src.isOptional());

if (src.hasValues()) { // Serialize it as a map
JsonObject map = new JsonObject();
Expand All @@ -67,8 +68,12 @@ public AttributeDisjunction deserialize(JsonElement json, Type typeOfT, JsonDese
try {
JsonObject object = json.getAsJsonObject();
String label = object.get("label").getAsString();
JsonElement optionalEl = object.get("optional");
boolean optional = false;
if (optionalEl != null)
optional = optionalEl.getAsBoolean();

AttributeDisjunction disjunction = new AttributeDisjunction(label);
AttributeDisjunction disjunction = new AttributeDisjunction(label, optional);

JsonElement attributes = object.get("attributes");
if (attributes.isJsonArray()) {
Expand Down