mirror of https://github.com/shred/acme4j
Pass serverUri to provider's connect() method
parent
bb35678c2d
commit
01398e1bbc
|
@ -113,7 +113,7 @@ public class Account extends AcmeJsonResource {
|
||||||
@Override
|
@Override
|
||||||
public void update() throws AcmeException {
|
public void update() throws AcmeException {
|
||||||
LOG.debug("update Account");
|
LOG.debug("update Account");
|
||||||
try (Connection conn = connect()) {
|
try (Connection conn = getSession().connect()) {
|
||||||
conn.sendSignedRequest(getLocation(), new JSONBuilder(), getLogin());
|
conn.sendSignedRequest(getLocation(), new JSONBuilder(), getLogin());
|
||||||
JSON json = conn.readJsonResponse();
|
JSON json = conn.readJsonResponse();
|
||||||
if (json != null) {
|
if (json != null) {
|
||||||
|
@ -181,7 +181,7 @@ public class Account extends AcmeJsonResource {
|
||||||
URL newAuthzUrl = getSession().resourceUrl(Resource.NEW_AUTHZ);
|
URL newAuthzUrl = getSession().resourceUrl(Resource.NEW_AUTHZ);
|
||||||
|
|
||||||
LOG.debug("preAuthorize {}", identifier);
|
LOG.debug("preAuthorize {}", identifier);
|
||||||
try (Connection conn = connect()) {
|
try (Connection conn = getSession().connect()) {
|
||||||
JSONBuilder claims = new JSONBuilder();
|
JSONBuilder claims = new JSONBuilder();
|
||||||
claims.put("identifier", identifier.toMap());
|
claims.put("identifier", identifier.toMap());
|
||||||
|
|
||||||
|
@ -219,7 +219,7 @@ public class Account extends AcmeJsonResource {
|
||||||
|
|
||||||
LOG.debug("key-change");
|
LOG.debug("key-change");
|
||||||
|
|
||||||
try (Connection conn = connect()) {
|
try (Connection conn = getSession().connect()) {
|
||||||
URL keyChangeUrl = getSession().resourceUrl(Resource.KEY_CHANGE);
|
URL keyChangeUrl = getSession().resourceUrl(Resource.KEY_CHANGE);
|
||||||
PublicJsonWebKey newKeyJwk = PublicJsonWebKey.Factory.newPublicJwk(newKeyPair.getPublic());
|
PublicJsonWebKey newKeyJwk = PublicJsonWebKey.Factory.newPublicJwk(newKeyPair.getPublic());
|
||||||
|
|
||||||
|
@ -256,7 +256,7 @@ public class Account extends AcmeJsonResource {
|
||||||
*/
|
*/
|
||||||
public void deactivate() throws AcmeException {
|
public void deactivate() throws AcmeException {
|
||||||
LOG.debug("deactivate");
|
LOG.debug("deactivate");
|
||||||
try (Connection conn = connect()) {
|
try (Connection conn = getSession().connect()) {
|
||||||
JSONBuilder claims = new JSONBuilder();
|
JSONBuilder claims = new JSONBuilder();
|
||||||
claims.put(KEY_STATUS, "deactivated");
|
claims.put(KEY_STATUS, "deactivated");
|
||||||
|
|
||||||
|
@ -344,7 +344,7 @@ public class Account extends AcmeJsonResource {
|
||||||
*/
|
*/
|
||||||
public void commit() throws AcmeException {
|
public void commit() throws AcmeException {
|
||||||
LOG.debug("modify/commit");
|
LOG.debug("modify/commit");
|
||||||
try (Connection conn = connect()) {
|
try (Connection conn = getSession().connect()) {
|
||||||
JSONBuilder claims = new JSONBuilder();
|
JSONBuilder claims = new JSONBuilder();
|
||||||
if (!editContacts.isEmpty()) {
|
if (!editContacts.isEmpty()) {
|
||||||
claims.put(KEY_CONTACT, editContacts);
|
claims.put(KEY_CONTACT, editContacts);
|
||||||
|
|
|
@ -198,7 +198,7 @@ public class AccountBuilder {
|
||||||
|
|
||||||
LOG.debug("create");
|
LOG.debug("create");
|
||||||
|
|
||||||
try (Connection conn = session.provider().connect()) {
|
try (Connection conn = session.connect()) {
|
||||||
URL resourceUrl = session.resourceUrl(Resource.NEW_ACCOUNT);
|
URL resourceUrl = session.resourceUrl(Resource.NEW_ACCOUNT);
|
||||||
|
|
||||||
JSONBuilder claims = new JSONBuilder();
|
JSONBuilder claims = new JSONBuilder();
|
||||||
|
|
|
@ -115,7 +115,7 @@ public abstract class AcmeJsonResource extends AcmeResource {
|
||||||
public void update() throws AcmeException {
|
public void update() throws AcmeException {
|
||||||
String resourceType = getClass().getSimpleName();
|
String resourceType = getClass().getSimpleName();
|
||||||
LOG.debug("update {}", resourceType);
|
LOG.debug("update {}", resourceType);
|
||||||
try (Connection conn = connect()) {
|
try (Connection conn = getSession().connect()) {
|
||||||
conn.sendSignedPostAsGetRequest(getLocation(), getLogin());
|
conn.sendSignedPostAsGetRequest(getLocation(), getLogin());
|
||||||
JSON json = conn.readJsonResponse();
|
JSON json = conn.readJsonResponse();
|
||||||
if (json != null) {
|
if (json != null) {
|
||||||
|
|
|
@ -19,8 +19,6 @@ import java.util.Objects;
|
||||||
|
|
||||||
import javax.annotation.ParametersAreNonnullByDefault;
|
import javax.annotation.ParametersAreNonnullByDefault;
|
||||||
|
|
||||||
import org.shredzone.acme4j.connector.Connection;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A generic ACME resource.
|
* A generic ACME resource.
|
||||||
*/
|
*/
|
||||||
|
@ -61,13 +59,6 @@ public abstract class AcmeResource implements Serializable {
|
||||||
return getLogin().getSession();
|
return getLogin().getSession();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Opens a {@link Connection} to the provider.
|
|
||||||
*/
|
|
||||||
protected Connection connect() {
|
|
||||||
return getSession().provider().connect();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Rebinds this resource to a {@link Login}.
|
* Rebinds this resource to a {@link Login}.
|
||||||
* <p>
|
* <p>
|
||||||
|
|
|
@ -144,7 +144,7 @@ public class Authorization extends AcmeJsonResource {
|
||||||
*/
|
*/
|
||||||
public void deactivate() throws AcmeException {
|
public void deactivate() throws AcmeException {
|
||||||
LOG.debug("deactivate");
|
LOG.debug("deactivate");
|
||||||
try (Connection conn = connect()) {
|
try (Connection conn = getSession().connect()) {
|
||||||
JSONBuilder claims = new JSONBuilder();
|
JSONBuilder claims = new JSONBuilder();
|
||||||
claims.put("status", "deactivated");
|
claims.put("status", "deactivated");
|
||||||
|
|
||||||
|
|
|
@ -70,7 +70,7 @@ public class Certificate extends AcmeResource {
|
||||||
public void download() throws AcmeException {
|
public void download() throws AcmeException {
|
||||||
if (certChain == null) {
|
if (certChain == null) {
|
||||||
LOG.debug("download");
|
LOG.debug("download");
|
||||||
try (Connection conn = connect()) {
|
try (Connection conn = getSession().connect()) {
|
||||||
conn.sendCertificateRequest(getLocation(), getLogin());
|
conn.sendCertificateRequest(getLocation(), getLogin());
|
||||||
alternates = new ArrayList<>(conn.getLinks("alternate"));
|
alternates = new ArrayList<>(conn.getLinks("alternate"));
|
||||||
certChain = new ArrayList<>(conn.readCertificates());
|
certChain = new ArrayList<>(conn.readCertificates());
|
||||||
|
@ -150,7 +150,7 @@ public class Certificate extends AcmeResource {
|
||||||
LOG.debug("revoke");
|
LOG.debug("revoke");
|
||||||
URL resUrl = getSession().resourceUrl(Resource.REVOKE_CERT);
|
URL resUrl = getSession().resourceUrl(Resource.REVOKE_CERT);
|
||||||
|
|
||||||
try (Connection conn = connect()) {
|
try (Connection conn = getSession().connect()) {
|
||||||
JSONBuilder claims = new JSONBuilder();
|
JSONBuilder claims = new JSONBuilder();
|
||||||
claims.putBase64("certificate", getCertificate().getEncoded());
|
claims.putBase64("certificate", getCertificate().getEncoded());
|
||||||
if (reason != null) {
|
if (reason != null) {
|
||||||
|
@ -187,7 +187,7 @@ public class Certificate extends AcmeResource {
|
||||||
throw new AcmeException("Server does not allow certificate revocation");
|
throw new AcmeException("Server does not allow certificate revocation");
|
||||||
}
|
}
|
||||||
|
|
||||||
try (Connection conn = session.provider().connect()) {
|
try (Connection conn = session.connect()) {
|
||||||
JSONBuilder claims = new JSONBuilder();
|
JSONBuilder claims = new JSONBuilder();
|
||||||
claims.putBase64("certificate", cert.getEncoded());
|
claims.putBase64("certificate", cert.getEncoded());
|
||||||
if (reason != null) {
|
if (reason != null) {
|
||||||
|
|
|
@ -162,7 +162,7 @@ public class Order extends AcmeJsonResource {
|
||||||
*/
|
*/
|
||||||
public void execute(byte[] csr) throws AcmeException {
|
public void execute(byte[] csr) throws AcmeException {
|
||||||
LOG.debug("finalize");
|
LOG.debug("finalize");
|
||||||
try (Connection conn = connect()) {
|
try (Connection conn = getSession().connect()) {
|
||||||
JSONBuilder claims = new JSONBuilder();
|
JSONBuilder claims = new JSONBuilder();
|
||||||
claims.putBase64("csr", csr);
|
claims.putBase64("csr", csr);
|
||||||
|
|
||||||
|
@ -235,7 +235,7 @@ public class Order extends AcmeJsonResource {
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG.debug("cancel");
|
LOG.debug("cancel");
|
||||||
try (Connection conn = connect()) {
|
try (Connection conn = getSession().connect()) {
|
||||||
JSONBuilder claims = new JSONBuilder();
|
JSONBuilder claims = new JSONBuilder();
|
||||||
claims.put("status", "canceled");
|
claims.put("status", "canceled");
|
||||||
|
|
||||||
|
|
|
@ -245,7 +245,7 @@ public class OrderBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG.debug("create");
|
LOG.debug("create");
|
||||||
try (Connection conn = session.provider().connect()) {
|
try (Connection conn = session.connect()) {
|
||||||
JSONBuilder claims = new JSONBuilder();
|
JSONBuilder claims = new JSONBuilder();
|
||||||
claims.array("identifiers", identifierSet.stream().map(Identifier::toMap).collect(toList()));
|
claims.array("identifiers", identifierSet.stream().map(Identifier::toMap).collect(toList()));
|
||||||
|
|
||||||
|
|
|
@ -32,6 +32,7 @@ import javax.annotation.Nullable;
|
||||||
import javax.annotation.ParametersAreNonnullByDefault;
|
import javax.annotation.ParametersAreNonnullByDefault;
|
||||||
import javax.annotation.concurrent.ThreadSafe;
|
import javax.annotation.concurrent.ThreadSafe;
|
||||||
|
|
||||||
|
import org.shredzone.acme4j.connector.Connection;
|
||||||
import org.shredzone.acme4j.connector.Resource;
|
import org.shredzone.acme4j.connector.Resource;
|
||||||
import org.shredzone.acme4j.exception.AcmeException;
|
import org.shredzone.acme4j.exception.AcmeException;
|
||||||
import org.shredzone.acme4j.provider.AcmeProvider;
|
import org.shredzone.acme4j.provider.AcmeProvider;
|
||||||
|
@ -172,6 +173,15 @@ public class Session {
|
||||||
return provider;
|
return provider;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a new {@link Connection} to the ACME server.
|
||||||
|
*
|
||||||
|
* @return {@link Connection}
|
||||||
|
*/
|
||||||
|
public Connection connect() {
|
||||||
|
return provider.connect(getServerUri());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the {@link URL} of the given {@link Resource}. This may involve connecting to
|
* Gets the {@link URL} of the given {@link Resource}. This may involve connecting to
|
||||||
* the server and getting a directory. The result is cached.
|
* the server and getting a directory. The result is cached.
|
||||||
|
|
|
@ -148,7 +148,7 @@ public class Challenge extends AcmeJsonResource {
|
||||||
*/
|
*/
|
||||||
public void trigger() throws AcmeException {
|
public void trigger() throws AcmeException {
|
||||||
LOG.debug("trigger");
|
LOG.debug("trigger");
|
||||||
try (Connection conn = connect()) {
|
try (Connection conn = getSession().connect()) {
|
||||||
JSONBuilder claims = new JSONBuilder();
|
JSONBuilder claims = new JSONBuilder();
|
||||||
prepareResponse(claims);
|
prepareResponse(claims);
|
||||||
|
|
||||||
|
|
|
@ -144,7 +144,7 @@ public class ResourceIterator<T extends AcmeResource> implements Iterator<T> {
|
||||||
*/
|
*/
|
||||||
private void readAndQueue() throws AcmeException {
|
private void readAndQueue() throws AcmeException {
|
||||||
Session session = login.getSession();
|
Session session = login.getSession();
|
||||||
try (Connection conn = session.provider().connect()) {
|
try (Connection conn = session.connect()) {
|
||||||
conn.sendSignedPostAsGetRequest(nextUrl, login);
|
conn.sendSignedPostAsGetRequest(nextUrl, login);
|
||||||
|
|
||||||
JSON json = conn.readJsonResponse();
|
JSON json = conn.readJsonResponse();
|
||||||
|
|
|
@ -48,13 +48,13 @@ public abstract class AbstractAcmeProvider implements AcmeProvider {
|
||||||
private static final Map<String, BiFunction<Login, JSON, Challenge>> CHALLENGES = challengeMap();
|
private static final Map<String, BiFunction<Login, JSON, Challenge>> CHALLENGES = challengeMap();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Connection connect() {
|
public Connection connect(URI serverUri) {
|
||||||
return new DefaultConnection(createHttpConnector());
|
return new DefaultConnection(createHttpConnector());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JSON directory(Session session, URI serverUri) throws AcmeException {
|
public JSON directory(Session session, URI serverUri) throws AcmeException {
|
||||||
try (Connection conn = connect()) {
|
try (Connection conn = connect(serverUri)) {
|
||||||
conn.sendRequest(resolve(serverUri), session);
|
conn.sendRequest(resolve(serverUri), session);
|
||||||
|
|
||||||
// use nonce header if there is one, saves a HEAD request...
|
// use nonce header if there is one, saves a HEAD request...
|
||||||
|
|
|
@ -60,9 +60,11 @@ public interface AcmeProvider {
|
||||||
/**
|
/**
|
||||||
* Creates a {@link Connection} for communication with the ACME server.
|
* Creates a {@link Connection} for communication with the ACME server.
|
||||||
*
|
*
|
||||||
|
* @param serverUri
|
||||||
|
* Server {@link URI}
|
||||||
* @return {@link Connection} that was generated
|
* @return {@link Connection} that was generated
|
||||||
*/
|
*/
|
||||||
Connection connect();
|
Connection connect(URI serverUri);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the provider's directory. The structure must contain resource URLs, and may
|
* Returns the provider's directory. The structure must contain resource URLs, and may
|
||||||
|
|
|
@ -67,7 +67,7 @@ public class LetsEncryptAcmeProvider extends AbstractAcmeProvider {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
public Connection connect() {
|
public Connection connect(URI serverUri) {
|
||||||
return new org.shredzone.acme4j.connector.PreDraft15Connection(createHttpConnector());
|
return new org.shredzone.acme4j.connector.PreDraft15Connection(createHttpConnector());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -39,7 +39,6 @@ import org.shredzone.acme4j.challenge.Http01Challenge;
|
||||||
import org.shredzone.acme4j.connector.Resource;
|
import org.shredzone.acme4j.connector.Resource;
|
||||||
import org.shredzone.acme4j.exception.AcmeException;
|
import org.shredzone.acme4j.exception.AcmeException;
|
||||||
import org.shredzone.acme4j.exception.AcmeServerException;
|
import org.shredzone.acme4j.exception.AcmeServerException;
|
||||||
import org.shredzone.acme4j.provider.AcmeProvider;
|
|
||||||
import org.shredzone.acme4j.provider.TestableConnectionProvider;
|
import org.shredzone.acme4j.provider.TestableConnectionProvider;
|
||||||
import org.shredzone.acme4j.toolbox.JSON;
|
import org.shredzone.acme4j.toolbox.JSON;
|
||||||
import org.shredzone.acme4j.toolbox.JSONBuilder;
|
import org.shredzone.acme4j.toolbox.JSONBuilder;
|
||||||
|
@ -343,13 +342,7 @@ public class AccountTest {
|
||||||
|
|
||||||
provider.putTestResource(Resource.KEY_CHANGE, locationUrl);
|
provider.putTestResource(Resource.KEY_CHANGE, locationUrl);
|
||||||
|
|
||||||
Session session = new Session(new URI(TestUtils.ACME_SERVER_URI)) {
|
Session session = TestUtils.session(provider);
|
||||||
@Override
|
|
||||||
public AcmeProvider provider() {
|
|
||||||
return provider;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
Login login = new Login(locationUrl, oldKeyPair, session);
|
Login login = new Login(locationUrl, oldKeyPair, session);
|
||||||
|
|
||||||
assertThat(login.getKeyPair(), is(sameInstance(oldKeyPair)));
|
assertThat(login.getKeyPair(), is(sameInstance(oldKeyPair)));
|
||||||
|
|
|
@ -77,7 +77,7 @@ public class SessionProviderTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Connection connect() {
|
public Connection connect(URI serverUri) {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,7 +105,7 @@ public class SessionProviderTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Connection connect() {
|
public Connection connect(URI serverUri) {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -50,6 +50,8 @@ public class AbstractAcmeProviderTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testConnect() {
|
public void testConnect() {
|
||||||
|
final URI testServerUri = URI.create("http://example.com/acme");
|
||||||
|
|
||||||
final AtomicBoolean invoked = new AtomicBoolean();
|
final AtomicBoolean invoked = new AtomicBoolean();
|
||||||
|
|
||||||
AbstractAcmeProvider provider = new AbstractAcmeProvider() {
|
AbstractAcmeProvider provider = new AbstractAcmeProvider() {
|
||||||
|
@ -70,7 +72,7 @@ public class AbstractAcmeProviderTest {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Connection connection = provider.connect();
|
Connection connection = provider.connect(testServerUri);
|
||||||
assertThat(connection, not(nullValue()));
|
assertThat(connection, not(nullValue()));
|
||||||
assertThat(connection, instanceOf(DefaultConnection.class));
|
assertThat(connection, instanceOf(DefaultConnection.class));
|
||||||
assertThat(invoked.get(), is(true));
|
assertThat(invoked.get(), is(true));
|
||||||
|
@ -90,7 +92,8 @@ public class AbstractAcmeProviderTest {
|
||||||
|
|
||||||
AbstractAcmeProvider provider = new AbstractAcmeProvider() {
|
AbstractAcmeProvider provider = new AbstractAcmeProvider() {
|
||||||
@Override
|
@Override
|
||||||
public Connection connect() {
|
public Connection connect(URI serverUri) {
|
||||||
|
assertThat(serverUri, is(testServerUri));
|
||||||
return connection;
|
return connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -119,7 +119,7 @@ public class TestableConnectionProvider extends DummyConnection implements AcmeP
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Connection connect() {
|
public Connection connect(URI serverUri) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -75,7 +75,7 @@ public class LetsEncryptAcmeProviderTest {
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
public void testConnect() {
|
public void testConnect() {
|
||||||
LetsEncryptAcmeProvider provider = new LetsEncryptAcmeProvider();
|
LetsEncryptAcmeProvider provider = new LetsEncryptAcmeProvider();
|
||||||
Connection connection = provider.connect();
|
Connection connection = provider.connect(URI.create("acme://letsencrypt.org"));
|
||||||
assertThat(connection, is(instanceOf(org.shredzone.acme4j.connector.PreDraft15Connection.class)));
|
assertThat(connection, is(instanceOf(org.shredzone.acme4j.connector.PreDraft15Connection.class)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -54,6 +54,7 @@ import org.jose4j.keys.HmacKey;
|
||||||
import org.shredzone.acme4j.Login;
|
import org.shredzone.acme4j.Login;
|
||||||
import org.shredzone.acme4j.Problem;
|
import org.shredzone.acme4j.Problem;
|
||||||
import org.shredzone.acme4j.Session;
|
import org.shredzone.acme4j.Session;
|
||||||
|
import org.shredzone.acme4j.connector.Connection;
|
||||||
import org.shredzone.acme4j.provider.AcmeProvider;
|
import org.shredzone.acme4j.provider.AcmeProvider;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -161,6 +162,11 @@ public final class TestUtils {
|
||||||
public AcmeProvider provider() {
|
public AcmeProvider provider() {
|
||||||
return provider;
|
return provider;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Connection connect() {
|
||||||
|
return provider.connect(getServerUri());
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue