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
|
||||
public void update() throws AcmeException {
|
||||
LOG.debug("update Account");
|
||||
try (Connection conn = connect()) {
|
||||
try (Connection conn = getSession().connect()) {
|
||||
conn.sendSignedRequest(getLocation(), new JSONBuilder(), getLogin());
|
||||
JSON json = conn.readJsonResponse();
|
||||
if (json != null) {
|
||||
|
@ -181,7 +181,7 @@ public class Account extends AcmeJsonResource {
|
|||
URL newAuthzUrl = getSession().resourceUrl(Resource.NEW_AUTHZ);
|
||||
|
||||
LOG.debug("preAuthorize {}", identifier);
|
||||
try (Connection conn = connect()) {
|
||||
try (Connection conn = getSession().connect()) {
|
||||
JSONBuilder claims = new JSONBuilder();
|
||||
claims.put("identifier", identifier.toMap());
|
||||
|
||||
|
@ -219,7 +219,7 @@ public class Account extends AcmeJsonResource {
|
|||
|
||||
LOG.debug("key-change");
|
||||
|
||||
try (Connection conn = connect()) {
|
||||
try (Connection conn = getSession().connect()) {
|
||||
URL keyChangeUrl = getSession().resourceUrl(Resource.KEY_CHANGE);
|
||||
PublicJsonWebKey newKeyJwk = PublicJsonWebKey.Factory.newPublicJwk(newKeyPair.getPublic());
|
||||
|
||||
|
@ -256,7 +256,7 @@ public class Account extends AcmeJsonResource {
|
|||
*/
|
||||
public void deactivate() throws AcmeException {
|
||||
LOG.debug("deactivate");
|
||||
try (Connection conn = connect()) {
|
||||
try (Connection conn = getSession().connect()) {
|
||||
JSONBuilder claims = new JSONBuilder();
|
||||
claims.put(KEY_STATUS, "deactivated");
|
||||
|
||||
|
@ -344,7 +344,7 @@ public class Account extends AcmeJsonResource {
|
|||
*/
|
||||
public void commit() throws AcmeException {
|
||||
LOG.debug("modify/commit");
|
||||
try (Connection conn = connect()) {
|
||||
try (Connection conn = getSession().connect()) {
|
||||
JSONBuilder claims = new JSONBuilder();
|
||||
if (!editContacts.isEmpty()) {
|
||||
claims.put(KEY_CONTACT, editContacts);
|
||||
|
|
|
@ -198,7 +198,7 @@ public class AccountBuilder {
|
|||
|
||||
LOG.debug("create");
|
||||
|
||||
try (Connection conn = session.provider().connect()) {
|
||||
try (Connection conn = session.connect()) {
|
||||
URL resourceUrl = session.resourceUrl(Resource.NEW_ACCOUNT);
|
||||
|
||||
JSONBuilder claims = new JSONBuilder();
|
||||
|
|
|
@ -115,7 +115,7 @@ public abstract class AcmeJsonResource extends AcmeResource {
|
|||
public void update() throws AcmeException {
|
||||
String resourceType = getClass().getSimpleName();
|
||||
LOG.debug("update {}", resourceType);
|
||||
try (Connection conn = connect()) {
|
||||
try (Connection conn = getSession().connect()) {
|
||||
conn.sendSignedPostAsGetRequest(getLocation(), getLogin());
|
||||
JSON json = conn.readJsonResponse();
|
||||
if (json != null) {
|
||||
|
|
|
@ -19,8 +19,6 @@ import java.util.Objects;
|
|||
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
|
||||
import org.shredzone.acme4j.connector.Connection;
|
||||
|
||||
/**
|
||||
* A generic ACME resource.
|
||||
*/
|
||||
|
@ -61,13 +59,6 @@ public abstract class AcmeResource implements Serializable {
|
|||
return getLogin().getSession();
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens a {@link Connection} to the provider.
|
||||
*/
|
||||
protected Connection connect() {
|
||||
return getSession().provider().connect();
|
||||
}
|
||||
|
||||
/**
|
||||
* Rebinds this resource to a {@link Login}.
|
||||
* <p>
|
||||
|
|
|
@ -144,7 +144,7 @@ public class Authorization extends AcmeJsonResource {
|
|||
*/
|
||||
public void deactivate() throws AcmeException {
|
||||
LOG.debug("deactivate");
|
||||
try (Connection conn = connect()) {
|
||||
try (Connection conn = getSession().connect()) {
|
||||
JSONBuilder claims = new JSONBuilder();
|
||||
claims.put("status", "deactivated");
|
||||
|
||||
|
|
|
@ -70,7 +70,7 @@ public class Certificate extends AcmeResource {
|
|||
public void download() throws AcmeException {
|
||||
if (certChain == null) {
|
||||
LOG.debug("download");
|
||||
try (Connection conn = connect()) {
|
||||
try (Connection conn = getSession().connect()) {
|
||||
conn.sendCertificateRequest(getLocation(), getLogin());
|
||||
alternates = new ArrayList<>(conn.getLinks("alternate"));
|
||||
certChain = new ArrayList<>(conn.readCertificates());
|
||||
|
@ -150,7 +150,7 @@ public class Certificate extends AcmeResource {
|
|||
LOG.debug("revoke");
|
||||
URL resUrl = getSession().resourceUrl(Resource.REVOKE_CERT);
|
||||
|
||||
try (Connection conn = connect()) {
|
||||
try (Connection conn = getSession().connect()) {
|
||||
JSONBuilder claims = new JSONBuilder();
|
||||
claims.putBase64("certificate", getCertificate().getEncoded());
|
||||
if (reason != null) {
|
||||
|
@ -187,7 +187,7 @@ public class Certificate extends AcmeResource {
|
|||
throw new AcmeException("Server does not allow certificate revocation");
|
||||
}
|
||||
|
||||
try (Connection conn = session.provider().connect()) {
|
||||
try (Connection conn = session.connect()) {
|
||||
JSONBuilder claims = new JSONBuilder();
|
||||
claims.putBase64("certificate", cert.getEncoded());
|
||||
if (reason != null) {
|
||||
|
|
|
@ -162,7 +162,7 @@ public class Order extends AcmeJsonResource {
|
|||
*/
|
||||
public void execute(byte[] csr) throws AcmeException {
|
||||
LOG.debug("finalize");
|
||||
try (Connection conn = connect()) {
|
||||
try (Connection conn = getSession().connect()) {
|
||||
JSONBuilder claims = new JSONBuilder();
|
||||
claims.putBase64("csr", csr);
|
||||
|
||||
|
@ -235,7 +235,7 @@ public class Order extends AcmeJsonResource {
|
|||
}
|
||||
|
||||
LOG.debug("cancel");
|
||||
try (Connection conn = connect()) {
|
||||
try (Connection conn = getSession().connect()) {
|
||||
JSONBuilder claims = new JSONBuilder();
|
||||
claims.put("status", "canceled");
|
||||
|
||||
|
|
|
@ -245,7 +245,7 @@ public class OrderBuilder {
|
|||
}
|
||||
|
||||
LOG.debug("create");
|
||||
try (Connection conn = session.provider().connect()) {
|
||||
try (Connection conn = session.connect()) {
|
||||
JSONBuilder claims = new JSONBuilder();
|
||||
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.concurrent.ThreadSafe;
|
||||
|
||||
import org.shredzone.acme4j.connector.Connection;
|
||||
import org.shredzone.acme4j.connector.Resource;
|
||||
import org.shredzone.acme4j.exception.AcmeException;
|
||||
import org.shredzone.acme4j.provider.AcmeProvider;
|
||||
|
@ -172,6 +173,15 @@ public class Session {
|
|||
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
|
||||
* the server and getting a directory. The result is cached.
|
||||
|
|
|
@ -148,7 +148,7 @@ public class Challenge extends AcmeJsonResource {
|
|||
*/
|
||||
public void trigger() throws AcmeException {
|
||||
LOG.debug("trigger");
|
||||
try (Connection conn = connect()) {
|
||||
try (Connection conn = getSession().connect()) {
|
||||
JSONBuilder claims = new JSONBuilder();
|
||||
prepareResponse(claims);
|
||||
|
||||
|
|
|
@ -144,7 +144,7 @@ public class ResourceIterator<T extends AcmeResource> implements Iterator<T> {
|
|||
*/
|
||||
private void readAndQueue() throws AcmeException {
|
||||
Session session = login.getSession();
|
||||
try (Connection conn = session.provider().connect()) {
|
||||
try (Connection conn = session.connect()) {
|
||||
conn.sendSignedPostAsGetRequest(nextUrl, login);
|
||||
|
||||
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();
|
||||
|
||||
@Override
|
||||
public Connection connect() {
|
||||
public Connection connect(URI serverUri) {
|
||||
return new DefaultConnection(createHttpConnector());
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSON directory(Session session, URI serverUri) throws AcmeException {
|
||||
try (Connection conn = connect()) {
|
||||
try (Connection conn = connect(serverUri)) {
|
||||
conn.sendRequest(resolve(serverUri), session);
|
||||
|
||||
// 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.
|
||||
*
|
||||
* @param serverUri
|
||||
* Server {@link URI}
|
||||
* @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
|
||||
|
|
|
@ -67,7 +67,7 @@ public class LetsEncryptAcmeProvider extends AbstractAcmeProvider {
|
|||
|
||||
@Override
|
||||
@SuppressWarnings("deprecation")
|
||||
public Connection connect() {
|
||||
public Connection connect(URI serverUri) {
|
||||
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.exception.AcmeException;
|
||||
import org.shredzone.acme4j.exception.AcmeServerException;
|
||||
import org.shredzone.acme4j.provider.AcmeProvider;
|
||||
import org.shredzone.acme4j.provider.TestableConnectionProvider;
|
||||
import org.shredzone.acme4j.toolbox.JSON;
|
||||
import org.shredzone.acme4j.toolbox.JSONBuilder;
|
||||
|
@ -343,13 +342,7 @@ public class AccountTest {
|
|||
|
||||
provider.putTestResource(Resource.KEY_CHANGE, locationUrl);
|
||||
|
||||
Session session = new Session(new URI(TestUtils.ACME_SERVER_URI)) {
|
||||
@Override
|
||||
public AcmeProvider provider() {
|
||||
return provider;
|
||||
};
|
||||
};
|
||||
|
||||
Session session = TestUtils.session(provider);
|
||||
Login login = new Login(locationUrl, oldKeyPair, session);
|
||||
|
||||
assertThat(login.getKeyPair(), is(sameInstance(oldKeyPair)));
|
||||
|
|
|
@ -77,7 +77,7 @@ public class SessionProviderTest {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Connection connect() {
|
||||
public Connection connect(URI serverUri) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
|
@ -105,7 +105,7 @@ public class SessionProviderTest {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Connection connect() {
|
||||
public Connection connect(URI serverUri) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
|
|
|
@ -50,6 +50,8 @@ public class AbstractAcmeProviderTest {
|
|||
*/
|
||||
@Test
|
||||
public void testConnect() {
|
||||
final URI testServerUri = URI.create("http://example.com/acme");
|
||||
|
||||
final AtomicBoolean invoked = new AtomicBoolean();
|
||||
|
||||
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, instanceOf(DefaultConnection.class));
|
||||
assertThat(invoked.get(), is(true));
|
||||
|
@ -90,7 +92,8 @@ public class AbstractAcmeProviderTest {
|
|||
|
||||
AbstractAcmeProvider provider = new AbstractAcmeProvider() {
|
||||
@Override
|
||||
public Connection connect() {
|
||||
public Connection connect(URI serverUri) {
|
||||
assertThat(serverUri, is(testServerUri));
|
||||
return connection;
|
||||
}
|
||||
|
||||
|
|
|
@ -119,7 +119,7 @@ public class TestableConnectionProvider extends DummyConnection implements AcmeP
|
|||
}
|
||||
|
||||
@Override
|
||||
public Connection connect() {
|
||||
public Connection connect(URI serverUri) {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
|
@ -75,7 +75,7 @@ public class LetsEncryptAcmeProviderTest {
|
|||
@SuppressWarnings("deprecation")
|
||||
public void testConnect() {
|
||||
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)));
|
||||
}
|
||||
|
||||
|
|
|
@ -54,6 +54,7 @@ import org.jose4j.keys.HmacKey;
|
|||
import org.shredzone.acme4j.Login;
|
||||
import org.shredzone.acme4j.Problem;
|
||||
import org.shredzone.acme4j.Session;
|
||||
import org.shredzone.acme4j.connector.Connection;
|
||||
import org.shredzone.acme4j.provider.AcmeProvider;
|
||||
|
||||
/**
|
||||
|
@ -161,6 +162,11 @@ public final class TestUtils {
|
|||
public AcmeProvider provider() {
|
||||
return provider;
|
||||
};
|
||||
|
||||
@Override
|
||||
public Connection connect() {
|
||||
return provider.connect(getServerUri());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue