Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for empty parameters in signature #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,52 @@ public synchronized HttpRequest sign(HttpRequest request) throws OAuthMessageSig
return request;
}

public synchronized HttpRequest sign(HttpRequest request, boolean addEmptyParameters) throws OAuthMessageSignerException,
OAuthExpectationFailedException, OAuthCommunicationException {
if (consumerKey == null) {
throw new OAuthExpectationFailedException("consumer key not set");
}
if (consumerSecret == null) {
throw new OAuthExpectationFailedException("consumer secret not set");
}

requestParameters = new HttpParameters();
try {
if (additionalParameters != null) {
requestParameters.putAll(additionalParameters, false);
}
collectHeaderParameters(request, requestParameters);
collectQueryParameters(request, requestParameters, addEmptyParameters);
collectBodyParameters(request, requestParameters);

// add any OAuth params that haven't already been set
completeOAuthParameters(requestParameters);

requestParameters.remove(OAuth.OAUTH_SIGNATURE);

} catch (IOException e) {
throw new OAuthCommunicationException(e);
}

String signature = messageSigner.sign(request, requestParameters);
OAuth.debugOut("signature", signature);

signingStrategy.writeSignature(signature, request, requestParameters);
OAuth.debugOut("Request URL", request.getRequestUrl());

return request;
}

public synchronized HttpRequest sign(Object request) throws OAuthMessageSignerException,
OAuthExpectationFailedException, OAuthCommunicationException {
return sign(wrap(request));
}

public synchronized HttpRequest sign(Object request, boolean addEmptyParameters) throws OAuthMessageSignerException,
OAuthExpectationFailedException, OAuthCommunicationException {
return sign(wrap(request), addEmptyParameters);
}

public synchronized String sign(String url) throws OAuthMessageSignerException,
OAuthExpectationFailedException, OAuthCommunicationException {
HttpRequest request = new UrlStringRequestAdapter(url);
Expand Down Expand Up @@ -252,6 +293,16 @@ protected void collectQueryParameters(HttpRequest request, HttpParameters out) {
}
}

protected void collectQueryParameters(HttpRequest request, HttpParameters out, boolean addEmptyParameters) {

String url = request.getRequestUrl();
int q = url.indexOf('?');
if (q >= 0) {
// Combine the URL query string with the other parameters:
out.putAll(OAuth.decodeForm(url.substring(q + 1), addEmptyParameters), true);
}
}

protected String generateTimestamp() {
return Long.toString(System.currentTimeMillis() / 1000L);
}
Expand Down
23 changes: 23 additions & 0 deletions signpost-core/src/main/java/oauth/signpost/OAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,29 @@ public static HttpParameters decodeForm(String form) {
name = percentDecode(nvp.substring(0, equals));
value = percentDecode(nvp.substring(equals + 1));
}
params.put(name, value);
}
return params;
}

/** Parse a form-urlencoded document. */
public static HttpParameters decodeForm(String form, boolean addEmptyParameters) {
HttpParameters params = new HttpParameters();
if (isEmpty(form)) {
return params;
}

for (String nvp : form.split("\\&")) {
int equals = nvp.indexOf('=');
String name;
String value;
if (equals < 0) {
name = percentDecode(nvp);
value = (addEmptyParameters) ? "" : null;
} else {
name = percentDecode(nvp.substring(0, equals));
value = percentDecode(nvp.substring(equals + 1));
}

params.put(name, value);
}
Expand Down