Return nonce header

pull/61/head
Richard Körber 2018-02-21 19:58:50 +01:00
parent b1ac68181c
commit 0d42089318
No known key found for this signature in database
GPG Key ID: AAB9FD19C78AA3E0
6 changed files with 29 additions and 28 deletions

View File

@ -116,12 +116,11 @@ public interface Connection extends AutoCloseable {
void handleRetryAfter(String message) throws AcmeException;
/**
* Updates a {@link Session} by evaluating the HTTP response header.
* Gets the nonce from the nonce header.
*
* @param session
* {@link Session} instance to be updated
* @return Nonce, or {@code null} if no nonce header was set
*/
void updateSession(Session session);
byte[] getNonce();
/**
* Gets a location from the {@code Location} header.

View File

@ -112,11 +112,11 @@ public class DefaultConnection implements Connection {
throwAcmeException();
}
updateSession(session);
if (session.getNonce() == null) {
byte[] nonce = getNonce();
if (nonce == null) {
throw new AcmeProtocolException("Server did not provide a nonce");
}
session.setNonce(nonce);
} catch (IOException ex) {
throw new AcmeNetworkException(ex);
} finally {
@ -241,12 +241,12 @@ public class DefaultConnection implements Connection {
}
@Override
public void updateSession(Session session) {
public byte[] getNonce() {
assertConnectionIsOpen();
String nonceHeader = conn.getHeaderField(REPLAY_NONCE_HEADER);
if (nonceHeader == null || nonceHeader.trim().isEmpty()) {
return;
return null;
}
if (!BASE64URL_PATTERN.matcher(nonceHeader).matches()) {
@ -255,7 +255,7 @@ public class DefaultConnection implements Connection {
LOG.debug("Replay Nonce: {}", nonceHeader);
session.setNonce(Base64Url.decode(nonceHeader));
return Base64Url.decode(nonceHeader);
}
@Override
@ -353,7 +353,7 @@ public class DefaultConnection implements Connection {
logHeaders();
updateSession(session);
session.setNonce(getNonce());
int rc = conn.getResponseCode();
if ((httpStatus.length == 0 && rc != HttpURLConnection.HTTP_OK)

View File

@ -53,7 +53,10 @@ public abstract class AbstractAcmeProvider implements AcmeProvider {
conn.sendRequest(resolve(serverUri), session);
// use nonce header if there is one, saves a HEAD request...
conn.updateSession(session);
byte[] nonce = conn.getNonce();
if (nonce != null) {
session.setNonce(nonce);
}
return conn.readJsonResponse();
}

View File

@ -97,9 +97,8 @@ public class DefaultConnectionTest {
assertThat(session.getNonce(), is(nullValue()));
try (DefaultConnection conn = new DefaultConnection(mockHttpConnection)) {
conn.conn = mockUrlConnection;
conn.updateSession(session);
assertThat(conn.getNonce(), is(nullValue()));
}
assertThat(session.getNonce(), is(nullValue()));
verify(mockUrlConnection).getHeaderField("Replay-Nonce");
verifyNoMoreInteractions(mockUrlConnection);
@ -116,9 +115,8 @@ public class DefaultConnectionTest {
try (DefaultConnection conn = new DefaultConnection(mockHttpConnection)) {
conn.conn = mockUrlConnection;
conn.updateSession(session);
assertThat(conn.getNonce(), is(TestUtils.DUMMY_NONCE));
}
assertThat(session.getNonce(), is(TestUtils.DUMMY_NONCE));
verify(mockUrlConnection).getHeaderField("Replay-Nonce");
verifyNoMoreInteractions(mockUrlConnection);
@ -136,7 +134,7 @@ public class DefaultConnectionTest {
try (DefaultConnection conn = new DefaultConnection(mockHttpConnection)) {
conn.conn = mockUrlConnection;
conn.updateSession(session);
conn.getNonce();
fail("Expected to fail");
} catch (AcmeProtocolException ex) {
assertThat(ex.getMessage(), org.hamcrest.Matchers.startsWith("Invalid replay nonce"));
@ -677,17 +675,18 @@ public class DefaultConnectionTest {
} else {
fail("unknown nonce");
}
};
}
@Override
public void updateSession(Session session) {
public byte[] getNonce() {
assertThat(session, is(sameInstance(DefaultConnectionTest.this.session)));
if (session.getNonce() == nonce1) {
session.setNonce(nonce2);
return nonce2;
} else {
fail("unknown nonce");
return null;
}
};
}
}) {
JSONBuilder cb = new JSONBuilder();
cb.put("foo", 123).put("bar", "a-string");
@ -752,17 +751,18 @@ public class DefaultConnectionTest {
} else {
fail("unknown nonce");
}
};
}
@Override
public void updateSession(Session session) {
public byte[] getNonce() {
assertThat(session, is(sameInstance(DefaultConnectionTest.this.session)));
if (session.getNonce() == nonce1) {
session.setNonce(nonce2);
return nonce2;
} else {
fail("unknown nonce");
return null;
}
};
}
}) {
JSONBuilder cb = new JSONBuilder();
cb.put("foo", 123).put("bar", "a-string");

View File

@ -67,7 +67,7 @@ public class DummyConnection implements Connection {
}
@Override
public void updateSession(Session session) {
public byte[] getNonce() {
throw new UnsupportedOperationException();
}

View File

@ -15,7 +15,6 @@ package org.shredzone.acme4j.provider;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.shredzone.acme4j.toolbox.TestUtils.getJSON;
import static uk.co.datumedge.hamcrest.json.SameJSONAs.sameJSONAs;
@ -110,7 +109,7 @@ public class AbstractAcmeProviderTest {
assertThat(map.toString(), sameJSONAs(TestUtils.getJSON("directory").toString()));
verify(connection).sendRequest(testResolvedUrl, session);
verify(connection).updateSession(any(Session.class));
verify(connection).getNonce();
verify(connection).readJsonResponse();
verify(connection).close();
verifyNoMoreInteractions(connection);