From 489cb4b78e76857485d25a618f3539bad885e108 Mon Sep 17 00:00:00 2001 From: Sam Douglass Date: Mon, 28 Nov 2011 12:20:25 -0800 Subject: [PATCH] SOCIAL-274 changes to allow customizing character sets used in OAuth 1 process For working with providers with non-standard OAuth 1 implementations, e.g. Tumblr. --- .../oauth1/OAuth1RequestInterceptor.java | 7 +++-- .../social/oauth1/SigningSupport.java | 31 ++++++++++++++----- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/spring-social-core/src/main/java/org/springframework/social/oauth1/OAuth1RequestInterceptor.java b/spring-social-core/src/main/java/org/springframework/social/oauth1/OAuth1RequestInterceptor.java index 2e0a93ccf..ae4b03d06 100644 --- a/spring-social-core/src/main/java/org/springframework/social/oauth1/OAuth1RequestInterceptor.java +++ b/spring-social-core/src/main/java/org/springframework/social/oauth1/OAuth1RequestInterceptor.java @@ -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; @@ -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; + } } \ No newline at end of file diff --git a/spring-social-core/src/main/java/org/springframework/social/oauth1/SigningSupport.java b/spring-social-core/src/main/java/org/springframework/social/oauth1/SigningSupport.java index 5f8940ce8..919d1c334 100644 --- a/spring-social-core/src/main/java/org/springframework/social/oauth1/SigningSupport.java +++ b/spring-social-core/src/main/java/org/springframework/social/oauth1/SigningSupport.java @@ -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. @@ -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); @@ -204,7 +208,7 @@ private MultiValueMap 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); } @@ -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); @@ -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); } @@ -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; + } } \ No newline at end of file