From 1adfb8c9df0b1e6d22e34b567fea73502813ff52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20K=C3=B6rber?= Date: Thu, 7 Jul 2016 00:08:01 +0200 Subject: [PATCH] Add method to update registration. --- .../org/shredzone/acme4j/Registration.java | 61 +++++++++++++++++++ .../shredzone/acme4j/RegistrationTest.java | 49 ++++++++++++++- .../src/test/resources/json.properties | 7 +++ 3 files changed, 115 insertions(+), 2 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 42d450f0..ccf47a29 100644 --- a/acme4j-client/src/main/java/org/shredzone/acme4j/Registration.java +++ b/acme4j-client/src/main/java/org/shredzone/acme4j/Registration.java @@ -16,9 +16,11 @@ package org.shredzone.acme4j; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URI; +import java.net.URISyntaxException; import java.security.KeyPair; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Map; @@ -85,6 +87,37 @@ public class Registration extends AcmeResource { return Collections.unmodifiableList(contacts); } + /** + * Updates the registration to the current account status. + */ + public void update() throws AcmeException { + LOG.debug("update"); + try (Connection conn = getSession().provider().connect()) { + ClaimBuilder claims = new ClaimBuilder(); + claims.putResource("reg"); + + int rc = conn.sendSignedRequest(getLocation(), claims, getSession()); + if (rc != HttpURLConnection.HTTP_CREATED && rc != HttpURLConnection.HTTP_ACCEPTED) { + conn.throwAcmeException(); + } + + Map json = conn.readJsonResponse(); + unmarshal(json); + + URI location = conn.getLocation(); + if (location != null) { + setLocation(conn.getLocation()); + } + + URI tos = conn.getLink("terms-of-service"); + if (tos != null) { + this.agreement = tos; + } + } catch (IOException ex) { + throw new AcmeNetworkException(ex); + } + } + /** * Authorizes a domain. The domain is associated with this registration. * @@ -231,6 +264,34 @@ public class Registration extends AcmeResource { } } + /** + * Sets registration properties according to the given JSON data. + * + * @param json + * JSON data + */ + @SuppressWarnings("unchecked") + private void unmarshal(Map json) { + if (json.containsKey("agreement")) { + try { + this.agreement = new URI((String) json.get("agreement")); + } catch (ClassCastException | URISyntaxException ex) { + throw new AcmeProtocolException("Illegal agreement URI", ex); + } + } + + if (json.containsKey("contact")) { + contacts.clear(); + for (Object c : (Collection) json.get("contact")) { + try { + contacts.add(new URI((String) c)); + } catch (ClassCastException | URISyntaxException ex) { + throw new AcmeProtocolException("Illegal contact URI", ex); + } + } + } + } + /** * Modifies the registration data of the account. * 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 101bf71d..9e90b5da 100644 --- a/acme4j-client/src/test/java/org/shredzone/acme4j/RegistrationTest.java +++ b/acme4j-client/src/test/java/org/shredzone/acme4j/RegistrationTest.java @@ -46,8 +46,9 @@ import org.shredzone.acme4j.util.TestUtils; */ public class RegistrationTest { - private URI resourceUri = URI.create("http://example.com/acme/resource"); - private URI locationUri = URI.create("http://example.com/acme/registration"); + private URI resourceUri = URI.create("http://example.com/acme/resource"); + private URI locationUri = URI.create("http://example.com/acme/registration"); + private URI agreementUri = URI.create("http://example.com/agreement.pdf"); /** * Test getters. Make sure object cannot be modified. @@ -68,6 +69,50 @@ public class RegistrationTest { } } + /** + * Test that a registration can be updated. + */ + @Test + public void testUpdateRegistration() throws AcmeException, IOException { + TestableConnectionProvider provider = new TestableConnectionProvider() { + @Override + public int sendSignedRequest(URI uri, ClaimBuilder claims, Session session) { + assertThat(uri, is(locationUri)); + assertThat(claims.toString(), sameJSONAs(getJson("updateRegistration"))); + assertThat(session, is(notNullValue())); + return HttpURLConnection.HTTP_ACCEPTED; + } + + @Override + public Map readJsonResponse() { + return getJsonAsMap("updateRegistrationResponse"); + } + + @Override + public URI getLocation() { + return locationUri; + } + + @Override + public URI getLink(String relation) { + switch(relation) { + case "terms-of-service": return agreementUri; + default: return null; + } + } + }; + + Registration registration = new Registration(provider.createSession(), locationUri); + registration.update(); + + assertThat(registration.getLocation(), is(locationUri)); + assertThat(registration.getAgreement(), is(agreementUri)); + assertThat(registration.getContacts(), hasSize(1)); + assertThat(registration.getContacts().get(0), is(URI.create("mailto:foo2@example.com"))); + + provider.close(); + } + /** * Test that a new {@link Authorization} can be created. */ diff --git a/acme4j-client/src/test/resources/json.properties b/acme4j-client/src/test/resources/json.properties index 2818a6dc..b4b760cf 100644 --- a/acme4j-client/src/test/resources/json.properties +++ b/acme4j-client/src/test/resources/json.properties @@ -27,6 +27,13 @@ recoverRegistration = \ "base":"https://example.com/acme/some-location",\ "contact":["mailto:foo@example.com"]} +updateRegistration = \ + {"resource":"reg"} + +updateRegistrationResponse = \ + {"agreement":"http://example.com/agreement.pdf",\ + "contact":["mailto:foo2@example.com"]} + newAuthorizationRequest = \ {"resource":"new-authz",\ "identifier":{"type":"dns","value":"example.org"}}