From 3ee7a06e6f36ab1eb804976033f7eff2ecbe9a89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20K=C3=B6rber?= Date: Sun, 13 Dec 2015 19:18:39 +0100 Subject: [PATCH] Extend findCombination behavior. The mandant passes in all challenge types it supports, and the method returns the shortest combination required for validation. --- .../org/shredzone/acme4j/Authorization.java | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/acme4j-client/src/main/java/org/shredzone/acme4j/Authorization.java b/acme4j-client/src/main/java/org/shredzone/acme4j/Authorization.java index 87373b0f..69d155e7 100644 --- a/acme4j-client/src/main/java/org/shredzone/acme4j/Authorization.java +++ b/acme4j-client/src/main/java/org/shredzone/acme4j/Authorization.java @@ -124,30 +124,39 @@ public class Authorization { /** * Finds a combination of {@link Challenge} types that the client supports. The client - * has to respond to all of the {@link Challenge}s returned. + * has to respond to all of the {@link Challenge}s returned. However, this + * method attempts to find the combination with the smallest number of + * {@link Challenge}s. * * @param types * Challenge name or names (e.g. "http-01"), in no particular order. + * Basically this is a collection of all challenge types supported by your + * implementation. * @return Matching {@link Challenge} combination, or {@code null} if the ACME server - * does not support this challenge combination. The challenges are returned - * in no particular order. + * does not support any of your challenges. The challenges are returned in no + * particular order. The result may be a subset of the types you have + * provided, if fewer challenges are actually required for a successful + * validation. */ public Collection findCombination(String... types) { - Collection reference = Arrays.asList(types); + Collection available = Arrays.asList(types); + Collection combinationTypes = new ArrayList<>(); + + Collection result = null; for (List combination : combinations) { - Collection combinationTypes = new ArrayList<>(); + combinationTypes.clear(); for (Challenge c : combination) { combinationTypes.add(c.getType()); } - if (reference.size() == combinationTypes.size() - && reference.containsAll(combinationTypes)) { - return combination; + if (available.containsAll(combinationTypes) && + (result == null || result.size() > combination.size())) { + result = combination; } } - return null; + return result; } }