Skip to content

Commit d9507e4

Browse files
authored
fix: oauth1 signing for url encoded content (#538)
* Fixed OAuth 1.0 signing for requests with URL Encoded Content (typical of POST and PUT HTTP methods) A simple change the the OAuthParameters intercept method that checks if the request has UrlEncodedContent. If it does, then its content is added to the GenericUrl so that the signature can be computed correctly. After the call to computeSignature, the content parameters are removed from the GenericUrl to leave it in its original form. As per the OAuth 1.0 spec here (https://tools.ietf.org/html/rfc5849#page-28), any form encoded request body with a Content-Type of "application/x-www-form-urlencoded" must be included when the request signature is created. * Changes to support java 1.7. Also added new test code. * style: expanded the wild card imports.
1 parent bf7530b commit d9507e4

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

google-oauth-client/src/main/java/com/google/api/client/auth/oauth/OAuthParameters.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@
1414

1515
package com.google.api.client.auth.oauth;
1616

17+
import com.google.api.client.http.HttpContent;
1718
import com.google.api.client.http.GenericUrl;
1819
import com.google.api.client.http.HttpExecuteInterceptor;
1920
import com.google.api.client.http.HttpRequest;
2021
import com.google.api.client.http.HttpRequestInitializer;
22+
import com.google.api.client.http.UrlEncodedContent;
2123
import com.google.api.client.util.Beta;
24+
import com.google.api.client.util.Data;
2225
import com.google.api.client.util.escape.PercentEscaper;
2326
import com.google.common.collect.Multiset;
2427
import com.google.common.collect.SortedMultiset;
2528
import com.google.common.collect.TreeMultiset;
29+
2630
import java.io.IOException;
2731
import java.security.GeneralSecurityException;
2832
import java.security.SecureRandom;
@@ -53,7 +57,8 @@
5357
* @author Yaniv Inbar
5458
*/
5559
@Beta
56-
public final class OAuthParameters implements HttpExecuteInterceptor, HttpRequestInitializer {
60+
public final class OAuthParameters implements HttpExecuteInterceptor, HttpRequestInitializer
61+
{
5762

5863
/** Secure random number generator to sign requests. */
5964
private static final SecureRandom RANDOM = new SecureRandom();
@@ -271,7 +276,19 @@ public void intercept(HttpRequest request) throws IOException {
271276
computeNonce();
272277
computeTimestamp();
273278
try {
274-
computeSignature(request.getRequestMethod(), request.getUrl());
279+
GenericUrl url = request.getUrl();
280+
HttpContent content = request.getContent();
281+
Map<String, Object> urlEncodedParams = null;
282+
if (content instanceof UrlEncodedContent) {
283+
urlEncodedParams = Data.mapOf(((UrlEncodedContent) content).getData());
284+
url.putAll(urlEncodedParams);
285+
}
286+
computeSignature(request.getRequestMethod(), url);
287+
if (urlEncodedParams != null) {
288+
for (Map.Entry<String, Object> entry : urlEncodedParams.entrySet()) {
289+
url.remove(entry.getKey());
290+
}
291+
}
275292
} catch (GeneralSecurityException e) {
276293
IOException io = new IOException();
277294
io.initCause(e);

google-oauth-client/src/test/java/com/google/api/client/auth/oauth/OAuthParametersTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@
1515
package com.google.api.client.auth.oauth;
1616

1717
import com.google.api.client.http.GenericUrl;
18+
19+
import java.io.IOException;
1820
import java.security.GeneralSecurityException;
21+
import java.util.Collections;
22+
import java.util.Map;
23+
24+
import com.google.api.client.http.HttpRequest;
25+
import com.google.api.client.http.UrlEncodedContent;
26+
import com.google.api.client.http.javanet.NetHttpTransport;
1927
import junit.framework.TestCase;
2028

2129
/**
@@ -75,6 +83,22 @@ public void testSignature() throws GeneralSecurityException {
7583
parameters.signature);
7684
}
7785

86+
public void testSignatureWithUrlEncodedContent() throws IOException {
87+
OAuthParameters parameters = new OAuthParameters();
88+
parameters.signer = new MockSigner();
89+
90+
GenericUrl url = new GenericUrl("https://example.local?foo=bar");
91+
Map<String, Object> contentParameters = Collections.singletonMap("this", (Object) "that");
92+
UrlEncodedContent content = new UrlEncodedContent(contentParameters);
93+
94+
HttpRequest request = new NetHttpTransport.Builder().build()
95+
.createRequestFactory().buildPostRequest(url, content);
96+
parameters.intercept(request);
97+
98+
assertTrue(parameters.signature.endsWith("%26this%3Dthat"));
99+
assertEquals("https://example.local?foo=bar", url.build());
100+
}
101+
78102
public void testSignatureWithRepeatedParameter() throws GeneralSecurityException {
79103
OAuthParameters parameters = new OAuthParameters();
80104
parameters.signer = new MockSigner();

0 commit comments

Comments
 (0)