Skip to content
This repository has been archived by the owner on Apr 5, 2022. It is now read-only.

SOCIAL-274 changes to allow customizing character sets used in OAuth 1 p... #22

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 @@ -28,7 +28,7 @@
* @author Keith Donald
* @author Craig Walls
*/
class OAuth1RequestInterceptor implements ClientHttpRequestInterceptor {
public class OAuth1RequestInterceptor implements ClientHttpRequestInterceptor {

private final SigningSupport signingUtils;
private final OAuth1Credentials oauth1Credentials;
Expand All @@ -53,5 +53,8 @@ public ClientHttpResponse intercept(final HttpRequest request, final byte[] body
private String getAuthorizationHeaderValue(HttpRequest request, byte[] body) {
return signingUtils.buildAuthorizationHeaderValue(request, body, oauth1Credentials);
}


public SigningSupport getSigningUtils() {
return signingUtils;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@
class SigningSupport {

private TimestampGenerator timestampGenerator = new DefaultTimestampGenerator();


private String urlDecodingCharsetName = UTF8_CHARSET_NAME;
private String percentEncodingCharsetName = UTF8_CHARSET_NAME;
private String signatureGenerationCharsetName = UTF8_CHARSET_NAME;

/**
* Builds the authorization header.
* The elements in additionalParameters are expected to not be encoded.
Expand Down Expand Up @@ -186,10 +190,10 @@ private String sign(String signatureBaseString, String key) {
Mac mac = Mac.getInstance(HMAC_SHA1_MAC_NAME);
SecretKeySpec spec = new SecretKeySpec(key.getBytes(), HMAC_SHA1_MAC_NAME);
mac.init(spec);
byte[] text = signatureBaseString.getBytes(UTF8_CHARSET_NAME);
byte[] text = signatureBaseString.getBytes(signatureGenerationCharsetName);
byte[] signatureBytes = mac.doFinal(text);
signatureBytes = Base64.encode(signatureBytes);
String signature = new String(signatureBytes, UTF8_CHARSET_NAME);
String signature = new String(signatureBytes, signatureGenerationCharsetName);
return signature;
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(e);
Expand All @@ -204,7 +208,7 @@ private MultiValueMap<String, String> readFormParameters(MediaType bodyType, byt
if (bodyType != null && bodyType.equals(MediaType.APPLICATION_FORM_URLENCODED)) {
String body;
try {
body = new String(bodyBytes, UTF8_CHARSET_NAME);
body = new String(bodyBytes, urlDecodingCharsetName);
} catch (UnsupportedEncodingException shouldntHappen) {
throw new IllegalStateException(shouldntHappen);
}
Expand Down Expand Up @@ -290,10 +294,10 @@ private int getPort(URI uri) {
UNRESERVED = unreserved;
}

private static String oauthEncode(String param) {
private String oauthEncode(String param) {
try {
// See http://tools.ietf.org/html/rfc5849#section-3.6
byte[] bytes = encode(param.getBytes(UTF8_CHARSET_NAME), UNRESERVED);
byte[] bytes = encode(param.getBytes(percentEncodingCharsetName), UNRESERVED);
return new String(bytes, "US-ASCII");
} catch (Exception shouldntHappen) {
throw new IllegalStateException(shouldntHappen);
Expand Down Expand Up @@ -322,9 +326,9 @@ private static byte[] encode(byte[] source, BitSet notEncoded) {
return bos.toByteArray();
}

private static String formDecode(String encoded) {
private String formDecode(String encoded) {
try {
return URLDecoder.decode(encoded, UTF8_CHARSET_NAME);
return URLDecoder.decode(encoded, urlDecodingCharsetName);
} catch (UnsupportedEncodingException shouldntHappen) {
throw new IllegalStateException(shouldntHappen);
}
Expand All @@ -336,4 +340,15 @@ private static String formDecode(String encoded) {

private static final String UTF8_CHARSET_NAME = "UTF-8";

public void setUrlDecodingCharsetName(String urlDecodingCharsetName) {
this.urlDecodingCharsetName = urlDecodingCharsetName;
}

public void setPercentEncodingCharsetName(String percentEncodingCharsetName) {
this.percentEncodingCharsetName = percentEncodingCharsetName;
}

public void setSignatureGenerationCharsetName(String signatureGenerationCharsetName) {
this.signatureGenerationCharsetName = signatureGenerationCharsetName;
}
}