aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/network
diff options
context:
space:
mode:
authorSamuel Tardieu <sam@rfc1149.net>2011-10-10 12:55:50 +0200
committerSamuel Tardieu <sam@rfc1149.net>2011-10-10 13:10:59 +0200
commit826a466ed5d59054972c1fc886c739111bc70426 (patch)
tree8efe4831cedd8dfd0a558759eb1d6b9cfba1b48a /main/src/cgeo/geocaching/network
parente1e34ef836d97daf9b89d4fd2d7aef3b45ab7f38 (diff)
downloadcgeo-826a466ed5d59054972c1fc886c739111bc70426.zip
cgeo-826a466ed5d59054972c1fc886c739111bc70426.tar.gz
cgeo-826a466ed5d59054972c1fc886c739111bc70426.tar.bz2
Use existing HTTP connection code for Twitter authorization dialog
Also, move OAuth signing into package cgeo.geocaching.network. This is a tentative fix for #280.
Diffstat (limited to 'main/src/cgeo/geocaching/network')
-rw-r--r--main/src/cgeo/geocaching/network/OAuth.java35
1 files changed, 35 insertions, 0 deletions
diff --git a/main/src/cgeo/geocaching/network/OAuth.java b/main/src/cgeo/geocaching/network/OAuth.java
new file mode 100644
index 0000000..38b04e9
--- /dev/null
+++ b/main/src/cgeo/geocaching/network/OAuth.java
@@ -0,0 +1,35 @@
+package cgeo.geocaching.network;
+
+import cgeo.geocaching.Parameters;
+import cgeo.geocaching.Settings;
+import cgeo.geocaching.cgBase;
+import cgeo.geocaching.utils.CryptUtils;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.http.NameValuePair;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+public class OAuth {
+ public static void signOAuth(final String host, final String path, final String method, final boolean https, final Parameters params, final String token, final String tokenSecret) {
+ params.put(
+ "oauth_consumer_key", Settings.getKeyConsumerPublic(),
+ "oauth_nonce", CryptUtils.md5(Long.toString(System.currentTimeMillis())),
+ "oauth_signature_method", "HMAC-SHA1",
+ "oauth_timestamp", Long.toString((long) Math.floor(new Date().getTime() / 1000)),
+ "oauth_token", StringUtils.defaultString(token),
+ "oauth_version", "1.0");
+ params.sort();
+
+ final List<String> paramsEncoded = new ArrayList<String>();
+ for (final NameValuePair nameValue : params) {
+ paramsEncoded.add(nameValue.getName() + "=" + cgBase.urlencode_rfc3986(nameValue.getValue()));
+ }
+
+ final String keysPacked = Settings.getKeyConsumerSecret() + "&" + StringUtils.defaultString(tokenSecret); // both even if empty some of them!
+ final String requestPacked = method + "&" + cgBase.urlencode_rfc3986((https ? "https" : "http") + "://" + host + path) + "&" + cgBase.urlencode_rfc3986(StringUtils.join(paramsEncoded.toArray(), '&'));
+ params.put("oauth_signature", cgBase.base64Encode(CryptUtils.hashHmac(requestPacked, keysPacked)));
+ }
+}