Skip to content

Commit 2192eda

Browse files
author
Mark Adamcin
committed
Merge branch 'Sparkling18-master'
2 parents 3afe802 + aa1908a commit 2192eda

File tree

12 files changed

+67
-40
lines changed

12 files changed

+67
-40
lines changed

api/src/main/java/net/adamcin/httpsig/api/Constants.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,22 @@ public final class Constants {
6565
*/
6666
public static final String KEY_ID = "keyId";
6767

68+
/**
69+
* replaced with {@code request-target} in draft-cavage-http-signatures-03
70+
*/
71+
@Deprecated
6872
public static final String HEADER_REQUEST_LINE = "request-line";
6973

74+
public static final String HEADER_REQUEST_TARGET = "(request-target)";
75+
7076
public static final String HEADER_DATE = "date";
7177

7278
public static final List<String> DEFAULT_HEADERS = Arrays.asList(HEADER_DATE);
7379

7480
/**
7581
* List of headers to always exclude from signature calculation
7682
*/
77-
public static final List<String> IGNORE_HEADERS = Arrays.asList("authorization");
83+
public static final List<String> IGNORE_HEADERS = Arrays.asList("authorization", HEADER_REQUEST_LINE);
7884

7985
/**
8086
* Http request header representing client credentials

api/src/main/java/net/adamcin/httpsig/api/RequestContent.java

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,41 @@ public final class RequestContent implements Serializable {
5353

5454
private static final long serialVersionUID = -2968642080214687631L;
5555

56+
@Deprecated
5657
private final String requestLine;
58+
private final String method;
59+
private final String path;
5760
private final Map<String, List<String>> headers;
5861

59-
private RequestContent(final String requestLine, final Map<String, List<String>> headers) {
62+
private RequestContent(final String requestLine, final String method, final String path, final Map<String, List<String>> headers) {
6063
this.requestLine = requestLine;
64+
this.method = method;
65+
this.path = path;
6166
this.headers = headers;
6267
}
6368

6469
public static final class Builder {
6570

71+
@Deprecated
6672
private String requestLine = null;
73+
74+
private String method = null;
75+
private String path = null;
6776
private final Map<String, List<String>> headers = new LinkedHashMap<String, List<String>>();
6877

78+
@Deprecated
6979
public Builder setRequestLine(String requestLine) {
80+
LOGGER.warning("[setRequestLine] Use of the request-line header is deprecated. Please use (request-target) instead.");
7081
this.requestLine = requestLine;
7182
return this;
7283
}
7384

85+
public Builder setRequestTarget(String method, String path) {
86+
this.method = method.toUpperCase();
87+
this.path = path;
88+
return this;
89+
}
90+
7491
/**
7592
* Adds a header name and value pair
7693
*
@@ -83,7 +100,7 @@ public Builder addHeader(final String name, final String value) {
83100
if (Constants.IGNORE_HEADERS.contains(_name) || _name.startsWith(":")) {
84101
/* skip ignored headers and names which begin with a colon */
85102
return this;
86-
} else if (Constants.HEADER_REQUEST_LINE.equals(_name)) {
103+
} else if (Constants.HEADER_REQUEST_TARGET.equals(_name)) {
87104
return this;
88105
} else if (!Constants.HEADER_DATE.equals(_name) || tryParseDate(value)) {
89106
List<String> values = null;
@@ -148,7 +165,7 @@ public Builder addDateNow() {
148165
}
149166

150167
public RequestContent build() {
151-
return new RequestContent(requestLine, Collections.unmodifiableMap(headers));
168+
return new RequestContent(requestLine, method, path, Collections.unmodifiableMap(headers));
152169
}
153170
}
154171

@@ -174,9 +191,9 @@ public String getContentString(List<String> headers) {
174191
for (String header : headers) {
175192
String _header = header.toLowerCase();
176193
if (!Constants.IGNORE_HEADERS.contains(_header) && !_header.startsWith(":")) {
177-
if (Constants.HEADER_REQUEST_LINE.equals(_header)) {
178-
if (this.requestLine != null) {
179-
hashBuilder.append(this.requestLine).append("\n");
194+
if (Constants.HEADER_REQUEST_TARGET.equals(_header)) {
195+
if (this.getRequestTarget() != null) {
196+
hashBuilder.append(this.getRequestTarget()).append("\n");
180197
}
181198
} else {
182199
for (String value : this.getHeaderValues(_header)) {
@@ -196,12 +213,12 @@ public String toString() {
196213

197214
/**
198215
* @return the list of header names contained in this {@link RequestContent}, in the order in which they were added, except
199-
* for request-line, which is listed first if present
216+
* for request-target, which is listed first if present
200217
*/
201218
public List<String> getHeaderNames() {
202219
List<String> headerNames = new ArrayList<String>();
203-
if (requestLine != null) {
204-
headerNames.add(Constants.HEADER_REQUEST_LINE);
220+
if (method != null && path != null) {
221+
headerNames.add(Constants.HEADER_REQUEST_TARGET);
205222
}
206223
headerNames.addAll(this.headers.keySet());
207224
return Collections.unmodifiableList(headerNames);
@@ -211,10 +228,22 @@ public List<String> getHeaderNames() {
211228
/**
212229
* @return the request-line if set
213230
*/
231+
@Deprecated
214232
public String getRequestLine() {
215233
return requestLine;
216234
}
217235

236+
/**
237+
* @return the request-target if set
238+
*/
239+
public String getRequestTarget() {
240+
if (method == null || path == null) {
241+
return null;
242+
} else {
243+
return method.toLowerCase() + " " + path;
244+
}
245+
}
246+
218247
/**
219248
* @return the first date header value if set, null if not
220249
*/
@@ -229,9 +258,9 @@ public String getDate() {
229258
*/
230259
public List<String> getHeaderValues(String name) {
231260
String _name = name.toLowerCase();
232-
if (Constants.HEADER_REQUEST_LINE.equals(_name)) {
233-
return this.requestLine != null ? Collections.singletonList(
234-
this.requestLine
261+
if (Constants.HEADER_REQUEST_TARGET.equals(_name)) {
262+
return this.getRequestTarget() != null ? Collections.singletonList(
263+
this.getRequestTarget()
235264
) : Collections.<String>emptyList();
236265
} else if (this.headers.containsKey(_name)) {
237266
return Collections.unmodifiableList(this.headers.get(_name));

http-helpers/src/main/java/net/adamcin/httpsig/http/apache3/Http3SignatureAuthScheme.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,8 @@ public String authenticate(Credentials credentials, HttpMethod method) throws Au
8686
}
8787

8888
RequestContent.Builder sigBuilder = new RequestContent.Builder();
89-
sigBuilder.setRequestLine(
90-
String.format("%s %s HTTP/1.1", method.getName(),
91-
method.getPath() + (method.getQueryString() != null ? "?" + method.getQueryString() : "")));
89+
90+
sigBuilder.setRequestTarget(method.getName(), method.getPath() + (method.getQueryString() != null ? "?" + method.getQueryString() : ""));
9291

9392
for (Header header : method.getRequestHeaders()) {
9493
sigBuilder.addHeader(header.getName(), header.getValue());

http-helpers/src/main/java/net/adamcin/httpsig/http/apache4/Http4SignatureAuthScheme.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ public Header authenticate(Credentials credentials, HttpRequest request)
8383
}
8484

8585
RequestContent.Builder sigBuilder = new RequestContent.Builder();
86-
sigBuilder.setRequestLine(request.getRequestLine().toString());
86+
sigBuilder.setRequestTarget(request.getRequestLine().getMethod(),
87+
request.getRequestLine().getUri());
8788

8889
for (Header header : request.getAllHeaders()) {
8990
if (header.getName().toLowerCase().equals("connection")) {

http-helpers/src/main/java/net/adamcin/httpsig/http/ning/AsyncSignatureCalculator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ public AsyncSignatureCalculator(Signer signer, SignatureCalculator delegatee) {
5757
*/
5858
public void calculateAndAddSignature(String url, Request request, RequestBuilderBase<?> requestBuilder) {
5959
delegatee.calculateAndAddSignature(url, request, requestBuilder);
60-
AsyncUtil.calculateSignature(this.signer, request, requestBuilder, AsyncUtil.REQUEST_LINE_FORMAT);
60+
AsyncUtil.calculateSignature(this.signer, request, requestBuilder);
6161
}
6262
}

http-helpers/src/main/java/net/adamcin/httpsig/http/ning/AsyncUtil.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@
4747

4848
public final class AsyncUtil {
4949

50-
static final String REQUEST_LINE_FORMAT = "%s %s HTTP/1.1";
51-
5250
/**
5351
* Attaches the provided {@link Signer} to the {@link AsyncHttpClient} as a signature calculator.
5452
* It is expected that key rotation and {@link net.adamcin.httpsig.api.Challenge} management is outside the scope
@@ -166,22 +164,21 @@ public static <T> Future<T> login(final AsyncHttpClient client,
166164
return response;
167165
}
168166

169-
public static String getRequestLine(Request request, String requestLineFormat) {
170-
String path = "";
167+
public static String getRequestPath(Request request) {
171168
try {
172169
URL url = new URL(request.getUrl());
173-
path = url.getPath() + (url.getQuery() != null ? "?" + url.getQuery() : "");
170+
return url.getPath() + (url.getQuery() != null ? "?" + url.getQuery() : "");
174171
} catch (Exception e) {
175172
e.printStackTrace(System.err);
176173
}
177174

178-
return String.format(requestLineFormat != null ? requestLineFormat : REQUEST_LINE_FORMAT, request.getMethod(), path);
175+
return null;
179176
}
180177

181-
public static void calculateSignature(Signer signer, Request request, RequestBuilderBase<?> requestBuilder, String requestLineFormat) {
178+
public static void calculateSignature(Signer signer, Request request, RequestBuilderBase<?> requestBuilder) {
182179
RequestContent.Builder sigBuilder = new RequestContent.Builder();
183180

184-
sigBuilder.setRequestLine(getRequestLine(request, requestLineFormat));
181+
sigBuilder.setRequestTarget(request.getMethod(), getRequestPath(request));
185182
for (FluentCaseInsensitiveStringsMap.Entry<String, List<String>> entry : request.getHeaders().entrySet()) {
186183
for (String value : entry.getValue()) {
187184
sigBuilder.addHeader(entry.getKey(), value);

http-helpers/src/main/java/net/adamcin/httpsig/http/ning/RotateAndReplayResponseFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public FilterContext filter(FilterContext ctx) throws FilterException {
8585
}
8686

8787
RequestBuilder builder = new RequestBuilder(request);
88-
AsyncUtil.calculateSignature(signer, request, builder, AsyncUtil.REQUEST_LINE_FORMAT);
88+
AsyncUtil.calculateSignature(signer, request, builder);
8989

9090
return new FilterContext.FilterContextBuilder(ctx)
9191
.replayRequest(replay)

http-helpers/src/main/java/net/adamcin/httpsig/http/servlet/ServletUtil.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,7 @@ public static RequestContent getRequestContent(HttpServletRequest request, Colle
9393
RequestContent.Builder signatureContent = new RequestContent.Builder();
9494
String path = request.getRequestURI() + (request.getQueryString() != null ? "?" + request.getQueryString() : "");
9595

96-
signatureContent.setRequestLine(
97-
String.format(
98-
"%s %s %s",
99-
request.getMethod(), path, request.getProtocol()
100-
)
101-
);
96+
signatureContent.setRequestTarget(request.getMethod(), path);
10297

10398
Enumeration headerNames = request.getHeaderNames();
10499

http-helpers/src/test/java/net/adamcin/httpsig/http/apache3/Http3UtilTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void testLogin() {
5858

5959
@Override protected void execute() throws Exception {
6060
List<String> headers = Arrays.asList(
61-
Constants.HEADER_REQUEST_LINE,
61+
Constants.HEADER_REQUEST_TARGET,
6262
Constants.HEADER_DATE);
6363
setServlet(new AdminServlet(headers));
6464

@@ -86,7 +86,7 @@ public void testAllHeaders() {
8686
TestBody.test(new HttpServerTestBody() {
8787
@Override protected void execute() throws Exception {
8888
List<String> headers = Arrays.asList(
89-
Constants.HEADER_REQUEST_LINE,
89+
Constants.HEADER_REQUEST_TARGET,
9090
Constants.HEADER_DATE,
9191
"x-test"
9292
);

http-helpers/src/test/java/net/adamcin/httpsig/http/apache4/Http4UtilTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void testLogin() {
6161
@Override protected void execute() throws Exception {
6262

6363
List<String> headers = Arrays.asList(
64-
Constants.HEADER_REQUEST_LINE,
64+
Constants.HEADER_REQUEST_TARGET,
6565
Constants.HEADER_DATE);
6666

6767
setServlet(new AdminServlet(headers));
@@ -89,7 +89,7 @@ public void testAllHeaders() {
8989
@Override protected void execute() throws Exception {
9090

9191
List<String> headers = Arrays.asList(
92-
Constants.HEADER_REQUEST_LINE,
92+
Constants.HEADER_REQUEST_TARGET,
9393
Constants.HEADER_DATE,
9494
"x-test"
9595
);

0 commit comments

Comments
 (0)