Skip to content
Draft
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
44 changes: 9 additions & 35 deletions src/main/java/org/cardanofoundation/signify/app/Contacting.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,19 @@
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.DigestException;
import java.util.HashMap;
import java.net.http.HttpResponse;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import java.util.concurrent.ExecutionException;

import org.cardanofoundation.signify.generated.keria.model.Challenge;
import org.cardanofoundation.signify.generated.keria.model.Contact;
import org.cardanofoundation.signify.generated.keria.model.Identifier;

public class Contacting {

@Getter
public static class Challenge {
public List<String> words;
}

@Getter
public static class Challenges {
public final SignifyClient client;
Expand Down Expand Up @@ -120,28 +116,6 @@ public Object responded(String source, String said) throws LibsodiumException, I
}
}

@Getter
public static class Contact {
private String alias;
private String oobi;
private String id;
private Map<String, Object> additionalProperties = new HashMap<>();

@JsonAnySetter
public void setAdditionalProperty(String key, Object value) {
additionalProperties.put(key, value);
}

public <T> T get(String key) {
return switch (key) {
case "alias" -> (T) alias;
case "oobi" -> (T) oobi;
case "id" -> (T) id;
default -> (T) additionalProperties.get(key);
};
}
}

@Getter
public static class Contacts {
private final SignifyClient client;
Expand Down Expand Up @@ -192,7 +166,7 @@ public Contact[] list() throws IOException, InterruptedException, LibsodiumExcep
* @param pre Prefix of the contact
* @return Optional containing the contact if found, or empty if not found
*/
public Optional<Object> get(String pre) throws InterruptedException, IOException, LibsodiumException {
public Optional<Contact> get(String pre) throws InterruptedException, IOException, LibsodiumException {
String path = "/contacts/" + pre;
String method = "GET";
HttpResponse<String> response = this.client.fetch(path, method, null);
Expand All @@ -201,7 +175,7 @@ public Optional<Object> get(String pre) throws InterruptedException, IOException
return Optional.empty();
}

return Optional.of(Utils.fromJson(response.body(), Object.class));
return Optional.of(Utils.fromJson(response.body(), Contact.class));
}

/**
Expand All @@ -210,11 +184,11 @@ public Optional<Object> get(String pre) throws InterruptedException, IOException
* @param info Information about the contact
* @return Result of the addition
*/
public Object add(String pre, Map<String, Object> info) throws IOException, InterruptedException, LibsodiumException {
public Contact add(String pre, Map<String, Object> info) throws IOException, InterruptedException, LibsodiumException {
String path = "/contacts/" + pre;
String method = "POST";
HttpResponse<String> response = this.client.fetch(path, method, info);
return Utils.fromJson(response.body(), Object.class);
return Utils.fromJson(response.body(), Contact.class);
}

/**
Expand All @@ -233,11 +207,11 @@ public void delete(String pre) throws IOException, InterruptedException, Libsodi
* @param info Updated information about the contact
* @return Result of the update
*/
public Object update(String pre, Object info) throws IOException, InterruptedException, LibsodiumException {
public Contact update(String pre, Object info) throws IOException, InterruptedException, LibsodiumException {
String path = "/contacts/" + pre;
String method = "PUT";
HttpResponse<String> response = this.client.fetch(path, method, info);
return Utils.fromJson(response.body(), Object.class);
return Utils.fromJson(response.body(), Contact.class);
}
}
}
29 changes: 15 additions & 14 deletions src/test/java/org/cardanofoundation/signify/e2e/ChallengesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.cardanofoundation.signify.cesr.Serder;
import org.cardanofoundation.signify.cesr.util.Utils;

import org.cardanofoundation.signify.generated.keria.model.Challenge;
import org.cardanofoundation.signify.generated.keria.model.Contact;
import org.cardanofoundation.signify.generated.keria.model.OOBI;
import org.cardanofoundation.signify.generated.keria.model.Tier;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -55,10 +57,10 @@ void ChallengeTest() throws Exception {
client2.state();

// Generate challenge words
Contacting.Challenge challenge1_small = client1.challenges().generate(128);
assertEquals(12, challenge1_small.words.size());
Contacting.Challenge challenge1_big = client1.challenges().generate(256);
assertEquals(24, challenge1_big.words.size());
Challenge challenge1_small = client1.challenges().generate(128);
assertEquals(12, challenge1_small.getWords().size());
Challenge challenge1_big = client1.challenges().generate(256);
assertEquals(24, challenge1_big.getWords().size());

// Create two identifiers, one for each client
CreateIdentifierArgs kargs1 = new CreateIdentifierArgs();
Expand Down Expand Up @@ -109,18 +111,18 @@ void ChallengeTest() throws Exception {

// List Client 1 contacts
Contacting.Contacts contacts1 = client1.contacts();
Contacting.Contact[] client1Contacts = contacts1.list();
Contacting.Contact bobContact = findContact(client1Contacts, "bob");
Contact[] client1Contacts = contacts1.list();
Contact bobContact = findContact(client1Contacts, "bob");
assert bobContact != null;
assertEquals("bob", bobContact.getAlias());
assertEquals(((List<Object>) bobContact.get("challenges")).size(), 0);
assertEquals(0, bobContact.getChallenges().size());

// Bob responds to Alice's challenge
client2.challenges().respond("bob", (String) opResponse1.get("i"), challenge1_small.words);
client2.challenges().respond("bob", (String) opResponse1.get("i"), challenge1_small.getWords());
System.out.println("Bob responded to Alice's challenge with signed words");

// Alice verifies Bob's response
Object verifyResult = client1.challenges().verify((String) opResponse2.get("i"), challenge1_small.words);
Object verifyResult = client1.challenges().verify((String) opResponse2.get("i"), challenge1_small.getWords());
Operation op = Operation.fromObject(waitOperation(client1, verifyResult));
System.out.println("Alice verified challenge response");
opResponse = (HashMap<String, Object>) op.getResponse();
Expand All @@ -134,16 +136,15 @@ void ChallengeTest() throws Exception {
bobContact = findContact(client1Contacts, "bob");

assertNotNull(bobContact);
Object challenges = bobContact.get("challenges");
assertInstanceOf(List.class, challenges);
assertTrue((Boolean) Utils.toMap(((List<?>) challenges).getFirst()).get("authenticated"));
List<Challenge> challenges = bobContact.getChallenges();
assertTrue(challenges.getFirst().getAuthenticated());

List<SignifyClient> clientList = new ArrayList<>(Arrays.asList(client1, client2));
assertOperations(clientList);
}

private static Contacting.Contact findContact(Contacting.Contact[] contacts, String alias) {
for (Contacting.Contact contact : contacts) {
private static Contact findContact(Contact[] contacts, String alias) {
for (Contact contact : contacts) {
if (alias.equals(contact.getAlias())) {
return contact;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void multisig() throws Exception {
// First member challenge the other members with a random list of words
// List of words should be passed to the other members out of band
// The other members should do the same challenge/response flow, not shown here for brevity
List<String> words = client1.challenges().generate(128).words;
List<String> words = client1.challenges().generate(128).getWords();
System.out.println("Member1 generated challenge words: " + words);

client2.challenges().respond("member2", aid1.getPrefix(), words);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.cardanofoundation.signify.cesr.Salter;
import org.cardanofoundation.signify.cesr.util.Utils;
import org.cardanofoundation.signify.cesr.exceptions.LibsodiumException;
import org.cardanofoundation.signify.generated.keria.model.Contact;
import org.cardanofoundation.signify.generated.keria.model.Identifier;

import java.io.IOException;
Expand Down Expand Up @@ -286,9 +287,9 @@ public static String[] getOrCreateIdentifier(SignifyClient client, String name,
}

public static String getOrCreateContact(SignifyClient client, String name, String oobi) throws IOException, InterruptedException, LibsodiumException {
List<Contacting.Contact> list = Arrays.asList(client.contacts().list(null, "alias", "^" + name + "$"));
List<Contact> list = Arrays.asList(client.contacts().list(null, "alias", "^" + name + "$"));
if (!list.isEmpty()) {
Contacting.Contact contact = list.getFirst();
Contact contact = list.getFirst();
if (contact.getOobi().equals(oobi)) {
return contact.getId();
}
Expand Down