From dcdf2408045f883d41e545e70dee81999c352a4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20K=C3=B6rber?= Date: Sun, 30 Jul 2017 15:57:44 +0200 Subject: [PATCH] Add support for only-return-existing flag --- .../org/shredzone/acme4j/AccountBuilder.java | 16 +++++++ .../shredzone/acme4j/AccountBuilderTest.java | 45 +++++++++++++++++++ .../json/newAccountOnlyExisting.json | 3 ++ src/site/markdown/usage/register.md | 11 +++++ 4 files changed, 75 insertions(+) create mode 100644 acme4j-client/src/test/resources/json/newAccountOnlyExisting.json diff --git a/acme4j-client/src/main/java/org/shredzone/acme4j/AccountBuilder.java b/acme4j-client/src/main/java/org/shredzone/acme4j/AccountBuilder.java index 0ef66e80..4a3d630e 100644 --- a/acme4j-client/src/main/java/org/shredzone/acme4j/AccountBuilder.java +++ b/acme4j-client/src/main/java/org/shredzone/acme4j/AccountBuilder.java @@ -41,6 +41,7 @@ public class AccountBuilder { private List contacts = new ArrayList<>(); private Boolean termsOfServiceAgreed; + private Boolean onlyExisting; private String keyIdentifier; /** @@ -81,6 +82,18 @@ public class AccountBuilder { return this; } + /** + * Signals that only an existing account should be returned. The server will not + * create a new account if the key is not known. This is useful if you only have your + * account's key pair available, but not your account's location URL. + * + * @return itself + */ + public AccountBuilder onlyExisting() { + this.onlyExisting = true; + return this; + } + /** * Sets a Key Identifier provided by the CA. Use this if your CA requires an * individual account identification, e.g. your customer number. @@ -125,6 +138,9 @@ public class AccountBuilder { claims.put("external-account-binding", createExternalAccountBinding(keyIdentifier, session.getKeyPair(), resourceUrl)); } + if (onlyExisting != null) { + claims.put("only-return-existing", onlyExisting); + } conn.sendSignedRequest(resourceUrl, claims, session, true); conn.accept(HttpURLConnection.HTTP_OK, HttpURLConnection.HTTP_CREATED); diff --git a/acme4j-client/src/test/java/org/shredzone/acme4j/AccountBuilderTest.java b/acme4j-client/src/test/java/org/shredzone/acme4j/AccountBuilderTest.java index 30bb9d95..1de6fdac 100644 --- a/acme4j-client/src/test/java/org/shredzone/acme4j/AccountBuilderTest.java +++ b/acme4j-client/src/test/java/org/shredzone/acme4j/AccountBuilderTest.java @@ -191,4 +191,49 @@ public class AccountBuilderTest { provider.close(); } + /** + * Test if an existing account is properly returned. + */ + @Test + public void testOnlyExistingRegistration() throws Exception { + TestableConnectionProvider provider = new TestableConnectionProvider() { + @Override + public void sendSignedRequest(URL url, JSONBuilder claims, Session session, boolean enforceJwk) { + assertThat(session, is(notNullValue())); + assertThat(url, is(resourceUrl)); + assertThat(claims.toString(), sameJSONAs(getJSON("newAccountOnlyExisting").toString())); + assertThat(enforceJwk, is(true)); + } + + @Override + public int accept(int... httpStatus) throws AcmeException { + assertThat(httpStatus, isIntArrayContainingInAnyOrder(HttpURLConnection.HTTP_OK, HttpURLConnection.HTTP_CREATED)); + return HttpURLConnection.HTTP_OK; + } + + @Override + public URL getLocation() { + return locationUrl; + } + + @Override + public JSON readJsonResponse() { + return getJSON("newAccountResponse"); + } + }; + + provider.putTestResource(Resource.NEW_ACCOUNT, resourceUrl); + + AccountBuilder builder = new AccountBuilder(); + builder.onlyExisting(); + + Session session = provider.createSession(); + Account account = builder.create(session); + + assertThat(account.getLocation(), is(locationUrl)); + assertThat(session.getKeyIdentifier(), is(locationUrl.toString())); + + provider.close(); + } + } diff --git a/acme4j-client/src/test/resources/json/newAccountOnlyExisting.json b/acme4j-client/src/test/resources/json/newAccountOnlyExisting.json new file mode 100644 index 00000000..5c1cb6f4 --- /dev/null +++ b/acme4j-client/src/test/resources/json/newAccountOnlyExisting.json @@ -0,0 +1,3 @@ +{ + "only-return-existing": true +} diff --git a/src/site/markdown/usage/register.md b/src/site/markdown/usage/register.md index 06feb3a0..6a2490c3 100644 --- a/src/site/markdown/usage/register.md +++ b/src/site/markdown/usage/register.md @@ -14,6 +14,17 @@ Account account = builder.create(session); URL accountLocationUrl = account.getLocation(); ``` +## Find out your account's location URL + +You can also use the `AccountBuilder` to find out the location URL of your existing account: + +```java +Account account = new AccountBuilder().onlyExisting().create(session); +URL accountLocationUrl = account.getLocation(); +``` + +If you do not have an account yet, an exception is raised instead, and no new account is created. + ## Update your Account At some point, you may want to update your account. For example your contact address might have changed. To do so, invoke `Account.modify()`, perform the changes, and invoke `commit()` to make them permanent.