From 5fc97fab34b9bd061222af5f37e594af9ef368a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20K=C3=B6rber?= Date: Wed, 16 Dec 2015 00:53:31 +0100 Subject: [PATCH] Evaluate terms-of-service header. Agreement property is now type URI. --- .../java/org/shredzone/acme4j/Registration.java | 14 +++++++------- .../acme4j/impl/AbstractAcmeClient.java | 16 ++++++++++++---- .../org/shredzone/acme4j/RegistrationTest.java | 6 +++--- .../java/org/shredzone/acme4j/ClientTest.java | 13 ++++++++----- 4 files changed, 30 insertions(+), 19 deletions(-) diff --git a/acme4j-client/src/main/java/org/shredzone/acme4j/Registration.java b/acme4j-client/src/main/java/org/shredzone/acme4j/Registration.java index 0a63c1ce..a80438da 100644 --- a/acme4j-client/src/main/java/org/shredzone/acme4j/Registration.java +++ b/acme4j-client/src/main/java/org/shredzone/acme4j/Registration.java @@ -24,22 +24,22 @@ import java.util.List; */ public class Registration { - private String agreementUrl; private List contacts = new ArrayList<>(); + private URI agreement; private URI location; /** - * Returns the URL of the agreement document the user is required to accept. + * Returns the URI of the agreement document the user is required to accept. */ - public String getAgreementUrl() { - return agreementUrl; + public URI getAgreement() { + return agreement; } /** - * Sets the URL of the agreement document the user is required to accept. + * Sets the URI of the agreement document the user is required to accept. */ - public void setAgreementUrl(String agreementUrl) { - this.agreementUrl = agreementUrl; + public void setAgreement(URI agreement) { + this.agreement = agreement; } /** diff --git a/acme4j-client/src/main/java/org/shredzone/acme4j/impl/AbstractAcmeClient.java b/acme4j-client/src/main/java/org/shredzone/acme4j/impl/AbstractAcmeClient.java index cf20317b..a6285db6 100644 --- a/acme4j-client/src/main/java/org/shredzone/acme4j/impl/AbstractAcmeClient.java +++ b/acme4j-client/src/main/java/org/shredzone/acme4j/impl/AbstractAcmeClient.java @@ -81,8 +81,6 @@ public abstract class AbstractAcmeClient implements AcmeClient { if (!registration.getContacts().isEmpty()) { claims.put("contact", registration.getContacts()); } - if (registration.getAgreementUrl() != null) { - claims.put("agreement", registration.getAgreementUrl()); int rc = conn.sendSignedRequest(resourceUri(Resource.NEW_REG), claims, session, account); if (rc != HttpURLConnection.HTTP_CREATED && rc != HttpURLConnection.HTTP_CONFLICT) { @@ -94,6 +92,11 @@ public abstract class AbstractAcmeClient implements AcmeClient { registration.setLocation(location); } + URI tos = conn.getLink("terms-of-service"); + if (tos != null) { + registration.setAgreement(tos); + } + if (rc == HttpURLConnection.HTTP_CONFLICT) { throw new AcmeConflictException("Account is already registered", location); } @@ -113,8 +116,8 @@ public abstract class AbstractAcmeClient implements AcmeClient { if (!registration.getContacts().isEmpty()) { claims.put("contact", registration.getContacts()); } - if (registration.getAgreementUrl() != null) { - claims.put("agreement", registration.getAgreementUrl()); + if (registration.getAgreement() != null) { + claims.put("agreement", registration.getAgreement()); } int rc = conn.sendSignedRequest(registration.getLocation(), claims, session, account); @@ -123,6 +126,11 @@ public abstract class AbstractAcmeClient implements AcmeClient { } registration.setLocation(conn.getLocation()); + + URI tos = conn.getLink("terms-of-service"); + if (tos != null) { + registration.setAgreement(tos); + } } } diff --git a/acme4j-client/src/test/java/org/shredzone/acme4j/RegistrationTest.java b/acme4j-client/src/test/java/org/shredzone/acme4j/RegistrationTest.java index 2d81104d..1358ede6 100644 --- a/acme4j-client/src/test/java/org/shredzone/acme4j/RegistrationTest.java +++ b/acme4j-client/src/test/java/org/shredzone/acme4j/RegistrationTest.java @@ -35,16 +35,16 @@ public class RegistrationTest { public void testGetterAndSetter() throws URISyntaxException { Registration registration = new Registration(); - assertThat(registration.getAgreementUrl(), is(nullValue())); + assertThat(registration.getAgreement(), is(nullValue())); assertThat(registration.getLocation(), is(nullValue())); assertThat(registration.getContacts(), is(empty())); - registration.setAgreementUrl("http://example.com/agreement.pdf"); + registration.setAgreement(new URI("http://example.com/agreement.pdf")); registration.setLocation(new URI("http://example.com/acme/12345")); registration.getContacts().add("mailto:foo@example.com"); registration.getContacts().add("mailto:bar@example.com"); - assertThat(registration.getAgreementUrl(), is("http://example.com/agreement.pdf")); + assertThat(registration.getAgreement(), is(new URI("http://example.com/agreement.pdf"))); assertThat(registration.getLocation(), is(new URI("http://example.com/acme/12345"))); assertThat(registration.getContacts(), contains("mailto:foo@example.com", "mailto:bar@example.com")); } diff --git a/acme4j-example/src/main/java/org/shredzone/acme4j/ClientTest.java b/acme4j-example/src/main/java/org/shredzone/acme4j/ClientTest.java index 52ecb98c..ff8c8670 100644 --- a/acme4j-example/src/main/java/org/shredzone/acme4j/ClientTest.java +++ b/acme4j-example/src/main/java/org/shredzone/acme4j/ClientTest.java @@ -28,6 +28,7 @@ import javax.swing.JOptionPane; import org.shredzone.acme4j.challenge.Challenge; import org.shredzone.acme4j.challenge.HttpChallenge; +import org.shredzone.acme4j.exception.AcmeConflictException; import org.shredzone.acme4j.exception.AcmeException; import org.shredzone.acme4j.util.CSRBuilder; import org.shredzone.acme4j.util.CertificateUtils; @@ -60,10 +61,10 @@ public class ClientTest { * * @param domains * Domains to get a common certificate for - * @param agreementUrl - * Agreement URL to be used for creating an account + * @param agreement + * Agreement URI to be used for creating an account */ - public void fetchCertificate(Collection domains, String agreementUrl) + public void fetchCertificate(Collection domains, URI agreement) throws IOException, AcmeException { // Load or create a key pair for the user's account KeyPair userKeyPair; @@ -87,10 +88,12 @@ public class ClientTest { // Register a new user Registration reg = new Registration(); - reg.setAgreementUrl(agreementUrl); + reg.setAgreement(agreement); try { client.newRegistration(account, reg); LOG.info("Registered a new user, URI: " + reg.getLocation()); + } catch (AcmeConflictException ex) { + LOG.info("Account does already exist, URI: " + reg.getLocation()); } catch (AcmeException ex) { LOG.warn("Registration failed", ex); @@ -198,7 +201,7 @@ public class ClientTest { Collection domains = Arrays.asList(args); try { ClientTest ct = new ClientTest(); - ct.fetchCertificate(domains, AGREEMENT_URL); + ct.fetchCertificate(domains, new URI(AGREEMENT_URL)); } catch (Exception ex) { LOG.error("Failed to get a certificate for domains " + domains, ex); }