Author: beaton
Date: Mon Mar 30 05:28:13 2009
New Revision: 759842
URL: http://svn.apache.org/viewvc?rev=759842&view=rev
Log:
Refactoring some OAuth stuff into common.
No functional changes.
Added:
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/auth/OAuthConstants.java
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/auth/OAuthUtil.java
(contents, props changed)
- copied, changed from r759839,
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthUtil.java
Removed:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthUtil.java
Modified:
incubator/shindig/trunk/java/common/pom.xml
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthCommandLine.java
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthProtocolException.java
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthRequest.java
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/testing/FakeOAuthServiceProvider.java
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/oauth/OAuthAuthenticationHandler.java
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/core/oauth/FakeOAuthRequest.java
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/core/oauth/OAuthAuthenticationHanderTest.java
Modified: incubator/shindig/trunk/java/common/pom.xml
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/pom.xml?rev=759842&r1=759841&r2=759842&view=diff
==============================================================================
--- incubator/shindig/trunk/java/common/pom.xml (original)
+++ incubator/shindig/trunk/java/common/pom.xml Mon Mar 30 05:28:13 2009
@@ -113,6 +113,10 @@
<artifactId>json</artifactId>
</dependency>
<dependency>
+ <groupId>net.oauth</groupId>
+ <artifactId>oauth-core</artifactId>
+ </dependency>
+ <dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.2</version>
Added:
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/auth/OAuthConstants.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/auth/OAuthConstants.java?rev=759842&view=auto
==============================================================================
---
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/auth/OAuthConstants.java
(added)
+++
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/auth/OAuthConstants.java
Mon Mar 30 05:28:13 2009
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.shindig.auth;
+
+public class OAuthConstants {
+ public static final String OAUTH_SESSION_HANDLE = "oauth_session_handle";
+ public static final String OAUTH_EXPIRES_IN = "oauth_expires_in";
+ public static final String OAUTH_BODY_HASH = "oauth_body_hash";
+}
Copied:
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/auth/OAuthUtil.java
(from r759839,
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthUtil.java)
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/auth/OAuthUtil.java?p2=incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/auth/OAuthUtil.java&p1=incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthUtil.java&r1=759839&r2=759842&rev=759842&view=diff
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthUtil.java
(original)
+++
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/auth/OAuthUtil.java
Mon Mar 30 05:28:13 2009
@@ -17,7 +17,7 @@
* under the License.
*/
-package org.apache.shindig.gadgets.oauth;
+package org.apache.shindig.auth;
import net.oauth.OAuth;
import net.oauth.OAuthAccessor;
@@ -25,8 +25,6 @@
import net.oauth.OAuthMessage;
import net.oauth.OAuth.Parameter;
-import org.apache.shindig.gadgets.http.HttpRequest;
-
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
@@ -88,11 +86,11 @@
URL_AND_BODY_HASH,
}
- public static SignatureType getSignatureType(HttpRequest request) {
- if (OAuth.isFormEncoded(request.getHeader("Content-Type"))) {
+ public static SignatureType getSignatureType(String method, String
contentType) {
+ if (OAuth.isFormEncoded(contentType)) {
return SignatureType.URL_AND_FORM_PARAMS;
}
- if ("GET".equals(request.getMethod()) ||
"HEAD".equals(request.getMethod())) {
+ if ("GET".equals(method) || "HEAD".equals(method)) {
return SignatureType.URL_ONLY;
}
return SignatureType.URL_AND_BODY_HASH;
Propchange:
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/auth/OAuthUtil.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/auth/OAuthUtil.java
------------------------------------------------------------------------------
svn:mergeinfo =
Modified:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthCommandLine.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthCommandLine.java?rev=759842&r1=759841&r2=759842&view=diff
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthCommandLine.java
(original)
+++
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthCommandLine.java
Mon Mar 30 05:28:13 2009
@@ -27,6 +27,9 @@
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
+
+import org.apache.shindig.auth.OAuthConstants;
+import org.apache.shindig.auth.OAuthUtil;
import org.apache.shindig.common.uri.Uri;
import org.apache.shindig.common.uri.UriBuilder;
import org.apache.shindig.common.util.CharsetUtil;
@@ -116,7 +119,7 @@
oauthParams.add(new OAuth.Parameter(request.getPostBodyAsString(), ""));
} else if (bodySigningEnum == BodySigning.hash) {
oauthParams.add(
- new OAuth.Parameter("oauth_body_hash",
+ new OAuth.Parameter(OAuthConstants.OAUTH_BODY_HASH,
new
String(Base64.encodeBase64(DigestUtils.sha(postBody.getBytes())), "UTF-8")));
}
Modified:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthProtocolException.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthProtocolException.java?rev=759842&r1=759841&r2=759842&view=diff
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthProtocolException.java
(original)
+++
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthProtocolException.java
Mon Mar 30 05:28:13 2009
@@ -17,6 +17,8 @@
*/
package org.apache.shindig.gadgets.oauth;
+import org.apache.shindig.auth.OAuthUtil;
+
import com.google.common.collect.ImmutableSet;
import net.oauth.OAuthMessage;
Modified:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthRequest.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthRequest.java?rev=759842&r1=759841&r2=759842&view=diff
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthRequest.java
(original)
+++
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthRequest.java
Mon Mar 30 05:28:13 2009
@@ -19,6 +19,9 @@
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
+
+import org.apache.shindig.auth.OAuthConstants;
+import org.apache.shindig.auth.OAuthUtil;
import org.apache.shindig.common.uri.Uri;
import org.apache.shindig.common.uri.UriBuilder;
import org.apache.shindig.common.util.CharsetUtil;
@@ -88,12 +91,6 @@
protected static final Pattern ALLOWED_PARAM_NAME =
Pattern.compile("[-:\\...@$*()_\\[\\]:,./]+");
- private static final String OAUTH_SESSION_HANDLE = "oauth_session_handle";
-
- private static final String OAUTH_EXPIRES_IN = "oauth_expires_in";
-
- private static final String OAUTH_BODY_HASH = "oauth_body_hash";
-
private static final long ACCESS_TOKEN_EXPIRE_UNKNOWN = 0;
private static final long ACCESS_TOKEN_FORCE_EXPIRE = -1;
@@ -462,7 +459,7 @@
target.setQuery(null);
params.addAll(sanitize(OAuth.decodeForm(query)));
- switch(OAuthUtil.getSignatureType(base)) {
+ switch(OAuthUtil.getSignatureType(base.getMethod(),
base.getHeader("Content-Type"))) {
case URL_ONLY:
break;
case URL_AND_FORM_PARAMS:
@@ -473,7 +470,7 @@
byte[] body = IOUtils.toByteArray(base.getPostBody());
byte[] hash = DigestUtils.sha(body);
String b64 = new String(Base64.encodeBase64(hash),
CharsetUtil.UTF8.name());
- params.add(new Parameter(OAUTH_BODY_HASH, b64));
+ params.add(new Parameter(OAuthConstants.OAUTH_BODY_HASH, b64));
} catch (IOException e) {
throw
responseParams.oauthRequestException(OAuthError.UNKNOWN_PROBLEM,
"Error taking body hash", e);
@@ -662,7 +659,8 @@
List<Parameter> msgParams = Lists.newArrayList();
msgParams.add(new Parameter(OAuth.OAUTH_TOKEN, accessor.requestToken));
if (accessorInfo.getSessionHandle() != null) {
- msgParams.add(new Parameter(OAUTH_SESSION_HANDLE,
accessorInfo.getSessionHandle()));
+ msgParams.add(new Parameter(OAuthConstants.OAUTH_SESSION_HANDLE,
+ accessorInfo.getSessionHandle()));
}
HttpRequest signed = sanitizeAndSign(request, msgParams);
@@ -671,11 +669,13 @@
accessor.accessToken = OAuthUtil.getParameter(reply, OAuth.OAUTH_TOKEN);
accessor.tokenSecret = OAuthUtil.getParameter(reply,
OAuth.OAUTH_TOKEN_SECRET);
- accessorInfo.setSessionHandle(OAuthUtil.getParameter(reply,
OAUTH_SESSION_HANDLE));
+ accessorInfo.setSessionHandle(OAuthUtil.getParameter(reply,
+ OAuthConstants.OAUTH_SESSION_HANDLE));
accessorInfo.setTokenExpireMillis(ACCESS_TOKEN_EXPIRE_UNKNOWN);
- if (OAuthUtil.getParameter(reply, OAUTH_EXPIRES_IN) != null) {
+ if (OAuthUtil.getParameter(reply, OAuthConstants.OAUTH_EXPIRES_IN) !=
null) {
try {
- int expireSecs = Integer.parseInt(OAuthUtil.getParameter(reply,
OAUTH_EXPIRES_IN));
+ int expireSecs = Integer.parseInt(OAuthUtil.getParameter(reply,
+ OAuthConstants.OAUTH_EXPIRES_IN));
long expireMillis = fetcherConfig.getClock().currentTimeMillis() +
expireSecs * 1000;
accessorInfo.setTokenExpireMillis(expireMillis);
} catch (NumberFormatException e) {
Modified:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/testing/FakeOAuthServiceProvider.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/testing/FakeOAuthServiceProvider.java?rev=759842&r1=759841&r2=759842&view=diff
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/testing/FakeOAuthServiceProvider.java
(original)
+++
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/testing/FakeOAuthServiceProvider.java
Mon Mar 30 05:28:13 2009
@@ -33,6 +33,9 @@
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
+
+import org.apache.shindig.auth.OAuthUtil;
+import org.apache.shindig.auth.OAuthUtil.SignatureType;
import org.apache.shindig.common.crypto.Crypto;
import org.apache.shindig.common.util.CharsetUtil;
import org.apache.shindig.common.util.TimeSource;
@@ -41,7 +44,6 @@
import org.apache.shindig.gadgets.http.HttpRequest;
import org.apache.shindig.gadgets.http.HttpResponse;
import org.apache.shindig.gadgets.http.HttpResponseBuilder;
-import org.apache.shindig.gadgets.oauth.OAuthUtil;
import org.apache.shindig.gadgets.oauth.AccessorInfo.OAuthParamLocation;
import java.io.IOException;
@@ -376,7 +378,7 @@
}
// Parse body
- switch(OAuthUtil.getSignatureType(request)) {
+ switch(OAuthUtil.getSignatureType(request.getMethod(),
request.getHeader("Content-Type"))) {
case URL_AND_FORM_PARAMS:
String body = request.getPostBodyAsString();
info.body = body;
@@ -674,7 +676,9 @@
throws OAuthException, IOException, URISyntaxException {
info.message.validateMessage(accessor, new FakeTimeOAuthValidator());
String bodyHash = info.message.getParameter("oauth_body_hash");
- switch (OAuthUtil.getSignatureType(info.request)) {
+ SignatureType sigType =
OAuthUtil.getSignatureType(info.request.getMethod(),
+ info.request.getHeader("Content-Type"));
+ switch (sigType) {
case URL_ONLY:
break;
case URL_AND_FORM_PARAMS:
Modified:
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/oauth/OAuthAuthenticationHandler.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/oauth/OAuthAuthenticationHandler.java?rev=759842&r1=759841&r2=759842&view=diff
==============================================================================
---
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/oauth/OAuthAuthenticationHandler.java
(original)
+++
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/oauth/OAuthAuthenticationHandler.java
Mon Mar 30 05:28:13 2009
@@ -35,6 +35,7 @@
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shindig.auth.AuthenticationHandler;
+import org.apache.shindig.auth.OAuthConstants;
import org.apache.shindig.auth.SecurityToken;
import org.apache.shindig.common.util.CharsetUtil;
import org.apache.shindig.social.opensocial.oauth.OAuthDataStore;
@@ -53,7 +54,6 @@
public class OAuthAuthenticationHandler implements AuthenticationHandler {
public static final String REQUESTOR_ID_PARAM = "xoauth_requestor_id";
- public static final String OAUTH_BODY_HASH = "oauth_body_hash";
private final OAuthDataStore store;
@@ -82,7 +82,7 @@
// Is not an oauth request
return null;
}
- String bodyHash = getParameter(message, OAUTH_BODY_HASH);
+ String bodyHash = getParameter(message, OAuthConstants.OAUTH_BODY_HASH);
if (!StringUtils.isEmpty(bodyHash)) {
verifyBodyHash(request, bodyHash);
}
Modified:
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/core/oauth/FakeOAuthRequest.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/core/oauth/FakeOAuthRequest.java?rev=759842&r1=759841&r2=759842&view=diff
==============================================================================
---
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/core/oauth/FakeOAuthRequest.java
(original)
+++
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/core/oauth/FakeOAuthRequest.java
Mon Mar 30 05:28:13 2009
@@ -27,6 +27,8 @@
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
+
+import org.apache.shindig.auth.OAuthConstants;
import org.apache.shindig.common.testing.FakeHttpServletRequest;
import org.apache.shindig.common.uri.Uri;
import org.apache.shindig.common.uri.UriBuilder;
@@ -96,7 +98,7 @@
oauthParams.add(new OAuth.Parameter(body, ""));
} else if (bodySigning == BodySigning.HASH) {
oauthParams.add(
- new OAuth.Parameter(OAuthAuthenticationHandler.OAUTH_BODY_HASH,
+ new OAuth.Parameter(OAuthConstants.OAUTH_BODY_HASH,
new
String(Base64.encodeBase64(DigestUtils.sha(body.getBytes())), "UTF-8")));
}
}
Modified:
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/core/oauth/OAuthAuthenticationHanderTest.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/core/oauth/OAuthAuthenticationHanderTest.java?rev=759842&r1=759841&r2=759842&view=diff
==============================================================================
---
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/core/oauth/OAuthAuthenticationHanderTest.java
(original)
+++
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/core/oauth/OAuthAuthenticationHanderTest.java
Mon Mar 30 05:28:13 2009
@@ -26,6 +26,7 @@
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shindig.auth.AnonymousSecurityToken;
import org.apache.shindig.auth.AuthenticationHandler;
+import org.apache.shindig.auth.OAuthConstants;
import org.apache.shindig.auth.SecurityToken;
import org.apache.shindig.common.EasyMockTestCase;
import org.apache.shindig.common.testing.FakeHttpServletRequest;
@@ -396,7 +397,7 @@
req.setPostData(CharsetUtil.getUtf8Bytes(body));
String hash = new
String(Base64.encodeBase64(DigestUtils.sha(CharsetUtil.getUtf8Bytes(body))),
"UTF-8");
- req.setParameter(OAuthAuthenticationHandler.OAUTH_BODY_HASH, hash);
+ req.setParameter(OAuthConstants.OAUTH_BODY_HASH, hash);
OAuthAuthenticationHandler.verifyBodyHash(req, hash);
}
@@ -408,7 +409,7 @@
req.setPostData(CharsetUtil.getUtf8Bytes(body));
String hash = new String(Base64.encodeBase64(
DigestUtils.sha(CharsetUtil.getUtf8Bytes("NOTBODY"))), "UTF-8");
- req.setParameter(OAuthAuthenticationHandler.OAUTH_BODY_HASH, hash);
+ req.setParameter(OAuthConstants.OAUTH_BODY_HASH, hash);
try {
OAuthAuthenticationHandler.verifyBodyHash(req, hash);
fail("Body verification should fail");
@@ -425,7 +426,7 @@
req.setPostData(CharsetUtil.getUtf8Bytes(body));
String hash = new
String(Base64.encodeBase64(DigestUtils.sha(CharsetUtil.getUtf8Bytes(body))),
"UTF-8");
- req.setParameter(OAuthAuthenticationHandler.OAUTH_BODY_HASH, hash);
+ req.setParameter(OAuthConstants.OAUTH_BODY_HASH, hash);
try {
OAuthAuthenticationHandler.verifyBodyHash(req, hash);
fail("Body verification should fail");
@@ -443,7 +444,7 @@
req.setMethod("GET");
String hash = new
String(Base64.encodeBase64(DigestUtils.sha(CharsetUtil.getUtf8Bytes(body))),
"UTF-8");
- req.setParameter(OAuthAuthenticationHandler.OAUTH_BODY_HASH, hash);
+ req.setParameter(OAuthConstants.OAUTH_BODY_HASH, hash);
try {
OAuthAuthenticationHandler.verifyBodyHash(req, hash);
fail("Body verification should fail");
@@ -461,7 +462,7 @@
req.setMethod("HEAD");
String hash = new
String(Base64.encodeBase64(DigestUtils.sha(CharsetUtil.getUtf8Bytes(body))),
"UTF-8");
- req.setParameter(OAuthAuthenticationHandler.OAUTH_BODY_HASH, hash);
+ req.setParameter(OAuthConstants.OAUTH_BODY_HASH, hash);
try {
OAuthAuthenticationHandler.verifyBodyHash(req, hash);
fail("Body verification should fail");